| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | class Category_m extends Winter_MVC_Model { |
| 9 | 9 | |
| 10 | 10 | public $_table_name = 'wdk_categories'; |
| 11 | - public $_order_by = 'order_index'; | |
| 11 | + public $_order_by = 'order_index, idcategory'; | |
| 12 | 12 | public $_primary_key = 'idcategory'; |
| 13 | 13 | public $_own_columns = array(); |
| 14 | 14 | public $_timestamps = TRUE; |
| 15 | 15 | protected $_primary_filter = 'intval'; |
| @@ -14,13 +14,50 @@ | ||
| 14 | 14 | public $_timestamps = TRUE; |
| 15 | 15 | protected $_primary_filter = 'intval'; |
| 16 | 16 | public $form_admin = array(); |
| 17 | 17 | public $fields_list = NULL; |
| 18 | - | |
| 18 | + public $allowedOrderBy = array( | |
| 19 | + 'idcategory', | |
| 20 | + 'parent_id', | |
| 21 | + 'page_id', | |
| 22 | + 'lang_code', | |
| 23 | + 'date', | |
| 24 | + 'parent_path', | |
| 25 | + 'order_index', | |
| 26 | + 'level', | |
| 27 | + 'icon_id', | |
| 28 | + 'image_id', | |
| 29 | + 'font_icon_code', | |
| 30 | + 'code', | |
| 31 | + 'hubspot_id', | |
| 32 | + 'category_title', | |
| 33 | + 'marker_image_id', | |
| 34 | + 'level_0_id', | |
| 35 | + 'icon_path', | |
| 36 | + 'image_path', | |
| 37 | + 'marker_image_path', | |
| 38 | + 'category_color', | |
| 39 | + 'titles_for_search', | |
| 40 | + 'listings_counter' | |
| 41 | + ); | |
| 42 | + | |
| 19 | 43 | public function __construct(){ |
| 20 | 44 | parent::__construct(); |
| 45 | + | |
| 46 | + // Add allowedOrderBy with table + prefix format | |
| 47 | + $table_prefix = $this->db->prefix . $this->_table_name . '.'; | |
| 48 | + $updatedAllowedOrderBy = array(); | |
| 49 | + foreach ($this->allowedOrderBy as $col) { | |
| 50 | + $updatedAllowedOrderBy[] = $col; | |
| 51 | + $updatedAllowedOrderBy[] = $table_prefix . $col; | |
| 52 | + } | |
| 53 | + $this->allowedOrderBy = array_unique($updatedAllowedOrderBy); | |
| 21 | 54 | } |
| 22 | 55 | |
| 56 | + public function getAllowedOrderBy() { | |
| 57 | + return $this->allowedOrderBy; | |
| 58 | + } | |
| 59 | + | |
| 23 | 60 | /* [START] For dynamic data table */ |
| 24 | 61 | |
| 25 | 62 | public function get_available_fields() |
| 26 | 63 | { |
| @@ -47,18 +84,48 @@ | ||
| 47 | 84 | } |
| 48 | 85 | |
| 49 | 86 | public function get_pagination($limit, $offset, $where = array(), $order_by = NULL) |
| 50 | 87 | { |
| 88 | + $this->load->model('listing_m'); | |
| 51 | 89 | |
| 52 | - $wp_usermeta_table = $this->db->prefix.'usermeta'; | |
| 90 | + $this->db->select($this->_table_name.'.*, COUNT(DISTINCT '.$this->listing_m->_table_name.'.post_id) AS listings_counter, MAX('.$this->_table_name.'.level) as level'); | |
| 53 | 91 | |
| 92 | + $this->db->join($this->_table_name.' AS category_table ON (CONCAT(",", category_table.parent_path, ",") LIKE CONCAT("%,", '.$this->_table_name.'.idcategory ,",%"))', TRUE, 'LEFT'); | |
| 93 | + $this->db->join($this->listing_m->_table_name.' ON ( | |
| 94 | + ('.$this->listing_m->_table_name.'.is_activated = 1 AND '.$this->listing_m->_table_name.'.is_approved = 1) AND | |
| 95 | + ('.$this->listing_m->_table_name.'.category_id = '.$this->_table_name.'.idcategory | |
| 96 | + OR '.$this->listing_m->_table_name.'.category_id = category_table.idcategory) | |
| 97 | + | |
| 98 | + )', TRUE, 'LEFT'); | |
| 99 | + | |
| 100 | + $this->db->from($this->_table_name); | |
| 101 | + | |
| 102 | + $this->db->where($where); | |
| 103 | + $this->db->limit($limit); | |
| 104 | + $this->db->offset($offset); | |
| 105 | + | |
| 106 | + $this->db->group_by('idcategory'); | |
| 107 | + | |
| 108 | + if(!empty($order_by)){ | |
| 109 | + $this->db->order_by($order_by); | |
| 110 | + } else { | |
| 111 | + $this->db->order_by($this->_order_by); | |
| 112 | + } | |
| 113 | + | |
| 114 | + $query = $this->db->get(); | |
| 115 | + | |
| 116 | + if ($this->db->num_rows() > 0) | |
| 117 | + return $this->db->results(); | |
| 118 | + | |
| 119 | + return array(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + public function get_pagination_simple($limit, $offset, $where = array(), $order_by = NULL) | |
| 123 | + { | |
| 54 | 124 | $this->load->model('listing_m'); |
| 55 | 125 | |
| 56 | - $this->db->select($this->_table_name.'.*, COUNT('.$this->listing_m->_table_name.'.post_id) AS listings_counter'); | |
| 57 | 126 | $this->db->from($this->_table_name); |
| 58 | 127 | |
| 59 | - $this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.category_id = '.$this->_table_name.'.idcategory', TRUE, 'LEFT'); | |
| 60 | - | |
| 61 | 128 | $this->db->where($where); |
| 62 | 129 | $this->db->limit($limit); |
| 63 | 130 | $this->db->offset($offset); |
| 64 | 131 | |
| @@ -96,8 +163,9 @@ | ||
| 96 | 163 | |
| 97 | 164 | if($this->_timestamps === TRUE && !isset($data['date'])) |
| 98 | 165 | $data['date'] = current_time( 'mysql' ); |
| 99 | 166 | |
| 167 | + $data['level_0_id'] = 0; | |
| 100 | 168 | $data['level'] = 0; |
| 101 | 169 | $data['parent_path'] = ''; |
| 102 | 170 | |
| 103 | 171 | if(!empty($data['parent_id'])) |
| @@ -107,8 +175,14 @@ | ||
| 107 | 175 | if(isset($parent[0])) |
| 108 | 176 | { |
| 109 | 177 | $data['level'] = $parent[0]->level + 1; |
| 110 | 178 | $data['parent_path'] = $parent[0]->parent_path.(!empty($parent[0]->parent_path)?',':'').$parent[0]->idcategory; |
| 179 | + | |
| 180 | + if(strpos($data['parent_path'], ',') !== FALSE) { | |
| 181 | + $data['level_0_id'] = substr($data['parent_path'],0,strpos($data['parent_path'], ',')); | |
| 182 | + } else { | |
| 183 | + $data['level_0_id'] = $data['parent_path']; | |
| 184 | + } | |
| 111 | 185 | } |
| 112 | 186 | } |
| 113 | 187 | |
| 114 | 188 | if(empty($data['order_index'])) |
| @@ -123,9 +197,9 @@ | ||
| 123 | 197 | |
| 124 | 198 | return $this->db->insert($this->_table_name, $data); |
| 125 | 199 | } |
| 126 | 200 | |
| 127 | - public function get_parents($id = NULL) | |
| 201 | + public function get_parents($id = NULL, $level_show=true) | |
| 128 | 202 | { |
| 129 | 203 | $this->db->select('*'); |
| 130 | 204 | |
| 131 | 205 | if(!empty($id)) |
| @@ -149,8 +223,11 @@ | ||
| 149 | 223 | $level_gen = str_pad('', $result->level*12, ' '); |
| 150 | 224 | |
| 151 | 225 | $level_gen.='|-'; |
| 152 | 226 | |
| 227 | + if(!$level_show) | |
| 228 | + $level_gen = ''; | |
| 229 | + | |
| 153 | 230 | $list_with_keys[$result->idcategory] = $level_gen.$result->category_title; |
| 154 | 231 | } |
| 155 | 232 | |
| 156 | 233 | |
| @@ -264,8 +341,28 @@ | ||
| 264 | 341 | } |
| 265 | 342 | |
| 266 | 343 | public function get_all_childs($id) |
| 267 | 344 | { |
| 345 | + $childs = array(); | |
| 346 | + | |
| 347 | + // Fetch pages without parents | |
| 348 | + $this->db->select('idcategory'); | |
| 349 | + $this->db->where(array('(CONCAT(",", parent_path, ",") LIKE CONCAT("%,", '.esc_sql($id).', ",%"))' => NULL)); | |
| 350 | + | |
| 351 | + $this->db->from($this->_table_name); | |
| 352 | + $categories = $this->get(); | |
| 353 | + if(count($categories)) | |
| 354 | + { | |
| 355 | + foreach($categories as $category) | |
| 356 | + { | |
| 357 | + $childs[] = $category->idcategory; | |
| 358 | + } | |
| 359 | + } | |
| 360 | + return $childs; | |
| 361 | + } | |
| 362 | + | |
| 363 | + public function get_all_childs_deprecated($id) | |
| 364 | + { | |
| 268 | 365 | $categories = array(); |
| 269 | 366 | $childs = array(); |
| 270 | 367 | // Fetch pages without parents |
| 271 | 368 | $this->db->select('idcategory, level, parent_id'); |
| @@ -300,7 +397,20 @@ | ||
| 300 | 397 | $childs[] = $category->idcategory; |
| 301 | 398 | $this->_get_all_childs_recursive($category->idcategory, $childs); |
| 302 | 399 | } |
| 303 | 400 | } |
| 401 | + } | |
| 402 | + | |
| 403 | + public function get_max_level() | |
| 404 | + { | |
| 405 | + $this->db->select('MAX(`level`) as `level`'); | |
| 406 | + $this->db->from($this->_table_name); | |
| 407 | + $row = $this->get(NULL, TRUE); | |
| 408 | + if(!empty($row)) | |
| 409 | + { | |
| 410 | + return (wmvc_show_data('level', $row, 0, TRUE, TRUE) + 1); | |
| 411 | + } | |
| 412 | + | |
| 413 | + return 1; | |
| 304 | 414 | } |
| 305 | 415 | } |
| 306 | 416 | ?> |