PluginProbe
WP Directory Kit / 1.5.9
WP Directory Kit v1.5.9
1.5.9 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 All 37 releases
wpdirectorykit / application / models / Category_m.php

Category_m.php in WP Directory Kit 1.5.9, at application/models/Category_m.php

416 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8 class Category_m extends Winter_MVC_Model {
9
10 public $_table_name = 'wdk_categories';
11 public $_order_by = 'order_index, idcategory';
12 public $_primary_key = 'idcategory';
13 public $_own_columns = array();
14 public $_timestamps = TRUE;
15 protected $_primary_filter = 'intval';
16 public $form_admin = array();
17 public $fields_list = NULL;
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
43 public function __construct(){
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);
54 }
55
56 public function getAllowedOrderBy() {
57 return $this->allowedOrderBy;
58 }
59
60 /* [START] For dynamic data table */
61
62 public function get_available_fields()
63 {
64 $fields = $this->db->list_fields($this->_table_name);
65
66 return $fields;
67 }
68
69 public function total($where = array())
70 {
71 $this->db->select('COUNT(*) as total_count');
72 $this->db->from($this->_table_name);
73 $this->db->where($where);
74 $this->db->order_by($this->_order_by);
75
76 $query = $this->db->get();
77
78 $res = $this->db->results();
79
80 if(isset($res[0]->total_count))
81 return $res[0]->total_count;
82
83 return 0;
84 }
85
86 public function get_pagination($limit, $offset, $where = array(), $order_by = NULL)
87 {
88 $this->load->model('listing_m');
89
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');
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 {
124 $this->load->model('listing_m');
125
126 $this->db->from($this->_table_name);
127
128 $this->db->where($where);
129 $this->db->limit($limit);
130 $this->db->offset($offset);
131
132 $this->db->group_by('idcategory');
133
134 if(!empty($order_by)){
135 $this->db->order_by($order_by);
136 } else {
137 $this->db->order_by($this->_order_by);
138 }
139
140 $query = $this->db->get();
141
142 if ($this->db->num_rows() > 0)
143 return $this->db->results();
144
145 return array();
146 }
147
148 public function check_deletable($id)
149 {
150 return true;
151 }
152
153
154 /* [END] For dynamic data table */
155
156 public function insert($data, $id = NULL)
157 {
158 if(!is_array($data))
159 {
160 echo 'Missing data for insert in model';
161 return NULL;
162 }
163
164 if($this->_timestamps === TRUE && !isset($data['date']))
165 $data['date'] = current_time( 'mysql' );
166
167 $data['level_0_id'] = 0;
168 $data['level'] = 0;
169 $data['parent_path'] = '';
170
171 if(!empty($data['parent_id']))
172 {
173 $parent = $this->get($data['parent_id']);
174
175 if(isset($parent[0]))
176 {
177 $data['level'] = $parent[0]->level + 1;
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 }
185 }
186 }
187
188 if(empty($data['order_index']))
189 {
190 $data['order_index'] = $this->max_order($data['parent_id'])+1;
191 }
192
193 if(!empty($id))
194 {
195 return $this->db->update($this->_table_name, $data, $id, $this->_primary_key);
196 }
197
198 return $this->db->insert($this->_table_name, $data);
199 }
200
201 public function get_parents($id = NULL, $level_show=true)
202 {
203 $this->db->select('*');
204
205 if(!empty($id))
206 {
207 $this->db->where(array('idcategory !=' => $id));
208 $this->db->where(array('parent_id !=' => $id));
209 }
210
211 $query = $this->get();
212
213 //$list_with_keys = array(0 => __('Root', 'wpdirectorykit'));
214 $list_with_keys = array();
215 if ($this->db->num_rows() > 0)
216 {
217 $results = $this->db->results();
218
219 $results = $this->parent_ordered_list($results);
220
221 foreach($results as $result)
222 {
223 $level_gen = str_pad('', $result->level*12, '&nbsp;');
224
225 $level_gen.='|-';
226
227 if(!$level_show)
228 $level_gen = '';
229
230 $list_with_keys[$result->idcategory] = $level_gen.$result->category_title;
231 }
232
233
234 }
235
236 //wmvc_dump($list_with_keys);
237
238 return $list_with_keys;
239 }
240
241 public function get_tree_table()
242 {
243 $this->db->select('*');
244 $query = $this->get();
245
246 $results = array();
247 if ($this->db->num_rows() > 0)
248 {
249 $results = $this->db->results();
250
251 $results = $this->parent_ordered_list($results);
252 }
253
254 return $results;
255 }
256
257 public function parent_ordered_list(&$results)
258 {
259 $tree_list = array();
260
261 foreach($results as $result)
262 {
263 $tree_list[$result->parent_id][$result->idcategory] = $result;
264 }
265
266 $ordered_list = array();
267
268 $this->_parent_ordered_list(0, $tree_list, $ordered_list);
269
270 return $ordered_list;
271 }
272
273 // recursive function because of tree structure and ordering
274 private function _parent_ordered_list($parent_id , &$tree_list, &$ordered_list, $level=-1)
275 {
276 $level++;
277
278 if(isset($tree_list[$parent_id]))
279 foreach($tree_list[$parent_id] as $tree_id => $tree_item)
280 {
281 $tree_item->level = $level;
282 $ordered_list[$tree_id] = $tree_item;
283
284 $this->_parent_ordered_list($tree_item->idcategory , $tree_list, $ordered_list, $level);
285 }
286 }
287
288 public function get_data($category_id = NULL) {
289 static $data = array();
290 if(empty($data)){
291 $query = $this->get();
292 if ($this->db->num_rows() > 0) {
293 foreach($this->db->results() as $category) {
294 $data[$category->idcategory] = $category;
295 }
296 }
297 }
298
299 if(!empty($category_id))
300 if(isset($data[(int)$category_id])){
301 return $data[(int)$category_id];
302 } else {
303 return NULL;
304 }
305
306 return $data;
307
308 }
309
310 public function delete($id)
311 {
312 $filter = $this->_primary_filter;
313 $id = $filter($id);
314
315 if(!$id)
316 {
317 return FALSE;
318 }
319
320 $item = $this->get($id, TRUE);
321
322 if(is_object($item))
323 {
324 // update all childs parent_id
325 $query = 'UPDATE '.$this->_table_name.' SET parent_id = '.$item->parent_id;
326 $query.= ' , `level` = '.$item->level;
327 $query.= ' , `order_index` = '.$item->order_index;
328 $query.= ' WHERE parent_id ='.$id.';';
329 $this->db->query($query);
330 }
331
332 $this->db->where($this->_primary_key, $id);
333 $this->db->limit(1);
334 $this->db->delete($this->_table_name);
335 }
336
337 /* only admin can edit */
338 public function is_related($item_id, $user_id, $method = 'edit')
339 {
340 return false;
341 }
342
343 public function get_all_childs($id)
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 {
365 $categories = array();
366 $childs = array();
367 // Fetch pages without parents
368 $this->db->select('idcategory, level, parent_id');
369 $this->db->where(array('(parent_id = '.esc_sql($id).')' => NULL));
370
371 $this->db->from($this->_table_name);
372 $categories = $this->get();
373 if(count($categories))
374 {
375 foreach($categories as $category)
376 {
377 $childs[] = $category->idcategory;
378 $this->_get_all_childs_recursive($category->idcategory, $childs);
379 }
380 }
381 return $childs;
382 }
383
384 private function _get_all_childs_recursive($parent_id, &$childs = array())
385 {
386 $categories = array();
387 // Fetch pages without parents
388 $this->db->select('idcategory, level, parent_id');
389 $this->db->where(array('(parent_id = '.esc_sql($parent_id).')' => NULL));
390
391 $this->db->from($this->_table_name);
392 $categories = $this->get();
393 if(count($categories))
394 {
395 foreach($categories as $category)
396 {
397 $childs[] = $category->idcategory;
398 $this->_get_all_childs_recursive($category->idcategory, $childs);
399 }
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;
414 }
415 }
416 ?>