PluginProbe
WP Directory Kit / 1.5.7
WP Directory Kit v1.5.7
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.7, at application/models/Category_m.php

415 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 );
41
42 public function __construct(){
43 parent::__construct();
44
45 // Add allowedOrderBy with table + prefix format
46 $table_prefix = $this->db->prefix . $this->_table_name . '.';
47 $updatedAllowedOrderBy = array();
48 foreach ($this->allowedOrderBy as $col) {
49 $updatedAllowedOrderBy[] = $col;
50 $updatedAllowedOrderBy[] = $table_prefix . $col;
51 }
52 $this->allowedOrderBy = array_unique($updatedAllowedOrderBy);
53 }
54
55 public function getAllowedOrderBy() {
56 return $this->allowedOrderBy;
57 }
58
59 /* [START] For dynamic data table */
60
61 public function get_available_fields()
62 {
63 $fields = $this->db->list_fields($this->_table_name);
64
65 return $fields;
66 }
67
68 public function total($where = array())
69 {
70 $this->db->select('COUNT(*) as total_count');
71 $this->db->from($this->_table_name);
72 $this->db->where($where);
73 $this->db->order_by($this->_order_by);
74
75 $query = $this->db->get();
76
77 $res = $this->db->results();
78
79 if(isset($res[0]->total_count))
80 return $res[0]->total_count;
81
82 return 0;
83 }
84
85 public function get_pagination($limit, $offset, $where = array(), $order_by = NULL)
86 {
87 $this->load->model('listing_m');
88
89 $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');
90
91 $this->db->join($this->_table_name.' AS category_table ON (CONCAT(",", category_table.parent_path, ",") LIKE CONCAT("%,", '.$this->_table_name.'.idcategory ,",%"))', TRUE, 'LEFT');
92 $this->db->join($this->listing_m->_table_name.' ON (
93 ('.$this->listing_m->_table_name.'.is_activated = 1 AND '.$this->listing_m->_table_name.'.is_approved = 1) AND
94 ('.$this->listing_m->_table_name.'.category_id = '.$this->_table_name.'.idcategory
95 OR '.$this->listing_m->_table_name.'.category_id = category_table.idcategory)
96
97 )', TRUE, 'LEFT');
98
99 $this->db->from($this->_table_name);
100
101 $this->db->where($where);
102 $this->db->limit($limit);
103 $this->db->offset($offset);
104
105 $this->db->group_by('idcategory');
106
107 if(!empty($order_by)){
108 $this->db->order_by($order_by);
109 } else {
110 $this->db->order_by($this->_order_by);
111 }
112
113 $query = $this->db->get();
114
115 if ($this->db->num_rows() > 0)
116 return $this->db->results();
117
118 return array();
119 }
120
121 public function get_pagination_simple($limit, $offset, $where = array(), $order_by = NULL)
122 {
123 $this->load->model('listing_m');
124
125 $this->db->from($this->_table_name);
126
127 $this->db->where($where);
128 $this->db->limit($limit);
129 $this->db->offset($offset);
130
131 $this->db->group_by('idcategory');
132
133 if(!empty($order_by)){
134 $this->db->order_by($order_by);
135 } else {
136 $this->db->order_by($this->_order_by);
137 }
138
139 $query = $this->db->get();
140
141 if ($this->db->num_rows() > 0)
142 return $this->db->results();
143
144 return array();
145 }
146
147 public function check_deletable($id)
148 {
149 return true;
150 }
151
152
153 /* [END] For dynamic data table */
154
155 public function insert($data, $id = NULL)
156 {
157 if(!is_array($data))
158 {
159 echo 'Missing data for insert in model';
160 return NULL;
161 }
162
163 if($this->_timestamps === TRUE && !isset($data['date']))
164 $data['date'] = current_time( 'mysql' );
165
166 $data['level_0_id'] = 0;
167 $data['level'] = 0;
168 $data['parent_path'] = '';
169
170 if(!empty($data['parent_id']))
171 {
172 $parent = $this->get($data['parent_id']);
173
174 if(isset($parent[0]))
175 {
176 $data['level'] = $parent[0]->level + 1;
177 $data['parent_path'] = $parent[0]->parent_path.(!empty($parent[0]->parent_path)?',':'').$parent[0]->idcategory;
178
179 if(strpos($data['parent_path'], ',') !== FALSE) {
180 $data['level_0_id'] = substr($data['parent_path'],0,strpos($data['parent_path'], ','));
181 } else {
182 $data['level_0_id'] = $data['parent_path'];
183 }
184 }
185 }
186
187 if(empty($data['order_index']))
188 {
189 $data['order_index'] = $this->max_order($data['parent_id'])+1;
190 }
191
192 if(!empty($id))
193 {
194 return $this->db->update($this->_table_name, $data, $id, $this->_primary_key);
195 }
196
197 return $this->db->insert($this->_table_name, $data);
198 }
199
200 public function get_parents($id = NULL, $level_show=true)
201 {
202 $this->db->select('*');
203
204 if(!empty($id))
205 {
206 $this->db->where(array('idcategory !=' => $id));
207 $this->db->where(array('parent_id !=' => $id));
208 }
209
210 $query = $this->get();
211
212 //$list_with_keys = array(0 => __('Root', 'wpdirectorykit'));
213 $list_with_keys = array();
214 if ($this->db->num_rows() > 0)
215 {
216 $results = $this->db->results();
217
218 $results = $this->parent_ordered_list($results);
219
220 foreach($results as $result)
221 {
222 $level_gen = str_pad('', $result->level*12, '&nbsp;');
223
224 $level_gen.='|-';
225
226 if(!$level_show)
227 $level_gen = '';
228
229 $list_with_keys[$result->idcategory] = $level_gen.$result->category_title;
230 }
231
232
233 }
234
235 //wmvc_dump($list_with_keys);
236
237 return $list_with_keys;
238 }
239
240 public function get_tree_table()
241 {
242 $this->db->select('*');
243 $query = $this->get();
244
245 $results = array();
246 if ($this->db->num_rows() > 0)
247 {
248 $results = $this->db->results();
249
250 $results = $this->parent_ordered_list($results);
251 }
252
253 return $results;
254 }
255
256 public function parent_ordered_list(&$results)
257 {
258 $tree_list = array();
259
260 foreach($results as $result)
261 {
262 $tree_list[$result->parent_id][$result->idcategory] = $result;
263 }
264
265 $ordered_list = array();
266
267 $this->_parent_ordered_list(0, $tree_list, $ordered_list);
268
269 return $ordered_list;
270 }
271
272 // recursive function because of tree structure and ordering
273 private function _parent_ordered_list($parent_id , &$tree_list, &$ordered_list, $level=-1)
274 {
275 $level++;
276
277 if(isset($tree_list[$parent_id]))
278 foreach($tree_list[$parent_id] as $tree_id => $tree_item)
279 {
280 $tree_item->level = $level;
281 $ordered_list[$tree_id] = $tree_item;
282
283 $this->_parent_ordered_list($tree_item->idcategory , $tree_list, $ordered_list, $level);
284 }
285 }
286
287 public function get_data($category_id = NULL) {
288 static $data = array();
289 if(empty($data)){
290 $query = $this->get();
291 if ($this->db->num_rows() > 0) {
292 foreach($this->db->results() as $category) {
293 $data[$category->idcategory] = $category;
294 }
295 }
296 }
297
298 if(!empty($category_id))
299 if(isset($data[(int)$category_id])){
300 return $data[(int)$category_id];
301 } else {
302 return NULL;
303 }
304
305 return $data;
306
307 }
308
309 public function delete($id)
310 {
311 $filter = $this->_primary_filter;
312 $id = $filter($id);
313
314 if(!$id)
315 {
316 return FALSE;
317 }
318
319 $item = $this->get($id, TRUE);
320
321 if(is_object($item))
322 {
323 // update all childs parent_id
324 $query = 'UPDATE '.$this->_table_name.' SET parent_id = '.$item->parent_id;
325 $query.= ' , `level` = '.$item->level;
326 $query.= ' , `order_index` = '.$item->order_index;
327 $query.= ' WHERE parent_id ='.$id.';';
328 $this->db->query($query);
329 }
330
331 $this->db->where($this->_primary_key, $id);
332 $this->db->limit(1);
333 $this->db->delete($this->_table_name);
334 }
335
336 /* only admin can edit */
337 public function is_related($item_id, $user_id, $method = 'edit')
338 {
339 return false;
340 }
341
342 public function get_all_childs($id)
343 {
344 $childs = array();
345
346 // Fetch pages without parents
347 $this->db->select('idcategory');
348 $this->db->where(array('(CONCAT(",", parent_path, ",") LIKE CONCAT("%,", '.esc_sql($id).', ",%"))' => NULL));
349
350 $this->db->from($this->_table_name);
351 $categories = $this->get();
352 if(count($categories))
353 {
354 foreach($categories as $category)
355 {
356 $childs[] = $category->idcategory;
357 }
358 }
359 return $childs;
360 }
361
362 public function get_all_childs_deprecated($id)
363 {
364 $categories = array();
365 $childs = array();
366 // Fetch pages without parents
367 $this->db->select('idcategory, level, parent_id');
368 $this->db->where(array('(parent_id = '.esc_sql($id).')' => NULL));
369
370 $this->db->from($this->_table_name);
371 $categories = $this->get();
372 if(count($categories))
373 {
374 foreach($categories as $category)
375 {
376 $childs[] = $category->idcategory;
377 $this->_get_all_childs_recursive($category->idcategory, $childs);
378 }
379 }
380 return $childs;
381 }
382
383 private function _get_all_childs_recursive($parent_id, &$childs = array())
384 {
385 $categories = array();
386 // Fetch pages without parents
387 $this->db->select('idcategory, level, parent_id');
388 $this->db->where(array('(parent_id = '.esc_sql($parent_id).')' => NULL));
389
390 $this->db->from($this->_table_name);
391 $categories = $this->get();
392 if(count($categories))
393 {
394 foreach($categories as $category)
395 {
396 $childs[] = $category->idcategory;
397 $this->_get_all_childs_recursive($category->idcategory, $childs);
398 }
399 }
400 }
401
402 public function get_max_level()
403 {
404 $this->db->select('MAX(`level`) as `level`');
405 $this->db->from($this->_table_name);
406 $row = $this->get(NULL, TRUE);
407 if(!empty($row))
408 {
409 return (wmvc_show_data('level', $row, 0, TRUE, TRUE) + 1);
410 }
411
412 return 1;
413 }
414 }
415 ?>