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 / Location_m.php

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

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