| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
/** |
| 6 |
* Viewing |
| 7 |
* |
| 8 |
* The Property Hive viewing class handles viewing data. |
| 9 |
* |
| 10 |
* @class PH_Viewing |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Viewing { |
| 17 |
|
| 18 |
/** @public int Viewing (post) ID */ |
| 19 |
public $id; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the viewing if ID is passed, otherwise the viewing is new and empty. |
| 23 |
* |
| 24 |
* @access public |
| 25 |
* @param string $id (default: '') |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function __construct( $id = '' ) { |
| 29 |
if ( $id > 0 ) { |
| 30 |
$this->get_viewing( $id ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Gets a viewing from the database. |
| 36 |
* |
| 37 |
* @access public |
| 38 |
* @param int $id (default: 0) |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function get_viewing( $id = 0 ) { |
| 42 |
if ( ! $id ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
if ( $result = get_post( $id ) ) { |
| 46 |
$this->populate( $result ); |
| 47 |
return true; |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* __isset function. |
| 54 |
* |
| 55 |
* @access public |
| 56 |
* @param mixed $key |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function __isset( $key ) { |
| 60 |
if ( ! $this->id ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
return metadata_exists( 'post', $this->id, '_' . $key ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* __get function. |
| 68 |
* |
| 69 |
* @access public |
| 70 |
* @param mixed $key |
| 71 |
* @return mixed |
| 72 |
*/ |
| 73 |
public function __get( $key ) { |
| 74 |
// Get values or default if not set |
| 75 |
$value = get_post_meta( $this->id, $key, true ); |
| 76 |
if ($value == '') |
| 77 |
{ |
| 78 |
$value = get_post_meta( $this->id, '_' . $key, true ); |
| 79 |
} |
| 80 |
return $value; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Populates a viewing from the loaded post data. |
| 85 |
* |
| 86 |
* @access public |
| 87 |
* @param mixed $result |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function populate( $result ) { |
| 91 |
// Standard post data |
| 92 |
$this->id = $result->ID; |
| 93 |
$this->post_title = $result->post_title; |
| 94 |
$this->post_status = $result->post_status; |
| 95 |
} |
| 96 |
|
| 97 |
public function get_related_viewings() |
| 98 |
{ |
| 99 |
$related_viewings = array( |
| 100 |
'previous' => array(), |
| 101 |
'next' => array(), |
| 102 |
'all' => array() |
| 103 |
); |
| 104 |
|
| 105 |
$applicant_contact_ids = get_post_meta($this->id, '_applicant_contact_id'); |
| 106 |
$applicant_contact_ids = !empty($applicant_contact_ids) ? ( is_array($applicant_contact_ids) ? $applicant_contact_ids : array($applicant_contact_ids) ) : array(); |
| 107 |
|
| 108 |
$property_id = get_post_meta($this->id, '_property_id', TRUE); |
| 109 |
$property_id = !empty($property_id) ? $property_id : ''; |
| 110 |
|
| 111 |
$primary_viewing_start_date_time = get_post_meta($this->id, '_start_date_time', TRUE); |
| 112 |
|
| 113 |
$meta_query = array( |
| 114 |
array( |
| 115 |
'key' => '_property_id', |
| 116 |
'value' => (int)$property_id, |
| 117 |
), |
| 118 |
array( |
| 119 |
'key' => '_status', |
| 120 |
'value' => array('cancelled', 'no_show'), |
| 121 |
'compare' => 'NOT IN', |
| 122 |
), |
| 123 |
); |
| 124 |
foreach ( $applicant_contact_ids as $applicant_contact_id ) |
| 125 |
{ |
| 126 |
$meta_query[] = array( |
| 127 |
'key' => '_applicant_contact_id', |
| 128 |
'value' => (int)$applicant_contact_id |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
$args = array( |
| 133 |
'fields' => 'ids', |
| 134 |
'post_type' => 'viewing', |
| 135 |
'nopaging' => true, |
| 136 |
'post_status' => 'publish', |
| 137 |
'meta_query' => $meta_query, |
| 138 |
'post__not_in' => array($this->id), |
| 139 |
'orderby' => 'none' |
| 140 |
); |
| 141 |
|
| 142 |
$viewings_query = new WP_Query( $args ); |
| 143 |
|
| 144 |
if ( $viewings_query->have_posts() ) |
| 145 |
{ |
| 146 |
while ( $viewings_query->have_posts() ) |
| 147 |
{ |
| 148 |
$viewings_query->the_post(); |
| 149 |
|
| 150 |
$viewing_start_date_time = get_post_meta(get_the_ID(), '_start_date_time', TRUE); |
| 151 |
|
| 152 |
if ( strtotime($viewing_start_date_time) <= strtotime($primary_viewing_start_date_time) ) |
| 153 |
{ |
| 154 |
$related_viewings['previous'][] = get_the_ID(); |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
$related_viewings['next'][] = get_the_ID(); |
| 159 |
} |
| 160 |
|
| 161 |
$related_viewings['all'][] = get_the_ID(); |
| 162 |
} |
| 163 |
} |
| 164 |
wp_reset_postdata(); |
| 165 |
|
| 166 |
return $related_viewings; |
| 167 |
} |
| 168 |
|
| 169 |
public function get_applicant_ids() |
| 170 |
{ |
| 171 |
$applicant_contact_ids = get_post_meta( $this->id, '_applicant_contact_id' ); |
| 172 |
if ( $applicant_contact_ids == '' ) |
| 173 |
{ |
| 174 |
$applicant_contact_ids = array(); |
| 175 |
} |
| 176 |
if ( !is_array($applicant_contact_ids) && $applicant_contact_ids != '' && $applicant_contact_ids != 0 ) |
| 177 |
{ |
| 178 |
$applicant_contact_ids = array($applicant_contact_ids); |
| 179 |
} |
| 180 |
|
| 181 |
return $applicant_contact_ids; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get the applicants |
| 186 |
* |
| 187 |
* @access public |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public function get_applicants( $add_hyperlinks = false, $additional_contact_details = false ) |
| 191 |
{ |
| 192 |
$applicant_contact_ids = get_post_meta($this->id, '_applicant_contact_id'); |
| 193 |
if (!empty($applicant_contact_ids)) |
| 194 |
{ |
| 195 |
$applicant_contacts = array(); |
| 196 |
foreach ($applicant_contact_ids as $applicant_contact_id) |
| 197 |
{ |
| 198 |
$applicant_name = get_the_title($applicant_contact_id); |
| 199 |
if ( $add_hyperlinks ) |
| 200 |
{ |
| 201 |
$edit_link = get_edit_post_link( $applicant_contact_id ); |
| 202 |
$applicant_name = '<a href="' . esc_url($edit_link) . '" target="' . esc_attr(apply_filters('propertyhive_subgrid_link_target', '')) . '">' . $applicant_name . '</a>'; |
| 203 |
} |
| 204 |
if ( $additional_contact_details ) |
| 205 |
{ |
| 206 |
$contact_details = array(); |
| 207 |
$telephone_number = get_post_meta( $applicant_contact_id, '_telephone_number', true ); |
| 208 |
if( !empty($telephone_number) ) |
| 209 |
{ |
| 210 |
$contact_details[] = 'T: ' . $telephone_number; |
| 211 |
} |
| 212 |
|
| 213 |
$email_address = get_post_meta( $applicant_contact_id, '_email_address', true ); |
| 214 |
if( !empty($email_address) ) |
| 215 |
{ |
| 216 |
$contact_details[] = 'E: ' . $email_address; |
| 217 |
} |
| 218 |
|
| 219 |
$contact_details = apply_filters( 'propertyhive_viewing_applicant_contact_details', $contact_details, $applicant_contact_id ); |
| 220 |
|
| 221 |
if ( !empty($contact_details) ) |
| 222 |
{ |
| 223 |
$applicant_name .= '<div class="row-actions visible">' . implode( "<br>", $contact_details ) . '</div>'; |
| 224 |
} |
| 225 |
} |
| 226 |
$applicant_contacts[] = $applicant_name; |
| 227 |
} |
| 228 |
return implode('<br>', $applicant_contacts); |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
return'-'; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Get the attending negotiator(s) |
| 238 |
* |
| 239 |
* @access public |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public function get_negotiators() |
| 243 |
{ |
| 244 |
$negotiator_ids = get_post_meta($this->id, '_negotiator_id'); |
| 245 |
if ( !empty($negotiator_ids) ) |
| 246 |
{ |
| 247 |
$negotiators = array(); |
| 248 |
foreach ($negotiator_ids as $negotiator_id) |
| 249 |
{ |
| 250 |
$userdata = get_userdata( $negotiator_id ); |
| 251 |
if ( $userdata !== FALSE ) |
| 252 |
{ |
| 253 |
$negotiators[] = $userdata->display_name; |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
$negotiators[] = '<em>' . __( 'Unknown user', 'propertyhive' ) . '</em>'; |
| 258 |
} |
| 259 |
} |
| 260 |
return implode(', ', $negotiators); |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
return 'Unattended'; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Get the viewing status |
| 270 |
* |
| 271 |
* @access public |
| 272 |
* @return string |
| 273 |
*/ |
| 274 |
public function get_status() |
| 275 |
{ |
| 276 |
$status = $this->_status; |
| 277 |
$status_items = array( __( ucwords(str_replace("_", " ", $status)), 'propertyhive' ) ); |
| 278 |
|
| 279 |
if ( $status == 'pending' ) |
| 280 |
{ |
| 281 |
// confirmation status |
| 282 |
if ( $this->_all_confirmed == 'yes' ) |
| 283 |
{ |
| 284 |
$status_items[] = __( 'All Parties Confirmed', 'propertyhive' ); |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
$status_items[] = __( 'Awaiting Confirmation', 'propertyhive' ); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if ( $status == 'carried_out' ) |
| 293 |
{ |
| 294 |
$feedback_status = $this->_feedback_status; |
| 295 |
switch ( $feedback_status ) |
| 296 |
{ |
| 297 |
case "interested": { $status_items[] = __( 'Applicant Interested', 'propertyhive' ); break; } |
| 298 |
case "not_interested": { $status_items[] = __( 'Applicant Not Interested', 'propertyhive' ); break; } |
| 299 |
case "not_required": { $status_items[] = __( 'Feedback Not Required', 'propertyhive' ); break; } |
| 300 |
default: { $status_items[] = __( 'Awaiting Feedback', 'propertyhive' ); } |
| 301 |
} |
| 302 |
|
| 303 |
if ( $feedback_status == 'interested' || $feedback_status == 'not_interested' ) |
| 304 |
{ |
| 305 |
$status_items[] = ($this->_feedback_passed_on == 'yes') ? __( 'Feedback Passed On', 'propertyhive' ) : __( 'Feedback Not Passed On', 'propertyhive' ); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
// Add text if this a second, third etc viewing |
| 310 |
$related_viewings = get_post_meta( $this->id, '_related_viewings', TRUE ); |
| 311 |
if ( isset($related_viewings['previous']) && count($related_viewings['previous']) > 0 ) |
| 312 |
{ |
| 313 |
$status_items[] = ph_ordinal_suffix(count($related_viewings['previous'])+1) . ' ' . __( 'Viewing', 'propertyhive' ); |
| 314 |
} |
| 315 |
|
| 316 |
return implode('<br>', $status_items); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Get the full address of the property attached to the viewing |
| 321 |
* |
| 322 |
* @access public |
| 323 |
* @return string |
| 324 |
*/ |
| 325 |
public function get_property_address() |
| 326 |
{ |
| 327 |
$property_id = (int)$this->_property_id; |
| 328 |
|
| 329 |
if ( !empty($property_id) ) |
| 330 |
{ |
| 331 |
$property = new PH_Property( $property_id ); |
| 332 |
return '<a href="' . get_edit_post_link( $property_id, '' ) . '" target="' . apply_filters('propertyhive_subgrid_link_target', '') . '">' . $property->get_formatted_full_address() . '</a>'; |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
return '-'; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|