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

320 lines 8.7 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 $this->db->select($this->_table_name.'.*, COUNT('.$this->listing_m->_table_name.'.post_id) AS listings_counter');
54 $this->db->from($this->_table_name);
55
56 $this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.location_id = '.$this->_table_name.'.idlocation', TRUE, 'LEFT');
57
58 $this->db->from($this->_table_name);
59 $this->db->where($where);
60 $this->db->limit($limit);
61 $this->db->offset($offset);
62 $this->db->group_by('idlocation');
63
64 if(!empty($order_by)){
65 $this->db->order_by($order_by);
66 } else {
67 $this->db->order_by($this->_order_by);
68 }
69
70 $query = $this->db->get();
71
72 if ($this->db->num_rows() > 0)
73 return $this->db->results();
74
75 return array();
76 }
77
78 public function check_deletable($id)
79 {
80 return true;
81 }
82
83
84 /* [END] For dynamic data table */
85
86 public function insert($data, $id = NULL)
87 {
88 if(!is_array($data))
89 {
90 echo 'Missing data for insert in model';
91 return NULL;
92 }
93
94 if($this->_timestamps === TRUE && !isset($data['date']))
95 $data['date'] = current_time( 'mysql' );
96
97 $data['level'] = 0;
98 $data['parent_path'] = '';
99
100 if(!empty($data['parent_id']))
101 {
102 $parent = $this->get($data['parent_id']);
103
104 if(isset($parent[0]))
105 {
106 $data['level'] = $parent[0]->level + 1;
107 $data['parent_path'] = $parent[0]->parent_path.(!empty($parent[0]->parent_path)?',':'').$parent[0]->idlocation;
108 }
109 }
110
111 if(empty($data['order_index']))
112 {
113 $data['order_index'] = $this->max_order($data['parent_id'])+1;
114 }
115
116 if(!empty($id))
117 {
118 return $this->db->update($this->_table_name, $data, $id, $this->_primary_key);
119 }
120
121 return $this->db->insert($this->_table_name, $data);
122 }
123
124 public function get_parents($id = NULL, $level_show=true)
125 {
126 $this->db->select('*');
127
128 if(!empty($id))
129 {
130 $this->db->where(array('idlocation !=' => $id));
131 $this->db->where(array('parent_id !=' => $id));
132 }
133
134 $query = $this->get();
135
136 //$list_with_keys = array(0 => __('Root', 'wpdirectorykit'));
137 $list_with_keys = array();
138 if ($this->db->num_rows() > 0)
139 {
140 $results = $this->db->results();
141
142 $results = $this->parent_ordered_list($results);
143
144 foreach($results as $result)
145 {
146 $level_gen = str_pad('', $result->level*12, '&nbsp;');
147
148 $level_gen.='|-';
149
150 if(!$level_show)
151 $level_gen = '';
152
153 $list_with_keys[$result->idlocation] = $level_gen.$result->location_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->idlocation] = $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->idlocation , $tree_list, $ordered_list, $level);
208 }
209 }
210
211 public function delete($id)
212 {
213 $filter = $this->_primary_filter;
214 $id = $filter($id);
215
216 if(!$id)
217 {
218 return FALSE;
219 }
220
221 $item = $this->get($id, TRUE);
222
223 if(is_object($item))
224 {
225 // update all childs parent_id
226 $query = 'UPDATE '.$this->_table_name.' SET parent_id = '.$item->parent_id;
227 $query.= ' , `level` = '.$item->level;
228 $query.= ' , `order_index` = '.$item->order_index;
229 $query.= ' WHERE parent_id ='.$id.';';
230 $this->db->query($query);
231 }
232
233 $this->db->where($this->_primary_key, $id);
234 $this->db->limit(1);
235 $this->db->delete($this->_table_name);
236 }
237
238 /* only admin can edit */
239 public function is_related($item_id, $user_id, $method = 'edit')
240 {
241 return false;
242 }
243
244 public function get_all_childs($id)
245 {
246 $childs = array();
247
248 // Fetch pages without parents
249 $this->db->select('idlocation');
250 $this->db->where(array('(CONCAT(",", parent_path, ",") LIKE CONCAT("%,", '.esc_sql($id).', ",%"))' => NULL));
251
252 $this->db->from($this->_table_name);
253 $locations = $this->get();
254 if(count($locations))
255 {
256 foreach($locations as $location)
257 {
258 $childs[] = $location->idlocation;
259 }
260 }
261 return $childs;
262 }
263
264 /* optimization */
265 public function get_all_childs_deprecated($id)
266 {
267 $locations = array();
268 $childs = array();
269 // Fetch pages without parents
270 $this->db->select('idlocation, level, parent_id');
271 $this->db->where(array('(parent_id = '.esc_sql($id).')' => NULL));
272
273 $this->db->from($this->_table_name);
274 $locations = $this->get();
275 if(count($locations))
276 {
277 foreach($locations as $location)
278 {
279 $childs[] = $location->idlocation;
280 $this->_get_all_childs_recursive($location->idlocation, $childs);
281 }
282 }
283 return $childs;
284 }
285
286 private function _get_all_childs_recursive($parent_id, &$childs = array())
287 {
288 $locations = array();
289 // Fetch pages without parents
290 $this->db->select('idlocation, level, parent_id');
291 $this->db->where(array('(parent_id = '.esc_sql($parent_id).')' => NULL));
292
293 $this->db->from($this->_table_name);
294 $locations = $this->get();
295 if(count($locations))
296 {
297 foreach($locations as $location)
298 {
299 $childs[] = $location->idlocation;
300 $this->_get_all_childs_recursive($location->idlocation, $childs);
301 }
302 }
303 }
304
305
306 public function get_max_level()
307 {
308 $this->db->select('MAX(`level`) as `level`');
309 $this->db->from($this->_table_name);
310 $row = $this->get(NULL, TRUE);
311 if(!empty($row))
312 {
313 return (wmvc_show_data('level', $row, 0, TRUE, TRUE) + 1);
314 }
315
316 return 1;
317 }
318
319 }
320 ?>