PluginProbe
WP Directory Kit / 1.5.9
WP Directory Kit v1.5.9
1.5.9 1.5.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 All 37 releases
wpdirectorykit / application / models / Location_m.php

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

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