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