PluginProbe
WP Directory Kit / 1.2.4
WP Directory Kit v1.2.4
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.4, at application/models/Location_m.php

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