PluginProbe
WP Directory Kit / 1.2.3
WP Directory Kit v1.2.3
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.2.3, at application/controllers/Wdk_listing.php

612 lines 27.9 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 // 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' => ''
170 ),
171 array(
172 'field' => 'lng',
173 'label' => __('lng', 'wpdirectorykit'),
174 'rules' => ''
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 if($this->form->run($rules))
312 {
313 // Check _wpnonce
314 check_admin_referer( 'wdk-listing-edit_'.$listing_post_id, '_wpnonce' );
315
316 $data = $this->listing_m->prepare_data(wdk_get_post(), $rules, FALSE);
317 // Save standard wp post
318
319 // Create post object
320 $listing_post = array(
321 'ID' => $listing_post_id,
322 'post_type' => 'wdk-listing',
323 'post_title' => wp_strip_all_tags( $data['post_title'] ),
324 'post_content' => $data['post_content'],
325 'post_status' => 'publish',
326 'post_author' => get_current_user_id()
327 );
328
329 // Insert the post into the database
330 $id = wp_insert_post( $listing_post );
331
332 // Save our main listing data
333
334 $listing_data = array('post_id' => $id);
335
336 $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',
337 'user_id_editor', 'listing_parent_post_id','listing_related_ids');
338 foreach($listing_data_fields as $field_name)
339 {
340 $listing_data[$field_name] = $data[$field_name];
341 }
342
343 $image_ids = explode(',', $data['listing_images']);
344
345 $listing_data['listing_images_path'] = '';
346 $listing_data['listing_images_path_medium'] = '';
347
348 if(is_array($image_ids)) {
349 foreach ($image_ids as $image_id) {
350 if(is_numeric($image_id))
351 {
352 $image_path = wp_get_original_image_path( $image_id);
353 if($image_path) {
354 /* path of image */
355 $next_path = str_replace(WP_CONTENT_DIR . '/uploads/','', $image_path);
356
357 if(!empty($listing_data['listing_images_path']))
358 $listing_data['listing_images_path'] .= ',';
359
360 $listing_data['listing_images_path'] .= $next_path;
361 }
362
363 $image_url = wp_get_attachment_image_url($image_id, 'large');
364 if($image_url) {
365 $parsed = parse_url($image_url);
366 $next_path = substr($parsed['path'], strpos($parsed['path'], 'uploads/')+8);
367
368 if(!empty($listing_data['listing_images_path_medium']))
369 $listing_data['listing_images_path_medium'] .= ',';
370
371 $listing_data['listing_images_path_medium'] .= $next_path;
372 }
373 }
374 }
375 }
376
377 if(wmvc_user_in_role('administrator') && $this->input->post('slug')) {
378 // update the post slug
379 wp_update_post( array(
380 'ID' => $id,
381 'post_name' => sanitize_text_field($this->input->post('slug'))
382 ));
383
384 }
385
386 if(!function_exists('run_wdk_membership')) {
387 if($listing_data['is_activated'] == 1) {
388 $listing_data['is_approved'] = 1;
389 } else {
390 $listing_data['is_approved'] = 0;
391 }
392 }
393
394 if(!wmvc_user_in_role('administrator')) {
395 unset($listing_data['rank']);
396 }
397
398 if(function_exists('run_wdk_payments') && wmvc_show_data('package_id', $listing_data_field, false)) {
399
400 global $Winter_MVC_wdk_payments;
401 $Winter_MVC_wdk_payments->model('package_m');
402 $package = $Winter_MVC_wdk_payments->package_m->get(wmvc_show_data('package_id', $listing_data_field, false), TRUE);
403 if ($packages) {
404 if(!isset($listing_data['rank']) || empty($listing_data['rank']))
405 $listing_data['rank'] = wdk_show_data('featured_rank',$package, 0, TRUE, TRUE);
406 $listing_data['date_package_expire'] = date('Y-m-d H:i:s', strtotime('+'.wdk_show_data('days_limit',$package, 0, TRUE, TRUE).'days'));
407 }
408 }
409
410 /* dates set */
411 if(isset($this->data['db_data']['date']))
412 $listing_data['date'] = $this->data['db_data']['date'];
413
414 $listing_data['date_modified'] = date('Y-m-d H:i:s');
415
416 if(empty($listing_db_data))
417 {
418 $this->listing_m->insert($listing_data, NULL);
419 }
420 else
421 {
422 $this->listing_m->insert($listing_data, $id);
423 }
424
425 // insert users/agents
426
427 if(function_exists('run_wdk_membership')){
428 $this->listingusers_m->delete_where(array('post_id' => $id));
429 if(is_array($data['listing_agents']))
430 foreach($data['listing_agents'] as $val)
431 {
432 $this->listingusers_m->insert(array('post_id' => $id, 'user_id' => $val), NULL);
433 }
434 }
435
436 $data_other_categories = array();
437 if(isset($data['listing_sub_categories']) || is_null($data['listing_sub_categories'])) {
438 $this->categorieslistings_m->delete_where(array('post_id' => $id));
439 if(is_array($data['listing_sub_categories']))
440 foreach($data['listing_sub_categories'] as $val)
441 {
442 $this->categorieslistings_m->insert(array('post_id' => $id, 'category_id' => $val), NULL);
443
444 $data_other_categories []= $val;
445 }
446 }
447
448 $data_other_locations = array();
449 if(isset($data['listing_sub_locations']) || is_null($data['listing_sub_locations'])) {
450 $this->locationslistings_m->delete_where(array('post_id' => $id));
451 if(is_array($data['listing_sub_locations']))
452 foreach($data['listing_sub_locations'] as $val)
453 {
454 $this->locationslistings_m->insert(array('post_id' => $id, 'location_id' => $val), NULL);
455 $data_other_locations []= $val;
456 }
457 }
458
459 $data_update = array(
460 'categories_list'=> '',
461 'locations_list'=>''
462 );
463
464 if(!empty($data_other_categories)) {
465 $data_update['categories_list'] = ','.join(',',$data_other_categories).',';
466 }
467
468 if(!empty($data_other_locations)) {
469 $data_update['locations_list'] = ','.join(',',$data_other_locations).',';
470 }
471
472
473 if(!empty($data_update))
474 $this->listing_m->insert($data_update, $id);
475
476 //exit($this->db->last_query());
477
478 // Save dynamic fields data
479
480 $data['post_id'] = $id;
481
482 if(empty($listingfield_db_data))
483 {
484 $this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, NULL);
485 }
486 else
487 {
488 $this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, $id);
489 }
490
491 //exit($this->db->last_query());
492
493 //exit($this->db->last_error());
494
495 if(wdk_get_option('wdk_sub_listings_enable')) {
496 /* generate fast childs ids for parents */
497
498 if(isset($listing_data['listing_parent_post_id']) && !empty($listing_data['listing_parent_post_id'])) {
499
500 $this->db->select('post_id');
501 $this->db->order_by('sublisting_order,idlisting');
502 $this->db->from($this->listing_m->_table_name);
503 $this->db->where(array('listing_parent_post_id' => intval($listing_data['listing_parent_post_id'])));
504 $query = $this->db->get();
505
506 $listings_childs = '';
507 if ($this->db->num_rows() > 0)
508 foreach ($this->db->results() as $listing) {
509 $listings_childs .= $listing->post_id.',';
510 }
511
512 if(!empty($listings_childs))
513 $listings_childs = substr($listings_childs,0, -1);
514
515 $this->listing_m->insert(array('listing_related_ids' => $listings_childs), intval($listing_data['listing_parent_post_id']));
516 }
517
518 if(isset($listing_data['listing_related_ids']) && !empty($listing_data['listing_related_ids'])) {
519
520 $old_childs_ids = wmvc_show_data('listing_related_ids', $this->data['db_data'],'',TRUE, TRUE);
521 $order_index = 0;
522 $listings_childs = '';
523
524 $old_childs_ids = explode(',', $old_childs_ids);
525 $old_childs_ids = array_flip($old_childs_ids);
526 foreach (explode(',', $listing_data['listing_related_ids']) as $idlisting) {
527 $this->listing_m->insert(array('listing_parent_post_id' => $listing_post_id, 'sublisting_order'=>$order_index++), intval($idlisting));
528
529 if(isset($old_childs_ids[$idlisting])) {
530 unset($old_childs_ids[$idlisting]);
531 }
532 }
533
534 /* clear old related listings */
535 if(!empty($old_childs_ids)) {
536 foreach ($old_childs_ids as $idlisting => $v) {
537 $this->listing_m->insert(array('listing_parent_post_id' => NULL), intval($idlisting));
538 }
539 }
540 }
541 }
542
543 // redirect
544 if(empty($listing_post_id) && !empty($id))
545 {
546 wp_redirect(admin_url("admin.php?page=wdk_listing&id=$id&is_updated=true"));
547 exit;
548 }
549
550 /* fix checkbox after submit */
551 if(!empty($id))
552 {
553 $is_approved_before_save = $this->data['db_data']['is_approved'];
554
555 $listing_post = get_post( $id );
556 $listing_db_data = $this->listing_m->get($id, TRUE);
557 $listingfield_db_data = $this->listingfield_m->get($id, TRUE);
558 $listingusers_db_data = $this->listingusers_m->get($id);
559 $this->data['db_data'] = array_merge((array) $listing_post,
560 (array) $listing_db_data,
561 (array) $listingfield_db_data);
562
563 $this->data['db_data']['listing_agents'] = array();
564 $this->data['db_data']['listing_sub_locations'] = array();
565 $this->data['db_data']['listing_sub_categories'] = array();
566
567 foreach ($listingusers_db_data as $key => $listinguser) {
568 if(isset($this->data['agents'][$listinguser->user_id]))
569 $this->data['db_data']['listing_agents'] [$listinguser->user_id]= $this->data['agents'][$listinguser->user_id];
570 }
571
572 if(isset($_POST['listing_sub_locations'])) {
573 $this->data['db_data']['listing_sub_locations'] = $_POST['listing_sub_locations'];
574 unset($_POST['listing_sub_locations']);
575 }
576 if(isset($_POST['listing_sub_categories'])) {
577 $this->data['db_data']['listing_sub_categories'] = $_POST['listing_sub_categories'];
578 unset($_POST['listing_sub_categories']);
579 }
580
581 /* if approved message */
582 if (!empty($listing_post_id) && wmvc_show_data('user_id', $data, false)) {
583 if( $is_approved_before_save != 1 && wmvc_show_data('is_approved', $data) == 1 && wmvc_show_data('is_activated', $data) == 1) {
584
585 $user = get_userdata( wmvc_show_data('user_id', $data) );
586
587 $data_message = array();
588 $data_message['user'] = $user;
589 $data_message['post_id'] = $id;
590 $data_message['post'] = $this->data['db_data'];
591 $ret = wdk_mail($user->user_email, __('Your Listing Approved', 'wpdirectorykit'), $data_message, 'new_listing_approved');
592 }
593 }
594 }
595
596 }
597
598 $this->data['calendar_id'] = NULL;
599 if(!empty($listing_post_id) && function_exists('run_wdk_bookings')) {
600 global $Winter_MVC_wdk_bookings;
601 $Winter_MVC_wdk_bookings->model('calendar_m');
602 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id'=>$listing_post_id), TRUE);
603 if($calendar) {
604 $this->data['calendar_id'] = $calendar->idcalendar;
605 }
606 }
607
608 $this->load->view('wdk_listing/index', $this->data);
609 }
610
611 }
612