PluginProbe
WP Directory Kit / 1.1.8
WP Directory Kit v1.1.8
1.5.9 1.5.7 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 All 37 releases
wpdirectorykit / application / controllers / Wdk_listing.php

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

552 lines 24.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 = $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' => ''
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 );
232
233 $rules[] = array(
234 'field' => 'rank',
235 'label' => __('Rank', 'wpdirectorykit'),
236 'rules' => 'numeric'
237 );
238
239
240 foreach($this->data['fields'] as $key => $field)
241 {
242 if($field->field_type == 'SECTION'){
243 $this->data['fields'][$key]->is_required = '';
244 continue;
245 }
246
247 $rule_required = (wmvc_show_data('is_required', $field) == 1) ? 'required' : '';
248
249 if(wmvc_show_data('validation', $field)) {
250 if(!empty($rule_required)) {
251 $rule_required .= '|';
252 }
253 $rule_required .= wmvc_show_data('validation', $field);
254 }
255
256 if(!empty(wmvc_show_data('min_length', $field))) {
257 if(!empty($rule_required)) {
258 $rule_required .= '|';
259 }
260
261 if(wmvc_show_data('field_type', $field) == "NUMBER") {
262 $rule_required .= "min_number";
263 } else {
264 $rule_required .= "min_length";
265 }
266 $rule_required .= "[".wmvc_show_data('min_length', $field)."]";
267
268 }
269
270 if(!empty(wmvc_show_data('max_length', $field))) {
271 if(!empty($rule_required)) {
272 $rule_required .= '|';
273 }
274 if(wmvc_show_data('field_type', $field) == "NUMBER") {
275 $rule_required .= "max_number";
276 } else {
277 $rule_required .= "max_length";
278 }
279 $rule_required .= "[".wmvc_show_data('max_length', $field)."]";
280 }
281
282 /* clear rules, because hidden field */
283 if(isset($_POST['category_id']) && !empty($_POST['category_id'])) {
284 if(wdk_depend_is_hidden_field($field->idfield, intval($_POST['category_id']))) {
285 $rule_required = '';
286 }
287 }
288
289 $rules[] =
290 array(
291 'field' => 'field_'.$field->idfield,
292 'label' => $field->field_label,
293 'rules' => $rule_required
294 );
295
296 if(isset($this->data['db_data']['field_'.$field->idfield.'_'.$field->field_type]))
297 $this->data['db_data']['field_'.$field->idfield] =
298 $this->data['db_data']['field_'.$field->idfield.'_'.$field->field_type];
299 }
300
301 if($this->form->run($rules))
302 {
303 // Save procedure for basic data
304
305 $data = $this->listing_m->prepare_data(wdk_get_post(), $rules, FALSE);
306 // Save standard wp post
307
308 // Create post object
309 $listing_post = array(
310 'ID' => $listing_post_id,
311 'post_type' => 'wdk-listing',
312 'post_title' => wp_strip_all_tags( $data['post_title'] ),
313 'post_content' => $data['post_content'],
314 'post_status' => 'publish',
315 'post_author' => get_current_user_id()
316 );
317
318 // Insert the post into the database
319 $id = wp_insert_post( $listing_post );
320
321 // Save our main listing data
322
323 $listing_data = array('post_id' => $id);
324
325 $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','user_id_editor');
326 foreach($listing_data_fields as $field_name)
327 {
328 $listing_data[$field_name] = $data[$field_name];
329 }
330
331 $image_ids = explode(',', $data['listing_images']);
332
333 $listing_data['listing_images_path'] = '';
334 $listing_data['listing_images_path_medium'] = '';
335
336 if(is_array($image_ids)) {
337 foreach ($image_ids as $image_id) {
338 if(is_numeric($image_id))
339 {
340 $image_path = wp_get_original_image_path( $image_id);
341 if($image_path) {
342 /* path of image */
343 $next_path = str_replace(WP_CONTENT_DIR . '/uploads/','', $image_path);
344
345 if(!empty($listing_data['listing_images_path']))
346 $listing_data['listing_images_path'] .= ',';
347
348 $listing_data['listing_images_path'] .= $next_path;
349 }
350
351 $image_url = wp_get_attachment_image_url($image_id, 'large');
352 if($image_url) {
353 $parsed = parse_url($image_url);
354 $next_path = substr($parsed['path'], strpos($parsed['path'], 'uploads/')+8);
355
356 if(!empty($listing_data['listing_images_path_medium']))
357 $listing_data['listing_images_path_medium'] .= ',';
358
359 $listing_data['listing_images_path_medium'] .= $next_path;
360 }
361 }
362 }
363 }
364
365 if(wmvc_user_in_role('administrator') && $this->input->post('slug')) {
366 // update the post slug
367 wp_update_post( array(
368 'ID' => $id,
369 'post_name' => sanitize_text_field($this->input->post('slug'))
370 ));
371
372 }
373
374 if(!function_exists('run_wdk_membership')) {
375 if($listing_data['is_activated'] == 1) {
376 $listing_data['is_approved'] = 1;
377 } else {
378 $listing_data['is_approved'] = 0;
379 }
380 }
381
382 if(!wmvc_user_in_role('administrator')) {
383 unset($listing_data['rank']);
384 }
385
386 if(function_exists('run_wdk_payments') && wmvc_show_data('package_id', $listing_data_field, false)) {
387
388 global $Winter_MVC_wdk_payments;
389 $Winter_MVC_wdk_payments->model('package_m');
390 $package = $Winter_MVC_wdk_payments->package_m->get(wmvc_show_data('package_id', $listing_data_field, false), TRUE);
391 if ($packages) {
392 if(!isset($listing_data['rank']) || empty($listing_data['rank']))
393 $listing_data['rank'] = wdk_show_data('featured_rank',$package, 0, TRUE, TRUE);
394 $listing_data['date_package_expire'] = date('Y-m-d H:i:s', strtotime('+'.wdk_show_data('days_limit',$package, 0, TRUE, TRUE).'days'));
395 }
396 }
397
398 /* dates set */
399 if(isset($this->data['db_data']['date']))
400 $listing_data['date'] = $this->data['db_data']['date'];
401
402 $listing_data['date_modified'] = date('Y-m-d H:i:s');
403
404 if(empty($listing_db_data))
405 {
406 $this->listing_m->insert($listing_data, NULL);
407 }
408 else
409 {
410 $this->listing_m->insert($listing_data, $id);
411 }
412
413 // insert users/agents
414
415 if(function_exists('run_wdk_membership')){
416 $this->listingusers_m->delete_where(array('post_id' => $id));
417 if(is_array($data['listing_agents']))
418 foreach($data['listing_agents'] as $val)
419 {
420 $this->listingusers_m->insert(array('post_id' => $id, 'user_id' => $val), NULL);
421 }
422 }
423
424 $data_other_categories = array();
425 if(isset($data['listing_sub_categories']) || is_null($data['listing_sub_categories'])) {
426 $this->categorieslistings_m->delete_where(array('post_id' => $id));
427 if(is_array($data['listing_sub_categories']))
428 foreach($data['listing_sub_categories'] as $val)
429 {
430 $this->categorieslistings_m->insert(array('post_id' => $id, 'category_id' => $val), NULL);
431
432 $data_other_categories []= $val;
433 }
434 }
435
436 $data_other_locations = array();
437 if(isset($data['listing_sub_locations']) || is_null($data['listing_sub_locations'])) {
438 $this->locationslistings_m->delete_where(array('post_id' => $id));
439 if(is_array($data['listing_sub_locations']))
440 foreach($data['listing_sub_locations'] as $val)
441 {
442 $this->locationslistings_m->insert(array('post_id' => $id, 'location_id' => $val), NULL);
443 $data_other_locations []= $val;
444 }
445 }
446
447 $data_update = array(
448 'categories_list'=> '',
449 'locations_list'=>''
450 );
451
452 if(!empty($data_other_categories)) {
453 $data_update['categories_list'] = ','.join(',',$data_other_categories).',';
454 }
455
456 if(!empty($data_other_locations)) {
457 $data_update['locations_list'] = ','.join(',',$data_other_locations).',';
458 }
459
460
461 if(!empty($data_update))
462 $this->listing_m->insert($data_update, $id);
463
464 //exit($this->db->last_query());
465
466 // Save dynamic fields data
467
468 $data['post_id'] = $id;
469
470 if(empty($listingfield_db_data))
471 {
472 $this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, NULL);
473 }
474 else
475 {
476 $this->listingfield_m->insert_custom_fields($this->data['listing_fields'], $data, $id);
477 }
478
479 //exit($this->db->last_query());
480
481 //exit($this->db->last_error());
482
483 // redirect
484 if(empty($listing_post_id) && !empty($id))
485 {
486 wp_redirect(admin_url("admin.php?page=wdk_listing&id=$id&is_updated=true"));
487 exit;
488 }
489
490 /* fix checkbox after submit */
491 if(!empty($id))
492 {
493 $is_approved_before_save = $this->data['db_data']['is_approved'];
494
495 $listing_post = get_post( $id );
496 $listing_db_data = $this->listing_m->get($id, TRUE);
497 $listingfield_db_data = $this->listingfield_m->get($id, TRUE);
498 $listingusers_db_data = $this->listingusers_m->get($id);
499 $this->data['db_data'] = array_merge((array) $listing_post,
500 (array) $listing_db_data,
501 (array) $listingfield_db_data);
502
503 $this->data['db_data']['listing_agents'] = array();
504 $this->data['db_data']['listing_sub_locations'] = array();
505 $this->data['db_data']['listing_sub_categories'] = array();
506
507 foreach ($listingusers_db_data as $key => $listinguser) {
508 if(isset($this->data['agents'][$listinguser->user_id]))
509 $this->data['db_data']['listing_agents'] [$listinguser->user_id]= $this->data['agents'][$listinguser->user_id];
510 }
511
512 if(isset($_POST['listing_sub_locations'])) {
513 $this->data['db_data']['listing_sub_locations'] = $_POST['listing_sub_locations'];
514 unset($_POST['listing_sub_locations']);
515 }
516 if(isset($_POST['listing_sub_categories'])) {
517 $this->data['db_data']['listing_sub_categories'] = $_POST['listing_sub_categories'];
518 unset($_POST['listing_sub_categories']);
519 }
520
521 /* if approved message */
522 if (!empty($listing_post_id) && wmvc_show_data('user_id', $data, false)) {
523 if( $is_approved_before_save != 1 && wmvc_show_data('is_approved', $data) == 1 && wmvc_show_data('is_activated', $data) == 1) {
524
525 $user = get_userdata( wmvc_show_data('user_id', $data) );
526
527 $data_message = array();
528 $data_message['user'] = $user;
529 $data_message['post_id'] = $id;
530 $data_message['post'] = $this->data['db_data'];
531 $ret = wdk_mail($user->user_email, __('Your Listing Approved', 'wpdirectorykit'), $data_message, 'new_listing_approved');
532 }
533 }
534 }
535
536 }
537
538 $this->data['calendar_id'] = NULL;
539 if(!empty($listing_post_id) && function_exists('run_wdk_bookings')) {
540 global $Winter_MVC_wdk_bookings;
541 $Winter_MVC_wdk_bookings->model('calendar_m');
542 $calendar = $Winter_MVC_wdk_bookings->calendar_m->get_by(array('post_id'=>$listing_post_id), TRUE);
543 if($calendar) {
544 $this->data['calendar_id'] = $calendar->idcalendar;
545 }
546 }
547
548 $this->load->view('wdk_listing/index', $this->data);
549 }
550
551 }
552