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

284 lines 7.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';
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->db->select('*');
52
53 $this->load->model('listing_m');
54
55 $this->db->select($this->_table_name.'.*, COUNT('.$this->listing_m->_table_name.'.post_id) AS listings_counter');
56 $this->db->from($this->_table_name);
57
58 $this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.location_id = '.$this->_table_name.'.idlocation', TRUE, 'LEFT');
59
60 $this->db->from($this->_table_name);
61 $this->db->where($where);
62 $this->db->limit($limit);
63 $this->db->offset($offset);
64 $this->db->group_by('idlocation');
65
66 if(!empty($order_by)){
67 $this->db->order_by($order_by);
68 } else {
69 $this->db->order_by($this->_order_by);
70 }
71
72 $query = $this->db->get();
73
74 if ($this->db->num_rows() > 0)
75 return $this->db->results();
76
77 return array();
78 }
79
80 public function check_deletable($id)
81 {
82 return true;
83 }
84
85
86 /* [END] For dynamic data table */
87
88 public function insert($data, $id = NULL)
89 {
90 if(!is_array($data))
91 {
92 echo 'Missing data for insert in model';
93 return NULL;
94 }
95
96 if($this->_timestamps === TRUE && !isset($data['date']))
97 $data['date'] = current_time( 'mysql' );
98
99 $data['level'] = 0;
100 $data['parent_path'] = '';
101
102 if(!empty($data['parent_id']))
103 {
104 $parent = $this->get($data['parent_id']);
105
106 if(isset($parent[0]))
107 {
108 $data['level'] = $parent[0]->level + 1;
109 $data['parent_path'] = $parent[0]->parent_path.(!empty($parent[0]->parent_path)?',':'').$parent[0]->idlocation;
110 }
111 }
112
113 if(empty($data['order_index']))
114 {
115 $data['order_index'] = $this->max_order($data['parent_id'])+1;
116 }
117
118 if(!empty($id))
119 {
120 return $this->db->update($this->_table_name, $data, $id, $this->_primary_key);
121 }
122
123 return $this->db->insert($this->_table_name, $data);
124 }
125
126 public function get_parents($id = NULL)
127 {
128 $this->db->select('*');
129
130 if(!empty($id))
131 {
132 $this->db->where(array('idlocation !=' => $id));
133 $this->db->where(array('parent_id !=' => $id));
134 }
135
136 $query = $this->get();
137
138 //$list_with_keys = array(0 => __('Root', 'wpdirectorykit'));
139 $list_with_keys = array();
140 if ($this->db->num_rows() > 0)
141 {
142 $results = $this->db->results();
143
144 $results = $this->parent_ordered_list($results);
145
146 foreach($results as $result)
147 {
148 $level_gen = str_pad('', $result->level*12, '&nbsp;');
149
150 $level_gen.='|-';
151
152 $list_with_keys[$result->idlocation] = $level_gen.$result->location_title;
153 }
154
155
156 }
157
158 //wmvc_dump($list_with_keys);
159
160 return $list_with_keys;
161 }
162
163 public function get_tree_table()
164 {
165 $this->db->select('*');
166 $query = $this->get();
167
168 $results = array();
169 if ($this->db->num_rows() > 0)
170 {
171 $results = $this->db->results();
172
173 $results = $this->parent_ordered_list($results);
174 }
175
176 return $results;
177 }
178
179 public function parent_ordered_list(&$results)
180 {
181 $tree_list = array();
182
183 foreach($results as $result)
184 {
185 $tree_list[$result->parent_id][$result->idlocation] = $result;
186 }
187
188 $ordered_list = array();
189
190 $this->_parent_ordered_list(0, $tree_list, $ordered_list);
191
192 return $ordered_list;
193 }
194
195 // recursive function because of tree structure and ordering
196 private function _parent_ordered_list($parent_id , &$tree_list, &$ordered_list, $level=-1)
197 {
198 $level++;
199
200 if(isset($tree_list[$parent_id]))
201 foreach($tree_list[$parent_id] as $tree_id => $tree_item)
202 {
203 $tree_item->level = $level;
204 $ordered_list[$tree_id] = $tree_item;
205
206 $this->_parent_ordered_list($tree_item->idlocation , $tree_list, $ordered_list, $level);
207 }
208 }
209
210 public function delete($id)
211 {
212 $filter = $this->_primary_filter;
213 $id = $filter($id);
214
215 if(!$id)
216 {
217 return FALSE;
218 }
219
220 $item = $this->get($id, TRUE);
221
222 if(is_object($item))
223 {
224 // update all childs parent_id
225 $query = 'UPDATE '.$this->_table_name.' SET parent_id = '.$item->parent_id;
226 $query.= ' , `level` = '.$item->level;
227 $query.= ' , `order_index` = '.$item->order_index;
228 $query.= ' WHERE parent_id ='.$id.';';
229 $this->db->query($query);
230 }
231
232 $this->db->where($this->_primary_key, $id);
233 $this->db->limit(1);
234 $this->db->delete($this->_table_name);
235 }
236
237 /* only admin can edit */
238 public function is_related($item_id, $user_id, $method = 'edit')
239 {
240 return false;
241 }
242
243 public function get_all_childs($id)
244 {
245 $locations = array();
246 $childs = array();
247 // Fetch pages without parents
248 $this->db->select('idlocation, level, parent_id');
249 $this->db->where(array('(parent_id = '.esc_sql($id).')' => NULL));
250
251 $this->db->from($this->_table_name);
252 $locations = $this->get();
253 if(count($locations))
254 {
255 foreach($locations as $location)
256 {
257 $childs[] = $location->idlocation;
258 $this->_get_all_childs_recursive($location->idlocation, $childs);
259 }
260 }
261 return $childs;
262 }
263
264 private function _get_all_childs_recursive($parent_id, &$childs = array())
265 {
266 $locations = array();
267 // Fetch pages without parents
268 $this->db->select('idlocation, level, parent_id');
269 $this->db->where(array('(parent_id = '.esc_sql($parent_id).')' => NULL));
270
271 $this->db->from($this->_table_name);
272 $locations = $this->get();
273 if(count($locations))
274 {
275 foreach($locations as $location)
276 {
277 $childs[] = $location->idlocation;
278 $this->_get_all_childs_recursive($location->idlocation, $childs);
279 }
280 }
281 }
282
283 }
284 ?>