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

306 lines 8.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';
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
52 $wp_usermeta_table = $this->db->prefix.'usermeta';
53
54 $this->load->model('listing_m');
55
56 $this->db->select($this->_table_name.'.*, COUNT('.$this->listing_m->_table_name.'.post_id) AS listings_counter');
57 $this->db->from($this->_table_name);
58
59 $this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.category_id = '.$this->_table_name.'.idcategory', TRUE, 'LEFT');
60
61 $this->db->where($where);
62 $this->db->limit($limit);
63 $this->db->offset($offset);
64
65 $this->db->group_by('idcategory');
66
67 if(!empty($order_by)){
68 $this->db->order_by($order_by);
69 } else {
70 $this->db->order_by($this->_order_by);
71 }
72
73 $query = $this->db->get();
74
75 if ($this->db->num_rows() > 0)
76 return $this->db->results();
77
78 return array();
79 }
80
81 public function check_deletable($id)
82 {
83 return true;
84 }
85
86
87 /* [END] For dynamic data table */
88
89 public function insert($data, $id = NULL)
90 {
91 if(!is_array($data))
92 {
93 echo 'Missing data for insert in model';
94 return NULL;
95 }
96
97 if($this->_timestamps === TRUE && !isset($data['date']))
98 $data['date'] = current_time( 'mysql' );
99
100 $data['level'] = 0;
101 $data['parent_path'] = '';
102
103 if(!empty($data['parent_id']))
104 {
105 $parent = $this->get($data['parent_id']);
106
107 if(isset($parent[0]))
108 {
109 $data['level'] = $parent[0]->level + 1;
110 $data['parent_path'] = $parent[0]->parent_path.(!empty($parent[0]->parent_path)?',':'').$parent[0]->idcategory;
111 }
112 }
113
114 if(empty($data['order_index']))
115 {
116 $data['order_index'] = $this->max_order($data['parent_id'])+1;
117 }
118
119 if(!empty($id))
120 {
121 return $this->db->update($this->_table_name, $data, $id, $this->_primary_key);
122 }
123
124 return $this->db->insert($this->_table_name, $data);
125 }
126
127 public function get_parents($id = NULL)
128 {
129 $this->db->select('*');
130
131 if(!empty($id))
132 {
133 $this->db->where(array('idcategory !=' => $id));
134 $this->db->where(array('parent_id !=' => $id));
135 }
136
137 $query = $this->get();
138
139 //$list_with_keys = array(0 => __('Root', 'wpdirectorykit'));
140 $list_with_keys = array();
141 if ($this->db->num_rows() > 0)
142 {
143 $results = $this->db->results();
144
145 $results = $this->parent_ordered_list($results);
146
147 foreach($results as $result)
148 {
149 $level_gen = str_pad('', $result->level*12, '&nbsp;');
150
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 $categories = array();
269 $childs = array();
270 // Fetch pages without parents
271 $this->db->select('idcategory, level, parent_id');
272 $this->db->where(array('(parent_id = '.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 $this->_get_all_childs_recursive($category->idcategory, $childs);
282 }
283 }
284 return $childs;
285 }
286
287 private function _get_all_childs_recursive($parent_id, &$childs = array())
288 {
289 $categories = array();
290 // Fetch pages without parents
291 $this->db->select('idcategory, level, parent_id');
292 $this->db->where(array('(parent_id = '.esc_sql($parent_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 }
305 }
306 ?>