PluginProbe
Flexmls® IDX Plugin / 2.3
Flexmls® IDX Plugin v2.3
4.0.1 4.0.0 3.18.2 trunk 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 2.0 2.1 2.2 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 2.4.2 3.0.0 3.0.1 3.0.10 3.0.11 All 157 releases
flexmls-idx / components / search.php

search.php in Flexmls® IDX Plugin 2.3, at components/search.php

514 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4
5 class fmcSearch extends fmcWidget {
6
7 function fmcSearch() {
8 global $fmc_widgets;
9
10 $widget_info = $fmc_widgets[ get_class($this) ];
11
12 $widget_ops = array( 'description' => $widget_info['description'] );
13 $this->WP_Widget( get_class($this) , $widget_info['title'], $widget_ops);
14
15 // have WP replace instances of [first_argument] with the return from the second_argument function
16 add_shortcode($widget_info['shortcode'], array(&$this, 'shortcode'));
17
18 // register where the AJAX calls should be routed when they come in
19 add_action('wp_ajax_'.get_class($this).'_shortcode', array(&$this, 'shortcode_form') );
20 add_action('wp_ajax_'.get_class($this).'_shortcode_gen', array(&$this, 'shortcode_generate') );
21
22 }
23
24
25 function jelly($args, $settings, $type) {
26 global $fmc_api;
27
28 extract($args);
29
30 if ($type == "widget" && empty($settings['title']) && flexmlsConnect::use_default_titles()) {
31 $settings['title'] = "IDX Search";
32 }
33
34 $return = '';
35
36 $rand = mt_rand();
37
38 $title = trim($settings['title']);
39 $width = trim($settings['width']);
40 $buttontext = htmlspecialchars(trim($settings['buttontext']), ENT_QUOTES);
41 $location_search = trim($settings['location_search']);
42 $property_type = trim($settings['property_type']);
43 $my_link = trim($settings['link']);
44 $std_fields = trim($settings['std_fields']);
45
46 $std_fields_selected = explode(",", $std_fields);
47 $property_types_selected = explode(",", $property_type);
48
49 $api_prop_types = $fmc_api->PropertyTypes();
50 $api_system_info = $fmc_api->SystemInfo();
51 $api_location_search_api = $fmc_api->GetLocationSearchApiUrl();
52 $api_links = $fmc_api->GetIDXLinks();
53
54 if ($api_prop_types === false || $api_system_info === false || $api_location_search_api === false || $api_links === false) {
55 return flexmlsConnect::widget_not_available($fmc_api, false, $args, $settings);
56 }
57
58 if ($my_link == "default") {
59 $my_link = flexmlsConnect::get_default_idx_link();
60 }
61
62 $good_link = false;
63 foreach ($api_links as $link) {
64 if ($link['LinkId'] == $my_link) {
65 $good_link = true;
66 }
67 }
68
69 if (!$good_link) {
70 return flexmlsConnect::widget_not_available($fmc_api, false, $args, $settings);
71 }
72
73 $return .= $before_widget;
74
75 if ( !empty($title) ) {
76 $return .= $before_title;
77 $return .= $title;
78 $return .= $after_title;
79 $return .= "\n";
80 }
81
82 if (empty($buttontext)) {
83 $buttontext = "Search";
84 }
85
86 if (empty($width)) {
87 $width = 400;
88 }
89
90 $search_fields = array();
91
92 $this_target = "";
93 if (flexmlsConnect::get_destination_window_pref() == "new") {
94 $this_target = " target='_blank'";
95 }
96
97 $return .= "<div class='flexmls_connect__search' style='width:{$width}px;'>\n";
98 $return .= "<form action='{$_SERVER['REQUEST_URI']}' method='post'{$this_target}>\n";
99 $return .= "<table cellpadding='0' cellspacing='0' border='0'>\n";
100
101 if ($location_search == "on") {
102 $return .= "\t<tr><td colspan='4'>\n";
103 $return .= "\t\t<input type='text' name='location' data-connect-url='{$api_location_search_api}' class='flexmls_connect__location_search' autocomplete='off' value='City, Zip, Address or Other Location' />\n";
104 $return .= "\t</td></tr>\n";
105 $return .= "\t<tr class='shade'><td colspan='4' class='flexmls_connect__location_list'></td></tr>\n";
106 $search_fields[] = "Location";
107 }
108
109 $good_prop_types = array();
110
111 foreach ($api_prop_types as $k => $v) {
112 if (in_array($k, $property_types_selected)) {
113 $good_prop_types[] = $k;
114 }
115 }
116
117 $return .= "\n\n";
118
119 if (count($good_prop_types) > 1) {
120 $return .= "\t<tr class='shade' data-connect-type='property-type' data-connect-field='property-type'><td colspan='4'>\n";
121 foreach ($good_prop_types as $type) {
122 $return .= "\t\t<span><input type='checkbox' class='property-type-checkbox' checked='checked' name='property-type-{$type}' id='{$rand}-property-type-{$type}' value='{$type}'> <label for='{$rand}-property-type-{$type}'>{$api_prop_types[$type]}</label></span>\n";
123 }
124 $return .= "\t</td></tr>\n";
125 }
126 else {
127 $return .= "\t<input type='hidden' name='property-type' value='".implode("", $good_prop_types)."' />\n";
128 }
129 $search_fields[] = "PropertyType";
130
131 $return .= "\n\n";
132
133 foreach ($std_fields_selected as $fi) {
134 if ( $fi == "list_price" ) {
135 $return .= "<tr class='shade' data-connect-type='number' data-connect-field='list_price'>\n";
136 $return .= "<td class='first'><label for='{$rand}-list_price_from'>Price: </label></td>\n";
137 $return .= "<td><input type='text' class='text' name='list_price_from' id='{$rand}-list_price_from' data-connect-default='Min' /></td>\n";
138 $return .= "<td><label for='{$rand}-list_price_to'>to</label></td>\n";
139 $return .= "<td><input type='text' class='text' name='list_price_to' id='{$rand}-list_price_to' data-connect-default='Max' /></td>\n";
140 $return .= "</tr>\n";
141 $search_fields[] = "ListPrice";
142 }
143
144 if ( $fi == "beds" ) {
145 $return .= "<tr class='shade' data-connect-type='number' data-connect-field='beds'>\n";
146 $return .= "<td class='first'><label for='{$rand}-beds_from'>Bedrooms: </label></td>\n";
147 $return .= "<td><input type='text' class='text' name='beds_from' id='{$rand}-beds_from' data-connect-default='Min' /></td>\n";
148 $return .= "<td><label for='{$rand}-beds_to'>to</label></td>\n";
149 $return .= "<td><input type='text' class='text' name='beds_to' id='{$rand}-beds_to' data-connect-default='Max' /></td>\n";
150 $return .= "</tr>\n";
151 $search_fields[] = "BedsTotal";
152 }
153
154 if ( $fi == "baths" ) {
155 $return .= "<tr class='shade' data-connect-type='number' data-connect-field='baths'>\n";
156 $return .= "<td class='first'><label for='{$rand}-baths_from'>Bathrooms: </label></td>\n";
157 $return .= "<td><input type='text' class='text' name='baths_from' id='{$rand}-baths_from' data-connect-default='Min' /></td>\n";
158 $return .= "<td><label for='{$rand}-baths_to'>to</label></td>\n";
159 $return .= "<td><input type='text' class='text' name='baths_to' id='{$rand}-baths_to' data-connect-default='Max' /></td>\n";
160 $return .= "</tr>\n";
161 $search_fields[] = "BathsTotal";
162 }
163
164 if ( $fi == "square_footage" ) {
165 $return .= "<tr class='shade' data-connect-type='number' data-connect-field='square_footage'>\n";
166 $return .= "<td class='first'><label for='{$rand}-square_footage_from'>Square Footage: </label></td>\n";
167 $return .= "<td><input type='text' class='text' name='square_footage_from' id='{$rand}-square_footage_from' data-connect-default='Min' /></td>\n";
168 $return .= "<td><label for='{$rand}-square_footage_to'>to</label></td>\n";
169 $return .= "<td><input type='text' class='text' name='square_footage_to' id='{$rand}-square_footage_to' data-connect-default='Max' /></td>\n";
170 $return .= "</tr>\n";
171 $search_fields[] = "BuildingAreaTotal";
172 }
173
174 if ( $fi == "age" ) {
175 $return .= "<tr class='shade' data-connect-type='number' data-connect-field='age'>\n";
176 $return .= "<td class='first'><label for='{$rand}-age_from'>Age: </label></td>\n";
177 $return .= "<td><input type='text' class='text' name='age_from' id='{$rand}-age_from' data-connect-default='Min' /></td>\n";
178 $return .= "<td><label for='{$rand}-age_to'>to</label></td>\n";
179 $return .= "<td><input type='text' class='text' name='age_to' id='{$rand}-age_to' data-connect-default='Max' /></td>\n";
180 $return .= "</tr>\n";
181 $search_fields[] = "YearBuilt";
182 }
183
184 }
185
186 $return .= "<tr><td colspan='4'>\n";
187 $return .= "<input type='hidden' name='fmc_do' value='fmc_search' />\n";
188 $return .= "<input type='hidden' name='link' class='flexmls_connect__link' value='{$my_link}' />\n";
189 $return .= "<input type='hidden' name='query' value='' />\n";
190 $return .= "<input type='hidden' name='tech_id' class='flexmls_connect__tech_id' value=\"x'{$api_system_info['Id']}'\" />\n";
191 $return .= "<input type='hidden' name='ma_tech_id' class='flexmls_connect__ma_tech_id' value=\"x'{$api_system_info['MlsId']}'\" />\n";
192 $return .= "<input type='hidden' name='destlink' value='".flexmlsConnect::get_destination_link()."' />\n";
193 $return .= "<input type='submit' value='{$buttontext}' />\n";
194 $return .= "</td></tr>\n";
195
196 $return .= "</table>\n";
197 $return .= "</form>\n";
198 $return .= "</div>\n";
199
200 $return .= $after_widget;
201
202 return $return;
203
204 }
205
206
207 function widget($args, $instance) {
208 echo $this->jelly($args, $instance, "widget");
209 }
210
211
212 function shortcode($attr = array()) {
213
214 $args = array(
215 'before_title' => '<h3>',
216 'after_title' => '</h3>',
217 'before_widget' => '',
218 'after_widget' => ''
219 );
220
221 return $this->jelly($args, $attr, "shortcode");
222
223 }
224
225
226 function settings_form($instance) {
227
228 $title = esc_attr($instance['title']);
229 $width = esc_attr($instance['width']);
230 $property_type = esc_attr($instance['property_type']);
231 $location_search = esc_attr($instance['location_search']);
232 $std_fields = esc_attr($instance['std_fields']);
233 $standard_fields = explode(",", $std_fields);
234 $link = esc_attr($instance['link']);
235 $buttontext = esc_attr($instance['buttontext']);
236
237 $property_types_selected = explode(",", $property_type);
238
239 $possible_standard_fields = array(
240 'age' => "Age",
241 'baths' => "Bathrooms",
242 'beds' => "Bedrooms",
243 'square_footage' => "Square Footage",
244 'list_price' => "Price"
245 );
246
247 $location_search_options = array(
248 'on' => 'On',
249 'off' => 'Off'
250 );
251
252
253 $fmc_api = new FlexmlsApiWp;
254 $api_property_type_options = $fmc_api->PropertyTypes();
255 $api_links = $fmc_api->GetIDXLinks();
256
257 if ($api_property_type_options === false || $api_links === false) {
258 return flexmlsConnect::widget_not_available($fmc_api, true);
259 }
260
261 $return = "";
262
263 $selected_code = " selected='selected'";
264
265 $return .= "
266
267 <p>
268 <label for='".$this->get_field_id('title')."'>" . __('Title:') . "</label>
269 <input fmc-field='title' fmc-type='text' type='text' class='widefat' id='".$this->get_field_id('title')."' name='".$this->get_field_name('title')."' value='{$title}'>
270 </p>
271
272 <p>
273 <label for='".$this->get_field_id('width')."'>" . __('Width:') . "</label>
274 <input fmc-field='width' fmc-type='text' type='text' id='".$this->get_field_id('width')."' name='".$this->get_field_name('width')."' value='{$width}'>
275 </p>
276
277 <p>
278 <label for='".$this->get_field_id('link')."'>" . __('IDX Link:') . "</label>
279 <select fmc-field='link' fmc-type='select' id='".$this->get_field_id('link')."' name='".$this->get_field_name('link')."'>
280 ";
281
282 $is_selected = ($link == "default") ? $selected_code : "";
283 $return .= "<option value='default'{$is_selected}>(Use Saved Default)</option>\n";
284
285 foreach ($api_links as $my_l) {
286 $is_selected = ($my_l['LinkId'] == $link) ? $selected_code : "";
287 $return .= "<option value='{$my_l['LinkId']}'{$is_selected}{$is_disabled}>{$my_l['Name']}</option>\n";
288 }
289
290 $return .= "
291 </select><br /><span class='description'>Link used when search is executed</span>
292 </p>
293
294 <p><b>Filters:</b></p>
295 <div style='padding: 2px 5px 10px 10px; border: 1px dashed #C0C0C0;'>
296
297 <p>
298 <label for='".$this->get_field_id('property_type')."'>" . __('Property Type:') . "</label><br />
299 <select fmc-field='property_type' fmc-type='select' name='".$this->get_field_name('property_type')."[]' style='height: 75px;' multiple='multiple'>
300 ";
301
302 foreach ($api_property_type_options as $k => $v) {
303 $is_selected = (in_array($k, $property_types_selected)) ? $selected_code : "";
304 $return .= "<option value='{$k}'{$is_selected}>{$v}</option>\n";
305 }
306
307 $return .= "
308 </select><br />
309 </p>
310
311 <p>
312 <label for='".$this->get_field_id('location_search')."'>" . __('Location Search:') . "</label>
313 <select fmc-field='location_search' fmc-type='select' name='".$this->get_field_name('location_search')."' id='".$this->get_field_id('location_search')."'>
314 ";
315
316 foreach ($location_search_options as $k => $v) {
317 $is_selected = ($k == $location_search) ? $selected_code : "";
318 $return .= "<option value='{$k}'{$is_selected}>{$v}</option>\n";
319 }
320
321 $return .= "
322 </select><br />
323 <span class='description'>Show the Location Search</span>
324 </p>
325
326 <p>
327 <label for='".$this->get_field_id('std_fields')."'>" . __('Fields:') . "</label>
328 <input fmc-field='std_fields' fmc-type='text' type='hidden' name='".$this->get_field_name('std_fields')."' class='flexmls_connect__std_fields' value='{$std_fields}' />
329 <ul class='flexmls_connect__sortable'>
330 ";
331
332 foreach ($standard_fields as $fi) {
333 $fi = trim($fi);
334 if (empty($fi)) {
335 continue;
336 }
337 $return .= "
338 <li data-connect-name='{$fi}'>
339 <span class='remove' title='Remove this field'>&times;</span>
340 <span class='ui-icon ui-icon-arrowthick-2-n-s'></span>
341 {$possible_standard_fields[$fi]}
342 </li>
343 ";
344 }
345
346 $return .= "
347 </ul>
348
349 <hr />
350 <select name='".$this->get_field_name('available_fields')."' class='flexmls_connect__available_fields'>
351 ";
352
353 foreach ($possible_standard_fields as $k => $v) {
354 $return .= "<option value='{$k}'>{$v}</option>\n";
355 }
356
357 $return .= "
358 </select>
359 <a href='javascript:void(0);' title='Add this field to the search' class='flexmls_connect__add_std_field'>Add Field</a>
360
361 </p>
362
363 </div>
364
365 <p><br />
366 <label for='".$this->get_field_id('buttontext')."'>" . __('Button Text:') . "</label>
367 <input fmc-field='buttontext' fmc-type='text' type='text' class='widefat' id='".$this->get_field_id('buttontext')."' name='".$this->get_field_name('buttontext')."' value='{$buttontext}'>
368 <span class='description'>Customize the name of the submit button</span>
369 </p>
370
371
372 <img src='x' class='flexmls_connect__bootloader' onerror='flexmls_connect.sortable_setup(this);' />
373
374
375 ";
376
377
378 $return .= "<input type='hidden' name='shortcode_fields_to_catch' value='title,width,link,property_type,location_search,buttontext,std_fields' />\n";
379 $return .= "<input type='hidden' name='widget' value='". get_class($this) ."' />\n";
380
381 return $return;
382
383 }
384
385
386
387 function update($new_instance, $old_instance) {
388 $instance = $old_instance;
389
390 $instance['title'] = strip_tags($new_instance['title']);
391 $instance['width'] = strip_tags($new_instance['width']);
392 $instance['location_search'] = strip_tags($new_instance['location_search']);
393 $instance['property_type'] = implode(",", array_map('strip_tags', $new_instance['property_type']));
394 $instance['buttontext'] = strip_tags($new_instance['buttontext']);
395 $instance['link'] = strip_tags($new_instance['link']);
396 $instance['std_fields'] = strip_tags($new_instance['std_fields']);
397
398 return $instance;
399 }
400
401
402 function submit_search() {
403 global $fmc_api;
404
405 // translate from form field names to standard names
406 $fields_to_catch = array(
407 'PropertyType' => 'PropertyType',
408 'MapOverlay' => 'MapOverlay',
409 'list_price' => 'ListPrice',
410 'beds' => 'BedsTotal',
411 'baths' => 'BathsTotal',
412 'age' => 'YearBuilt',
413 'square_footage' => 'BuildingAreaTotal'
414 );
415
416 $query = stripslashes($_POST['query']);
417 $my_link = stripslashes($_POST['link']);
418
419 $query_conditions = array();
420 $std_query_conditions = array();
421
422 // break the 'query' value apart and start saving the operators and values separately
423 $conds = explode("&", $query);
424 foreach ($conds as $c) {
425 $key = "";
426 $value = "";
427 $operator = "";
428
429 // check for the special operators
430 if ( strpos($c, ">=") !== false ) {
431 $operator = ">=";
432 }
433 elseif ( strpos($c, "<=") !== false ) {
434 $operator = "<=";
435 }
436 else {
437 $operator = "=";
438 }
439
440 // break the key/value apart based on the operator found
441 list($key, $value) = explode($operator, $c, 2);
442
443 // special handling for the 'age' field to convert it into years
444 if ($key == "age") {
445 if ( strpos($value, ",") !== false) {
446 // range search. convert both to years
447 list($from, $to) = explode(",", $value);
448 $from = date("Y") - $from;
449 $to = date("Y") - $to;
450 // swap values since it makes sense for year searches.
451 // 5 to 25 years becomes 2005 to 1985 otherwise
452 $value = "{$to},{$from}";
453 }
454 else {
455 $value = date("Y") - $value;
456 }
457 }
458 else {
459 $vals = explode(",", $value);
460 $vals = array_map( array('flexmlsConnect', 'strip_quotes') , $vals);
461 $vals = array_map( array('flexmlsConnect', 'remove_starting_equals') , $vals);
462 $value = implode(",", $vals);
463 }
464
465 $query_conditions[$key] = array('v' => $value, 'o' => $operator);
466
467 }
468
469 // build the transform link and map fields to their standard names
470 $link_transform_params = array();
471 foreach ($query_conditions as $k => $qc) {
472 if (array_key_exists($k, $fields_to_catch)) {
473 $std_cond_field = $fields_to_catch[$k];
474 }
475 else {
476 $std_cond_field = $k;
477 }
478 $std_query_conditions[$std_cond_field] = array('v' => $qc['v'], 'o' => $qc['o']);
479 $link_transform_params[$std_cond_field] = "*{$std_cond_field}*";
480 }
481
482 // get transformed link
483 $outbound_link = $fmc_api->GetTransformedIDXLink($my_link, $link_transform_params);
484
485 // change the placeholders back to actual values with the given operator
486 foreach ($std_query_conditions as $k => $sqc) {
487 $outbound_link = str_replace("=*{$k}*", $sqc['o'] . $sqc['v'], $outbound_link);
488 }
489
490 // take out all remaining placeholders
491 $outbound_link = preg_replace('/\*(.*?)\*/', "", $outbound_link);
492
493 $outbound_link = urlencode($outbound_link);
494
495 $permalink = stripslashes($_POST['destlink']);
496
497 if (strpos($permalink, '?') !== false) {
498 $outbound_link = $permalink . '&url=' . $outbound_link;
499 }
500 else {
501 $outbound_link = $permalink . '?url=' . $outbound_link;
502 }
503
504 // forward the user on if we have someplace for them to go
505 if (!empty($outbound_link)) {
506 header("Location: {$outbound_link}");
507 exit;
508 }
509
510 }
511
512
513 }
514