| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
class fmcLocationLinks extends fmcWidget { |
| 6 |
|
| 7 |
function fmcLocationLinks() { |
| 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 |
// set default title if a widget, none given, and the default_titles setting is turned on |
| 31 |
if ($type == "widget" && empty($settings['title']) && flexmlsConnect::use_default_titles()) { |
| 32 |
$settings['title'] = "Location Links"; |
| 33 |
} |
| 34 |
|
| 35 |
$return = ''; |
| 36 |
|
| 37 |
$title = trim($settings['title']); |
| 38 |
$my_link = trim($settings['link']); |
| 39 |
$property_type = trim($settings['property_type']); |
| 40 |
$my_locations = html_entity_decode(FlexmlsApiWp::clean_comma_list($settings['locations'])); |
| 41 |
|
| 42 |
// check if required parameters were given |
| 43 |
if (empty($my_link) || empty($my_locations) || empty($property_type)) { |
| 44 |
return flexmlsConnect::widget_missing_requirements("Location Links", "Link, Locations and Property Type"); |
| 45 |
} |
| 46 |
|
| 47 |
if ($my_link == "default") { |
| 48 |
$my_link = flexmlsConnect::get_default_idx_link(); |
| 49 |
} |
| 50 |
|
| 51 |
// make API call |
| 52 |
$api_links = $fmc_api->GetIDXLinks(); |
| 53 |
|
| 54 |
if ($api_links === false) { |
| 55 |
return flexmlsConnect::widget_not_available($fmc_api, false, $args, $settings); |
| 56 |
} |
| 57 |
|
| 58 |
// break the Location Search string into separate pieces |
| 59 |
$locations = flexmlsConnect::parse_location_search_string($my_locations); |
| 60 |
|
| 61 |
// re-arrange the structure a bit for easier testing |
| 62 |
$valid_links = array(); |
| 63 |
foreach ($api_links as $link) { |
| 64 |
$valid_links[$link['LinkId']] = array('Uri' => $link['Uri'], 'Name' => $link['Name']); |
| 65 |
} |
| 66 |
|
| 67 |
// test if the selected link is valid. bail out if not |
| 68 |
if ( !array_key_exists($my_link, $valid_links) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// make a list of all of the standard field names used in the Location Search value |
| 73 |
$location_field_names = array(); |
| 74 |
foreach ($locations as $loc) { |
| 75 |
$location_field_names[] = $loc['f']; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
// make that list unique |
| 80 |
$uniq_location_field_names = array_unique($location_field_names); |
| 81 |
|
| 82 |
// prepare some values for the transformation API call. |
| 83 |
// this allows us to get the transformed link for all of the fields at once rather than requiring |
| 84 |
// a separate API call for each unique field we're generating links for |
| 85 |
$link_transform_params = array(); |
| 86 |
foreach ($uniq_location_field_names as $loc_name) { |
| 87 |
$link_transform_params["{$loc_name}"] = "*{$loc_name}*"; |
| 88 |
} |
| 89 |
|
| 90 |
$link_transform_params["PropertyType"] = "*PropertyType*"; |
| 91 |
|
| 92 |
// make the API call to translate standard field names |
| 93 |
$outbound_link = $fmc_api->GetTransformedIDXLink($my_link, $link_transform_params); |
| 94 |
|
| 95 |
foreach ($locations as $loc) { |
| 96 |
|
| 97 |
// start replacing the placeholders in the link with the real values for this link |
| 98 |
$this_link = $outbound_link; |
| 99 |
$this_link = preg_replace('/\*'.preg_quote($loc['f']).'\*/', $loc['v'], $this_link); |
| 100 |
$this_link = preg_replace('/\*PropertyType\*/', $property_type, $this_link); |
| 101 |
// replace all remaining placeholders with a blank value since it doesn't apply to this link |
| 102 |
$this_link = preg_replace('/\*(.*?)\*/', "", $this_link); |
| 103 |
$this_target = ""; |
| 104 |
if (flexmlsConnect::get_destination_window_pref() == "new") { |
| 105 |
$this_target = " target='_blank'"; |
| 106 |
} |
| 107 |
$links_to_show .= "<li><a href=\"".flexmlsConnect::make_destination_link($this_link)."\" title=\"{$valid_links[$my_link]['Name']} - {$loc['l']}\"{$this_target}>{$loc['l']}</a></li>\n"; |
| 108 |
} |
| 109 |
|
| 110 |
if (empty($links_to_show)) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$return .= $before_widget; |
| 115 |
|
| 116 |
if ( !empty($title) ) { |
| 117 |
$return .= $before_title; |
| 118 |
$return .= $title; |
| 119 |
$return .= $after_title; |
| 120 |
$return .= "\n"; |
| 121 |
} |
| 122 |
|
| 123 |
$return .= "<ul>\n"; |
| 124 |
$return .= $links_to_show; |
| 125 |
$return .= "</ul>\n"; |
| 126 |
|
| 127 |
$return .= $after_widget; |
| 128 |
|
| 129 |
return $return; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
function widget($args, $instance) { |
| 135 |
echo $this->jelly($args, $instance, "widget"); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
function shortcode($attr = array()) { |
| 140 |
|
| 141 |
$args = array( |
| 142 |
'before_title' => '<h3>', |
| 143 |
'after_title' => '</h3>', |
| 144 |
'before_widget' => '', |
| 145 |
'after_widget' => '' |
| 146 |
); |
| 147 |
|
| 148 |
return $this->jelly($args, $attr, "shortcode"); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
function settings_form($instance) { |
| 154 |
|
| 155 |
$title = esc_attr($instance['title']); |
| 156 |
$link = esc_attr($instance['link']); |
| 157 |
$property_type = esc_attr($instance['property_type']); |
| 158 |
$locations = $instance['locations']; |
| 159 |
|
| 160 |
$selected_code = " selected='selected'"; |
| 161 |
|
| 162 |
$fmc_api = new FlexmlsApiWp; |
| 163 |
$api_links = $fmc_api->GetIDXLinks(); |
| 164 |
$api_property_type_options = $fmc_api->PropertyTypes(); |
| 165 |
$api_system_info = $fmc_api->SystemInfo(); |
| 166 |
$api_location_search_api = $fmc_api->GetLocationSearchApiUrl(); |
| 167 |
|
| 168 |
if ($api_links === false || $api_property_type_options === false || $api_system_info === false || $api_location_search_api === false) { |
| 169 |
return flexmlsConnect::widget_not_available($fmc_api, true); |
| 170 |
} |
| 171 |
|
| 172 |
$return = ""; |
| 173 |
|
| 174 |
$return .= " |
| 175 |
|
| 176 |
<p> |
| 177 |
<label for='".$this->get_field_id('title')."'>" . __('Title:') . "</label> |
| 178 |
<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}'> |
| 179 |
</p> |
| 180 |
|
| 181 |
<p> |
| 182 |
<label for='".$this->get_field_id('link')."'>" . __('IDX Link:') . "</label> |
| 183 |
<select fmc-field='link' fmc-type='select' id='".$this->get_field_id('link')."' name='".$this->get_field_name('link')."'> |
| 184 |
"; |
| 185 |
|
| 186 |
$is_selected = ($link == "default") ? $selected_code : ""; |
| 187 |
$return .= "<option value='default'{$is_selected}>(Use Saved Default)</option>\n"; |
| 188 |
|
| 189 |
foreach ($api_links as $my_l) { |
| 190 |
$is_selected = ($my_l['LinkId'] == $link) ? $selected_code : ""; |
| 191 |
$return .= "<option value='{$my_l['LinkId']}'{$is_selected}{$is_disabled}>{$my_l['Name']}</option>\n"; |
| 192 |
} |
| 193 |
|
| 194 |
$return .= " |
| 195 |
</select><br /><span class='description'>Link these locations are built upon</span> |
| 196 |
</p> |
| 197 |
|
| 198 |
<p> |
| 199 |
<label for='".$this->get_field_id('property_type')."'>" . __('Property Type:') . "</label> |
| 200 |
<select fmc-field='property_type' fmc-type='select' id='".$this->get_field_id('property_type')."' name='".$this->get_field_name('property_type')."' class='flexmls_connect__property_type'> |
| 201 |
"; |
| 202 |
|
| 203 |
foreach ($api_property_type_options as $k => $v) { |
| 204 |
$is_selected = ($k == $property_type) ? $selected_code : ""; |
| 205 |
$return .= "<option value='{$k}'{$is_selected}>{$v}</option>\n"; |
| 206 |
} |
| 207 |
|
| 208 |
$return .= " |
| 209 |
</select> |
| 210 |
</p> |
| 211 |
|
| 212 |
<div class='flexmls_connect__location'> |
| 213 |
<label for='horizontal'>Location:</label> |
| 214 |
<input type='text' name='location_input' data-connect-url='{$api_location_search_api}' class='flexmls_connect__location_search' autocomplete='off' value='City, Postal Code, etc.' /> |
| 215 |
|
| 216 |
<a href='javascript:void(0);' title='Click here to browse through available locations' class='flexmls_connect__location_browse'>Browse »</a> |
| 217 |
<div class='flexmls_connect__location_list' data-connect-multiple='true'> |
| 218 |
<p>All Locations Included</p> |
| 219 |
</div> |
| 220 |
<input type='hidden' name='{$this->get_field_name('tech_id')}' class='flexmls_connect__tech_id' value=\"x'{$api_system_info['Id']}'\" /> |
| 221 |
<input type='hidden' name='{$this->get_field_name('ma_tech_id')}' class='flexmls_connect__ma_tech_id' value=\"x'{$api_system_info['MlsId']}'\" /> |
| 222 |
<input fmc-field='locations' fmc-type='text' type='hidden' name='{$this->get_field_name('locations')}' class='flexmls_connect__location_fields' value=\"{$locations}\" /> |
| 223 |
</div> |
| 224 |
|
| 225 |
<img src='x' class='flexmls_connect__bootloader' onerror='flexmls_connect.location_setup(this);' /> |
| 226 |
|
| 227 |
"; |
| 228 |
|
| 229 |
$return .= "<input type='hidden' name='shortcode_fields_to_catch' value='title,link,property_type,locations' />\n"; |
| 230 |
$return .= "<input type='hidden' name='widget' value='". get_class($this) ."' />\n"; |
| 231 |
|
| 232 |
return $return; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
function update($new_instance, $old_instance) { |
| 238 |
$instance = $old_instance; |
| 239 |
|
| 240 |
$instance['title'] = strip_tags($new_instance['title']); |
| 241 |
$instance['link'] = strip_tags($new_instance['link']); |
| 242 |
$instance['property_type'] = strip_tags($new_instance['property_type']); |
| 243 |
$instance['locations'] = strip_tags($new_instance['locations']); |
| 244 |
|
| 245 |
return $instance; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
} |
| 250 |
|