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 / class-ph-admin-applicant-list.php

class-ph-admin-applicant-list.php in Property Hive 2.2.3, at includes/admin/class-ph-admin-applicant-list.php

1,098 lines 53.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PropertyHive Admin Generate Applicant List Class.
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_Admin_Applicant_List' ) ) :
14
15 /**
16 * PH_Admin_Applicant_List
17 */
18 class PH_Admin_Applicant_List {
19
20 /**
21 * Handles the display of the main Property Hive reports page in admin.
22 *
23 * @access public
24 * @return void
25 */
26 public function output() {
27
28 $property_types = array();
29 $locations = array();
30 ?>
31 <div class="wrap propertyhive">
32
33 <h1>Generate Applicant List</h1>
34
35 <form method="post" id="mainform" action="" class="applicant-list-form">
36
37 <input type="hidden" name="submitted_applicant_list" value="1">
38 <?php wp_nonce_field( 'ph_applicant_export', 'ph_applicant_export_nonce' ); ?>
39
40 <div id="poststuff" class="propertyhive_meta_box">
41
42 <p class="form-field">
43 <label><?php echo esc_html__( 'Looking For', 'propertyhive' ); ?></label>
44 <select name="department">
45 <?php
46
47 $departments = ph_get_departments();
48
49 $department_options = array();
50 foreach ( $departments as $key => $value )
51 {
52 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
53 {
54 $department_options[$key] = $value;
55 }
56 }
57
58 foreach ( $department_options as $key => $department )
59 {
60 echo '<option value="' . esc_attr($key) . '"';
61 if ( isset($_POST['department']) && $_POST['department'] == $key )
62 {
63 echo ' selected';
64 }
65 elseif ( !isset($_POST['department']) && $key == get_option( 'propertyhive_primary_department' ) )
66 {
67 echo ' selected';
68 }
69 echo '>' . esc_html($department) . '</option>';
70 }
71 ?>
72
73 </select>
74 </p>
75
76 <p class="form-field sales-only">
77 <label><?php echo esc_html__( 'Maximum Price', 'propertyhive' ); ?> <img class="help_tip" data-tip="This will search the applicant's Match Price Range if one is set and return applicants where the price entered falls into this range. Otherwise it will search the Maximum Price and return applicants that have maximum price higher than the value entered" src="<?php echo esc_url(PH()->plugin_url()); ?>/assets/images/help.png" height="16" width="16" /></label>
78 <input type="text" name="maximum_price" value="<?php if ( isset($_POST['maximum_price']) ) { echo esc_attr( $_POST['maximum_price'] ); } ?>">
79 </p>
80
81 <p class="form-field lettings-only">
82 <label><?php echo esc_html__( 'Maximum Rent (PCM)', 'propertyhive' ); ?></label>
83 <input type="text" name="maximum_rent" value="<?php if ( isset($_POST['maximum_rent']) ) { echo esc_attr( $_POST['maximum_rent'] ); } ?>">
84 </p>
85
86 <p class="form-field residential-only">
87 <label><?php echo esc_html__( 'Minimum Bedrooms', 'propertyhive' ); ?></label>
88 <input type="number" name="minimum_bedrooms" class="short" value="<?php if ( isset($_POST['minimum_bedrooms']) ) { echo esc_attr( $_POST['minimum_bedrooms'] ); } ?>">
89 </p>
90
91 <p class="form-field residential-only">
92 <label><?php echo esc_html__( 'Property Types', 'propertyhive' ); ?></label>
93 <select id="property_types" name="property_types[]" multiple="multiple" data-placeholder="<?php echo esc_attr__( 'Start typing to add property types', 'propertyhive' ); ?>..." class="multiselect attribute_values">
94 <?php
95 $options = array( '' => '' );
96 $args = array(
97 'hide_empty' => false,
98 'parent' => 0
99 );
100 $terms = get_terms( 'property_type', $args );
101
102 if ( !empty( $terms ) && !is_wp_error( $terms ) )
103 {
104 foreach ($terms as $term)
105 {
106 $property_types[$term->term_id] = esc_html( $term->name );
107
108 echo '<option value="' . esc_attr( $term->term_id ) . '"';
109 if ( isset($_POST['property_types']) && in_array( $term->term_id, $_POST['property_types'] ) )
110 {
111 echo ' selected';
112 }
113 echo '>' . esc_html( $term->name ) . '</option>';
114
115 $args = array(
116 'hide_empty' => false,
117 'parent' => $term->term_id
118 );
119 $subterms = get_terms( 'property_type', $args );
120
121 if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
122 {
123 foreach ($subterms as $term)
124 {
125 $property_types[$term->term_id] = esc_html( $term->name );
126
127 echo '<option value="' . esc_attr( $term->term_id ) . '"';
128 if ( isset($_POST['property_types']) && in_array( $term->term_id, $_POST['property_types'] ) )
129 {
130 echo ' selected';
131 }
132 echo '>- ' . esc_html( $term->name ) . '</option>';
133 }
134 }
135 }
136 }
137 ?>
138 </select>
139 </p>
140
141 <p class="form-field">
142 <label><?php echo esc_html__( 'Location', 'propertyhive' ); ?></label>
143 <select id="locations" name="locations[]" multiple="multiple" data-placeholder="<?php echo esc_attr__( 'Start typing to add locations', 'propertyhive' ); ?>..." class="multiselect attribute_values">
144 <?php
145 $options = array( '' => '' );
146 $args = array(
147 'hide_empty' => false,
148 'parent' => 0
149 );
150 $terms = get_terms( 'location', $args );
151
152 if ( !empty( $terms ) && !is_wp_error( $terms ) )
153 {
154 foreach ($terms as $term)
155 {
156 $locations[$term->term_id] = esc_html( $term->name );
157
158 echo '<option value="' . esc_attr( $term->term_id ) . '"';
159 if ( isset($_POST['locations']) && in_array( $term->term_id, $_POST['locations'] ) )
160 {
161 echo ' selected';
162 }
163 echo '>' . esc_html( $term->name ) . '</option>';
164
165 $args = array(
166 'hide_empty' => false,
167 'parent' => $term->term_id
168 );
169 $subterms = get_terms( 'location', $args );
170
171 if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
172 {
173 foreach ($subterms as $term)
174 {
175 $locations[$term->term_id] = esc_html( $term->name );
176
177 echo '<option value="' . esc_attr( $term->term_id ) . '"';
178 if ( isset($_POST['locations']) && in_array( $term->term_id, $_POST['locations'] ) )
179 {
180 echo ' selected';
181 }
182 echo '>- ' . esc_html( $term->name ) . '</option>';
183
184 $args = array(
185 'hide_empty' => false,
186 'parent' => $term->term_id
187 );
188 $subsubterms = get_terms( 'location', $args );
189
190 if ( !empty( $subsubterms ) && !is_wp_error( $subsubterms ) )
191 {
192 foreach ($subsubterms as $term)
193 {
194 $locations[$term->term_id] = esc_html( $term->name );
195
196 echo '<option value="' . esc_attr( $term->term_id ) . '"';
197 if ( isset($_POST['locations']) && in_array( $term->term_id, $_POST['locations'] ) )
198 {
199 echo ' selected';
200 }
201 echo '>- - ' . esc_html( $term->name ) . '</option>';
202 }
203 }
204 }
205 }
206 }
207 }
208 ?>
209 </select>
210 </p>
211
212 <p class="form-field">
213 <label><?php echo esc_html__( 'Include Applicants with \'Send Matching Properties\' Unticked', 'propertyhive' ); ?></label>
214 <input type="checkbox" name="include_non_send_matching_properties" value="yes"<?php if ( isset($_POST['include_non_send_matching_properties']) && sanitize_text_field($_POST['include_non_send_matching_properties']) == 'yes' ) { echo ' checked'; } ?>>
215 </p>
216
217 <?php do_action('propertyhive_applicant_list_additional_fields'); ?>
218
219 <p class="form-field">
220 <input type="submit" value="<?php echo esc_attr(__( 'Generate Applicant List', 'propertyhive' )); ?>" class="button-primary">
221 <a href="" class="button" id="export_applicant_list_results_button"><?php echo esc_html__( 'Export To CSV', 'propertyhive' ); ?></a>
222 <input type="hidden" name="export_applicant_list_results" value="">
223 </p>
224
225 </div>
226
227 </form>
228
229 <div class="applicant-list-results">
230
231 <?php
232 if ( isset($_POST['submitted_applicant_list']) && $_POST['submitted_applicant_list'] == '1' )
233 {
234 $results = $this->generate_results();
235 ?>
236 <br>
237 <div class="applicant-list-results">
238 <h3><?php echo esc_html(number_format(count($results))); ?> Applicants Found Matching Your Criteria</h3>
239 <table width="100%" cellpadding="8" cellspacing="0">
240 <thead>
241 <tr>
242 <th style="text-align:left;"><?php echo esc_html__( 'Applicant Name', 'propertyhive' ); ?></th>
243 <th style="text-align:left;"><?php echo esc_html__( 'Contact Details', 'propertyhive' ); ?></th>
244 <th style="text-align:left;"><?php echo esc_html__( 'Requirements', 'propertyhive' ); ?></th>
245 </tr>
246 </thead>
247 <tbody>
248 <?php
249 if ( !empty($results) )
250 {
251 $percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' );
252 $percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' );
253
254 foreach ( $results as $result )
255 {
256 $currency = '&pound;';
257 if ( isset($result['profile']['currency']) && !empty($result['profile']['currency']) )
258 {
259 $PH_Countries = new PH_Countries();
260 $selected_currency = $PH_Countries->get_currency($result['profile']['currency']);
261 if ( $selected_currency !== false )
262 {
263 $currency = $selected_currency['currency_symbol'];
264 }
265 }
266 ?>
267 <tr>
268 <td><a href="<?php echo esc_attr($result['edit_link']); ?>" target="_blank"><?php echo esc_html($result['name']); ?></a></td>
269 <td><?php
270 $contact_details = array();
271 if ( $result['telephone_number'] != '' )
272 {
273 $contact_details[] = 'T: ' . esc_html($result['telephone_number']);
274 }
275 if ( $result['email_address'] != '' )
276 {
277 $contact_details[] = 'E: ' . esc_html($result['email_address']);
278 }
279 echo !empty($contact_details) ? implode("<br>", $contact_details) : '-';
280 ?></td>
281 <td><?php
282 if ( isset($result['profile']['department']) )
283 {
284 switch ( $result['profile']['department'] )
285 {
286 case "residential-sales":
287 {
288 $output = array();
289 if ( isset($result['profile']['max_price']) && $result['profile']['max_price'] != '' && $result['profile']['max_price'] != 0 )
290 {
291 $output[] = '<strong>Max Price:</strong> ' . $currency . esc_html(ph_display_price_field($result['profile']['max_price']));
292
293 if ( $percentage_lower != '' && $percentage_higher != '' )
294 {
295 $match_price_range_lower = '';
296 if ( !isset($result['profile']['match_price_range_lower_actual']) || ( isset($result['profile']['match_price_range_lower_actual']) && $result['profile']['match_price_range_lower_actual'] == '' ) )
297 {
298 if ( isset($result['profile']['max_price']) && $result['profile']['max_price'] != '' )
299 {
300 $match_price_range_lower = $result['profile']['max_price'] - ( $result['profile']['max_price'] * ( $percentage_lower / 100 ) );
301 }
302 }
303 else
304 {
305 $match_price_range_lower = $result['profile']['match_price_range_lower'];
306 }
307
308 $match_price_range_higher = '';
309 if ( !isset($result['profile']['match_price_range_higher_actual']) || ( isset($result['profile']['match_price_range_higher_actual']) && $result['profile']['match_price_range_higher_actual'] == '' ) )
310 {
311 if ( isset($result['profile']['max_price']) && $result['profile']['max_price'] != '' )
312 {
313 $match_price_range_higher = $result['profile']['max_price'] + ( $result['profile']['max_price'] * ( $percentage_higher / 100 ) );
314 }
315 }
316 else
317 {
318 $match_price_range_higher = $result['profile']['match_price_range_higher'];
319 }
320
321 if (
322 $match_price_range_lower != '' && $match_price_range_higher != ''
323 )
324 {
325 $output[] = '<strong>Max Price Range:</strong> ' . $currency . esc_html(ph_display_price_field($match_price_range_lower)) . ' to ' . $currency . esc_html(ph_display_price_field($match_price_range_higher));
326 }
327 }
328 }
329 if ( isset($result['profile']['min_beds']) && $result['profile']['min_beds'] != '' && $result['profile']['min_beds'] != 0 )
330 {
331 $output[] = '<strong>Min Beds:</strong> ' . esc_html(number_format($result['profile']['min_beds']));
332 }
333 if ( isset($result['profile']['property_types']) && is_array($result['profile']['property_types']) && !empty($result['profile']['property_types']) )
334 {
335 $output_types = array();
336 foreach ( $result['profile']['property_types'] as $profile_type )
337 {
338 if ( isset($property_types[$profile_type]) )
339 {
340 $output_types[] = $property_types[$profile_type];
341 }
342 }
343 $output[] = '<strong>Property Types:</strong> ' . esc_html(implode(", ", $output_types));
344 }
345 if ( isset($result['profile']['locations']) && is_array($result['profile']['locations']) && !empty($result['profile']['locations']) )
346 {
347 $output_locations = array();
348 foreach ( $result['profile']['locations'] as $profile_location )
349 {
350 if ( isset($locations[$profile_location]) )
351 {
352 $output_locations[] = $locations[$profile_location];
353 }
354 }
355 $output[] = '<strong>Locations:</strong> ' . esc_html(implode(", ", $output_locations));
356 }
357 if ( isset($result['profile']['notes']) && $result['profile']['notes'] != '' )
358 {
359 $output[] = '<strong>Additional Requirements:</strong> ' . nl2br(esc_html($result['profile']['notes']));
360 }
361 echo( !empty($output) ? implode("<br>", $output) : '-' );
362 break;
363 }
364 case "residential-lettings":
365 {
366 $output = array();
367 if ( isset($result['profile']['max_rent']) && $result['profile']['max_rent'] != '' && $result['profile']['max_rent'] != 0 )
368 {
369 $output[] = '<strong>Max Rent:</strong> ' . $currency . esc_html(ph_display_price_field($result['profile']['max_rent']) . $result['profile']['rent_frequency']);
370 }
371 if ( isset($result['profile']['min_beds']) && $result['profile']['min_beds'] != '' && $result['profile']['min_beds'] != 0 )
372 {
373 $output[] = '<strong>Min Beds:</strong> ' . esc_html(number_format($result['profile']['min_beds']));
374 }
375 if ( isset($result['profile']['property_types']) && is_array($result['profile']['property_types']) && !empty($result['profile']['property_types']) )
376 {
377 $output_types = array();
378 foreach ( $result['profile']['property_types'] as $profile_type )
379 {
380 if ( isset($property_types[$profile_type]) )
381 {
382 $output_types[] = $property_types[$profile_type];
383 }
384 }
385 $output[] = '<strong>Property Types:</strong> ' . esc_html(implode(", ", $output_types));
386 }
387 if ( isset($result['profile']['locations']) && is_array($result['profile']['locations']) && !empty($result['profile']['locations']) )
388 {
389 $output_locations = array();
390 foreach ( $result['profile']['locations'] as $profile_location )
391 {
392 if ( isset($locations[$profile_location]) )
393 {
394 $output_locations[] = $locations[$profile_location];
395 }
396 }
397 $output[] = '<strong>Locations:</strong> ' . esc_html(implode(", ", $output_locations));
398 }
399 if ( isset($result['profile']['notes']) && $result['profile']['notes'] != '' )
400 {
401 $output[] = '<strong>Additional Requirements:</strong> ' . nl2br(esc_html($result['profile']['notes']));
402 }
403 echo( !empty($output) ? implode("<br>", $output) : '-' );
404 break;
405 }
406 case "commercial":
407 {
408 $output = array();
409 if ( isset($result['profile']['locations']) && is_array($result['profile']['locations']) && !empty($result['profile']['locations']) )
410 {
411 $output_locations = array();
412 foreach ( $result['profile']['locations'] as $profile_location )
413 {
414 $output_locations[] = $locations[$profile_location];
415 }
416 $output[] = '<strong>Locations:</strong> ' . esc_html(implode(", ", $output_locations));
417 }
418 if ( isset($result['profile']['notes']) && $result['profile']['notes'] != '' )
419 {
420 $output[] = '<strong>Additional Requirements:</strong> ' . nl2br(esc_html($result['profile']['notes']));
421 }
422 echo( !empty($output) ? implode("<br>", $output) : '-' );
423 break;
424 }
425 }
426 }
427 ?></td>
428 </tr>
429 <?php
430 }
431 }
432 else
433 {
434 ?>
435 <tr>
436 <td colspan="3" style="text-align:center"><?php echo esc_html(__( 'No matching applicants found', 'propertyhive' )); ?></td>
437 </tr>
438 <?php
439 }
440 ?>
441 </tbody>
442 </table>
443 </div>
444 <?php
445 }
446 ?>
447
448 </div>
449
450 </div>
451 <script>
452
453 var custom_departments = <?php echo json_encode(ph_get_custom_departments()); ?>;
454 function toggleDepartmentFields()
455 {
456 if (jQuery('#mainform').length > 0)
457 {
458 // There may be multiple forms on the page so treat each one individually
459 jQuery('#mainform').each(function()
460 {
461 var selectedDepartment = "residential-sales"; // TODO: Use default from settings
462
463 var selected = jQuery(this).find('[name=\'department\']');
464
465 jQuery(this).find('.sales-only').hide();
466 jQuery(this).find('.lettings-only').hide();
467 jQuery(this).find('.residential-only').hide();
468 jQuery(this).find('.commercial-only').hide();
469
470 if (selected.length > 0)
471 {
472 selectedDepartment = selected.val();
473
474 // controls won't always be display:block so we should get the
475 // first visible component (that isnt sales/lettings-only) and
476 // use that display
477 var display = 'block';
478
479 if ( selectedDepartment == 'residential-sales' || ( custom_departments[selectedDepartment] && custom_departments[selectedDepartment].based_on == 'residential-sales' ) )
480 {
481 jQuery(this).find('.sales-only').css('display', display);
482 jQuery(this).find('.residential-only').css('display', display);
483 }
484 else if ( selectedDepartment == 'residential-lettings' || ( custom_departments[selectedDepartment] && custom_departments[selectedDepartment].based_on == 'residential-lettings' ) )
485 {
486 jQuery(this).find('.lettings-only').css('display', display);
487 jQuery(this).find('.residential-only').css('display', display);
488 }
489 else if ( selectedDepartment == 'commercial' || ( custom_departments[selectedDepartment] && custom_departments[selectedDepartment].based_on == 'commercial' ) )
490 {
491 jQuery(this).find('.commercial-only').css('display', display);
492 }
493 }
494 });
495 }
496 }
497
498 jQuery( function(jQuery) {
499
500 // Multiselect
501 jQuery("#mainform select.multiselect").chosen();
502
503 toggleDepartmentFields();
504
505 jQuery('#mainform [name=\'department\']').change(function()
506 {
507 toggleDepartmentFields();
508 });
509
510 jQuery('#export_applicant_list_results_button').click(function(e)
511 {
512 e.preventDefault();
513 jQuery('input[name=\'export_applicant_list_results\']').val('1');
514 jQuery('#mainform').submit();
515
516 setTimeout(function() { jQuery('input[name=\'export_applicant_list_results\']').val(''); }, 1000);
517 });
518 });
519
520 jQuery(window).resize(function() {
521 toggleDepartmentFields();
522 });
523
524 </script>
525 <?php
526 }
527
528 public function generate_results()
529 {
530 $search_property_types = array();
531 if (
532 isset($_POST['department']) &&
533 (
534 $_POST['department'] == 'residential-sales' ||
535 $_POST['department'] == 'residential-lettings' ||
536 ph_get_custom_department_based_on($_POST['department']) == 'residential-sales' ||
537 ph_get_custom_department_based_on($_POST['department']) == 'residential-lettings'
538 )
539 )
540 {
541 if ( isset($_POST['property_types']) && is_array($_POST['property_types']) && !empty($_POST['property_types']) )
542 {
543 foreach ( $_POST['property_types'] as $property_type )
544 {
545 $search_property_types[] = (int)$property_type;
546
547 $args = array(
548 'hide_empty' => false,
549 'parent' => $property_type
550 );
551 $terms = get_terms( 'property_type', $args );
552
553 if ( !empty( $terms ) && !is_wp_error( $terms ) )
554 {
555 foreach ($terms as $term)
556 {
557 $search_property_types[] = $term->term_id;
558
559 $args = array(
560 'hide_empty' => false,
561 'parent' => $term->term_id
562 );
563 $subterms = get_terms( 'property_type', $args );
564
565 if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
566 {
567 foreach ($subterms as $term)
568 {
569 $search_property_types[] = $term->term_id;
570 }
571 }
572 }
573 }
574 }
575 }
576 $search_property_types = array_unique($search_property_types);
577 }
578
579 $search_locations = array();
580 if ( isset($_POST['locations']) && is_array($_POST['locations']) && !empty($_POST['locations']) )
581 {
582 foreach ( $_POST['locations'] as $location )
583 {
584 $search_locations[] = (int)$location;
585
586 $args = array(
587 'hide_empty' => false,
588 'parent' => $location
589 );
590 $terms = get_terms( 'location', $args );
591
592 if ( !empty( $terms ) && !is_wp_error( $terms ) )
593 {
594 foreach ($terms as $term)
595 {
596 $search_locations[] = $term->term_id;
597
598 $args = array(
599 'hide_empty' => false,
600 'parent' => $term->term_id
601 );
602 $subterms = get_terms( 'location', $args );
603
604 if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
605 {
606 foreach ($subterms as $term)
607 {
608 $search_locations[] = $term->term_id;
609 }
610 }
611 }
612 }
613 }
614 }
615 $search_locations = array_unique($search_locations);
616
617 $args = array(
618 'post_type' => 'contact',
619 'fields' => 'ids',
620 'nopaging' => true,
621 );
622
623 $args['meta_query'] = array();
624
625 $args['meta_query'][] = array(
626 'key' => '_contact_types',
627 'value' => 'applicant',
628 'compare' => 'LIKE'
629 );
630
631 $applicant_query = new WP_Query($args);
632
633 $results = array();
634
635 if ( $applicant_query->have_posts() )
636 {
637 $percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' );
638 $percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' );
639
640 while ( $applicant_query->have_posts() )
641 {
642 $applicant_query->the_post();
643
644 $num_applicant_profiles = get_post_meta( get_the_ID(), '_applicant_profiles', TRUE );
645 if ( $num_applicant_profiles == '' )
646 {
647 $num_applicant_profiles = 0;
648 }
649
650 for ( $i = 0; $i < $num_applicant_profiles; ++$i )
651 {
652 $profile = get_post_meta( get_the_ID(), '_applicant_profile_' . $i, TRUE );
653
654 $match = true;
655
656 if ( !isset($_POST['include_non_send_matching_properties']) )
657 {
658 if ( !isset($profile['send_matching_properties']) || ( isset($profile['send_matching_properties']) && $profile['send_matching_properties'] != 'yes' ) )
659 {
660 $match = false;
661 }
662 }
663
664 if ( isset($_POST['department']) )
665 {
666 if ( isset($profile['department']) && $profile['department'] != ph_clean($_POST['department']) )
667 {
668 $match = false;
669 }
670 }
671
672 if ( isset($_POST['department']) && ( $_POST['department'] == 'residential-sales' || ph_get_custom_department_based_on($_POST['department']) == 'residential-sales' ) )
673 {
674 if ( isset($_POST['maximum_price']) && ph_clean($_POST['maximum_price']) != '' )
675 {
676 $price = preg_replace("/[^0-9.]/", '', ph_clean($_POST['maximum_price']));
677
678 if ( $percentage_lower != '' && $percentage_higher != '' )
679 {
680 $match_price_range_lower = '';
681 if ( !isset($profile['match_price_range_lower_actual']) || ( isset($profile['match_price_range_lower_actual']) && $profile['match_price_range_lower_actual'] == '' ) )
682 {
683 if ( isset($profile['max_price_actual']) && $profile['max_price_actual'] != '' )
684 {
685 if ( $percentage_lower != '' )
686 {
687 $match_price_range_lower = $profile['max_price_actual'] - ( $profile['max_price_actual'] * ( $percentage_lower / 100 ) );
688 }
689 }
690 }
691 else
692 {
693 $match_price_range_lower = $profile['match_price_range_lower_actual'];
694 }
695
696 $match_price_range_higher = '';
697 if ( !isset($profile['match_price_range_higher_actual']) || ( isset($profile['match_price_range_higher_actual']) && $profile['match_price_range_higher_actual'] == '' ) )
698 {
699 if ( isset($profile['max_price_actual']) && $profile['max_price_actual'] != '' )
700 {
701 if ( $percentage_higher != '' )
702 {
703 $match_price_range_higher = $profile['max_price_actual'] + ( $profile['max_price_actual'] * ( $percentage_higher / 100 ) );
704 }
705 }
706 }
707 else
708 {
709 $match_price_range_higher = $profile['match_price_range_higher_actual'];
710 }
711
712 if (
713 !($match_price_range_lower == '' && $match_price_range_higher == '') && // Both bounds are not empty
714 !(
715 $price >= $match_price_range_lower &&
716 $price <= $match_price_range_higher
717 ) // Price is not within the bounds
718 ) {
719 $match = false; // Assuming you want to set match to false; adjust as needed
720 }
721 }
722 else
723 {
724 if (
725 isset($profile['max_price_actual']) &&
726 $profile['max_price_actual'] !== '' && // Checks if max_price_actual is not an empty string
727 $price > $profile['max_price_actual'] // Checks if price is greater than max_price_actual
728 )
729 {
730 $match = false;
731 }
732 }
733 }
734 }
735 if ( isset($_POST['department']) && ( $_POST['department'] == 'residential-lettings' || ph_get_custom_department_based_on($_POST['department']) == 'residential-lettings' ) )
736 {
737 if ( isset($_POST['maximum_rent']) && ph_clean($_POST['maximum_rent']) != '' )
738 {
739 $price = preg_replace("/[^0-9.]/", '', ph_clean($_POST['maximum_rent']));
740
741 if ( isset($profile['max_price_actual']) && $profile['max_price_actual'] != '' && $profile['max_price_actual'] != 0 && $profile['max_price_actual'] < $price )
742 {
743 $match = false;
744 }
745 }
746 }
747 if (
748 isset($_POST['department']) &&
749 (
750 $_POST['department'] == 'residential-sales' ||
751 $_POST['department'] == 'residential-lettings' ||
752 ph_get_custom_department_based_on($_POST['department']) == 'residential-sales' ||
753 ph_get_custom_department_based_on($_POST['department']) == 'residential-lettings'
754 )
755 )
756 {
757 if ( isset($_POST['minimum_bedrooms']) && ph_clean($_POST['minimum_bedrooms']) != '' )
758 {
759 $beds = preg_replace("/[^0-9.]/", '', ph_clean($_POST['minimum_bedrooms']));
760
761 if ( isset($profile['min_beds']) && $profile['min_beds'] != '' && $profile['min_beds'] != 0 && $profile['min_beds'] > $beds )
762 {
763 $match = false;
764 }
765 }
766
767 // Property Types
768 if ( isset($_POST['property_types']) && is_array($_POST['property_types']) && !empty($_POST['property_types']) )
769 {
770 $found_type = false;
771 foreach ( $search_property_types as $search_property_type )
772 {
773 if ( isset($profile['property_types']) && is_array($profile['property_types']) && in_array($search_property_type, $profile['property_types']) )
774 {
775 $found_type = true;
776 }
777 }
778
779 if ( !$found_type )
780 {
781 $match = false;
782 }
783 }
784 }
785
786 if ( isset($_POST['locations']) && is_array($_POST['locations']) && !empty($_POST['locations']) )
787 {
788 $found_type = false;
789 foreach ( $search_locations as $search_location )
790 {
791 if ( isset($profile['locations']) && is_array($profile['locations']) && in_array($search_location, $profile['locations']) )
792 {
793 $found_type = true;
794 }
795 }
796
797 if ( !$found_type )
798 {
799 $match = false;
800 }
801 }
802
803 $match = apply_filters( 'propertyhive_applicant_list_check', $match, get_the_ID(), $profile );
804
805 if ( $match )
806 {
807 $contact = new PH_Contact( get_the_ID() );
808
809 $results[] = array(
810 'contact_id' => get_the_ID(),
811 'applicant_profile_id' => $i,
812 'name' => get_the_title(),
813 'edit_link' => get_edit_post_link(get_the_ID()),
814 'telephone_number' => $contact->_telephone_number,
815 'email_address' => $contact->_email_address,
816 'address' => $contact->get_formatted_full_address(),
817 'profile' => $profile
818 );
819 }
820 }
821 }
822 }
823
824 wp_reset_postdata();
825
826 return $results;
827 }
828
829 private function array_2_csv($results)
830 {
831 $locations = array();
832 $args = array(
833 'hide_empty' => false,
834 'parent' => 0
835 );
836 $terms = get_terms( 'location', $args );
837
838 if ( !empty( $terms ) && !is_wp_error( $terms ) )
839 {
840 foreach ($terms as $term)
841 {
842 $locations[$term->term_id] = esc_html( $term->name );
843
844 $args = array(
845 'hide_empty' => false,
846 'parent' => $term->term_id
847 );
848 $subterms = get_terms( 'location', $args );
849
850 if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
851 {
852 foreach ($subterms as $term)
853 {
854 $locations[$term->term_id] = esc_html( $term->name );
855
856 $args = array(
857 'hide_empty' => false,
858 'parent' => $term->term_id
859 );
860 $subsubterms = get_terms( 'location', $args );
861
862 if ( !empty( $subsubterms ) && !is_wp_error( $subsubterms ) )
863 {
864 foreach ($subsubterms as $term)
865 {
866 $locations[$term->term_id] = esc_html( $term->name );
867 }
868 }
869 }
870 }
871 }
872 }
873
874 $property_types = array();
875 $args = array(
876 'hide_empty' => false,
877 'parent' => 0
878 );
879 $terms = get_terms( 'property_type', $args );
880
881 if ( !empty( $terms ) && !is_wp_error( $terms ) )
882 {
883 foreach ($terms as $term)
884 {
885 $property_types[$term->term_id] = esc_html( $term->name );
886
887 $args = array(
888 'hide_empty' => false,
889 'parent' => $term->term_id
890 );
891 $subterms = get_terms( 'property_type', $args );
892
893 if ( !empty( $subterms ) && !is_wp_error( $subterms ) )
894 {
895 foreach ($subterms as $term)
896 {
897 $property_types[$term->term_id] = esc_html( $term->name );
898 }
899 }
900 }
901 }
902
903 $percentage_lower = get_option( 'propertyhive_applicant_match_price_range_percentage_lower', '' );
904 $percentage_higher = get_option( 'propertyhive_applicant_match_price_range_percentage_higher', '' );
905
906 ob_start();
907
908 $df = fopen("php://output", 'w');
909
910 $columns = array(
911 'name' => __( 'Name', 'propertyhive' ),
912 'email_address' => __( 'Email Address', 'propertyhive' ),
913 'telephone_number' => __( 'Telephone Number', 'propertyhive' ),
914 'address' => __( 'Address', 'propertyhive' ),
915 'department' => __( 'Department', 'propertyhive' ),
916 );
917
918 if ( isset($_POST['department']) )
919 {
920 $department = ph_clean($_POST['department']);
921 if ( ph_get_custom_department_based_on($department) !== FALSE )
922 {
923 $department = ph_get_custom_department_based_on($department);
924 }
925 }
926 if ( isset($department) )
927 {
928 switch ( $department )
929 {
930 case "residential-sales":
931 {
932 $columns['maximum_price'] = __( 'Maximum Price', 'propertyhive' );
933 break;
934 }
935 case "residential-lettings":
936 {
937 $columns['maximum_rent'] = __( 'Maximum Rent (PCM)', 'propertyhive' );
938 break;
939 }
940 }
941 if ( $department == 'residential-sales' || $department == 'residential-lettings' )
942 {
943 $columns['currency'] = __( 'Currency', 'propertyhive' );
944 $columns['minimum_bedrooms'] = __( 'Minimum Bedrooms', 'propertyhive' );
945 $columns['property_types'] = __( 'Property Types', 'propertyhive' );
946 }
947 }
948
949 $columns['locations'] = __( 'Locations', 'propertyhive' );
950 $columns['additional_requirements'] = __( 'Additional Requirements', 'propertyhive' );
951
952 $columns = apply_filters( 'propertyhive_export_applicant_list_columns', $columns, $_POST );
953
954 fputcsv($df, $columns);
955
956 foreach ($results as $result)
957 {
958 $columns = array(
959 'name' => $result['name'],
960 'email_address' => $result['email_address'],
961 'telephone_number' => $result['telephone_number'],
962 'address' => $result['address'],
963 'department' => ( isset($result['profile']['department']) ? __( ucwords(str_replace("-", " ", $result['profile']['department'])), 'propertyhive' ) : '-' ),
964 );
965
966 if ( isset($department) )
967 {
968 switch ( $department )
969 {
970 case "residential-sales":
971 {
972 $columns['maximum_price'] = ( isset($result['profile']['max_price']) ? $result['profile']['max_price'] : '' );
973
974 if ( !empty($columns['maximum_price']) )
975 {
976 if ( $percentage_lower != '' && $percentage_higher != '' )
977 {
978 $match_price_range_lower = '';
979 if ( !isset($result['profile']['match_price_range_lower_actual']) || ( isset($result['profile']['match_price_range_lower_actual']) && $result['profile']['match_price_range_lower_actual'] == '' ) )
980 {
981 if ( isset($result['profile']['max_price']) && $result['profile']['max_price'] != '' )
982 {
983 $match_price_range_lower = $result['profile']['max_price'] - ( $result['profile']['max_price'] * ( $percentage_lower / 100 ) );
984 }
985 }
986 else
987 {
988 $match_price_range_lower = $result['profile']['match_price_range_lower'];
989 }
990
991 $match_price_range_higher = '';
992 if ( !isset($result['profile']['match_price_range_higher_actual']) || ( isset($result['profile']['match_price_range_higher_actual']) && $result['profile']['match_price_range_higher_actual'] == '' ) )
993 {
994 if ( isset($result['profile']['max_price']) && $result['profile']['max_price'] != '' )
995 {
996 $match_price_range_higher = $result['profile']['max_price'] + ( $result['profile']['max_price'] * ( $percentage_higher / 100 ) );
997 }
998 }
999 else
1000 {
1001 $match_price_range_higher = $result['profile']['match_price_range_higher'];
1002 }
1003
1004 if (
1005 $match_price_range_lower != '' && $match_price_range_higher != ''
1006 )
1007 {
1008 $columns['maximum_price_range'] = $match_price_range_lower . ' - ' . $match_price_range_higher;
1009 }
1010 }
1011 }
1012
1013 $columns['currency'] = ( isset($result['profile']['currency']) ? $result['profile']['currency'] : 'GBP' );
1014
1015 break;
1016 }
1017 case "residential-lettings":
1018 {
1019 $columns['maximum_rent'] = ( isset($result['profile']['max_rent']) ? $result['profile']['max_rent'] : '' );
1020 $columns['currency'] = ( isset($result['profile']['currency']) ? $result['profile']['currency'] : 'GBP' );
1021 break;
1022 }
1023 }
1024 if ( $department == 'residential-sales' || $department == 'residential-lettings' )
1025 {
1026 $columns['minimum_bedrooms'] = ( isset($result['profile']['min_beds']) ? $result['profile']['min_beds'] : '' );
1027
1028 $output_types = array();
1029 if ( isset($result['profile']['property_types']) && is_array($result['profile']['property_types']) && !empty($result['profile']['property_types']) )
1030 {
1031 foreach ( $result['profile']['property_types'] as $profile_type )
1032 {
1033 $output_types[] = $property_types[$profile_type];
1034 }
1035 }
1036 $columns['property_types'] = implode(", ", $output_types);
1037 }
1038 }
1039
1040 $output_locations = array();
1041 if ( isset($result['profile']['locations']) && is_array($result['profile']['locations']) && !empty($result['profile']['locations']) )
1042 {
1043 foreach ( $result['profile']['locations'] as $profile_location )
1044 {
1045 $output_locations[] = $locations[$profile_location];
1046 }
1047 }
1048 $columns['locations'] = implode(", ", $output_locations);
1049
1050 $columns['additional_requirements'] = isset($result['profile']['notes']) ? $result['profile']['notes'] : '';
1051
1052 $columns = apply_filters( 'propertyhive_export_applicant_list_row_data', $columns, $_POST, $result['contact_id'], $result['applicant_profile_id'] );
1053
1054 fputcsv($df, $columns);
1055 }
1056 fclose($df);
1057
1058 return ob_get_clean();
1059 }
1060
1061 public function export()
1062 {
1063 if ( !isset( $_POST['ph_applicant_export_nonce'] ) || ! check_admin_referer( 'ph_applicant_export', 'ph_applicant_export_nonce', false ) )
1064 {
1065 wp_die( esc_html__( 'Invalid request (nonce failure)', 'propertyhive' ), 403 );
1066 }
1067
1068 if ( !current_user_can( 'manage_propertyhive' ) )
1069 {
1070 wp_die( esc_html__( 'Insufficient permissions', 'propertyhive' ), 403 );
1071 }
1072
1073 $filename = 'applicant-list-' . date("YmdHis") . '.csv';
1074
1075 // disable caching
1076 $now = gmdate("D, d M Y H:i:s");
1077 header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
1078 header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
1079 header("Last-Modified: {$now} GMT");
1080
1081 // force download
1082 header("Content-Type: application/force-download");
1083 header("Content-Type: application/octet-stream");
1084 header("Content-Type: application/download");
1085
1086 // disposition / encoding on response body
1087 header("Content-Disposition: attachment;filename={$filename}");
1088 header("Content-Transfer-Encoding: binary");
1089
1090 $results = $this->generate_results();
1091
1092 echo $this->array_2_csv($results);
1093
1094 die();
1095 }
1096 }
1097
1098 endif;