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

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