PluginProbe
Property Hive / trunk
Property Hive vtrunk
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 1.4.62 1.4.63 All 259 releases
propertyhive / includes / class-ph-offer.php

class-ph-offer.php in Property Hive trunk, at includes/class-ph-offer.php

276 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 /**
6 * Offer
7 *
8 * The Property Hive offer class handles offer data.
9 *
10 * @class PH_Offer
11 * @version 1.0.0
12 * @package PropertyHive/Classes
13 * @category Class
14 * @author PropertyHive
15 */
16 class PH_Offer {
17
18 /** @public int Offer (post) ID */
19 public $id;
20
21 /**
22 * Get the offer if ID is passed, otherwise the offer 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_offer( $id );
31 }
32 }
33
34 /**
35 * Gets a offer from the database.
36 *
37 * @access public
38 * @param int $id (default: 0)
39 * @return bool
40 */
41 public function get_offer( $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 offer 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 offer amount
99 *
100 * @access public
101 * @return string
102 */
103 public function get_formatted_amount( ) {
104
105 $amount = $this->_amount;
106 $prefix = '&pound;';
107 return ( ( $amount != '' ) ? $prefix . ph_display_price_field($amount) : '-' );
108
109 }
110
111 /**
112 * Get a list of applicants on the offer
113 *
114 * @access public
115 * @param bool $add_hyperlinks
116 * @param bool $additional_contact_details
117 * @param bool $contact_details_visible
118 * @return array
119 */
120 public function get_applicants( $add_hyperlinks = false, $additional_contact_details = false, $contact_details_visible = true )
121 {
122 $applicant_contact_ids = get_post_meta( $this->id, '_applicant_contact_id' );
123 if ( is_array($applicant_contact_ids) && !empty($applicant_contact_ids) )
124 {
125 $applicants = array();
126 foreach ( $applicant_contact_ids as $applicant_contact_id )
127 {
128 $applicant_name = get_the_title($applicant_contact_id);
129 if ( $add_hyperlinks )
130 {
131 $edit_link = get_edit_post_link( $applicant_contact_id );
132 $applicant_name = '<a href="' . esc_url( $edit_link ) . '">' . $applicant_name . '</a>';
133 }
134 if ( $additional_contact_details )
135 {
136 $contact_details = array();
137 $telephone_number = get_post_meta( $applicant_contact_id, '_telephone_number', true );
138 if( !empty($telephone_number) )
139 {
140 $contact_details[] = 'T: ' . $telephone_number;
141 }
142
143 $email_address = get_post_meta( $applicant_contact_id, '_email_address', true );
144 if( !empty($email_address) )
145 {
146 $contact_details[] = 'E: ' . $email_address;
147 }
148
149 $contact_details = apply_filters( 'propertyhive_offer_applicant_contact_details', $contact_details, $applicant_contact_id );
150
151 if ( !empty($contact_details) )
152 {
153 $applicant_name .= '<div class="row-actions' . ($contact_details_visible ? ' visible' : '') . '">' . implode( "<br>", $contact_details ) . '</div>';
154 }
155 }
156 $applicants[] = $applicant_name;
157 }
158 return implode("<br>", $applicants);
159 }
160 else
161 {
162 return '-';
163 }
164 }
165
166 public function get_applicant_ids()
167 {
168 $applicant_contact_ids = get_post_meta( $this->id, '_applicant_contact_id' );
169 if ( $applicant_contact_ids == '' )
170 {
171 $applicant_contact_ids = array();
172 }
173 if ( !is_array($applicant_contact_ids) && $applicant_contact_ids != '' && $applicant_contact_ids != 0 )
174 {
175 $applicant_contact_ids = array($applicant_contact_ids);
176 }
177
178 return $applicant_contact_ids;
179 }
180
181 /**
182 * Get the full address of the property attached to the offer
183 *
184 * @access public
185 * @return string
186 */
187 public function get_property_address()
188 {
189 $property_id = (int)$this->_property_id;
190
191 if ( !empty($property_id) )
192 {
193 $property = new PH_Property( $property_id );
194 return '<a href="' . get_edit_post_link( $property_id, '' ) . '" target="' . apply_filters('propertyhive_subgrid_link_target', '') . '">' . $property->get_formatted_full_address() . '</a>';
195 }
196 else
197 {
198 return '-';
199 }
200 }
201
202 /**
203 * Get the contact details of the owner(s) of the property attached to the offer
204 *
205 * @access public
206 * @return string
207 */
208 public function get_property_owners()
209 {
210 $property_owners = '-';
211
212 $property_id = (int)$this->_property_id;
213
214 if ( !empty($property_id) )
215 {
216 $property = new PH_Property( $property_id );
217 $owner_contact_ids = $property->_owner_contact_id;
218 if (
219 ( !is_array($owner_contact_ids) && $owner_contact_ids != '' && $owner_contact_ids != 0 )
220 ||
221 ( is_array($owner_contact_ids) && !empty($owner_contact_ids) )
222 )
223 {
224 $property_owners = '';
225
226 if ( !is_array($owner_contact_ids) )
227 {
228 $owner_contact_ids = array($owner_contact_ids);
229 }
230
231 foreach ( $owner_contact_ids as $owner_contact_id )
232 {
233 $property_owners .= $this->formatted_contact_meta_box_data($owner_contact_id, false);
234 }
235 }
236 }
237
238 return $property_owners;
239 }
240
241 /**
242 * Format contact data for display in a meta box list
243 */
244 private function formatted_contact_meta_box_data( $contact_post_id, $add_edit_link = true )
245 {
246 if ( $add_edit_link )
247 {
248 $contact_text = '<a href="' . get_edit_post_link( $contact_post_id, '' ) . '" target="' . apply_filters('propertyhive_subgrid_link_target', '') . '">' . get_the_title($contact_post_id) . '</a>';
249 }
250 else
251 {
252 $contact_text = get_the_title($contact_post_id);
253 }
254
255 $telephone_number = get_post_meta( $contact_post_id, '_telephone_number', true );
256 if( !empty($telephone_number) )
257 {
258 $contact_details = 'T: ' . $telephone_number;
259 }
260
261 $email_address = get_post_meta( $contact_post_id, '_email_address', true );
262 if( !empty($email_address) )
263 {
264 $contact_details .= ( $contact_details != '' ) ? '<br>' : '';
265 $contact_details .= 'E: ' . $email_address;
266 }
267
268 if ( $contact_details != '' )
269 {
270 $contact_text .= '<div class="row-actions visible">' . $contact_details . '</div>';
271 }
272
273 return $contact_text;
274 }
275 }
276