PluginProbe
Property Hive / 2.2.3
Property Hive v2.2.3
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / admin / settings / class-ph-settings-offices.php

class-ph-settings-offices.php in Property Hive 2.2.3, at includes/admin/settings/class-ph-settings-offices.php

648 lines 27.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Office Settings
4 *
5 * @author PropertyHive
6 * @category Admin
7 * @package PropertyHive/Admin
8 * @version 1.0.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
12
13 if ( ! class_exists( 'PH_Settings_Offices' ) ) :
14
15 /**
16 * PH_Settings_Offices
17 */
18 class PH_Settings_Offices extends PH_Settings_Page {
19
20 /**
21 * Constructor.
22 */
23 public function __construct() {
24 $this->id = 'offices';
25 $this->label = __( 'Offices', 'propertyhive' );
26
27 add_filter( 'propertyhive_settings_tabs_array', array( $this, 'add_settings_page' ), 10 );
28 add_action( 'propertyhive_settings_' . $this->id, array( $this, 'output' ) );
29 add_action( 'propertyhive_settings_save_' . $this->id, array( $this, 'save' ) );
30 add_action( 'propertyhive_sections_' . $this->id, array( $this, 'output_sections' ) );
31 add_action( 'propertyhive_admin_field_offices', array( $this, 'offices_setting' ) );
32 }
33
34 /**
35 * Get settings array
36 *
37 * @return array
38 */
39 public function get_settings() {
40
41 return apply_filters( 'propertyhive_office_settings', array(
42
43 array( 'title' => __( 'Offices', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'offices_options' ),
44
45 array(
46 'type' => 'offices',
47 ),
48
49 array( 'type' => 'sectionend', 'id' => 'offices_options' ),
50 ));
51
52 }
53
54 /**
55 * Get add/edit office settings array
56 *
57 * @return array
58 */
59 public function get_office_settings() {
60
61 global $current_section;
62
63 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
64
65 $args = array(
66
67 array( 'title' => __( ( $current_section == 'add' ? 'Add New Office' : 'Edit Office Details' ), 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_options' ),
68
69 array(
70 'title' => __( 'Office Name', 'propertyhive' ),
71 'id' => 'office_name',
72 //'css' => 'width:50px;',
73 'default' => get_the_title($current_id),
74 'type' => 'text',
75 'desc_tip' => false,
76 ),
77
78 array(
79 'title' => __( 'Address Line 1', 'propertyhive' ),
80 'id' => '_office_address_1',
81 //'css' => 'width:50px;',
82 'default' => get_post_meta($current_id, '_office_address_1', TRUE),
83 'type' => 'text',
84 'desc_tip' => false,
85 ),
86
87 array(
88 'title' => __( 'Address Line 2', 'propertyhive' ),
89 'id' => '_office_address_2',
90 //'css' => 'width:50px;',
91 'default' => get_post_meta($current_id, '_office_address_2', TRUE),
92 'type' => 'text',
93 'desc_tip' => false,
94 ),
95
96 array(
97 'title' => __( 'Address Line 3', 'propertyhive' ),
98 'id' => '_office_address_3',
99 //'css' => 'width:50px;',
100 'default' => get_post_meta($current_id, '_office_address_3', TRUE),
101 'type' => 'text',
102 'desc_tip' => false,
103 ),
104
105 array(
106 'title' => __( 'Address Line 4', 'propertyhive' ),
107 'id' => '_office_address_4',
108 //'css' => 'width:50px;',
109 'default' => get_post_meta($current_id, '_office_address_4', TRUE),
110 'type' => 'text',
111 'desc_tip' => false,
112 ),
113
114 array(
115 'title' => __( 'Postcode', 'propertyhive' ),
116 'id' => '_office_address_postcode',
117 'css' => 'width:85px;',
118 'default' => get_post_meta($current_id, '_office_address_postcode', TRUE),
119 'type' => 'text',
120 'desc_tip' => false,
121 ),
122
123 array( 'type' => 'sectionend', 'id' => 'office_options' ),
124
125 array( 'title' => __( 'Contact Details', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_contact_options' ),
126
127 );
128
129 $departments = ph_get_departments();
130
131 foreach ( $departments as $key => $value )
132 {
133 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
134 {
135 $args[] = array(
136 'title' => sprintf(
137 /* translators: %s: department (e.g. Sales, Lettings, Commercial) */
138 __( 'Telephone Number (%s)', 'propertyhive' ),
139 $value
140 ),
141 'id' => '_office_telephone_number_' . str_replace("residential-", "", $key),
142 //'css' => 'width:50px;',
143 'default' => get_post_meta($current_id, '_office_telephone_number_' . str_replace("residential-", "", $key), TRUE),
144 'type' => 'text',
145 'desc_tip' => false,
146 );
147
148 $args[] = array(
149 'title' => sprintf(
150 /* translators: %s: department (e.g. Sales, Lettings, Commercial) */
151 __( 'Email Address (%s)', 'propertyhive' ),
152 $value
153 ),
154 'id' => '_office_email_address_' . str_replace("residential-", "", $key),
155 //'css' => 'width:50px;',
156 'default' => get_post_meta($current_id, '_office_email_address_' . str_replace("residential-", "", $key), TRUE),
157 'type' => 'text',
158 'desc_tip' => false,
159 );
160 }
161 }
162
163 $args[] = array( 'type' => 'sectionend', 'id' => 'office_contact_options' );
164
165 $args[] = array( 'title' => __( 'Office Location', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_location_options' );
166
167 $args[] = array(
168 'title' => __( 'Latitude', 'propertyhive' ),
169 'id' => '_office_latitude',
170 //'css' => 'width:50px;',
171 'default' => get_post_meta($current_id, '_office_latitude', TRUE),
172 'type' => 'text',
173 'desc_tip' => false,
174 );
175
176 $args[] = array(
177 'title' => __( 'Longitude', 'propertyhive' ),
178 'id' => '_office_longitude',
179 //'css' => 'width:50px;',
180 'default' => get_post_meta($current_id, '_office_longitude', TRUE),
181 'type' => 'text',
182 'desc_tip' => false,
183 );
184
185 $args[] = array( 'type' => 'sectionend', 'id' => 'office_location_options' );
186
187 return apply_filters( 'propertyhive_office_details_settings', $args );
188
189 }
190
191 /**
192 * Get delete office settings array
193 *
194 * @return array
195 */
196 public function get_office_delete() {
197
198 global $save_button_text, $post;
199
200 $save_button_text = __( 'Delete', 'propertyhive' );
201
202 if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == 1 )
203 {
204 // A term has just been deleted
205 global $hide_save_button, $show_cancel_button, $cancel_button_href;
206
207 $hide_save_button = TRUE;
208 $show_cancel_button = TRUE;
209 $cancel_button_href = admin_url( 'admin.php?page=ph-settings&tab=offices' );
210
211 $args = array();
212
213 $args[] = array( 'title' => __( 'Successfully Deleted Office', 'propertyhive' ), 'type' => 'title', 'desc' => '', 'id' => 'office_delete' );
214
215 $args[] = array(
216 'title' => __( 'Office Deleted', 'propertyhive' ),
217 'id' => '',
218 'html' => __('Office deleted successfully', 'propertyhive' ) . ' <a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Go Back', 'propertyhive' ) . '</a>',
219 'type' => 'html',
220 'desc_tip' => false,
221 );
222
223 $args[] = array( 'type' => 'sectionend', 'id' => 'office_delete' );
224 }
225 else
226 {
227 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
228
229 if ($current_id == '')
230 {
231 die("ID not passed");
232 }
233 else
234 {
235 $post_type = get_post_type( $current_id );
236
237 if ( $post_type == 'office' )
238 {
239 $args = array();
240
241 $args[] = array( 'title' => __( 'Delete Office', 'propertyhive' ) . ': ' . get_the_title( $current_id ), 'type' => 'title', 'desc' => '', 'id' => 'office_delete_options' );
242
243 // Get number of properties assigned to this term
244 $query_args = array(
245 'post_type' => 'property',
246 'nopaging' => true,
247 'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
248 'meta_query' => array(
249 array(
250 'key' => '_office_id',
251 'value' => $current_id,
252 'compare' => '='
253 )
254 )
255 );
256 $property_query = new WP_Query( $query_args );
257
258 $num_properties = $property_query->found_posts;
259
260 // Get number of applicants assigned to this term (future)
261
262 if ($num_properties > 0)
263 {
264 $alternative_offices = array();
265
266 $query_args = array(
267 'post_type' => 'office',
268 'nopaging' => true,
269 'post__not_in' => array( $current_id ),
270 'orderby' => 'title',
271 'order' => 'ASC'
272 );
273 $office_query = new WP_Query( $query_args );
274
275 if ( $office_query->have_posts() )
276 {
277 while ( $office_query->have_posts() )
278 {
279 $office_query->the_post();
280
281 $alternative_terms[$post->ID] = get_the_title();
282 }
283 }
284
285 // There are properties assigned to this term
286 $args[] = array(
287 'title' => __( 'Re-assign to', 'propertyhive' ),
288 'id' => 'reassign_to',
289 'default' => '',
290 'options' => $alternative_terms,
291 'type' => 'select',
292 'desc_tip' => false,
293 'desc' => __( 'There are properties that are assigned to the office being deleted. Which office should they be reassigned to?' , 'propertyhive' )
294 );
295 }
296
297 $args[] = array(
298 'title' => __( 'Confirm removal?', 'propertyhive' ),
299 'id' => 'confirm_removal',
300 'type' => 'checkbox',
301 'desc_tip' => false,
302 );
303
304 $args[] = array( 'type' => 'sectionend', 'id' => 'office_delete_options' );
305 }
306 else
307 {
308 die("Trying to delete a post that isn't an office.");
309 }
310 }
311 }
312
313 return apply_filters( 'propertyhive_office_delete_settings', $args );
314
315 }
316
317 /**
318 * Output the settings
319 */
320 public function output() {
321 global $current_section, $redirect_after_save;
322
323 if ( $current_section == 'add' ) {
324
325 remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' ));
326
327 $settings = $this->get_office_settings();
328
329 $redirect_after_save = admin_url('admin.php?page=ph-settings&tab=offices');
330
331 PH_Admin_Settings::output_fields( $settings );
332
333 } elseif ( $current_section == 'edit' ) {
334
335 remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' ));
336
337 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
338
339 $settings = $this->get_office_settings();
340
341 $redirect_after_save = admin_url('admin.php?page=ph-settings&tab=offices');
342
343 PH_Admin_Settings::output_fields( $settings );
344
345 } elseif ( $current_section == 'delete' ) {
346
347 remove_action('propertyhive_admin_field_offices', array( $this, 'offices_setting' ));
348
349 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
350
351 $settings = $this->get_office_delete();
352
353 PH_Admin_Settings::output_fields( $settings );
354
355 } else {
356 $settings = $this->get_settings();
357
358 PH_Admin_Settings::output_fields( $settings );
359 }
360 }
361
362 /**
363 * Output list of offices
364 *
365 * @access public
366 * @return void
367 */
368 public function offices_setting() {
369 global $post;
370
371 ?>
372 <tr valign="top">
373 <th scope="row" class="titledesc">
374 &nbsp;
375 </th>
376 <td class="forminp forminp-button">
377 <a href="<?php echo esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=add' )); ?>" class="button alignright"><?php echo esc_html(__( 'Add New Office', 'propertyhive' )); ?></a>
378 </td>
379 </tr>
380 <tr valign="top">
381 <th scope="row" class="titledesc"><?php echo esc_html(__( 'Offices', 'propertyhive' )); ?></th>
382 <td class="forminp">
383 <table class="ph_offices widefat" cellspacing="0">
384 <thead>
385 <tr>
386 <th class="primary"><?php echo esc_html(__( 'Primary', 'propertyhive' )); ?></th>
387 <th class="name"><?php echo esc_html(__( 'Name', 'propertyhive' )); ?></th>
388 <th class="address"><?php echo esc_html(__( 'Address', 'propertyhive' )); ?></th>
389 <th class="contact"><?php echo esc_html(__( 'Contact Details', 'propertyhive' )); ?></th>
390 <?php do_action( 'propertyhive_office_table_header_columns' ); ?>
391 <th class="settings">&nbsp;</th>
392 </tr>
393 </thead>
394 <tbody>
395 <?php
396 $args = array(
397 'post_type' => 'office',
398 'nopaging' => true,
399 'orderby' => 'title',
400 'order' => 'ASC'
401 );
402 $office_query = new WP_Query($args);
403
404 if ($office_query->have_posts())
405 {
406 $num_offices = $office_query->found_posts;
407
408 while ($office_query->have_posts())
409 {
410 $office_query->the_post();
411
412 $address = '';
413 $address_part = get_post_meta($post->ID, '_office_address_1', TRUE);
414 $address .= $address_part . ', ';
415 $address_part = get_post_meta($post->ID, '_office_address_2', TRUE);
416 $address .= $address_part . ', ';
417 $address_part = get_post_meta($post->ID, '_office_address_3', TRUE);
418 $address .= $address_part . ', ';
419 $address_part = get_post_meta($post->ID, '_office_address_4', TRUE);
420 $address .= $address_part . ' ';
421 $address_part = get_post_meta($post->ID, '_office_address_postcode', TRUE);
422 $address .= $address_part;
423
424 $contact_details = '';
425
426 $departments = ph_get_departments();
427
428 foreach ( $departments as $key => $value )
429 {
430 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
431 {
432 $contact_details .= 'T: ' . esc_html(get_post_meta($post->ID, '_office_telephone_number_' . str_replace("residential-", "", $key), TRUE)) . '<br>';
433 $contact_details .= 'E: ' . esc_html(get_post_meta($post->ID, '_office_email_address_' . str_replace("residential-", "", $key), TRUE)) . '<br>';
434 }
435 }
436
437 echo '<tr>
438 <td width="1%" class="primary">
439 <input type="radio" name="primary" value="' . esc_attr( $post->ID ) . '" ' . checked( get_post_meta($post->ID, 'primary', TRUE), '1', false ) . ' />
440 </td>
441 <td class="name">
442 ' . esc_html(get_the_title()) . '
443 </td>
444 <td class="address">
445 ' . esc_html($address) . '
446 </td>
447 <td class="contact">
448 ' . $contact_details . '
449 </td>';
450 do_action( 'propertyhive_office_table_row_columns', get_the_ID() );
451 echo '
452 <td class="settings">
453 <a class="button" href="' . esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=edit&id=' . $post->ID )) . '">' . esc_html(__( 'Edit', 'propertyhive' )) . '</a>
454 ';
455 if ( $num_offices > 1 && get_post_meta($post->ID, 'primary', TRUE) != '1' )
456 {
457 echo '<a class="button" href="' . esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=delete&id=' . $post->ID )) . '">' . esc_html(__( 'Delete', 'propertyhive' )) . '</a>';
458 }
459 echo '
460 </td>
461 </tr>';
462 }
463 }
464 wp_reset_postdata();
465 ?>
466 </tbody>
467 </table>
468 </td>
469 </tr>
470 <tr valign="top">
471 <th scope="row" class="titledesc">
472 &nbsp;
473 </th>
474 <td class="forminp forminp-button">
475 <a href="<?php echo esc_url(admin_url( 'admin.php?page=ph-settings&tab=offices&section=add' )); ?>" class="button alignright"><?php echo esc_html(__( 'Add New Office', 'propertyhive' )); ?></a>
476 </td>
477 </tr>
478 <?php
479 }
480
481 /**
482 * Save settings
483 */
484 public function save() {
485 global $current_section, $post;
486
487 if ( $current_section == 'add' ) {
488
489 // TODO: Validate (check for blank fields, and that office name doest exist already)
490
491 // Insert office
492 $office_post = array(
493 'post_title' => ph_clean( $_POST['office_name'] ),
494 'post_content' => '',
495 'post_status' => 'publish',
496 'post_type' => 'office',
497 'comment_status' => 'closed',
498 'ping_status' => 'closed',
499 );
500
501 // Insert the post into the database
502 $office_post_id = wp_insert_post( $office_post );
503
504 // TODO: Check for errors returned from wp_insert_post()
505 // TODO: Add meta information
506
507 PH_Admin_Settings::add_message( __( 'Office added successfully', 'propertyhive' ) . ' ' . '<a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Return to offices', 'propertyhive' ) . '</a>' );
508
509 } elseif ( $current_section == 'edit' ) {
510
511 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
512
513 // TODO: Validate
514 // TODO: Make sure this ID belongs to an office
515 // TODO: Update slug?
516
517 // Update office
518 $office_post = array(
519 'ID' => $current_id,
520 'post_title' => ph_clean( $_POST['office_name'] )
521 );
522
523 wp_update_post( $office_post );
524
525 $office_post_id = $current_id;
526
527 // TODO: Check for errors returned from wp_update_post()
528 // TODO: Update meta information
529
530 PH_Admin_Settings::add_message( __( 'Office details updated successfully', 'propertyhive' ) . ' ' . '<a href="' . admin_url( 'admin.php?page=ph-settings&tab=offices' ) . '">' . __( 'Return to offices', 'propertyhive' ) . '</a>' );
531
532 } elseif ( $current_section == 'delete' ) {
533
534 if ( isset($_POST['confirm_removal']) && $_POST['confirm_removal'] == '1' )
535 {
536 $current_id = empty( $_REQUEST['id'] ) ? '' : (int)$_REQUEST['id'];
537
538 // Get number of properties assigned to this term
539 $query_args = array(
540 'post_type' => 'property',
541 'nopaging' => true,
542 'post_status' => array( 'pending', 'auto-draft', 'draft', 'private', 'publish', 'future', 'trash' ),
543 'meta_query' => array(
544 array(
545 'key' => '_office_id',
546 'value' => $current_id,
547 'compare' => '='
548 )
549 )
550 );
551 $property_query = new WP_Query( $query_args );
552
553 if ( $property_query->have_posts() )
554 {
555 if ( !isset($_POST['reassign_to']) || ( isset( $_POST['reassign_to'] ) && empty( $_POST['reassign_to'] ) ) )
556 {
557 die("Not assigning properties to new office. Please try again");
558 }
559 else
560 {
561 $post_type = get_post_type( (int)$_POST['reassign_to'] );
562
563 if ( $post_type != 'office' )
564 {
565 die("New office isn't of type office. It's of type: " . esc_html($post_type));
566 }
567 }
568
569 while ( $property_query->have_posts() )
570 {
571 $property_query->the_post();
572
573 update_post_meta( $post->ID, '_office_id', (int)$_POST['reassign_to'] );
574
575 // TODO: Check for WP_ERROR
576 }
577 }
578
579 wp_delete_post( $current_id );
580
581 // TODO: Check for WP_ERROR
582 }
583
584 }
585
586 if ( $current_section != 'delete' )
587 {
588 if ( $current_section != 'add' && $current_section != 'edit' )
589 {
590 // Set all offices as not primary
591 // Prevents multiple offices being set as primary
592 $args = array(
593 'post_type' => 'office',
594 'nopaging' => true,
595 'orderby' => 'title',
596 'order' => 'ASC'
597 );
598 $office_query = new WP_Query($args);
599
600 if ($office_query->have_posts())
601 {
602 while ($office_query->have_posts())
603 {
604 $office_query->the_post();
605
606 // Set selected office as primary
607 update_post_meta($post->ID, 'primary', '0');
608 }
609 }
610
611 wp_reset_postdata();
612
613 // Set selected office as primary
614 update_post_meta( (int)$_POST['primary'], 'primary', '1');
615 }
616 else
617 {
618 update_post_meta($office_post_id, '_office_address_1', ph_clean( $_POST['_office_address_1'] ));
619 update_post_meta($office_post_id, '_office_address_2', ph_clean( $_POST['_office_address_2'] ));
620 update_post_meta($office_post_id, '_office_address_3', ph_clean( $_POST['_office_address_3'] ));
621 update_post_meta($office_post_id, '_office_address_4', ph_clean( $_POST['_office_address_4'] ));
622 update_post_meta($office_post_id, '_office_address_postcode', ph_clean( $_POST['_office_address_postcode'] ));
623
624 $departments = ph_get_departments();
625
626 foreach ( $departments as $key => $value )
627 {
628 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
629 {
630 update_post_meta($office_post_id, '_office_telephone_number_' . str_replace("residential-", "", $key), (isset($_POST['_office_telephone_number_' . str_replace("residential-", "", $key)])) ? ph_clean( $_POST['_office_telephone_number_' . str_replace("residential-", "", $key)] ) : '');
631 update_post_meta($office_post_id, '_office_email_address_' . str_replace("residential-", "", $key), (isset($_POST['_office_email_address_' . str_replace("residential-", "", $key)])) ? ph_clean( $_POST['_office_email_address_' . str_replace("residential-", "", $key)] ) : '');
632 }
633 }
634
635 update_post_meta($office_post_id, '_office_latitude', ph_clean( $_POST['_office_latitude'] ));
636 update_post_meta($office_post_id, '_office_longitude', ph_clean( $_POST['_office_longitude'] ));
637
638 do_action( 'propertyhive_save_office', $office_post_id );
639 }
640 }
641 }
642
643 }
644
645 endif;
646
647 return new PH_Settings_Offices();
648