| 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 '.$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 |
|
| 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 '.$post_table.'.ID = favorite_table.post_id', NULL, 'LEFT'); |
| 63 |
} |
| 64 |
|
| 65 |
// [/Favorites join] |
| 66 |
|
| 67 |
$this->db->where($post_table.'.post_type = \'wdk-listing\'', NULL); |
| 68 |
|
| 69 |
if(isset($where['is_activated'])){ |
| 70 |
$where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated']; |
| 71 |
unset($where['is_activated']); |
| 72 |
} |
| 73 |
|
| 74 |
if(isset($where['is_approved'])){ |
| 75 |
$where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved']; |
| 76 |
unset($where['is_approved']); |
| 77 |
} |
| 78 |
|
| 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 ) ) |
| 81 |
{ |
| 82 |
if($show_other_agents_litings) |
| 83 |
{ |
| 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 |
|
| 92 |
$this->db->distinct($this->_table_name.'.post_id'); |
| 93 |
|
| 94 |
$user_check_id = NULL; |
| 95 |
if(!is_null($user_id) && !empty($user_id)) |
| 96 |
{ |
| 97 |
$user_check_id = $user_id; |
| 98 |
} |
| 99 |
elseif($this->current_user_id) |
| 100 |
{ |
| 101 |
$user_check_id = $this->current_user_id; |
| 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 |
|
| 113 |
} else { |
| 114 |
if(!is_null($user_id) && !empty($user_id)) |
| 115 |
{ |
| 116 |
$this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id); |
| 117 |
} |
| 118 |
elseif($this->current_user_id) |
| 119 |
{ |
| 120 |
$this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$this->db->where($where); |
| 126 |
|
| 127 |
if(wdk_get_option('wdk_sub_listings_enable')) { |
| 128 |
$this->db->where(array('(listing_parent_post_id IS NULL OR listing_parent_post_id = 0)'=>NULL)); |
| 129 |
} |
| 130 |
|
| 131 |
$this->db->order_by($this->_order_by); |
| 132 |
|
| 133 |
$query = $this->db->get(); |
| 134 |
|
| 135 |
$res = $this->db->results(); |
| 136 |
|
| 137 |
$this->_primary_key = 'post_id'; |
| 138 |
$this->_order_by = 'post_id DESC'; |
| 139 |
|
| 140 |
if(isset($res[0]->total_count)) |
| 141 |
return $res[0]->total_count; |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
public function get_pagination($limit, $offset, $where = array(), $user_check = FALSE, $user_id=NULL, $show_other_agents_litings = FALSE) |
| 147 |
{ |
| 148 |
$this->load->model('cachedusers_m'); |
| 149 |
|
| 150 |
$this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id'; |
| 151 |
$this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC'; |
| 152 |
|
| 153 |
$post_table = $this->db->prefix.'posts'; |
| 154 |
$fields_table = $this->db->prefix.'wdk_listings_fields'; |
| 155 |
|
| 156 |
|
| 157 |
if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) { |
| 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,'.$this->cachedusers_m->_table_name.'.*'); |
| 159 |
} else { |
| 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'); |
| 161 |
} |
| 162 |
|
| 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'); |
| 167 |
|
| 168 |
/* join cached user data */ |
| 169 |
if(defined( 'WDK_EXTENSIONS_CACHED_USERS_ACTIVATED' ) && WDK_EXTENSIONS_CACHED_USERS_ACTIVATED ) { |
| 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'); |
| 171 |
} |
| 172 |
|
| 173 |
/* locations */ |
| 174 |
$this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT'); |
| 175 |
$this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT'); |
| 176 |
/* for parents search*/ |
| 177 |
/*for($i = 2 ; $i <= 2; $i++) { |
| 178 |
$this->db->join($this->db->prefix.'wdk_locations AS location_'.$i.' ON location_'.($i-1).'.parent_id = location_'.$i.'.idlocation', NULL, 'LEFT'); |
| 179 |
}*/ |
| 180 |
|
| 181 |
$this->db->where($post_table.'.post_type = \'wdk-listing\'', NULL); |
| 182 |
|
| 183 |
if(isset($where['is_activated'])) { |
| 184 |
$where[$this->db->prefix.'wdk_listings`.`is_activated'] = $where['is_activated']; |
| 185 |
unset($where['is_activated']); |
| 186 |
} |
| 187 |
|
| 188 |
if(isset($where['is_approved'])) { |
| 189 |
$where[$this->db->prefix.'wdk_listings`.`is_approved'] = $where['is_approved']; |
| 190 |
unset($where['is_approved']); |
| 191 |
} |
| 192 |
|
| 193 |
// [Favorites join] |
| 194 |
if(!empty($user_id)) |
| 195 |
{ |
| 196 |
global $Winter_MVC_wdk_favorites; |
| 197 |
if(isset($Winter_MVC_wdk_favorites)) |
| 198 |
{ |
| 199 |
$this->db->select('favorite_table.idfavorite as is_favorite'); |
| 200 |
$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'); |
| 201 |
} |
| 202 |
} |
| 203 |
else { |
| 204 |
global $Winter_MVC_wdk_favorites; |
| 205 |
if($this->current_user_id != 0 && isset($Winter_MVC_wdk_favorites)) |
| 206 |
{ |
| 207 |
$this->db->select('favorite_table.idfavorite as is_favorite'); |
| 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'); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if( (!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage') && $user_check) || (!is_null($user_id) && !empty($user_id) && $user_check ) ) |
| 213 |
{ |
| 214 |
if($show_other_agents_litings) |
| 215 |
{ |
| 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 |
|
| 224 |
$this->db->distinct($this->_table_name.'.post_id'); |
| 225 |
|
| 226 |
$user_check_id = NULL; |
| 227 |
if(!is_null($user_id) && !empty($user_id)) |
| 228 |
{ |
| 229 |
$user_check_id = $user_id; |
| 230 |
} |
| 231 |
elseif($this->current_user_id) |
| 232 |
{ |
| 233 |
$user_check_id = $this->current_user_id; |
| 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 |
|
| 245 |
} else { |
| 246 |
if(!is_null($user_id) && !empty($user_id)) |
| 247 |
{ |
| 248 |
$this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $user_id); |
| 249 |
} |
| 250 |
elseif($this->current_user_id) |
| 251 |
{ |
| 252 |
$this->db->where($this->db->prefix.'wdk_listings`.`user_id_editor', $this->current_user_id); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
$this->db->where($where); |
| 257 |
} else { |
| 258 |
$this->db->where($where); |
| 259 |
} |
| 260 |
|
| 261 |
if(wdk_get_option('wdk_sub_listings_enable')) { |
| 262 |
$this->db->where(array('(listing_parent_post_id IS NULL OR listing_parent_post_id = 0)'=>NULL)); |
| 263 |
} |
| 264 |
|
| 265 |
$this->db->limit($limit); |
| 266 |
$this->db->offset($offset); |
| 267 |
|
| 268 |
$this->db->order_by($this->_order_by); |
| 269 |
|
| 270 |
$query = $this->db->get(); |
| 271 |
|
| 272 |
$this->_primary_key = 'post_id'; |
| 273 |
$this->_order_by = 'post_id DESC'; |
| 274 |
|
| 275 |
//echo $this->db->last_query(); |
| 276 |
|
| 277 |
if ($this->db->num_rows() > 0) |
| 278 |
return $this->db->results(); |
| 279 |
|
| 280 |
return array(); |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
public function get($id = NULL, $single = FALSE) |
| 285 |
{ |
| 286 |
$this->_primary_key = $this->db->prefix.'wdk_listings`.`post_id'; |
| 287 |
$this->_order_by = $this->db->prefix.'wdk_listings.post_id DESC'; |
| 288 |
|
| 289 |
$post_table = $this->db->prefix.'posts'; |
| 290 |
$fields_table = $this->db->prefix.'wdk_listings_fields'; |
| 291 |
|
| 292 |
$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'); |
| 293 |
$this->db->from($this->_table_name); |
| 294 |
$this->db->join($post_table.' ON '.$this->_table_name.'.post_id = '.$post_table.'.ID'); |
| 295 |
$this->db->join($fields_table.' ON '.$this->_table_name.'.post_id = '.$fields_table.'.post_id'); |
| 296 |
$this->db->join($this->db->prefix.'wdk_locations AS location_table ON '.$this->listing_m->_table_name.'.location_id = location_table.idlocation', NULL, 'LEFT'); |
| 297 |
$this->db->join($this->db->prefix.'wdk_categories AS category_table ON '.$this->listing_m->_table_name.'.category_id = category_table.idcategory', NULL, 'LEFT'); |
| 298 |
|
| 299 |
/*$query = $this->db->get(); |
| 300 |
|
| 301 |
if ($this->db->num_rows() > 0) |
| 302 |
return $this->db->row(); |
| 303 |
|
| 304 |
return array();*/ |
| 305 |
|
| 306 |
$return = parent::get($id, $single); |
| 307 |
|
| 308 |
$this->_primary_key = 'post_id'; |
| 309 |
$this->_order_by = 'post_id DESC'; |
| 310 |
|
| 311 |
return $return; |
| 312 |
} |
| 313 |
|
| 314 |
public function check_deletable($post_id, $user_id=NULL) |
| 315 |
{ |
| 316 |
if(wmvc_user_in_role('administrator') || current_user_can('wdk_listings_manage')) return true; |
| 317 |
|
| 318 |
$this->load->model('listing_m'); |
| 319 |
$listing = $this->listing_m->get($post_id, TRUE); |
| 320 |
|
| 321 |
if($user_id != NULL) |
| 322 |
if(wmvc_show_data('user_id_editor', $listing) == $user_id) |
| 323 |
return true; |
| 324 |
|
| 325 |
if(wmvc_show_data('user_id_editor', $listing) == $this->current_user_id) |
| 326 |
return true; |
| 327 |
|
| 328 |
return false; |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
public function delete($post_id, $user_id=NULL) { |
| 333 |
|
| 334 |
if(!$this->check_deletable($post_id, $user_id)) return false; |
| 335 |
|
| 336 |
$this->load->model('listingfield_m'); |
| 337 |
$this->load->model('listingusers_m'); |
| 338 |
|
| 339 |
/* remove listing */ |
| 340 |
parent::delete($post_id); |
| 341 |
$this->listingfield_m->delete($post_id); |
| 342 |
$this->listingusers_m->delete($post_id); |
| 343 |
|
| 344 |
/* remove post */ |
| 345 |
wp_delete_post($post_id, true); |
| 346 |
|
| 347 |
do_action('wpdirectorykit/model/listing/delete', $post_id); |
| 348 |
|
| 349 |
return true; |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
/* [END] For dynamic data table */ |
| 354 |
|
| 355 |
public function is_related($item_id, $user_id, $method = 'edit') |
| 356 |
{ |
| 357 |
$this->load->model('listing_m'); |
| 358 |
$listing = $this->listing_m->get($item_id, TRUE); |
| 359 |
if(wmvc_show_data('user_id_editor', $listing) == $user_id) |
| 360 |
return true; |
| 361 |
|
| 362 |
return false; |
| 363 |
} |
| 364 |
|
| 365 |
public function update_counter($listing_id = NULL) |
| 366 |
{ |
| 367 |
if(empty($listing_id) ) |
| 368 |
return false; |
| 369 |
|
| 370 |
$counter = 0; |
| 371 |
if(wdk_field_value('counter_views', $listing_id)) |
| 372 |
{ |
| 373 |
$counter = intval(wdk_field_value('counter_views', $listing_id)); |
| 374 |
} |
| 375 |
|
| 376 |
$this->update(array('counter_views' => ++$counter), $listing_id); |
| 377 |
} |
| 378 |
|
| 379 |
public function update_listings_views($listing_ids = [], $increment = 1) |
| 380 |
{ |
| 381 |
if(empty($listing_ids) ) |
| 382 |
return false; |
| 383 |
|
| 384 |
$sql = 'UPDATE '.$this->_table_name.' SET `counter_results_views` = `counter_results_views`+'.$increment.' WHERE post_id IN ('.join(',', $listing_ids).')'; |
| 385 |
$this->db->query($sql); |
| 386 |
} |
| 387 |
|
| 388 |
} |
| 389 |
?> |