PluginProbe
WP Directory Kit / 1.4.8
WP Directory Kit v1.4.8
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 / controllers / Wdk_listing.php

Wdk_listing.php in WP Directory Kit 1.4.8, at application/controllers/Wdk_listing.php

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