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

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