Skip to content

Commit

Permalink
Locator - filter by radius
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirish committed Feb 22, 2017
1 parent dde1ead commit f18b141
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
24 changes: 23 additions & 1 deletion code/form/LocatorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
*/
class LocatorForm extends Form
{
/**
* @var bool
*/
private static $show_radius = true;

/**
* @var array
*/
private static $radius_array = [
'25' => '25',
'50' => '50',
'75' => '75',
'100' => '100',
];

/**
* LocatorForm constructor.
Expand All @@ -30,11 +44,19 @@ public function __construct(Controller $controller, $name)
if ($categories) {
$categoriesField = DropdownField::create('CategoryID')
->setTitle('')
->setEmptyString('All Categories')
->setEmptyString('all categories')
->setSource($categories->map());
$fields->push($categoriesField);
}

if (Config::inst()->get('LocatorForm', 'show_radius')) {
$radiusArray = Config::inst()->get('LocatorForm', 'radius_array');
debug::show($radiusArray);
$fields->push(DropdownField::create('Radius', '', $radiusArray)
->setEmptyString('radius')
);
}

$actions = FieldList::create(
FormAction::create('doFilterLocations')
->setTitle('Search')
Expand Down
8 changes: 8 additions & 0 deletions code/pages/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@ public function setLocations(SS_HTTPRequest $request = null)
$locations = $locations->sort('distance');
}

if (Config::inst()->get('LocatorForm', 'show_radius')) {
if ($radius = (int)$request->getVar('Radius')) {
$locations = $locations->filterByCallback(function ($location) use (&$radius) {
return $location->distance <= $radius;
});
}
}

//allow for returning list to be set as
$this->extend('updateListType', $locations);

Expand Down
26 changes: 14 additions & 12 deletions templates/Layout/Locator.ss
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
<div class="form-container">
$LocationSearch
</div>
<% if $Locations %>
<div id="map-container">
<div id="map"></div>
<div class="loc-list">
<ul id="list">
</ul>
</div>
</div>
<% else %>
<div id="no-locals">
<p>No locations match your search criteria. Please refine your search and try again.</p>
</div>
<% if $getTrigger %>
<% if $Locations %>
<div id="map-container">
<div id="map"></div>
<div class="loc-list">
<ul id="list">
</ul>
</div>
</div>
<% else %>
<div id="no-locals">
<p>No locations match your search criteria. Please refine your search and try again.</p>
</div>
<% end_if %>
<% end_if %>
</div>

0 comments on commit f18b141

Please sign in to comment.