PluginProbe
WP Directory Kit / 1.2.7
WP Directory Kit v1.2.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 1.4.0 1.4.1 All 35 releases
wpdirectorykit / application / models / Location_m.php

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

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