-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget-listings.php
271 lines (249 loc) · 13 KB
/
widget-listings.php
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
<?php
class dsSearchAgent_ListingsWidget extends WP_Widget {
function dsSearchAgent_ListingsWidget() {
$this->WP_Widget("dsidx-listings", "IDX Listings", array(
"classname" => "dsidx-widget-listings",
"description" => "Show a list of real estate listings"
));
}
function widget($args, $instance) {
extract($args);
extract($instance);
$title = apply_filters("widget_title", $title);
$options = get_option(DSIDXPRESS_OPTION_NAME);
if (!$options["Activated"])
return;
wp_enqueue_script('jquery', false, array(), false, true);
echo $before_widget;
if ($title)
echo $before_title . $title . $after_title;
$apiRequestParams = array();
$apiRequestParams["directive.ResultsPerPage"] = $listingsToShow;
$apiRequestParams["responseDirective.ViewNameSuffix"] = "widget";
$apiRequestParams["responseDirective.DefaultDisplayType"] = $defaultDisplay;
$apiRequestParams['responseDirective.IncludeDisclaimer'] = 'true';
if ($querySource == "area") {
$sort = explode("|", $areaSourceConfig["sort"]);
switch ($areaSourceConfig["type"]) {
case "city":
$typeKey = "query.Cities";
break;
case "community":
$typeKey = "query.Communities";
break;
case "tract":
$typeKey = "query.TractIdentifiers";
break;
case "zip":
$typeKey = "query.ZipCodes";
break;
}
$apiRequestParams[$typeKey] = $areaSourceConfig["name"];
$apiRequestParams["directive.SortOrders[0].Column"] = $sort[0];
$apiRequestParams["directive.SortOrders[0].Direction"] = $sort[1];
} else if ($querySource == "link") {
$apiRequestParams["query.ForceUsePropertySearchConstraints"] = "true";
$apiRequestParams["query.LinkID"] = $linkSourceConfig["linkId"];
if($linkSourceConfig["sort"]){
$sort = explode("|", $linkSourceConfig["sort"]);
if(count($sort) > 1){
$apiRequestParams["directive.SortOrders[0].Column"] = $sort[0];
$apiRequestParams["directive.SortOrders[0].Direction"] = $sort[1];
}
}
} else if ($querySource == "agentlistings") {
$apiRequestParams["directive.SortOrders[0].Column"] = "DateAdded";
$apiRequestParams["directive.SortOrders[0].Direction"] = "DESC";
if (isset($options['AgentID']) && !empty($options['AgentID'])) $apiRequestParams["query.ListingAgentID"] = $options['AgentID'];
} else if ($querySource == "officelistings") {
$apiRequestParams["directive.SortOrders[0].Column"] = "DateAdded";
$apiRequestParams["directive.SortOrders[0].Direction"] = "DESC";
if (isset($options['OfficeID']) && !empty($options['OfficeID'])) $apiRequestParams["query.ListingOfficeID"] = $options['OfficeID'];
}
$apiHttpResponse = dsSearchAgent_ApiRequest::FetchData("Results", $apiRequestParams);
if (empty($apiHttpResponse["errors"]) && $apiHttpResponse["response"]["code"] == "200") {
$data = $apiHttpResponse["body"];
} else {
switch ($apiHttpResponse["response"]["code"]) {
case 403:
$data = '<p class="dsidx-error">'.DSIDXPRESS_INACTIVE_ACCOUNT_MESSAGE.'</p>';
break;
default:
$data = '<p class="dsidx-error">'.DSIDXPRESS_IDX_ERROR_MESSAGE.'</p>';
}
}
$data = str_replace('{$pluginUrlPath}', dsSearchAgent_ApiRequest::MakePluginsUrlRelative(plugin_dir_url(__FILE__)), $data);
echo $data;
echo $after_widget;
dsidx_footer::ensure_disclaimer_exists();
}
function update($new_instance, $old_instance) {
// we need to do this first-line awkwardness so that the title comes through in the sidebar display thing
$new_instance["listingsOptions"]["title"] = $new_instance["title"];
$new_instance = $new_instance["listingsOptions"];
return $new_instance;
}
function form($instance) {
wp_enqueue_script('dsidxpress_widget_listings', DSIDXPRESS_PLUGIN_URL . 'js/widget-listings.js', array('jquery'), DSIDXPRESS_PLUGIN_VERSION, true);
$options = get_option(DSIDXPRESS_OPTION_NAME);
$instance = wp_parse_args($instance, array(
"title" => "Latest Real Estate",
"listingsToShow" => "25",
"defaultDisplay" => "listed",
"querySource" => "area",
"areaSourceConfig" => array(
"type" => "city",
"name" => "",
"sort" => "DateAdded|DESC"
),
"linkSourceConfig" => array(
"linkId" => ""
)
));
$titleFieldId = $this->get_field_id("title");
$titleFieldName = $this->get_field_name("title");
$baseFieldId = $this->get_field_id("listingsOptions");
$baseFieldName = $this->get_field_name("listingsOptions");
$checkedDefaultDisplay = array($instance["defaultDisplay"] => "checked=\"checked\"");
$checkedQuerySource = array($instance["querySource"] => "checked=\"checked\"");
$selectedAreaType = array($instance["areaSourceConfig"]["type"] => "selected=\"selected\"");
$selectedAreaTypeNormalized = ucwords($instance["areaSourceConfig"]["type"]);
$selectedSortOrder = array(str_replace("|", "", $instance["areaSourceConfig"]["sort"]) => "selected=\"selected\"");
$selectedLinkSortOrder = array(str_replace("|", "", $instance["linkSourceConfig"]["sort"]) => "selected=\"selected\"");
$selectedLink = array($instance["linkSourceConfig"]["linkId"] => "selected=\"selected\"");
$availableLinks = dsSearchAgent_ApiRequest::FetchData("AccountAvailableLinks", array(), true, 0);
$availableLinks = json_decode($availableLinks["body"]);
$pluginUrl = DSIDXPRESS_PLUGIN_URL;
$agentListingsNote = null;
$officeListingsNote = null;
if ($options['AgentID'] == null) {
$agentListingsNote = "There are no listings to show with your current settings. Please make sure you have provided your Agent ID on the IDX > General page of your site dashboard, or change this widget's settings to show other listings.";
}
if ($options['OfficeID'] == null) {
$officeListingsNote = "There are no listings to show with your current settings. Please make sure you have provided your Office ID on the IDX > General page of your site dashboard, or change this widget's settings to show other listings.";
}
echo <<<HTML
<p>
<label for="{$titleFieldId}">Widget title</label>
<input id="{$titleFieldId}" name="{$titleFieldName}" value="{$instance[title]}" class="widefat" type="text" />
</p>
<p>
<label for="{$baseFieldId}[listingsToShow]"># of listings to show (max 50)</label>
<input id="{$baseFieldId}[listingsToShow]" name="{$baseFieldName}[listingsToShow]" value="{$instance[listingsToShow]}" class="widefat" type="text" />
</p>
<p>
<input type="radio" name="{$baseFieldName}[defaultDisplay]" id="{$baseFieldId}[defaultDisplay-listed]" value="listed" {$checkedDefaultDisplay[listed]}/>
<label for="{$baseFieldId}[defaultDisplay-listed]">Show in list by default</label>
<br />
<input type="radio" name="{$baseFieldName}[defaultDisplay]" id="{$baseFieldId}[defaultDisplay-slideshow]" value="slideshow" {$checkedDefaultDisplay[slideshow]}/>
<label for="{$baseFieldId}[defaultDisplay-slideshow]">Show slideshow details by default</label>
<br />
<input type="radio" name="{$baseFieldName}[defaultDisplay]" id="{$baseFieldId}[defaultDisplay-expanded]" value="expanded" onclick="document.getElementById('{$baseFieldId}[listingsToShow]').value = 4;" {$checkedDefaultDisplay[expanded]}/>
<label for="{$baseFieldId}[defaultDisplay-expanded]">Show expanded details by default</label>
<br />
<input type="radio" name="{$baseFieldName}[defaultDisplay]" id="{$baseFieldId}[defaultDisplay-map]" value="map" {$checkedDefaultDisplay[map]}/>
<label for="{$baseFieldId}[defaultDisplay-map]">Show on map by default</label>
</p>
<div class="widefat" style="border-width: 0 0 1px; margin: 20px 0;"></div>
<table>
<tr>
<td style="width: 20px;"><p><input type="radio" name="{$baseFieldName}[querySource]" id="{$baseFieldId}[querySource-area]" value="area" {$checkedQuerySource[area]}/></p></td>
<td><p><label for="{$baseFieldId}[querySource-area]">Pick an area</label></p></td>
</tr>
<tr>
<td></td>
<td>
<p>
<label for="{$baseFieldId}[areaSourceConfig][type]">Area type</label>
<select id="{$baseFieldId}_areaSourceConfig_type" name="{$baseFieldName}[areaSourceConfig][type]" class="widefat" onchange="dsWidgetListings.SwitchType(this, '{$baseFieldId}_areaSourceConfig_title')">
<option value="city" {$selectedAreaType[city]}>City</option>
<option value="community" {$selectedAreaType[community]}>Community</option>
<option value="tract" {$selectedAreaType[tract]}>Tract</option>
<option value="zip" {$selectedAreaType[zip]}>Zip Code</option>
</select>
</p>
<p>
<label for="{$baseFieldId}[areaSourceConfig][name]">Area name</label>
<input id="{$baseFieldId}[areaSourceConfig][name]" name="{$baseFieldName}[areaSourceConfig][name]" class="widefat" type="text" value="{$instance[areaSourceConfig][name]}" />
</p>
<p>
<span class="description">See all <span id="{$baseFieldId}_areaSourceConfig_title">{$selectedAreaTypeNormalized}</span> Names <a href="javascript:void(0);" onclick="dsWidgetListings.LaunchLookupList('{$pluginUrl}locations.php', '{$baseFieldId}_areaSourceConfig_type')">here</a></span>
</p>
<p>
<label for="{$baseFieldId}[areaSourceConfig][sort]">Sort order</label>
<select id="{$baseFieldId}[areaSourceConfig][sort]" name="{$baseFieldName}[areaSourceConfig][sort]" class="widefat">
<option value="DateAdded|DESC" {$selectedSortOrder[DateAddedDESC]}>Time on market, newest first</option>
<option value="Price|DESC" {$selectedSortOrder[PriceDESC]}>Price, highest first</option>
<option value="Price|ASC" {$selectedSortOrder[PriceASC]}>Price, lowest first</option>
<option value="OverallPriceDropPercent|DESC" {$selectedSortOrder[OverallPriceDropPercentDESC]}>Price drop %, largest first</option>
<option value="WalkScore|DESC" {$selectedSortOrder[WalkScoreDESC]}>Walk Score™, highest first</option>
<option value="ImprovedSqFt|DESC" {$selectedSortOrder[ImprovedSqFtDESC]}>Improved size, largest first</option>
<option value="LotSqFt|DESC" {$selectedSortOrder[LotSqFtDESC]}>Lot size, largest first</option>
</select>
</p>
</td>
</tr>
<tr>
<th colspan="2"><p> - OR - </p></th>
</tr>
<tr>
<td valign="top"><p><input type="radio" name="{$baseFieldName}[querySource]" id="{$baseFieldId}[querySource-agentlistings]" value="agentlistings" {$checkedQuerySource[agentlistings]}/></p></td>
<td>
<p><label for="{$baseFieldId}[querySource-agentlistings]">My own listings (via agent ID, newest listings first)</label></p>
<p><i>{$agentListingsNote}</i></p>
</td>
</tr>
<tr>
<th colspan="2"><p> - OR - </p></th>
</tr>
<tr>
<td valign="top"><p><input type="radio" name="{$baseFieldName}[querySource]" id="{$baseFieldId}[querySource-officelistings]" value="officelistings" {$checkedQuerySource[officelistings]}/></p></td>
<td>
<p><label for="{$baseFieldId}[querySource-officelistings]">My office's listings (via office ID, newest listings first)</label></p>
<p><i>{$officeListingsNote}</i></p>
</td>
</tr>
HTML;
if (!defined('ZPRESS_API')) {
echo <<<HTML
<tr>
<th colspan="2"><p> - OR - </p></th>
</tr>
<tr>
<td><p><input type="radio" name="{$baseFieldName}[querySource]" id="{$baseFieldId}[querySource-link]" value="link" {$checkedQuerySource[link]}/></p></td>
<td><p><label for="{$baseFieldId}[querySource-link]">Use a link you created in your website control panel</label></p></td>
</tr>
<tr>
<td></td>
<td>
<p>
<select name="{$baseFieldName}[linkSourceConfig][linkId]" class="widefat">
HTML;
foreach ($availableLinks as $link) {
echo "<option value=\"{$link->LinkID}\" {$selectedLink[$link->LinkID]}>{$link->Title}</option>";
}
echo <<<HTML
</select>
</p>
<p>
<label for="{$baseFieldId}[linkSourceConfig][sort]">Sort order</label>
<select id="{$baseFieldId}[linkSourceConfig][sort]" name="{$baseFieldName}[linkSourceConfig][sort]" class="widefat">
<option value="DateAdded|DESC" {$selectedLinkSortOrder[DateAddedDESC]}>Time on market, newest first</option>
<option value="Price|DESC" {$selectedLinkSortOrder[PriceDESC]}>Price, highest first</option>
<option value="Price|ASC" {$selectedSortOrder[PriceASC]}>Price, lowest first</option>
<option value="OverallPriceDropPercent|DESC" {$selectedLinkSortOrder[OverallPriceDropPercentDESC]}>Price drop %, largest first</option>
<option value="WalkScore|DESC" {$selectedLinkSortOrder[WalkScoreDESC]}>Walk Score™, highest first</option>
<option value="ImprovedSqFt|DESC" {$selectedLinkSortOrder[ImprovedSqFtDESC]}>Improved size, largest first</option>
<option value="LotSqFt|DESC" {$selectedLinkSortOrder[LotSqFtDESC]}>Lot size, largest first</option>
</select>
</p>
</td>
</tr>
HTML;
}
echo <<<HTML
</table>
HTML;
}
}
?>