| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly; |
| 3 |
|
| 4 |
class Wdk_listing extends Winter_MVC_Controller { |
| 5 |
|
| 6 |
public function __construct(){ |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
// Edit listing method |
| 11 |
public function index() |
| 12 |
{ |
| 13 |
$this->load->model('field_m'); |
| 14 |
$this->load->model('listing_m'); |
| 15 |
$this->load->model('listingfield_m'); |
| 16 |
$this->load->model('listingusers_m'); |
| 17 |
$this->load->model('category_m'); |
| 18 |
$this->load->model('location_m'); |
| 19 |
$this->load->model('user_m'); |
| 20 |
$this->load->model('categorieslistings_m'); |
| 21 |
$this->load->model('locationslistings_m'); |
| 22 |
$this->load->load_helper('listing'); |
| 23 |
|
| 24 |
$listing_post_id = (int) $this->input->post_get('id'); |
| 25 |
|
| 26 |
wdk_access_check('listing_m', $listing_post_id, NULL, 'edit'); |
| 27 |
|
| 28 |
$this->data['db_data'] = NULL; |
| 29 |
$this->data['db_data']['listing_agents'] = array(); |
| 30 |
$this->data['db_data']['listing_sub_locations'] = array(); |
| 31 |
$this->data['db_data']['listing_sub_categories'] = array(); |
| 32 |
$this->data['form'] = &$this->form; |
| 33 |
|
| 34 |
$this->data['categories'] = $this->category_m->get_parents(); |
| 35 |
$this->data['locations'] = $this->location_m->get_parents(); |
| 36 |
|
| 37 |
$or_like = "(meta_value LIKE '%administrator%' |
| 38 |
OR meta_value LIKE '%wdk_agency%' |
| 39 |
OR meta_value LIKE '%wdk_agent%' |
| 40 |
OR meta_value LIKE '%wdk_owner%')"; |
| 41 |
|
| 42 |
$this->data['agents'] = $this->user_m->get_agents_names(array(), $or_like); |
| 43 |
|
| 44 |
if(!empty($listing_post_id)) |
| 45 |
{ |
| 46 |
$listing_post = get_post( $listing_post_id ); |
| 47 |
|
| 48 |
$listing_db_data = $this->listing_m->get($listing_post_id, TRUE); |
| 49 |
|
| 50 |
$listingfield_db_data = $this->listingfield_m->get($listing_post_id, TRUE); |
| 51 |
$listingusers_db_data = $this->listingusers_m->get($listing_post_id); |
| 52 |
|
| 53 |
$this->data['db_data'] = array_merge((array) $listing_post, |
| 54 |
(array) $listing_db_data, |
| 55 |
(array) $listingfield_db_data); |
| 56 |
|
| 57 |
$this->data['db_data']['listing_agents'] = array(); |
| 58 |
$this->data['db_data']['listing_sub_locations'] = array(); |
| 59 |
$this->data['db_data']['listing_sub_categories'] = array(); |
| 60 |
|
| 61 |
foreach ($listingusers_db_data as $key => $listinguser) { |
| 62 |
if(isset($this->data['agents'][$listinguser->user_id])) |
| 63 |
$this->data['db_data']['listing_agents'] [$listinguser->user_id]= $this->data['agents'][$listinguser->user_id]; |
| 64 |
} |
| 65 |
|
| 66 |
$this->data['db_data']['listing_sub_locations'] = wdk_generate_other_locations_keys($listing_post_id); |
| 67 |
$this->data['db_data']['listing_sub_categories'] = wdk_generate_other_categories_keys($listing_post_id); |
| 68 |
if(function_exists('run_wdk_payments')) { |
| 69 |
$this->data['packages'] = array(); |
| 70 |
|
| 71 |
global $Winter_MVC_wdk_payments; |
| 72 |
$Winter_MVC_wdk_payments->model('package_m'); |
| 73 |
|
| 74 |
$packages = $Winter_MVC_wdk_payments->package_m->get_by(array('(date_from IS NULL OR date_from < \''.current_time( 'mysql' ).'\')'=>NULL, |
| 75 |
'(date_to IS NULL OR date_to > \''.current_time( 'mysql' ).'\')'=>NULL, |
| 76 |
'(location_id IS NULL OR location_id = '.wdk_show_data('location_id', $listing_db_data, '0', TRUE, TRUE).')'=>NULL, |
| 77 |
'(category_id IS NULL OR category_id = '.wdk_show_data('category_id', $listing_db_data, '0', TRUE, TRUE).')'=>NULL)); |
| 78 |
if (count($packages) > 0) { |
| 79 |
foreach($packages as $package) { |
| 80 |
$this->data['packages'][wdk_show_data('idpackage', $package, TRUE, TRUE)] = wdk_show_data('idpackage', $package,'', TRUE, TRUE) |
| 81 |
.', '.wdk_show_data('package_name', $package,'', TRUE, TRUE) |
| 82 |
.'('.wdk_get_date(wdk_show_data('date_to', $package,'', TRUE, TRUE)).')'; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if(isset($_POST['listing_agents'])) { |
| 89 |
$this->data['db_data']['listing_agents'] = array(); |
| 90 |
foreach ($_POST['listing_agents'] as $user_id) { |
| 91 |
if(isset($this->data['agents'][intval($user_id)])) |
| 92 |
$this->data['db_data']['listing_agents'] [intval($user_id)]= $this->data['agents'][intval($user_id)]; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if(isset($_POST['listing_sub_locations'])) { |
| 97 |
$this->data['db_data']['listing_sub_locations'] = $_POST['listing_sub_locations']; |
| 98 |
} |
| 99 |
if(isset($_POST['listing_sub_categories'])) { |
| 100 |
$this->data['db_data']['listing_sub_categories'] = $_POST['listing_sub_categories']; |
| 101 |
} |
| 102 |
|
| 103 |
$this->data['subscriptions'] = array(); |
| 104 |
$this->data['subscriptions_data'] = array(); |
| 105 |
|
| 106 |
if(wdk_get_option('wdk_membership_is_enable_subscriptions') && function_exists('run_wdk_membership')){ |
| 107 |
global $Winter_MVC_wdk_membership; |
| 108 |
$Winter_MVC_wdk_membership->model('subscription_user_m'); |
| 109 |
$Winter_MVC_wdk_membership->model('subscription_m'); |
| 110 |
|
| 111 |
$this->db->select('*'); |
| 112 |
$this->db->join($Winter_MVC_wdk_membership->subscription_m->_table_name.' ON '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.idsubscription = '.$Winter_MVC_wdk_membership->subscription_user_m->_table_name.'.subscription_id'); |
| 113 |
$this->db->join($this->db->prefix.'wdk_categories ON '.$this->db->prefix.'wdk_categories.idcategory = '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.category_id', TRUE, 'LEFT'); |
| 114 |
$this->db->join($this->db->prefix.'wdk_locations ON '.$this->db->prefix.'wdk_locations.idlocation = '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.location_id', TRUE, 'LEFT'); |
| 115 |
$this->db->where(array( |
| 116 |
'(date_expire > \''.current_time( 'mysql' ).'\')'=>NULL, |
| 117 |
'(user_id = \''.wmvc_show_data('user_id_editor', $this->data['db_data'], false).'\')'=>NULL, |
| 118 |
'(status = \'ACTIVE\')'=>NULL, |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
$subscriptions = $Winter_MVC_wdk_membership->subscription_user_m->get(); |
| 123 |
if (count($subscriptions) > 0) { |
| 124 |
foreach($subscriptions as $subscription) { |
| 125 |
$this->data['subscriptions'][wdk_show_data('subscription_id',$subscription,'', TRUE, TRUE)] = wdk_show_data('subscription_id', $subscription,'', TRUE, TRUE) |
| 126 |
.', '.wdk_show_data('subscription_name', $subscription,'', TRUE, TRUE) |
| 127 |
.', '.wdk_show_data('location_title',$subscription, esc_html__('Any', 'wpdirectorykit'), TRUE, TRUE).' '. esc_html__('Location', 'wpdirectorykit') |
| 128 |
.', '.wdk_show_data('category_title',$subscription, esc_html__('Any', 'wpdirectorykit'), TRUE, TRUE).' '. esc_html__('Category', 'wpdirectorykit') |
| 129 |
.'('.wdk_get_date(wdk_show_data('date_expire', $subscription,'', TRUE, TRUE)).')'; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
$this->db->where(array('field_type !='=> 'SECTION')); |
| 136 |
$this->data['listing_fields'] = $this->field_m->get(); |
| 137 |
|
| 138 |
$this->data['fields'] = $this->field_m->get(); |
| 139 |
|
| 140 |
$rules = array( |
| 141 |
array( |
| 142 |
'field' => 'post_title', |
| 143 |
'label' => __('Title', 'wpdirectorykit'), |
| 144 |
'rules' => 'required' |
| 145 |
), |
| 146 |
array( |
| 147 |
'field' => 'post_content', |
| 148 |
'label' => __('Content', 'wpdirectorykit'), |
| 149 |
'rules' => 'required' |
| 150 |
), |
| 151 |
array( |
| 152 |
'field' => 'category_id', |
| 153 |
'label' => __('Category', 'wpdirectorykit'), |
| 154 |
'rules' => (wdk_get_option('wdk_listing_category_required')) ? 'required':'' |
| 155 |
), |
| 156 |
array( |
| 157 |
'field' => 'location_id', |
| 158 |
'label' => __('Location', 'wpdirectorykit'), |
| 159 |
'rules' => (wdk_get_option('wdk_listing_location_required')) ? 'required':'' |
| 160 |
), |
| 161 |
array( |
| 162 |
'field' => 'address', |
| 163 |
'label' => __('Address', 'wpdirectorykit'), |
| 164 |
'rules' => '' |
| 165 |
), |
| 166 |
array( |
| 167 |
'field' => 'lat', |
| 168 |
'label' => __('lat', 'wpdirectorykit'), |
| 169 |
'rules' => (wdk_get_option('wdk_is_address_enabled')) ? 'wdk_gps_single':'' |
| 170 |
), |
| 171 |
array( |
| 172 |
'field' => 'lng', |
| 173 |
'label' => __('lng', 'wpdirectorykit'), |
| 174 |
'rules' => (wdk_get_option('wdk_is_address_enabled')) ? 'wdk_gps_single':'' |
| 175 |
), |
| 176 |
array( |
| 177 |
'field' => 'listing_images', |
| 178 |
'label' => __('Listing images', 'wpdirectorykit'), |
| 179 |
'rules' => (wdk_get_option('wdk_listings_images_required_enable')) ? 'required' : '' |
| 180 |
), |
| 181 |
array( |
| 182 |
'field' => 'listing_plans_documents', |
| 183 |
'label' => __('Listing plans and documents', 'wpdirectorykit'), |
| 184 |
'rules' => '' |
| 185 |
), |
| 186 |
array( |
| 187 |
'field' => 'is_featured', |
| 188 |
'label' => __('Is Featured', 'wpdirectorykit'), |
| 189 |
'rules' => '' |
| 190 |
), |
| 191 |
array( |
| 192 |
'field' => 'is_activated', |
| 193 |
'label' => __('Is Activated', 'wpdirectorykit'), |
| 194 |
'rules' => '' |
| 195 |
), |
| 196 |
array( |
| 197 |
'field' => 'is_approved', |
| 198 |
'label' => __('Is Approved', 'wpdirectorykit'), |
| 199 |
'rules' => '' |
| 200 |
), |
| 201 |
array( |
| 202 |
'field' => 'user_id_editor', |
| 203 |
'label' => __('Agents', 'wpdirectorykit'), |
| 204 |
'rules' => '' |
| 205 |
), |
| 206 |
array( |
| 207 |
'field' => 'listing_agents', |
| 208 |
'label' => __('Agents', 'wpdirectorykit'), |
| 209 |
'rules' => '' |
| 210 |
), |
| 211 |
array( |
| 212 |
'field' => 'listing_sub_locations', |
| 213 |
'label' => __('Locations', 'wpdirectorykit'), |
| 214 |
'rules' => '' |
| 215 |
), |
| 216 |
array( |
| 217 |
'field' => 'listing_sub_categories', |
| 218 |
'label' => __('Categories', 'wpdirectorykit'), |
| 219 |
'rules' => '' |
| 220 |
), |
| 221 |
array( |
| 222 |
'field' => 'package_id', |
| 223 |
'label' => __('Package', 'wpdirectorykit'), |
| 224 |
'rules' => '' |
| 225 |
), |
| 226 |
array( |
| 227 |
'field' => 'subscription_id', |
| 228 |
'label' => __('Subscription id', 'wpdirectorykit'), |
| 229 |
'rules' => '' |
| 230 |
), |
| 231 |
array( |
| 232 |
'field' => 'listing_parent_post_id', |
| 233 |
'label' => __('Listing parent id', 'wpdirectorykit'), |
| 234 |
'rules' => '' |
| 235 |
), |
| 236 |
array( |
| 237 |
'field' => 'listing_related_ids', |
| 238 |
'label' => __('Listing childs', 'wpdirectorykit'), |
| 239 |
'rules' => '' |
| 240 |
), |
| 241 |
); |
| 242 |
|
| 243 |
$rules[] = array( |
| 244 |
'field' => 'rank', |
| 245 |
'label' => __('Rank', 'wpdirectorykit'), |
| 246 |
'rules' => 'numeric' |
| 247 |
); |
| 248 |
|
| 249 |
|
| 250 |
foreach($this->data['fields'] as $key => $field) |
| 251 |
{ |
| 252 |
if($field->field_type == 'SECTION'){ |
| 253 |
$this->data['fields'][$key]->is_required = ''; |
| 254 |
continue; |
| 255 |
} |
| 256 |
|
| 257 |
$rule_required = (wmvc_show_data('is_required', $field) == 1) ? 'required' : ''; |
| 258 |
|
| 259 |
if(wmvc_show_data('validation', $field)) { |
| 260 |
if(!empty($rule_required)) { |
| 261 |
$rule_required .= '|'; |
| 262 |
} |
| 263 |
$rule_required .= wmvc_show_data('validation', $field); |
| 264 |
} |
| 265 |
|
| 266 |
if(!empty(wmvc_show_data('min_length', $field))) { |
| 267 |
if(!empty($rule_required)) { |
| 268 |
$rule_required .= '|'; |
| 269 |
} |
| 270 |
|
| 271 |
if(wmvc_show_data('field_type', $field) == "NUMBER") { |
| 272 |
$rule_required .= "min_number"; |
| 273 |
} else { |
| 274 |
$rule_required .= "min_length"; |
| 275 |
} |
| 276 |
$rule_required .= "[".wmvc_show_data('min_length', $field)."]"; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
if(!empty(wmvc_show_data('max_length', $field))) { |
| 281 |
if(!empty($rule_required)) { |
| 282 |
$rule_required .= '|'; |
| 283 |
} |
| 284 |
if(wmvc_show_data('field_type', $field) == "NUMBER") { |
| 285 |
$rule_required .= "max_number"; |
| 286 |
} else { |
| 287 |
$rule_required .= "max_length"; |
| 288 |
} |
| 289 |
$rule_required .= "[".wmvc_show_data('max_length', $field)."]"; |
| 290 |
} |
| 291 |
|
| 292 |
/* clear rules, because hidden field */ |
| 293 |
if(isset($_POST['category_id']) && !empty($_POST['category_id'])) { |
| 294 |
if(wdk_depend_is_hidden_field($field->idfield, intval($_POST['category_id']))) { |
| 295 |
$rule_required = ''; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
$rules[] = |
| 300 |
array( |
| 301 |
'field' => 'field_'.$field->idfield, |
| 302 |
'label' => $field->field_label, |
| 303 |
'rules' => $rule_required |
| 304 |
); |
| 305 |
|
| 306 |
if(isset($this->data['db_data']['field_'.$field->idfield.'_'.$field->field_type])) |
| 307 |
$this->data['db_data']['field_'.$field->idfield] = |
| 308 |
$this->data['db_data']['field_'.$field->idfield.'_'.$field->field_type]; |
| 309 |
} |
| 310 |
|
| 311 |
$this->form->add_error_message('wdk_gps_single', __('Gps field is not valid, should be like xx.xxxxxx (between -180 and 180)', 'wpdirectorykit')); |
| 312 |
if($this->form->run($rules)) |
| 313 |
{ |
| 314 |
// Check _wpnonce |
| 315 |
check_admin_referer( 'wdk-listing-edit_'.$listing_post_id, '_wpnonce' ); |
| 316 |
|
| 317 |
$data = $this->listing_m->prepare_data(wdk_get_post(), $rules, FALSE); |
| 318 |
// Save standard wp post |
| 319 |
|
| 320 |
// Create post object |
| 321 |
$listing_post = array( |
| 322 |
'ID' => $listing_post_id, |
| 323 |
'post_type' => 'wdk-listing', |
| 324 |
'post_title' => wp_strip_all_tags( $data['post_title'] ), |
| 325 |
'post_content' => $data['post_content'], |
| 326 |
'post_status' => 'publish', |
| 327 |
'post_author' => get_current_user_id() |
| 328 |
); |
| 329 |
|
| 330 |
// Insert the post into the database |
| 331 |
$id = wp_insert_post( $listing_post ); |
| 332 |
|
| 333 |
// Save our main listing data |
| 334 |
|
| 335 |
$listing_data = array('post_id' => $id); |
| 336 |
|
| 337 |
$listing_data_fields = array('category_id', 'location_id', 'address', 'lat', 'lng', 'listing_images','listing_plans_documents', 'is_featured', 'is_activated','is_approved', 'package_id','rank','subscription_id', |
| 338 |
'user_id_editor', 'listing_parent_post_id','listing_related_ids'); |
| 339 |
foreach($listing_data_fields as $field_name) |
| 340 |
{ |
| 341 |
$listing_data[$field_name] = $data[$field_name]; |
| 342 |
} |
| 343 |
|
| 344 |
$image_ids = explode(',', $data['listing_images']); |
| 345 |
|
| 346 |
$listing_data['listing_images_path'] = ''; |
| 347 |
$listing_data['listing_images_path_medium'] = ''; |
| 348 |
|
| 349 |
if(is_array($image_ids)) { |
| 350 |
foreach ($image_ids as $image_id) { |
| 351 |
if(is_numeric($image_id)) |
| 352 |
{ |
| 353 |
$image_path = wp_get_original_image_path( $image_id); |
| 354 |
if($image_path) { |
| 355 |
/* path of image */ |
| 356 |
$next_path = str_replace(WP_CONTENT_DIR . '/uploads/','', $image_path); |
| 357 |
|
| 358 |
if(!empty($listing_data['listing_images_path'])) |
| 359 |
$listing_data['listing_images_path'] .= ','; |
| 360 |
|
| 361 |
$listing_data['listing_images_path'] .= $next_path; |
| 362 |
} |
| 363 |
|
| 364 |
$image_url = wp_get_attachment_image_url($image_id, 'large'); |
| 365 |
if($image_url) { |
| 366 |
$parsed = parse_url($image_url); |
| 367 |
$next_path = substr($parsed['path'], strpos($parsed['path'], 'uploads/')+8); |
| 368 |
|
| 369 |
if(!empty($listing_data['listing_images_path_medium'])) |
| 370 |
$listing_data['listing_images_path_medium'] .= ','; |
| 371 |
|
| 372 |
$listing_data['listing_images_path_medium'] .= $next_path; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if(wmvc_user_in_role('administrator') && $this->input->post('slug')) { |
| 379 |
// update the post slug |
| 380 |
wp_update_post( array( |
| 381 |
'ID' => $id, |
| 382 |
'post_name' => sanitize_text_field($this->input->post('slug')) |
| 383 |
)); |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
if(!function_exists('run_wdk_membership')) { |
| 388 |
if($listing_data['is_activated'] == 1) { |
| 389 |
$listing_data['is_approved'] = 1; |
| 390 |
} else { |
| 391 |
$listing_data['is_approved'] = 0; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if(!wmvc_user_in_role('administrator')) { |
| 396 |
unset($listing_data['rank']); |
| 397 |
} |
| 398 |
|
| 399 |
if(function_exists('run_wdk_payments') && wmvc_show_data('package_id', $listing_data_field, false)) { |
| 400 |
|
| 401 |
global $Winter_MVC_wdk_payments; |
| 402 |
$Winter_MVC_wdk_payments->model('package_m'); |
| 403 |
$package = $Winter_MVC_wdk_payments->package_m->get(wmvc_show_data('package_id', $listing_data_field, false), TRUE); |
| 404 |
if ($packages) { |
| 405 |
if(!isset($listing_data['rank']) || empty($listing_data['rank'])) |
| 406 |
$listing_data['rank'] = wdk_show_data('featured_rank',$package, 0, TRUE, TRUE); |
| 407 |
$listing_data['date_package_expire'] = date('Y-m-d H:i:s', strtotime('+'.wdk_show_data('days_limit',$package, 0, TRUE, TRUE).'days')); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/* dates set */ |
| 412 |
if(isset($this->data['db_data']['date'])) |
| 413 |
$listing_data['date'] = $this->data['db_data']['date']; |
| 414 |
|
| 415 |
$listing_data['date_modified'] = date('Y-m-d H:i:s'); |
| 416 |
|
| 417 |
if(empty($listing_db_data)) |
| 418 |
{ |
| 419 |
$this->listing_m->insert($listing_data, NULL); |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
$this->listing_m->insert($listing_data, $id); |
| 424 |
} |
| 425 |
|
| 426 |
// insert users/agents |
| 427 |
|
| 428 |
if(function_exists('run_wdk_membership')){ |
| 429 |
$this->listingusers_m->delete_where(array('post_id' => $id)); |
| 430 |
if(is_array($data['listing_agents'])) |
| 431 |
foreach($data['listing_agents'] as $val) |
| 432 |
{ |
| 433 |
$this->listingusers_m->insert(array('post_id' => $id, 'user_id' => $val), NULL); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
$data_other_categories = array(); |
| 438 |
if(isset($data['listing_sub_categories']) || is_null($data['listing_sub_categories'])) { |
| 439 |
$this->categorieslistings_m->delete_where(array('post_id' => $id)); |
| 440 |
if(is_array($data['listing_sub_categories'])) |
| 441 |
foreach($data['listing_sub_categories'] as $val) |
| 442 |
{ |
| 443 |
$this->categorieslistings_m->insert(array('post_id' => $id, 'category_id' => $val), NULL); |
| 444 |
|
| 445 |
$data_other_categories []= $val; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
$data_other_locations = array(); |
| 450 |
if(isset($data['listing_sub_locations']) || is_null($data['listing_sub_locations'])) { |
| 451 |
$this->locationslistings_m->delete_where(array('post_id' => $id)); |
| 452 |
if(is_array($data['listing_sub_locations'])) |
| 453 |
foreach($data['listing_sub_locations'] as $val) |
| 454 |
{ |
| 455 |
$this->locationslistings_m->insert(array('post_id' => $id, 'location_id' => $val), NULL); |
| 456 |
$data_other_locations []= $val; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
$data_update = array( |
| 461 |
'categories_list'=> '', |
| 462 |
'locations_list'=>'' |
| 463 |
); |
| 464 |
|
| 465 |
if(!empty($data_other_categories)) { |
| 466 |
$data_update['categories_list'] = ','.join(',',$data_other_categories).','; |
| 467 |
} |
| 468 |
|
| 469 |
if(!empty($data_other_locations)) { |
| 470 |
$data_update['locations_list'] = ','.join(',',$data_other_locations).','; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
if(!empty($data_update)) |
| 475 |
$this->listing_m->insert($data_update, $id); |
| 476 |
|
| 477 |
//exit($this->db->last_query()); |
| 478 |
|
| 479 |
// Save dynamic fields data |
| 480 |
|
| 481 |
$data['post_id'] = $id; |
| 482 |
|
| 483 |
if(empty($listingfield_db_data)) |
| 484 |
{ |
| 485 |
$this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, NULL); |
| 486 |
} |
| 487 |
else |
| 488 |
{ |
| 489 |
$this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, $id); |
| 490 |
} |
| 491 |
|
| 492 |
//exit($this->db->last_query()); |
| 493 |
|
| 494 |
//exit($this->db->last_error()); |
| 495 |
|
| 496 |
if(wdk_get_option('wdk_sub_listings_enable')) { |
| 497 |
/* generate fast childs ids for parents */ |
| 498 |
|
| 499 |
if(isset($listing_data['listing_parent_post_id']) && !empty($listing_data['listing_parent_post_id'])) { |
| 500 |
|
| 501 |
$this->db->select('post_id'); |
| 502 |
$this->db->order_by('sublisting_order,idlisting'); |
| 503 |
$this->db->from($this->listing_m->_table_name); |
| 504 |
$this->db->where(array('listing_parent_post_id' => intval($listing_data['listing_parent_post_id']))); |
| 505 |
$query = $this->db->get(); |
| 506 |
|
| 507 |
$listings_childs = ''; |
| 508 |
if ($this->db->num_rows() > 0) |
| 509 |
foreach ($this->db->results() as $listing) { |
| 510 |
$listings_childs .= $listing->post_id.','; |
| 511 |
} |
| 512 |
|
| 513 |
if(!empty($listings_childs)) |
| 514 |
$listings_childs = substr($listings_childs,0, -1); |
| 515 |
|
| 516 |
$this->listing_m->insert(array('listing_related_ids' => $listings_childs), intval($listing_data['listing_parent_post_id'])); |
| 517 |
} |
| 518 |
|
| 519 |
if(isset($listing_data['listing_related_ids']) && !empty($listing_data['listing_related_ids'])) { |
| 520 |
|
| 521 |
$old_childs_ids = wmvc_show_data('listing_related_ids', $this->data['db_data'],'',TRUE, TRUE); |
| 522 |
$order_index = 0; |
| 523 |
$listings_childs = ''; |
| 524 |
|
| 525 |
$old_childs_ids = explode(',', $old_childs_ids); |
| 526 |
$old_childs_ids = array_flip($old_childs_ids); |
| 527 |
foreach (explode(',', $listing_data['listing_related_ids']) as $idlisting) { |
| 528 |
$this->listing_m->insert(array('listing_parent_post_id' => $listing_post_id, 'sublisting_order'=>$order_index++), intval($idlisting)); |
| 529 |
|
| 530 |
if(isset($old_childs_ids[$idlisting])) { |
| 531 |
unset($old_childs_ids[$idlisting]); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/* clear old related listings */ |
| 536 |
if(!empty($old_childs_ids)) { |
| 537 |
foreach ($old_childs_ids as $idlisting => $v) { |
| 538 |
$this->listing_m->insert(array('listing_parent_post_id' => NULL), intval($idlisting)); |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
// redirect |
| 545 |
if(empty($listing_post_id) && !empty($id)) |
| 546 |
{ |
| 547 |
wp_redirect(admin_url("admin.php?page=wdk_listing&id=$id&is_updated=true")); |
| 548 |
exit; |
| 549 |
} |
| 550 |
|
| 551 |
/* fix checkbox after submit */ |
| 552 |
if(!empty($id)) |
| 553 |
{ |
| 554 |
$is_approved_before_save = $this->data['db_data']['is_approved']; |
| 555 |
|
| 556 |
$listing_post = get_post( $id ); |
| 557 |
$listing_db_data = $this->listing_m->get($id, TRUE); |
| 558 |
$listingfield_db_data = $this->listingfield_m->get($id, TRUE); |
| 559 |
$listingusers_db_data = $this->listingusers_m->get($id); |
| 560 |
$this->data['db_data'] = array_merge((array) $listing_post, |
| 561 |
(array) $listing_db_data, |
| 562 |
(array) $listingfield_db_data); |
| 563 |
|
| 564 |
$this->data['db_data']['listing_agents'] = array(); |
| 565 |
$this->data['db_data']['listing_sub_locations'] = array(); |
| 566 |
$this->data['db_data']['listing_sub_categories'] = array(); |
| 567 |
|
| 568 |
foreach ($listingusers_db_data as $key => $listinguser) { |
| 569 |
if(isset($this->data['agents'][$listinguser->user_id])) |
| 570 |
$this->data['db_data']['listing_agents'] [$listinguser->user_id]= $this->data['agents'][$listinguser->user_id]; |
| 571 |
} |
| 572 |
|
| 573 |
if(isset($_POST['listing_sub_locations'])) { |
| 574 |
$this->data['db_data']['listing_sub_locations'] = $_POST['listing_sub_locations']; |
| 575 |
unset($_POST['listing_sub_locations']); |
| 576 |
} |
| 577 |
if(isset($_POST['listing_sub_categories'])) { |
| 578 |
$this->data['db_data']['listing_sub_categories'] = $_POST['listing_sub_categories']; |
| 579 |
unset($_POST['listing_sub_categories']); |
| 580 |
} |
| 581 |
|
| 582 |
/* if approved message */ |
| 583 |
if (!empty($listing_post_id) && wmvc_show_data('user_id', $data, false)) { |
| 584 |
if( $is_approved_before_save != 1 && wmvc_show_data('is_approved', $data) == 1 && wmvc_show_data('is_activated', $data) == 1) { |
| 585 |
|
| 586 |
$user = get_userdata( wmvc_show_data('user_id', $data) ); |
| 587 |
|
| 588 |
$data_message = array(); |
| 589 |
$data_message['user'] = $user; |
| 590 |
$data_message['post_id'] = $id; |
| 591 |
$data_message['post'] = $this->data['db_data']; |
| 592 |
$ret = wdk_mail($user->user_email, __('Your Listing Approved', 'wpdirectorykit'), $data_message, 'new_listing_approved'); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
} |
| 598 |
|
| 599 |
$this->data['calendar_id'] = NULL; |
| 600 |
if(!empty($listing_post_id) && function_exists('run_wdk_bookings')) { |
| 601 |
global $Winter_MVC_wdk_bookings; |
| 602 |
$Winter_MVC_wdk_bookings->model('calendar_m'); |
| 603 |
$calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id'=>$listing_post_id), TRUE); |
| 604 |
if($calendar) { |
| 605 |
$this->data['calendar_id'] = $calendar->idcalendar; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
$this->load->view('wdk_listing/index', $this->data); |
| 610 |
} |
| 611 |
|
| 612 |
} |
| 613 |
|