| 1 |
<?php |
| 2 |
/** |
| 3 |
* Models |
| 4 |
* |
| 5 |
* @package Models |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
use LassoLite\Admin\Constant; |
| 11 |
use LassoLite\Models\Model; |
| 12 |
|
| 13 |
/** |
| 14 |
* Model |
| 15 |
*/ |
| 16 |
class Group extends LL_Object { |
| 17 |
|
| 18 |
/** |
| 19 |
* Get groups query |
| 20 |
* |
| 21 |
* @param int $page Page number. |
| 22 |
* @param string $search Search text. |
| 23 |
* @param string $where Where statement. Default to '1=1'. |
| 24 |
* @param string $limit Limit. |
| 25 |
* @return Group[] |
| 26 |
*/ |
| 27 |
public static function get_list( $page = null, $search = '', $where = '1=1', $limit = Enum::LIMIT_ON_PAGE ) { |
| 28 |
|
| 29 |
$terms = Model::get_wpdb()->terms; |
| 30 |
$term_taxonomy = Model::get_wpdb()->term_taxonomy; |
| 31 |
$term_relationships = Model::get_wpdb()->term_relationships; |
| 32 |
$posts = Model::get_wpdb()->posts; |
| 33 |
if ( ! empty( $search ) ) { |
| 34 |
$search_body = '%' . str_replace( ' ', '%', $search ) . '%'; |
| 35 |
$search = "AND t.name LIKE '" . $search_body . "'"; |
| 36 |
} |
| 37 |
$sql = " |
| 38 |
SELECT |
| 39 |
t.term_id as `post_id`, |
| 40 |
t.term_id, |
| 41 |
t.name as post_title, |
| 42 |
t.slug, |
| 43 |
CASE |
| 44 |
WHEN LENGTH(tt.description) > 237 |
| 45 |
THEN CONCAT(SUBSTRING(tt.description, 1, 237), '...') |
| 46 |
ELSE tt.description |
| 47 |
END as description, |
| 48 |
SUM( |
| 49 |
CASE |
| 50 |
WHEN p.ID IS NOT NULL |
| 51 |
THEN 1 |
| 52 |
ELSE 0 |
| 53 |
END |
| 54 |
) as `count` |
| 55 |
FROM |
| 56 |
" . $terms . ' as t |
| 57 |
INNER JOIN |
| 58 |
' . $term_taxonomy . " as tt |
| 59 |
ON t.term_id = tt.term_id |
| 60 |
AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "' |
| 61 |
LEFT JOIN |
| 62 |
" . $term_relationships . ' as tr |
| 63 |
ON tt.term_taxonomy_id = tr.term_taxonomy_id |
| 64 |
LEFT JOIN |
| 65 |
' . $posts . " as p |
| 66 |
ON tr.object_id = p.ID |
| 67 |
AND p.post_type = '" . Constant::LASSO_POST_TYPE . "' |
| 68 |
AND p.post_status = 'publish' |
| 69 |
WHERE |
| 70 |
" . $where . ' |
| 71 |
' . $search . ' |
| 72 |
GROUP BY |
| 73 |
t.term_id, |
| 74 |
t.name, |
| 75 |
t.slug |
| 76 |
'; |
| 77 |
|
| 78 |
if ( ! is_null( $page ) ) { |
| 79 |
$sql = Helper::paginate( $sql, $page, $limit ); |
| 80 |
} |
| 81 |
|
| 82 |
$results = Model::get_results( $sql ); |
| 83 |
$list = array(); |
| 84 |
foreach ( $results as $result ) { |
| 85 |
$list[] = new self( $result ); |
| 86 |
} |
| 87 |
return $list; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get groups query |
| 92 |
* |
| 93 |
* @param string $search Search text. |
| 94 |
* @param string $where Where statement. Default to '1=1'. |
| 95 |
* @return Group[] |
| 96 |
*/ |
| 97 |
public static function total( $search = '', $where = '1=1' ) { |
| 98 |
|
| 99 |
$terms = Model::get_wpdb()->terms; |
| 100 |
$term_taxonomy = Model::get_wpdb()->term_taxonomy; |
| 101 |
$term_relationships = Model::get_wpdb()->term_relationships; |
| 102 |
$posts = Model::get_wpdb()->posts; |
| 103 |
if ( ! empty( $search ) ) { |
| 104 |
$search = "AND t.name LIKE '" . $search . "'"; |
| 105 |
} |
| 106 |
$sql = " |
| 107 |
SELECT |
| 108 |
t.term_id as `post_id`, |
| 109 |
t.term_id, |
| 110 |
t.name as post_title, |
| 111 |
t.slug, |
| 112 |
CASE |
| 113 |
WHEN LENGTH(tt.description) > 237 |
| 114 |
THEN CONCAT(SUBSTRING(tt.description, 1, 237), '...') |
| 115 |
ELSE tt.description |
| 116 |
END as description, |
| 117 |
SUM( |
| 118 |
CASE |
| 119 |
WHEN p.ID IS NOT NULL |
| 120 |
THEN 1 |
| 121 |
ELSE 0 |
| 122 |
END |
| 123 |
) as `count` |
| 124 |
FROM |
| 125 |
" . $terms . ' as t |
| 126 |
INNER JOIN |
| 127 |
' . $term_taxonomy . " as tt |
| 128 |
ON t.term_id = tt.term_id |
| 129 |
AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "' |
| 130 |
LEFT JOIN |
| 131 |
" . $term_relationships . ' as tr |
| 132 |
ON tt.term_taxonomy_id = tr.term_taxonomy_id |
| 133 |
LEFT JOIN |
| 134 |
' . $posts . " as p |
| 135 |
ON tr.object_id = p.ID |
| 136 |
AND p.post_type = '" . Constant::LASSO_POST_TYPE . "' |
| 137 |
AND p.post_status = 'publish' |
| 138 |
WHERE |
| 139 |
" . $where . ' |
| 140 |
' . $search . ' |
| 141 |
GROUP BY |
| 142 |
t.term_id, |
| 143 |
t.name, |
| 144 |
t.slug |
| 145 |
'; |
| 146 |
|
| 147 |
return Model::get_count( $sql ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get edit URL |
| 152 |
*/ |
| 153 |
public function get_link_detail() { |
| 154 |
return Page::get_page_url( Helper::add_prefix_page( Enum::PAGE_GROUP_DETAIL ) . '&post_id=' . $this->get_post_id() ) . '&subpage=' . Enum::SUB_PAGE_GROUP_URLS; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get by ID |
| 159 |
* |
| 160 |
* @param int $id ID. |
| 161 |
* |
| 162 |
* @return Group|null |
| 163 |
*/ |
| 164 |
public static function get_by_id( $id ) { |
| 165 |
$term_data = get_term( $id, Constant::LASSO_CATEGORY ); |
| 166 |
if ( ! empty( $term_data ) && ! is_wp_error( $term_data ) ) { |
| 167 |
return new self( $term_data ); |
| 168 |
} |
| 169 |
return null; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get group id |
| 174 |
* |
| 175 |
* @return int |
| 176 |
*/ |
| 177 |
public function get_id() { |
| 178 |
return (int) $this->get_term_id(); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get total links of group |
| 183 |
* |
| 184 |
* @return int|mixed |
| 185 |
*/ |
| 186 |
public function get_total_links() { |
| 187 |
$terms = Model::get_wpdb()->terms; |
| 188 |
$term_taxonomy = Model::get_wpdb()->term_taxonomy; |
| 189 |
$term_relationships = Model::get_wpdb()->term_relationships; |
| 190 |
$posts = Model::get_wpdb()->posts; |
| 191 |
|
| 192 |
$sql = ' |
| 193 |
SELECT |
| 194 |
count(*) as `count` |
| 195 |
FROM |
| 196 |
' . $terms . ' as t |
| 197 |
INNER JOIN |
| 198 |
' . $term_taxonomy . " as tt |
| 199 |
ON t.term_id = tt.term_id |
| 200 |
AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "' |
| 201 |
LEFT JOIN |
| 202 |
" . $term_relationships . ' as tr |
| 203 |
ON tt.term_taxonomy_id = tr.term_taxonomy_id |
| 204 |
INNER JOIN |
| 205 |
' . $posts . " as p |
| 206 |
ON tr.object_id = p.ID |
| 207 |
WHERE |
| 208 |
t.term_id = %d |
| 209 |
AND t.slug = %s |
| 210 |
AND p.post_type = '" . Constant::LASSO_POST_TYPE . "' |
| 211 |
AND p.post_status = 'publish' |
| 212 |
AND tt.taxonomy = '" . Constant::LASSO_CATEGORY . "'"; |
| 213 |
$sql = Model::prepare( $sql, $this->get_id(), $this->get_slug() ); |
| 214 |
$result = Model::get_row( $sql ); |
| 215 |
return $result->count; |
| 216 |
} |
| 217 |
} |
| 218 |
|