| @@ -38,13 +38,13 @@ | ||
| 38 | 38 | |
| 39 | 39 | $post_table = $this->db->prefix.'posts'; |
| 40 | 40 | $fields_table = $this->db->prefix.'wdk_listings_fields'; |
| 41 | 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'); | |
| 42 | + $this->db->select('COUNT(DISTINCT '.$post_table.'.ID) as total_count'); | |
| 43 | + $this->db->from($post_table); | |
| 44 | + | |
| 45 | + $this->db->join($this->_table_name.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT'); | |
| 46 | + $this->db->join($fields_table.' ON '.$post_table.'.ID = '.$fields_table.'.post_id', NULL, 'LEFT'); | |
| 47 | 47 | |
| 48 | 48 | /* locations */ |
| 49 | 49 | $this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT'); |
| 50 | 50 | $this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT'); |
| @@ -58,13 +58,15 @@ | ||
| 58 | 58 | |
| 59 | 59 | global $Winter_MVC_wdk_favorites; |
| 60 | 60 | if(!empty($user_id) && isset($Winter_MVC_wdk_favorites)) |
| 61 | 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'); | |
| 62 | + $this->db->join($this->db->prefix.'wdk_favorite AS favorite_table ON '.$post_table.'.ID = favorite_table.post_id', NULL, 'LEFT'); | |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | // [/Favorites join] |
| 66 | 66 | |
| 67 | + $this->db->where($post_table.'.post_type = \'wdk-listing\'', NULL); | |
| 68 | + | |
| 67 | 69 | if(isset($where['is_activated'])){ |
| 68 | 70 | $where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated']; |
| 69 | 71 | unset($where['is_activated']); |
| 70 | 72 | } |
| @@ -73,38 +75,58 @@ | ||
| 73 | 75 | $where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved']; |
| 74 | 76 | unset($where['is_approved']); |
| 75 | 77 | } |
| 76 | 78 | |
| 77 | - if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) ) | |
| 79 | + | |
| 80 | + if( (!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) ) | |
| 78 | 81 | { |
| 79 | 82 | if($show_other_agents_litings) |
| 80 | 83 | { |
| 81 | 84 | $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'); |
| 85 | + | |
| 86 | + if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) { | |
| 87 | + $this->db->join($this->db->prefix.'wdk_membership_agency_agent ON | |
| 88 | + ('.$this->db->prefix.'wdk_membership_agency_agent.agency_id = '.esc_sql($user_id).' | |
| 89 | + 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'); | |
| 90 | + } | |
| 91 | + | |
| 82 | 92 | $this->db->distinct($this->_table_name.'.post_id'); |
| 93 | + | |
| 94 | + $user_check_id = NULL; | |
| 83 | 95 | if(!is_null($user_id) && !empty($user_id)) |
| 84 | 96 | { |
| 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)); | |
| 97 | + $user_check_id = $user_id; | |
| 86 | 98 | } |
| 87 | 99 | elseif($this->current_user_id) |
| 88 | 100 | { |
| 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)); | |
| 101 | + $user_check_id = $this->current_user_id; | |
| 90 | 102 | } |
| 103 | + | |
| 104 | + if($user_check_id) | |
| 105 | + { | |
| 106 | + if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) { | |
| 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.' OR `'.$this->db->prefix.'wdk_membership_agency_agent`.`agency_id` = '.$user_check_id.' )'=>NULL)); | |
| 108 | + } else { | |
| 109 | + $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)); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 91 | 113 | } else { |
| 92 | 114 | if(!is_null($user_id) && !empty($user_id)) |
| 93 | 115 | { |
| 94 | 116 | $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id); |
| 95 | 117 | } |
| 96 | - elseif($this->current_user_id) | |
| 118 | + elseif($this->current_user_id) | |
| 97 | 119 | { |
| 98 | 120 | $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id); |
| 99 | 121 | } |
| 100 | 122 | } |
| 101 | 123 | } |
| 102 | - | |
| 124 | + | |
| 103 | 125 | $this->db->where($where); |
| 104 | 126 | |
| 105 | 127 | if(wdk_get_option('wdk_sub_listings_enable')) { |
| 106 | - $this->db->where(array('listing_parent_post_id IS NULL'=>NULL)); | |
| 128 | + $this->db->where(array('(listing_parent_post_id IS NULL OR listing_parent_post_id = 0)'=>NULL)); | |
| 107 | 129 | } |
| 108 | 130 | |
| 109 | 131 | $this->db->order_by($this->_order_by); |
| 110 | 132 | |
| @@ -137,11 +159,12 @@ | ||
| 137 | 159 | } else { |
| 138 | 160 | $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 | 161 | } |
| 140 | 162 | |
| 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'); | |
| 163 | + $this->db->from($post_table); | |
| 164 | + | |
| 165 | + $this->db->join($this->_table_name.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID', NULL, 'LEFT'); | |
| 166 | + $this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id', NULL, 'LEFT'); | |
| 144 | 167 | |
| 145 | 168 | /* join cached user data */ |
| 146 | 169 | if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) { |
| 147 | 170 | $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'); |
| @@ -154,8 +177,10 @@ | ||
| 154 | 177 | /*for($i = 2 ; $i <= 2; $i++) { |
| 155 | 178 | $this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT'); |
| 156 | 179 | }*/ |
| 157 | 180 | |
| 181 | + $this->db->where($post_table.'.post_type = \'wdk-listing\'', NULL); | |
| 182 | + | |
| 158 | 183 | if(isset($where['is_activated'])) { |
| 159 | 184 | $where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated']; |
| 160 | 185 | unset($where['is_activated']); |
| 161 | 186 | } |
| @@ -183,23 +208,41 @@ | ||
| 183 | 208 | $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 | 209 | } |
| 185 | 210 | } |
| 186 | 211 | |
| 187 | - // [/Favorites join] | |
| 188 | - if( (!wmvc_user_in_role('administrator') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) ) | |
| 212 | + if( (!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) ) | |
| 189 | 213 | { |
| 190 | 214 | if($show_other_agents_litings) |
| 191 | 215 | { |
| 192 | 216 | $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'); |
| 217 | + | |
| 218 | + if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) { | |
| 219 | + $this->db->join($this->db->prefix.'wdk_membership_agency_agent ON | |
| 220 | + ('.$this->db->prefix.'wdk_membership_agency_agent.agency_id = '.esc_sql($user_id).' | |
| 221 | + 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'); | |
| 222 | + } | |
| 223 | + | |
| 193 | 224 | $this->db->distinct($this->_table_name.'.post_id'); |
| 225 | + | |
| 226 | + $user_check_id = NULL; | |
| 194 | 227 | if(!is_null($user_id) && !empty($user_id)) |
| 195 | 228 | { |
| 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)); | |
| 229 | + $user_check_id = $user_id; | |
| 197 | 230 | } |
| 198 | 231 | elseif($this->current_user_id) |
| 199 | 232 | { |
| 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)); | |
| 233 | + $user_check_id = $this->current_user_id; | |
| 201 | 234 | } |
| 235 | + | |
| 236 | + if($user_check_id) | |
| 237 | + { | |
| 238 | + if(function_exists('run_wdk_membership') && file_exists(WDK_MEMBERSHIP_PATH.'application/models/Agency_agent_m.php')) { | |
| 239 | + $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)); | |
| 240 | + } else { | |
| 241 | + $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)); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 202 | 245 | } else { |
| 203 | 246 | if(!is_null($user_id) && !empty($user_id)) |
| 204 | 247 | { |
| 205 | 248 | $this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id); |
| @@ -215,9 +258,9 @@ | ||
| 215 | 258 | $this->db->where($where); |
| 216 | 259 | } |
| 217 | 260 | |
| 218 | 261 | if(wdk_get_option('wdk_sub_listings_enable')) { |
| 219 | - $this->db->where(array('listing_parent_post_id IS NULL'=>NULL)); | |
| 262 | + $this->db->where(array('(listing_parent_post_id IS NULL OR listing_parent_post_id = 0)'=>NULL)); | |
| 220 | 263 | } |
| 221 | 264 | |
| 222 | 265 | $this->db->limit($limit); |
| 223 | 266 | $this->db->offset($offset); |
| @@ -228,8 +271,10 @@ | ||
| 228 | 271 | |
| 229 | 272 | $this->_primary_key = 'post_id'; |
| 230 | 273 | $this->_order_by = 'post_id DESC'; |
| 231 | 274 | |
| 275 | + //echo $this->db->last_query(); | |
| 276 | + | |
| 232 | 277 | if ($this->db->num_rows() > 0) |
| 233 | 278 | return $this->db->results(); |
| 234 | 279 | |
| 235 | 280 | return array(); |
| @@ -267,9 +312,9 @@ | ||
| 267 | 312 | } |
| 268 | 313 | |
| 269 | 314 | public function check_deletable($post_id, $user_id=NULL) |
| 270 | 315 | { |
| 271 | - if(wmvc_user_in_role('administrator')) return true; | |
| 316 | + if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true; | |
| 272 | 317 | |
| 273 | 318 | $this->load->model('listing_m'); |
| 274 | 319 | $listing = $this->listing_m->get($post_id, TRUE); |
| 275 | 320 | |
| @@ -289,9 +334,23 @@ | ||
| 289 | 334 | if(!$this->check_deletable($post_id, $user_id)) return false; |
| 290 | 335 | |
| 291 | 336 | $this->load->model('listingfield_m'); |
| 292 | 337 | $this->load->model('listingusers_m'); |
| 338 | + | |
| 339 | + /* remove images direct */ | |
| 340 | + if ( get_option('wdk_remove_direct_listing_images', false)) { | |
| 341 | + global $Winter_MVC_WDK; | |
| 342 | + $Winter_MVC_WDK->load_helper('listing'); | |
| 293 | 343 | |
| 344 | + $listing_images = wdk_field_value('listing_images', $post_id); | |
| 345 | + if(!empty($listing_images)) { | |
| 346 | + $image_ids = explode(',', $listing_images); | |
| 347 | + foreach ($image_ids as $key => $image_id) { | |
| 348 | + wp_delete_attachment($image_id, true); | |
| 349 | + } | |
| 350 | + } | |
| 351 | + } | |
| 352 | + | |
| 294 | 353 | /* remove listing */ |
| 295 | 354 | parent::delete($post_id); |
| 296 | 355 | $this->listingfield_m->delete($post_id); |
| 297 | 356 | $this->listingusers_m->delete($post_id); |
| @@ -298,8 +357,12 @@ | ||
| 298 | 357 | |
| 299 | 358 | /* remove post */ |
| 300 | 359 | wp_delete_post($post_id, true); |
| 301 | 360 | |
| 361 | + // Delete listing images if setting enabled | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 302 | 365 | do_action('wpdirectorykit/model/listing/delete', $post_id); |
| 303 | 366 | |
| 304 | 367 | return true; |
| 305 | 368 | |
| @@ -328,8 +391,17 @@ | ||
| 328 | 391 | $counter = intval(wdk_field_value('counter_views', $listing_id)); |
| 329 | 392 | } |
| 330 | 393 | |
| 331 | 394 | $this->update(array('counter_views' => ++$counter), $listing_id); |
| 395 | + } | |
| 396 | + | |
| 397 | + public function update_listings_views($listing_ids = [], $increment = 1) | |
| 398 | + { | |
| 399 | + if(empty($listing_ids) ) | |
| 400 | + return false; | |
| 401 | + | |
| 402 | + $sql = 'UPDATE '.$this->_table_name.' SET `counter_results_views` = `counter_results_views`+'.$increment.' WHERE post_id IN ('.join(',', $listing_ids).')'; | |
| 403 | + $this->db->query($sql); | |
| 332 | 404 | } |
| 333 | 405 | |
| 334 | 406 | } |
| 335 | 407 | ?> |