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 / Listing_m.php

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

373 lines 16.0 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 Listing_m extends Winter_MVC_Model {
9
10 public $_table_name = 'wdk_listings';
11 public $_order_by = 'post_id DESC';
12 public $_primary_key = 'post_id';
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 public $current_user_id = NULL;
19
20 public function __construct(){
21 $this->current_user_id = get_current_user_id();
22 parent::__construct();
23 }
24
25 /* [START] For dynamic data table */
26
27 public function get_available_fields()
28 {
29 $fields = $this->db->list_fields($this->_table_name);
30
31 return $fields;
32 }
33
34 public function total($where = array(), $user_check=FALSE, $user_id=NULL, $show_other_agents_litings = FALSE)
35 {
36 $this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id';
37 $this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC';
38
39 $post_table = $this->db->prefix.'posts';
40 $fields_table = $this->db->prefix.'wdk_listings_fields';
41
42 $this->db->select('COUNT(DISTINCT '.$this->db->prefix.'wdk_listings.post_id) as total_count');
43 $this->db->from($this->_table_name);
44
45 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID');
46 $this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id');
47
48 /* locations */
49 $this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT');
50 $this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT');
51 /* for parents search*/
52 /*
53 for($i = 2 ; $i <= 2; $i++) {
54 $this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT');
55 }*/
56
57 // [Favorites join]
58
59 global $Winter_MVC_wdk_favorites;
60 if(!empty($user_id) && isset($Winter_MVC_wdk_favorites))
61 {
62 $this->db->join($this->db->prefix.'wdk_favorite AS favorite_table ON '.$this->_table_name.'.post_id = favorite_table.post_id', NULL, 'LEFT');
63 }
64
65 // [/Favorites join]
66
67 if(isset($where['is_activated'])){
68 $where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated'];
69 unset($where['is_activated']);
70 }
71
72 if(isset($where['is_approved'])){
73 $where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved'];
74 unset($where['is_approved']);
75 }
76
77
78 if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) )
79 {
80 if($show_other_agents_litings)
81 {
82 $this->db->join($this->db->prefix.'wdk_listings_users ON '.$this->_table_name.'.post_id = '.$this->db->prefix.'wdk_listings_users.post_id', NULL, 'LEFT');
83
84 if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) {
85 $this->db->join($this->db->prefix.'wdk_membership_agency_agent ON
86 ('.$this->db->prefix.'wdk_membership_agency_agent.agency_id = '.esc_sql($user_id).'
87 AND '.$this->db->prefix.'wdk_membership_agency_agent.agent_id = `'.$this->db->prefix.'wdk_listings`.`user_id_editor` AND '.$this->db->prefix.'wdk_membership_agency_agent.status = "CONFIRMED")', NULL, 'LEFT');
88 }
89
90 $this->db->distinct($this->_table_name.'.post_id');
91
92 $user_check_id = NULL;
93 if(!is_null($user_id) && !empty($user_id))
94 {
95 $user_check_id = $user_id;
96 }
97 elseif($this->current_user_id)
98 {
99 $user_check_id = $this->current_user_id;
100 }
101
102 if($user_check_id)
103 {
104 if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) {
105 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_check_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$user_check_id.' OR `'.$this->db->prefix.'wdk_membership_agency_agent`.`agency_id` = '.$user_check_id.' )'=>NULL));
106 } else {
107 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_check_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$user_check_id.' )'=>NULL));
108 }
109 }
110
111 } else {
112 if(!is_null($user_id) && !empty($user_id))
113 {
114 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id);
115 }
116 elseif($this->current_user_id)
117 {
118 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id);
119 }
120 }
121 }
122
123 $this->db->where($where);
124
125 if(wdk_get_option('wdk_sub_listings_enable')) {
126 $this->db->where(array('listing_parent_post_id IS NULL'=>NULL));
127 }
128
129 $this->db->order_by($this->_order_by);
130
131 $query = $this->db->get();
132
133 $res = $this->db->results();
134
135 $this->_primary_key = 'post_id';
136 $this->_order_by = 'post_id DESC';
137
138 if(isset($res[0]->total_count))
139 return $res[0]->total_count;
140
141 return 0;
142 }
143
144 public function get_pagination($limit, $offset, $where = array(), $user_check = FALSE, $user_id=NULL, $show_other_agents_litings = FALSE)
145 {
146 $this->load->model('cachedusers_m');
147
148 $this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id';
149 $this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC';
150
151 $post_table = $this->db->prefix.'posts';
152 $fields_table = $this->db->prefix.'wdk_listings_fields';
153
154
155 if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) {
156 $this->db->select($this->_table_name.'.*, '.$post_table.'.*,'.$fields_table.'.*,'.$fields_table.'.post_id AS fields_post_id, location_table.location_title, category_table.category_title,'.$this->cachedusers_m->_table_name.'.*');
157 } else {
158 $this->db->select($this->_table_name.'.*, '.$post_table.'.*,'.$fields_table.'.*,'.$fields_table.'.post_id AS fields_post_id, location_table.location_title, category_table.category_title');
159 }
160
161 $this->db->from($this->_table_name);
162 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID');
163 $this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id');
164
165 /* join cached user data */
166 if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) {
167 $this->db->join($this->cachedusers_m->_table_name.' ON '.$this->cachedusers_m->_table_name.'.cacheduser_user_id = '.$this->_table_name.'.user_id_editor', TRUE, 'LEFT');
168 }
169
170 /* locations */
171 $this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT');
172 $this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT');
173 /* for parents search*/
174 /*for($i = 2 ; $i <= 2; $i++) {
175 $this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT');
176 }*/
177
178 if(isset($where['is_activated'])) {
179 $where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated'];
180 unset($where['is_activated']);
181 }
182
183 if(isset($where['is_approved'])) {
184 $where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved'];
185 unset($where['is_approved']);
186 }
187
188 // [Favorites join]
189 if(!empty($user_id))
190 {
191 global $Winter_MVC_wdk_favorites;
192 if(isset($Winter_MVC_wdk_favorites))
193 {
194 $this->db->select('favorite_table.idfavorite as is_favorite');
195 $this->db->join($this->db->prefix.'wdk_favorite AS favorite_table ON ('.$this->_table_name.'.post_id = favorite_table.post_id AND favorite_table.user_id = '.esc_sql($user_id).')', NULL, 'LEFT');
196 }
197 }
198 else {
199 global $Winter_MVC_wdk_favorites;
200 if($this->current_user_id != 0 && isset($Winter_MVC_wdk_favorites))
201 {
202 $this->db->select('favorite_table.idfavorite as is_favorite');
203 $this->db->join($this->db->prefix.'wdk_favorite AS favorite_table ON ('.$this->_table_name.'.post_id = favorite_table.post_id AND favorite_table.user_id = '.esc_sql($this->current_user_id).')', NULL, 'LEFT');
204 }
205 }
206
207 if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) )
208 {
209 if($show_other_agents_litings)
210 {
211 $this->db->join($this->db->prefix.'wdk_listings_users ON '.$this->_table_name.'.post_id = '.$this->db->prefix.'wdk_listings_users.post_id', NULL, 'LEFT');
212
213 if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) {
214 $this->db->join($this->db->prefix.'wdk_membership_agency_agent ON
215 ('.$this->db->prefix.'wdk_membership_agency_agent.agency_id = '.esc_sql($user_id).'
216 AND '.$this->db->prefix.'wdk_membership_agency_agent.agent_id = `'.$this->db->prefix.'wdk_listings`.`user_id_editor` AND '.$this->db->prefix.'wdk_membership_agency_agent.status = "CONFIRMED")', NULL, 'LEFT');
217 }
218
219 $this->db->distinct($this->_table_name.'.post_id');
220
221 $user_check_id = NULL;
222 if(!is_null($user_id) && !empty($user_id))
223 {
224 $user_check_id = $user_id;
225 }
226 elseif($this->current_user_id)
227 {
228 $user_check_id = $this->current_user_id;
229 }
230
231 if($user_check_id)
232 {
233 if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) {
234 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_check_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$user_check_id.' OR `'.$this->db->prefix.'wdk_membership_agency_agent`.`agency_id` = '.$user_check_id.' )'=>NULL));
235 } else {
236 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_check_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$user_check_id.' )'=>NULL));
237 }
238 }
239
240 } else {
241 if(!is_null($user_id) && !empty($user_id))
242 {
243 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id);
244 }
245 elseif($this->current_user_id)
246 {
247 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id);
248 }
249 }
250
251 $this->db->where($where);
252 } else {
253 $this->db->where($where);
254 }
255
256 if(wdk_get_option('wdk_sub_listings_enable')) {
257 $this->db->where(array('listing_parent_post_id IS NULL'=>NULL));
258 }
259
260 $this->db->limit($limit);
261 $this->db->offset($offset);
262
263 $this->db->order_by($this->_order_by);
264
265 $query = $this->db->get();
266
267 $this->_primary_key = 'post_id';
268 $this->_order_by = 'post_id DESC';
269
270 if ($this->db->num_rows() > 0)
271 return $this->db->results();
272
273 return array();
274
275 }
276
277 public function get($id = NULL, $single = FALSE)
278 {
279 $this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id';
280 $this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC';
281
282 $post_table = $this->db->prefix.'posts';
283 $fields_table = $this->db->prefix.'wdk_listings_fields';
284
285 $this->db->select($this->_table_name.'.*, '.$post_table.'.*,'.$fields_table.'.*,'.$fields_table.'.post_id AS fields_post_id,location_table.location_title, category_table.category_title');
286 $this->db->from($this->_table_name);
287 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID');
288 $this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id');
289 $this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->listing_m->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT');
290 $this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->listing_m->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT');
291
292 /*$query = $this->db->get();
293
294 if ($this->db->num_rows() > 0)
295 return $this->db->row();
296
297 return array();*/
298
299 $return = parent::get($id, $single);
300
301 $this->_primary_key = 'post_id';
302 $this->_order_by = 'post_id DESC';
303
304 return $return;
305 }
306
307 public function check_deletable($post_id, $user_id=NULL)
308 {
309 if(wmvc_user_in_role('administrator')) return true;
310
311 $this->load->model('listing_m');
312 $listing = $this->listing_m->get($post_id, TRUE);
313
314 if($user_id != NULL)
315 if(wmvc_show_data('user_id_editor', $listing) == $user_id)
316 return true;
317
318 if(wmvc_show_data('user_id_editor', $listing) == $this->current_user_id)
319 return true;
320
321 return false;
322
323 }
324
325 public function delete($post_id, $user_id=NULL) {
326
327 if(!$this->check_deletable($post_id, $user_id)) return false;
328
329 $this->load->model('listingfield_m');
330 $this->load->model('listingusers_m');
331
332 /* remove listing */
333 parent::delete($post_id);
334 $this->listingfield_m->delete($post_id);
335 $this->listingusers_m->delete($post_id);
336
337 /* remove post */
338 wp_delete_post($post_id, true);
339
340 do_action('wpdirectorykit/model/listing/delete', $post_id);
341
342 return true;
343
344 }
345
346 /* [END] For dynamic data table */
347
348 public function is_related($item_id, $user_id, $method = 'edit')
349 {
350 $this->load->model('listing_m');
351 $listing = $this->listing_m->get($item_id, TRUE);
352 if(wmvc_show_data('user_id_editor', $listing) == $user_id)
353 return true;
354
355 return false;
356 }
357
358 public function update_counter($listing_id = NULL)
359 {
360 if(empty($listing_id) )
361 return false;
362
363 $counter = 0;
364 if(wdk_field_value('counter_views', $listing_id))
365 {
366 $counter = intval(wdk_field_value('counter_views', $listing_id));
367 }
368
369 $this->update(array('counter_views' => ++$counter), $listing_id);
370 }
371
372 }
373 ?>