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

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

335 lines 13.8 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 if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) )
78 {
79 if($show_other_agents_litings)
80 {
81 $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');
82 $this->db->distinct($this->_table_name.'.post_id');
83 if(!is_null($user_id) && !empty($user_id))
84 {
85 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$user_id.' )'=>NULL));
86 }
87 elseif($this->current_user_id)
88 {
89 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$this->current_user_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$this->current_user_id.' )'=>NULL));
90 }
91 } else {
92 if(!is_null($user_id) && !empty($user_id))
93 {
94 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id);
95 }
96 elseif($this->current_user_id)
97 {
98 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id);
99 }
100 }
101 }
102
103 $this->db->where($where);
104
105 if(wdk_get_option('wdk_sub_listings_enable')) {
106 $this->db->where(array('listing_parent_post_id IS NULL'=>NULL));
107 }
108
109 $this->db->order_by($this->_order_by);
110
111 $query = $this->db->get();
112
113 $res = $this->db->results();
114
115 $this->_primary_key = 'post_id';
116 $this->_order_by = 'post_id DESC';
117
118 if(isset($res[0]->total_count))
119 return $res[0]->total_count;
120
121 return 0;
122 }
123
124 public function get_pagination($limit, $offset, $where = array(), $user_check = FALSE, $user_id=NULL, $show_other_agents_litings = FALSE)
125 {
126 $this->load->model('cachedusers_m');
127
128 $this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id';
129 $this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC';
130
131 $post_table = $this->db->prefix.'posts';
132 $fields_table = $this->db->prefix.'wdk_listings_fields';
133
134
135 if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) {
136 $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.'.*');
137 } else {
138 $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');
139 }
140
141 $this->db->from($this->_table_name);
142 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID');
143 $this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id');
144
145 /* join cached user data */
146 if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) {
147 $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');
148 }
149
150 /* locations */
151 $this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT');
152 $this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT');
153 /* for parents search*/
154 /*for($i = 2 ; $i <= 2; $i++) {
155 $this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT');
156 }*/
157
158 if(isset($where['is_activated'])) {
159 $where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated'];
160 unset($where['is_activated']);
161 }
162
163 if(isset($where['is_approved'])) {
164 $where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved'];
165 unset($where['is_approved']);
166 }
167
168 // [Favorites join]
169 if(!empty($user_id))
170 {
171 global $Winter_MVC_wdk_favorites;
172 if(isset($Winter_MVC_wdk_favorites))
173 {
174 $this->db->select('favorite_table.idfavorite as is_favorite');
175 $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');
176 }
177 }
178 else {
179 global $Winter_MVC_wdk_favorites;
180 if($this->current_user_id != 0 && isset($Winter_MVC_wdk_favorites))
181 {
182 $this->db->select('favorite_table.idfavorite as is_favorite');
183 $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');
184 }
185 }
186
187 // [/Favorites join]
188 if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) )
189 {
190 if($show_other_agents_litings)
191 {
192 $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');
193 $this->db->distinct($this->_table_name.'.post_id');
194 if(!is_null($user_id) && !empty($user_id))
195 {
196 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$user_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$user_id.' )'=>NULL));
197 }
198 elseif($this->current_user_id)
199 {
200 $this->db->where(array('(`'.$this->db->prefix.'wdk_listings`.`user_id_editor` = '.$this->current_user_id.' OR `'.$this->db->prefix.'wdk_listings_users`.`user_id` = '.$this->current_user_id.' )'=>NULL));
201 }
202 } else {
203 if(!is_null($user_id) && !empty($user_id))
204 {
205 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id);
206 }
207 elseif($this->current_user_id)
208 {
209 $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id);
210 }
211 }
212
213 $this->db->where($where);
214 } else {
215 $this->db->where($where);
216 }
217
218 if(wdk_get_option('wdk_sub_listings_enable')) {
219 $this->db->where(array('listing_parent_post_id IS NULL'=>NULL));
220 }
221
222 $this->db->limit($limit);
223 $this->db->offset($offset);
224
225 $this->db->order_by($this->_order_by);
226
227 $query = $this->db->get();
228
229 $this->_primary_key = 'post_id';
230 $this->_order_by = 'post_id DESC';
231
232 if ($this->db->num_rows() > 0)
233 return $this->db->results();
234
235 return array();
236
237 }
238
239 public function get($id = NULL, $single = FALSE)
240 {
241 $this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id';
242 $this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC';
243
244 $post_table = $this->db->prefix.'posts';
245 $fields_table = $this->db->prefix.'wdk_listings_fields';
246
247 $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');
248 $this->db->from($this->_table_name);
249 $this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID');
250 $this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id');
251 $this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->listing_m->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT');
252 $this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->listing_m->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT');
253
254 /*$query = $this->db->get();
255
256 if ($this->db->num_rows() > 0)
257 return $this->db->row();
258
259 return array();*/
260
261 $return = parent::get($id, $single);
262
263 $this->_primary_key = 'post_id';
264 $this->_order_by = 'post_id DESC';
265
266 return $return;
267 }
268
269 public function check_deletable($post_id, $user_id=NULL)
270 {
271 if(wmvc_user_in_role('administrator')) return true;
272
273 $this->load->model('listing_m');
274 $listing = $this->listing_m->get($post_id, TRUE);
275
276 if($user_id != NULL)
277 if(wmvc_show_data('user_id_editor', $listing) == $user_id)
278 return true;
279
280 if(wmvc_show_data('user_id_editor', $listing) == $this->current_user_id)
281 return true;
282
283 return false;
284
285 }
286
287 public function delete($post_id, $user_id=NULL) {
288
289 if(!$this->check_deletable($post_id, $user_id)) return false;
290
291 $this->load->model('listingfield_m');
292 $this->load->model('listingusers_m');
293
294 /* remove listing */
295 parent::delete($post_id);
296 $this->listingfield_m->delete($post_id);
297 $this->listingusers_m->delete($post_id);
298
299 /* remove post */
300 wp_delete_post($post_id, true);
301
302 do_action('wpdirectorykit/model/listing/delete', $post_id);
303
304 return true;
305
306 }
307
308 /* [END] For dynamic data table */
309
310 public function is_related($item_id, $user_id, $method = 'edit')
311 {
312 $this->load->model('listing_m');
313 $listing = $this->listing_m->get($item_id, TRUE);
314 if(wmvc_show_data('user_id_editor', $listing) == $user_id)
315 return true;
316
317 return false;
318 }
319
320 public function update_counter($listing_id = NULL)
321 {
322 if(empty($listing_id) )
323 return false;
324
325 $counter = 0;
326 if(wdk_field_value('counter_views', $listing_id))
327 {
328 $counter = intval(wdk_field_value('counter_views', $listing_id));
329 }
330
331 $this->update(array('counter_views' => ++$counter), $listing_id);
332 }
333
334 }
335 ?>