PluginProbe
WP Directory Kit / 1.4.8
WP Directory Kit v1.4.8
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 1.4.0 1.4.1 All 35 releases
wpdirectorykit / application / models / Category_m.php

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

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