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
← All changes | application/models/Location_m.php +115 -6 1.1.0 → 1.5.9 View file →
@@ -7,9 +7,9 @@
7 7
8 8 class Location_m extends Winter_MVC_Model {
9 9
10 10 public $_table_name = 'wdk_locations';
11 - public $_order_by = 'order_index';
11 + public $_order_by = 'order_index, idlocation';
12 12 public $_primary_key = 'idlocation';
13 13 public $_own_columns = array();
14 14 public $_timestamps = TRUE;
15 15 protected $_primary_filter = 'intval';
@@ -14,13 +14,49 @@
14 14 public $_timestamps = TRUE;
15 15 protected $_primary_filter = 'intval';
16 16 public $form_admin = array();
17 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 + );
18 41
19 42 public function __construct(){
20 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);
21 53 }
22 54
55 + public function getAllowedOrderBy() {
56 + return $this->allowedOrderBy;
57 + }
58 +
23 59 /* [START] For dynamic data table */
24 60
25 61 public function get_available_fields()
26 62 {
@@ -47,17 +83,45 @@
47 83 }
48 84
49 85 public function get_pagination($limit, $offset, $where = array(), $order_by = NULL)
50 86 {
51 - $this->db->select('*');
87 + $this->load->model('listing_m');
52 88
53 - $this->load->model('listing_m');
54 89
55 - $this->db->select($this->_table_name.'.*, COUNT('.$this->listing_m->_table_name.'.post_id) AS listings_counter');
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 +
56 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');
57 105
58 - $this->db->join($this->listing_m->_table_name.' ON '.$this->listing_m->_table_name.'.location_id = '.$this->_table_name.'.idlocation', TRUE, 'LEFT');
106 + if(!empty($order_by)){
107 + $this->db->order_by($order_by);
108 + } else {
109 + $this->db->order_by($this->_order_by);
110 + }
59 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 +
60 124 $this->db->from($this->_table_name);
61 125 $this->db->where($where);
62 126 $this->db->limit($limit);
63 127 $this->db->offset($offset);
@@ -95,8 +159,9 @@
95 159
96 160 if($this->_timestamps === TRUE && !isset($data['date']))
97 161 $data['date'] = current_time( 'mysql' );
98 162
163 + $data['level_0_id'] = 0;
99 164 $data['level'] = 0;
100 165 $data['parent_path'] = '';
101 166
102 167 if(!empty($data['parent_id']))
@@ -106,8 +171,14 @@
106 171 if(isset($parent[0]))
107 172 {
108 173 $data['level'] = $parent[0]->level + 1;
109 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 + }
110 181 }
111 182 }
112 183
113 184 if(empty($data['order_index']))
@@ -122,9 +193,9 @@
122 193
123 194 return $this->db->insert($this->_table_name, $data);
124 195 }
125 196
126 - public function get_parents($id = NULL)
197 + public function get_parents($id = NULL, $level_show=true)
127 198 {
128 199 $this->db->select('*');
129 200
130 201 if(!empty($id))
@@ -148,8 +219,11 @@
148 219 $level_gen = str_pad('', $result->level*12, ' ');
149 220
150 221 $level_gen.='|-';
151 222
223 + if(!$level_show)
224 + $level_gen = '';
225 +
152 226 $list_with_keys[$result->idlocation] = $level_gen.$result->location_title;
153 227 }
154 228
155 229
@@ -241,8 +315,29 @@
241 315 }
242 316
243 317 public function get_all_childs($id)
244 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 + {
245 340 $locations = array();
246 341 $childs = array();
247 342 // Fetch pages without parents
248 343 $this->db->select('idlocation, level, parent_id');
@@ -277,8 +372,22 @@
277 372 $childs[] = $location->idlocation;
278 373 $this->_get_all_childs_recursive($location->idlocation, $childs);
279 374 }
280 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;
281 390 }
282 391
283 392 }
284 393 ?>