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

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

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