| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
/** |
| 6 |
* Tenancy |
| 7 |
* |
| 8 |
* The Property Hive tenancy class handles tenancy data. |
| 9 |
* |
| 10 |
* @class PH_Tenancy |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Tenancy { |
| 17 |
|
| 18 |
/** @public int Tenancy (post) ID */ |
| 19 |
public $id; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the tenancy if ID is passed, otherwise the tenancy 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_tenancy( $id ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Gets a tenancy from the database. |
| 36 |
* |
| 37 |
* @access public |
| 38 |
* @param int $id (default: 0) |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function get_tenancy( $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 tenancy 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 |
/** |
| 98 |
* Get the formatted tenancy rent |
| 99 |
* |
| 100 |
* @access public |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function get_formatted_rent( ) { |
| 104 |
|
| 105 |
$ph_countries = new PH_Countries(); |
| 106 |
|
| 107 |
if ($this->_currency != '') |
| 108 |
{ |
| 109 |
$currency = $ph_countries->get_currency( $this->_currency ); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
$currency = $ph_countries->get_currency( 'GBP' ); |
| 114 |
} |
| 115 |
|
| 116 |
$prefix = ( ($currency['currency_prefix']) ? $currency['currency_symbol'] : '' ); |
| 117 |
$suffix = ( (!$currency['currency_prefix']) ? $currency['currency_symbol'] : '' ); |
| 118 |
|
| 119 |
$amount = $this->_rent; |
| 120 |
|
| 121 |
return ( ( $amount != '' ) ? $prefix . ph_display_price_field($amount) : '-' ) . $suffix . ' ' . __( $this->_rent_frequency, 'propertyhive' ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get the tenancy status |
| 127 |
* |
| 128 |
* @access public |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function get_status() |
| 132 |
{ |
| 133 |
if ( $this->_start_date && strtotime( $this->_start_date ) > time() ) |
| 134 |
{ |
| 135 |
return __( 'Pending', 'propertyhive' ); |
| 136 |
} |
| 137 |
elseif ( |
| 138 |
$this->_start_date && strtotime( $this->_start_date ) <= time() && |
| 139 |
( time() <= strtotime( $this->_end_date ) || $this->_end_date == '' ) |
| 140 |
) |
| 141 |
{ |
| 142 |
return __( 'Current', 'propertyhive' ); |
| 143 |
} |
| 144 |
elseif ( $this->_end_date && strtotime( $this->_end_date ) < time() ) |
| 145 |
{ |
| 146 |
return __( 'Finished', 'propertyhive' ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get a list of tenants on the property |
| 152 |
* |
| 153 |
* @access public |
| 154 |
* @param bool $add hyperlinks |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
public function get_tenants( $add_hyperlinks = false, $additional_contact_details = false ) |
| 158 |
{ |
| 159 |
$applicant_contact_ids = get_post_meta( $this->id, '_applicant_contact_id' ); |
| 160 |
if ( is_array($applicant_contact_ids) && !empty($applicant_contact_ids) ) |
| 161 |
{ |
| 162 |
$applicants = array(); |
| 163 |
foreach ( $applicant_contact_ids as $applicant_contact_id ) |
| 164 |
{ |
| 165 |
$applicant_name = get_the_title($applicant_contact_id); |
| 166 |
if ( $add_hyperlinks ) |
| 167 |
{ |
| 168 |
$edit_link = get_edit_post_link( $applicant_contact_id ); |
| 169 |
$applicant_name = '<a href="' . esc_url( $edit_link ) . '">' . $applicant_name . '</a>'; |
| 170 |
} |
| 171 |
if ( $additional_contact_details ) |
| 172 |
{ |
| 173 |
$contact_details = array(); |
| 174 |
$telephone_number = get_post_meta( $applicant_contact_id, '_telephone_number', true ); |
| 175 |
if( !empty($telephone_number) ) |
| 176 |
{ |
| 177 |
$contact_details[] = 'T: ' . $telephone_number; |
| 178 |
} |
| 179 |
|
| 180 |
$email_address = get_post_meta( $applicant_contact_id, '_email_address', true ); |
| 181 |
if( !empty($email_address) ) |
| 182 |
{ |
| 183 |
$contact_details[] = 'E: ' . $email_address; |
| 184 |
} |
| 185 |
|
| 186 |
if ( !empty($contact_details) ) |
| 187 |
{ |
| 188 |
$applicant_name .= '<div class="row-actions visible">' . implode( "<br>", $contact_details ) . '</div>'; |
| 189 |
} |
| 190 |
} |
| 191 |
$applicants[] = $applicant_name; |
| 192 |
} |
| 193 |
return implode("<br>", $applicants); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
return '-'; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|