Skip to content

Instantly share code, notes, and snippets.

@maxchirkov
Last active March 12, 2022 03:47
Show Gist options
  • Save maxchirkov/c426965df7a0422f6719 to your computer and use it in GitHub Desktop.
Save maxchirkov/c426965df7a0422f6719 to your computer and use it in GitHub Desktop.
Add Extended Filters to Quick Search Widget
<?php
/*
This implementaion works out of the box for Sandicor market.
Simply copy and paste it into the active theme's functions.php file
and a new Property Views filter should appear in both
front- and back-end Quick Search Widget.
*/
/**
* Adds extended filter field to the Admin Widget UI
* Sandicor market only AND Residential listing types
*/
add_filter('ridx_ui_search_filters_schema', 'sandicor_xf_lfd_view_44', 10, 3);
function sandicor_xf_lfd_view_44($filtersSchema, $market, $listingType)
{
if ($market == 'sandicor' && strtolower($listingType) == 'residential' && is_admin())
{
$data = enumerateFilter('xf_lfd_view_44', $market, $listingType);
$filtersSchema['xf_lfd_view_44'] = $data;
}
return $filtersSchema;
}
/**
* Makes selections list field multi-select
*/
add_filter('ridx_widget_admin_featured_listings_filters_attributes', 'multiselect_xf_lfd_view_44', 10, 2);
function multiselect_xf_lfd_view_44($attributes, $key)
{
if ($key == 'xf_lfd_view_44')
{
$attributes = ' multiple';
}
return $attributes;
}
/**
* Adds custom label to the extended field in the Admin Widget UI
*/
add_filter('ridx_field_labels', 'label_for_sandicor_xf_lfd_view_44');
/**
* @param array $labels - array of field/filter labels and descriptions
*/
function label_for_sandicor_xf_lfd_view_44($labels)
{
$labels['xf_lfd_view_44'] = array(
'label' => 'Property Views'
);
return $labels;
}
/**
* Helper function to request enumerable values for a given filter
*
* @param string $filter - filter name
* @param string $market - market name
* @param string $listingType - listing type name
* @return array
*/
function enumerateFilter($filter, $market, $listingType)
{
$result = array();
$args = array(
'id' => $market,
'listingType' => $listingType,
'property' => $filter
);
$values = \hji\membership\Membership::getInstance()->apiController->api->enumerate($args);
if (is_array($values) && isset($values['values']) && !empty($values['values']))
{
foreach ($values['values'] as $value)
{
$result['values'][$value] = $value;
}
}
return $result;
}
/**
* Adds Property Views selection list filter to Sandicor Residential QSW
* Implements extended schema filter xf_lfd_view_44
*/
add_filter('ridx_search_form_residential', 'add_extended_filter_xf_lfd_view_44', 10, 2);
/**
* @param array $vars - array of QSW fields and their properties
* @param object $widgetModal - \hji\ResponsiveIDX\models\QuickSearchViewModel
*/
function add_extended_filter_xf_lfd_view_44($vars, $widgetModel)
{
// Current extended filter only supported by Sandicor market
if ($widgetModel->market != 'sandicor')
return $vars;
// Start creating custom field
// Get currently selected value
$currentValue = $widgetModel->getParam('xf_lfd_view_44');
// Get filter's enumerable values from API
$enum = enumerateFilter('xf_lfd_view_44', $widgetModel->market, 'Residential');
// Define field properties
$fieldProperties = array(
'type' => 'select',
'name' => 'xf_lfd_view_44',
'label' => 'Property Views',
'atts' => array(
'id' => 'xf_lfd_view',
),
'before' => '<div class="hji-fieldwrap hji-xf_lfd_view_44-wrap">',
'after' => '</div>',
'value' => $enum['values'],
'currentValue' => $currentValue,
);
// Create field's HTML view
$viewField = $widgetModel->createFormField($fieldProperties);
// Add field into the array of current fields
$vars['mainFields'] = $vars['mainFields'] + array('id' => $viewField);
return $vars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment