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

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

353 lines 10.1 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 check_deletable($id)
86 {
87 return true;
88 }
89
90
91 /* [END] For dynamic data table */
92
93 public function insert($data, $id = NULL)
94 {
95 if(!is_array($data))
96 {
97 echo 'Missing data for insert in model';
98 return NULL;
99 }
100
101 if($this->_timestamps === TRUE && !isset($data['date']))
102 $data['date'] = current_time( 'mysql' );
103
104 $data['level_0_id'] = 0;
105 $data['level'] = 0;
106 $data['parent_path'] = '';
107
108 if(!empty($data['parent_id']))
109 {
110 $parent = $this->get($data['parent_id']);
111
112 if(isset($parent[0]))
113 {
114 $data['level'] = $parent[0]->level + 1;
115 $data['parent_path'] = $parent[0]->parent_path.(!empty($parent[0]->parent_path)?',':'').$parent[0]->idcategory;
116
117 if(strpos($data['parent_path'], ',') !== FALSE) {
118 $data['level_0_id'] = substr($data['parent_path'],0,strpos($data['parent_path'], ','));
119 } else {
120 $data['level_0_id'] = $data['parent_path'];
121 }
122 }
123 }
124
125 if(empty($data['order_index']))
126 {
127 $data['order_index'] = $this->max_order($data['parent_id'])+1;
128 }
129
130 if(!empty($id))
131 {
132 return $this->db->update($this->_table_name, $data, $id, $this->_primary_key);
133 }
134
135 return $this->db->insert($this->_table_name, $data);
136 }
137
138 public function get_parents($id = NULL, $level_show=true)
139 {
140 $this->db->select('*');
141
142 if(!empty($id))
143 {
144 $this->db->where(array('idcategory !=' => $id));
145 $this->db->where(array('parent_id !=' => $id));
146 }
147
148 $query = $this->get();
149
150 //$list_with_keys = array(0 => __('Root', 'wpdirectorykit'));
151 $list_with_keys = array();
152 if ($this->db->num_rows() > 0)
153 {
154 $results = $this->db->results();
155
156 $results = $this->parent_ordered_list($results);
157
158 foreach($results as $result)
159 {
160 $level_gen = str_pad('', $result->level*12, '&nbsp;');
161
162 $level_gen.='|-';
163
164 if(!$level_show)
165 $level_gen = '';
166
167 $list_with_keys[$result->idcategory] = $level_gen.$result->category_title;
168 }
169
170
171 }
172
173 //wmvc_dump($list_with_keys);
174
175 return $list_with_keys;
176 }
177
178 public function get_tree_table()
179 {
180 $this->db->select('*');
181 $query = $this->get();
182
183 $results = array();
184 if ($this->db->num_rows() > 0)
185 {
186 $results = $this->db->results();
187
188 $results = $this->parent_ordered_list($results);
189 }
190
191 return $results;
192 }
193
194 public function parent_ordered_list(&$results)
195 {
196 $tree_list = array();
197
198 foreach($results as $result)
199 {
200 $tree_list[$result->parent_id][$result->idcategory] = $result;
201 }
202
203 $ordered_list = array();
204
205 $this->_parent_ordered_list(0, $tree_list, $ordered_list);
206
207 return $ordered_list;
208 }
209
210 // recursive function because of tree structure and ordering
211 private function _parent_ordered_list($parent_id , &$tree_list, &$ordered_list, $level=-1)
212 {
213 $level++;
214
215 if(isset($tree_list[$parent_id]))
216 foreach($tree_list[$parent_id] as $tree_id => $tree_item)
217 {
218 $tree_item->level = $level;
219 $ordered_list[$tree_id] = $tree_item;
220
221 $this->_parent_ordered_list($tree_item->idcategory , $tree_list, $ordered_list, $level);
222 }
223 }
224
225 public function get_data($category_id = NULL) {
226 static $data = array();
227 if(empty($data)){
228 $query = $this->get();
229 if ($this->db->num_rows() > 0) {
230 foreach($this->db->results() as $category) {
231 $data[$category->idcategory] = $category;
232 }
233 }
234 }
235
236 if(!empty($category_id))
237 if(isset($data[(int)$category_id])){
238 return $data[(int)$category_id];
239 } else {
240 return NULL;
241 }
242
243 return $data;
244
245 }
246
247 public function delete($id)
248 {
249 $filter = $this->_primary_filter;
250 $id = $filter($id);
251
252 if(!$id)
253 {
254 return FALSE;
255 }
256
257 $item = $this->get($id, TRUE);
258
259 if(is_object($item))
260 {
261 // update all childs parent_id
262 $query = 'UPDATE '.$this->_table_name.' SET parent_id = '.$item->parent_id;
263 $query.= ' , `level` = '.$item->level;
264 $query.= ' , `order_index` = '.$item->order_index;
265 $query.= ' WHERE parent_id ='.$id.';';
266 $this->db->query($query);
267 }
268
269 $this->db->where($this->_primary_key, $id);
270 $this->db->limit(1);
271 $this->db->delete($this->_table_name);
272 }
273
274 /* only admin can edit */
275 public function is_related($item_id, $user_id, $method = 'edit')
276 {
277 return false;
278 }
279
280 public function get_all_childs($id)
281 {
282 $childs = array();
283
284 // Fetch pages without parents
285 $this->db->select('idcategory');
286 $this->db->where(array('(CONCAT(",", parent_path, ",") LIKE CONCAT("%,", '.esc_sql($id).', ",%"))' => NULL));
287
288 $this->db->from($this->_table_name);
289 $categories = $this->get();
290 if(count($categories))
291 {
292 foreach($categories as $category)
293 {
294 $childs[] = $category->idcategory;
295 }
296 }
297 return $childs;
298 }
299
300 public function get_all_childs_deprecated($id)
301 {
302 $categories = array();
303 $childs = array();
304 // Fetch pages without parents
305 $this->db->select('idcategory, level, parent_id');
306 $this->db->where(array('(parent_id = '.esc_sql($id).')' => NULL));
307
308 $this->db->from($this->_table_name);
309 $categories = $this->get();
310 if(count($categories))
311 {
312 foreach($categories as $category)
313 {
314 $childs[] = $category->idcategory;
315 $this->_get_all_childs_recursive($category->idcategory, $childs);
316 }
317 }
318 return $childs;
319 }
320
321 private function _get_all_childs_recursive($parent_id, &$childs = array())
322 {
323 $categories = array();
324 // Fetch pages without parents
325 $this->db->select('idcategory, level, parent_id');
326 $this->db->where(array('(parent_id = '.esc_sql($parent_id).')' => NULL));
327
328 $this->db->from($this->_table_name);
329 $categories = $this->get();
330 if(count($categories))
331 {
332 foreach($categories as $category)
333 {
334 $childs[] = $category->idcategory;
335 $this->_get_all_childs_recursive($category->idcategory, $childs);
336 }
337 }
338 }
339
340 public function get_max_level()
341 {
342 $this->db->select('MAX(`level`) as `level`');
343 $this->db->from($this->_table_name);
344 $row = $this->get(NULL, TRUE);
345 if(!empty($row))
346 {
347 return (wmvc_show_data('level', $row, 0, TRUE, TRUE) + 1);
348 }
349
350 return 1;
351 }
352 }
353 ?>