forked from SimplyRETS/simplyretswp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimply-rets-utils.php
More file actions
438 lines (335 loc) · 12.6 KB
/
simply-rets-utils.php
File metadata and controls
438 lines (335 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/*
*
* simply-rets-utils.php - Copyright (C) 2014-2015 SimplyRETS
* This file provides general utilities for the SimplyRETS plugin.
*
*/
/* Code starts here */
class SrUtils {
public static function isSingleVendor() {
$vendors = get_option('sr_adv_search_meta_vendors', array());
if(count($vendors) > 1) {
return false;
}
return true;
}
public static function srShowListingMeta() {
if( get_option('sr_show_listingmeta') ) {
$show_listing_meta = false;
} else {
$show_listing_meta = true;
}
return $show_listing_meta;
}
/**
* The naming for the database option is backwards.
* If it's 'checked', we _don't_ show data.
*/
public static function showAgentContact() {
if( get_option('sr_show_agent_contact') ) {
$show = false;
} else {
$show = true;
}
return $show;
}
/**
* Builds a link to a listings' details page. Used in search results.
*/
public static function buildDetailsLink($listing, $params = array()) {
$permalink_struct = get_option('permalink_structure', '');
$custom_permalink_struct = get_option('sr_permalink_structure', '');
// Are pretty permalinks enabled?
$prettify = true;
$prettify = $custom_permalink_struct === "pretty" ? true : $prettify;
$prettify = $custom_permalink_struct === "pretty_extra" ? true : $prettify;
$prettify = $custom_permalink_struct === "query_string" ? false : $prettify;
$prettify = $permalink_struct === "" ? false : $prettify;
// Build a query string
$_query = http_build_query($params);
$query = !empty($_query) ? $_query : "";
// Base of the URL we're building
$url = get_home_url();
// Listing details
$listing_id = $listing->mlsId;
$listing_city = $listing->address->city;
$listing_state = $listing->address->state;
$listing_zip = $listing->address->postalCode;
$listing_address = $listing->address->full;
// A listing might not have a null address if a flag like
// "Display address" is set to false. This just removes the
// comma in these cases, but the rest of the address remains
// the same.
$comma = $listing_address ? ', ' : '';
$listing_address_full = $listing_address
. $comma
. $listing_city
. ', '
. $listing_state
. ' '
. $listing_zip;
if($prettify && $custom_permalink_struct === "pretty_extra") {
$url .= "/listings/$listing_city/$listing_state/$listing_zip/$listing_address_full/$listing_id";
if(!empty($query)) {
$url .= "?" . $query;
}
} elseif($prettify && $custom_permalink_struct === "pretty") {
$url .= "/listings/$listing_id/$listing_address_full";
if(!empty($query)) {
$url .= "?" . $query;
}
} else {
$url .= "?sr-listings=sr-single"
. "&listing_id=$listing_id"
. "&listing_title=$listing_address_full";
if(!empty($query)) {
$url .= "&" . $query;
}
}
// URL encode special characters
$url = str_replace(' ', '+', $url);
$url = str_replace('#', '%23', $url);
$url = str_replace(',', '%2C', $url);
return $url;
}
public static function buildPaginationLinks( $pagination ) {
$pag = array(
'prev' => '',
'next' => ''
);
$siteUrl = get_home_url() . '/?sr-listings=sr-search&';
if( $pagination['prev'] !== null && !empty($pagination['prev'] ) ) {
$previous = $pagination['prev'];
$prev = str_replace( 'https://api.simplyrets.com/properties?', $siteUrl, $previous );
$prev_link = "<a href='{$prev}'>Prev</a>";
$pag['prev'] = $prev_link;
}
if( $pagination['next'] !== null && !empty($pagination['next'] ) ) {
$nextLink = $pagination['next'];
$next = str_replace( 'https://api.simplyrets.com/properties?', $siteUrl, $nextLink );
$next_link = "| <a href='{$next}'>Next</a>";
$pag['next'] = $next_link;
}
return $pag;
}
/**
* Use this instead of builting parse_str
* proper_parse_str will sanely handle duplicate
* keys in the query. id ?foo=1&foo2
*
* @param $str - a query string
* @result $arr - the query string in array form
*/
public static function proper_parse_str($str) {
if (empty($str)) {
return array();
}
$arr = array();
$pairs = explode('&', $str);
foreach ($pairs as $i) {
list($name,$value) = explode('=', $i, 2);
if( isset($arr[$name]) ) {
if( is_array($arr[$name]) ) {
$arr[$name][] = $value;
}
else {
$arr[$name] = array($arr[$name], $value);
}
}
else {
$arr[$name] = $value;
}
}
return $arr;
}
public static function ordinalSuffix($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13)) {
return $number. 'th';
} else {
return $number. $ends[$number % 10];
}
}
/**
* Create markup for showing various MLS compliance information
* based on users current admin settings.
*/
public static function mkListingSummaryCompliance($listing_office, $listing_agent) {
/** Get current settings */
$office_on_thumbnails = get_option('sr_office_on_thumbnails', false);
$agent_on_thumbnails = get_option('sr_agent_on_thumbnails', false);
$idx_img_on_thumbnails = get_option('sr_thumbnail_idx_image', false);
/** Helpers if agent or office CAN and SHOULD be shown */
$show_agent = !empty($agent_on_thumbnails) && trim($listing_agent) !== "";
$show_office = !empty($office_on_thumbnails) && trim($listing_office) !== "";
/** Initial markup */
$listing_by = "";
$listing_idx_img_markup = "";
/**
* Create a "Listing by" string that shows some combination of
* listing agent and/or office depending on current settings.
*/
if ($show_office || $show_agent) {
$listing_by = "Listing by ";
if ($show_agent) {
$listing_by .= $listing_agent;
}
if ($show_office) {
if ($show_agent) {
$listing_by .= ", {$listing_office}";
} else {
$listing_by .= $listing_office;
}
}
}
/**
* Create an <img> element if IDX image is available and
* setting is enabled.
*/
if (!empty($idx_img_on_thumbnails) && !empty($idx_img_on_thumbnails)) {
$listing_idx_img_markup = "<img src=\"{$idx_img_on_thumbnails}\"/>";
}
// Add a line break if both fields are enabled
if (!empty($listing_by) && !empty($listing_idx_img_markup)) {
return "<span class='sr-listing-summary-compliance'>"
. "{$listing_by}<br/>{$listing_idx_img_markup}"
. "</span>";
} else {
return "<span class='sr-listing-summary-compliance'>"
. "{$listing_by} {$listing_idx_img_markup}"
. "</span>";
}
}
/**
* Generate disclaimer text shown with short-code listings. If
* the user has provided a custom disclaimer in their settings
* page use that, otherwise use the SimplyRETS default.
*/
public static function mkDisclaimerText($lastUpdate) {
$custom_disclaimer = get_option('sr_custom_disclaimer', false);
if ($custom_disclaimer) {
// Splice lastUpdate date into custom disclaimer
$built_disclaimer = str_replace('{lastUpdate}', $lastUpdate, $custom_disclaimer);
return html_entity_decode($built_disclaimer);
} else {
return "This information is believed to be accurate, but without any warranty.";
}
}
/**
* Created the "Listing by" markup if
* sr_agent_office_above_the_fold is enabled. This also handles
* showing the correct info when only the agent name, or only the
* office name is available.
*/
public static function mkAgentOfficeAboveTheFold($agent, $office) {
// Initialize variables
$listing_by;
// Ensure we have all the info we need
$agentOfficeAboveTheFoldEnabled = get_option(
'sr_agent_office_above_the_fold',
false
);
if ($agentOfficeAboveTheFoldEnabled) {
if (!empty($agent) AND !empty($office)) {
/**
* Agent and office are available, show both of them
*/
$listing_by .= "Listing by: ";
$listing_by .= "<strong>$agent</strong>, ";
$listing_by .= "<strong>$office</strong>";
return "<p>$listing_by</p>";
} elseif (empty($agent) AND !empty($office)) {
/**
* Only office name is available, show that
*/
$listing_by = "Listing by: <strong>$office</strong>";
return "<p>$listing_by</p>";
} elseif (!empty($agent) AND empty($office)) {
/**
* Only agent name is available, show that
*/
$listing_by = "Listing by: <strong>$agent</strong>";
return "<p>$listing_by</p>";
} else {
return "";
}
}
return "";
}
/**
* Return the text "MLS". If the 'sr_show_mls_trademark_symbol'
* admin option is enabled, the trademark symbol is returned with
* the text: MLS®
*/
public static function mkMLSText() {
$td = get_option('sr_show_mls_trademark_symbol', false);
if (empty($td)) {
return "MLS";
} else {
return "MLS®";
}
}
}
class SrListing {
/**
* Return a 'display-ready' status for a listing. Checks the
* sr_show_mls_status_text option and returns either the
* statusText or status for the listing.
*/
public static function listingStatus($listing) {
$useStatusText = get_option('sr_show_mls_status_text', false);
return $useStatusText ? $listing->mls->statusText : $listing->mls->status;
}
}
class SrMessages {
public static function noResultsMsg($response) {
$response = (array)$response;
if($response['message']) {
return (
'<br><p><strong>'
. $response['message']
. '</br></p></strong>'
);
}
$noResultsMsg = "<br><p><strong>There are 0 listings that match this search. "
. "Please try to broaden your search criteria or feel free to try again later.</p></strong>";
return $noResultsMsg;
}
}
class SrViews {
public static function listDateResults( $date ) {
$markup = <<<HTML
<li>
<span>Listed on $date</span>
</li>
HTML;
return $markup;
}
}
/**
* Top level 'pollyfill' for 'http_parse_headers'
*
* Taken from PHP implementation:
* http://php.net/manual/it/function.http-parse-headers.php
*/
if (!function_exists('http_parse_headers')) {
function http_parse_headers ($raw_headers) {
$headers = array(); // $headers = [];
foreach (explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if(!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} else if(is_array($headers[$h[0]])) {
$tmp = array_merge($headers[$h[0]],array(trim($h[1])));
$headers[$h[0]] = $tmp;
} else {
$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
$headers[$h[0]] = $tmp;
}
}
}
return $headers;
}
}