Skip to content

Commit

Permalink
PHP Location filtering
Browse files Browse the repository at this point in the history
use SilverStripe to filter locations, rather than JS script

fixes dynamic#72
  • Loading branch information
jsirish committed Apr 8, 2016
1 parent 00088f7 commit 96ebf04
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 33 deletions.
23 changes: 23 additions & 0 deletions code/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function fieldLabels($includerelations = true)
$labels['ShowInLocator'] = 'Show';
$labels['ShowInLocator.NiceAsBoolean'] = 'Show';
$labels['Category.Name'] = 'Category';
$labels['Category.ID'] = 'Category';
$labels['Email'] = 'Email';
$labels['Featured.NiceAsBoolean'] = 'Featured';
$labels['Coords'] = 'Coords';
Expand Down Expand Up @@ -137,6 +138,28 @@ public function EmailAddress()
return false;
}

public function getCustomSearchContext()
{
$fields = $this->scaffoldSearchFields(array(
'restrictFields' => array('Address', 'Category.ID'),
));

$filters = array(
'Address' => new PartialMatchFilter('Address'),
'Suburb' => new PartialMatchFilter('Suburb'),
'State' => new PartialMatchFilter('State'),
'Postcode' => new PartialMatchFilter('Postcode'),
'Country' => new PartialMatchFilter('Postcode'),
'CategoryID' => new ExactMatchFilter('CategoryID'),
);

return new SearchContext(
$this->class,
$fields,
$filters
);
}

/**
* @param Member $member
*
Expand Down
158 changes: 126 additions & 32 deletions code/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,42 @@ public static function getPageCategories($id = null)

return false;
}

public static function locations(
$filter = array(),
$filterAny = array(),
$exclude = array(),
$filterByCallback = null
) {
$locationsList = ArrayList::create();

$locations = Location::get()->filter($filter);

if (!empty($filterAny)) {
$locations = $locations->filterAny($filterAny);
}
if (!empty($exclude)) {
$locations = $locations->exclude($exclude);
}

if ($filterByCallback !== null && is_callable($filterByCallback)) {
$locations = $locations->filterByCallback($filterByCallback);
}

if ($locations->exists()) {
$locationsList->merge($locations);
}

return $locationsList;
}
}

class Locator_Controller extends Page_Controller
{
// allowed actions
private static $allowed_actions = array('xml');
private static $allowed_actions = array(
'xml',
);

// Set Requirements based on input from CMS
public function init()
Expand Down Expand Up @@ -137,7 +167,14 @@ public function init()

$kilometer = ($this->data()->Unit == 'km') ? 'lengthUnit: "km"' : 'lengthUnit: "m"';

$link = $this->Link().'xml.xml';
// pass GET variables to xml action
$vars = $this->request->getVars();
unset($vars['url']);
$url = '';
if (count($vars)) {
$url .= '?'.http_build_query($vars);
}
$link = $this->Link().'xml.xml'.$url;

// init map
if (Locator::getLocations()) {
Expand All @@ -154,9 +191,10 @@ public function init()
slideMap: false,
zoomLevel: 0,
distanceAlert: 120,
noForm: true,
formID: 'Form_LocationSearch',
inputID: 'Form_LocationSearch_address',
categoryID: 'Form_LocationSearch_category',
inputID: 'Form_LocationSearch_Address',
categoryID: 'Form_LocationSearch_CategoryID',
distanceAlert: -1,
".$kilometer.'
});
Expand All @@ -166,34 +204,71 @@ public function init()
}

/**
* Find all locations for map.
*
* Will return a XML feed of all locations marked "show in locator".
* @param SS_HTTPRequest $request
*
* @return XML file
* @return ViewableData_Customised
*/
public function index(SS_HTTPRequest $request)
{
$locations = $this->Items($request);

return $this->customise(array(
'Locations' => $locations,
));
}

/**
* Return a XML feed of all locations marked "show in locator"
*
* @todo rename/refactor to allow for json/xml
* @todo allow $filter to run off of getVars key/val pair
* @param SS_HTTPRequest $request
* @return HTMLText
*/
public function xml(SS_HTTPRequest $request)
{
$locations = $this->Items($request);

return $this->customise(array(
'Locations' => $locations,
))->renderWith('LocationXML');
}

/**
* @param array $searchCriteria
*
* @return mixed
*/
public function Items($searchCriteria = array())
{
$request = ($this->request) ? $this->request : $this->parentController->getRequest();
if (empty($searchCriteria)) {
$searchCriteria = $request->requestVars();
}

$filter = array();
$exclude = array();
$filterAny = array();
$exclude = array();

//if a category filter selected
if ($this->Categories()->exists()) {
$categories = $this->Categories();
foreach ($categories as $category) {
$filterAny['CategoryID'] = $category->ID;
}
// only show locations marked as ShowInLocator
$filter['ShowInLocator'] = 1;

// search across all address related fields
$address = ($request->getVar('Address')) ? $request->getVar('Address') : false;
if ($address) {
$filterAny['Address:PartialMatch'] = $address;
$filterAny['Suburb:PartialMatch'] = $address;
$filterAny['State:PartialMatch'] = $address;
$filterAny['Postcode:PartialMatch'] = $address;
$filterAny['Country:PartialMatch'] = $address;
}

$Locations = Locator::getLocations($filter, $exclude, $filterAny);
$category = ($request->getVar('CategoryID')) ? $request->getVar('CategoryID') : false;
if ($category) {
$filter['CategoryID'] = $category;
}

return $this->customise(array(
'Locations' => $Locations,
))->renderWith('LocationXML');
$locations = Locator::locations($filter, $filterAny, $exclude);

return $locations;
}

/**
Expand All @@ -206,29 +281,48 @@ public function xml(SS_HTTPRequest $request)
public function LocationSearch()
{
$fields = FieldList::create(
$address = TextField::create('address', '')
$address = TextField::create('Address', '')
->setAttribute('placeholder', 'address or zip code')
);
$address->setAttribute('placeholder', 'address or zip code');

$locatorCategories = Locator::getPageCategories($this->ID);

if (LocationCategory::get()->Count() > 0 && $locatorCategories && $locatorCategories->Count() != 1) {
$categories = LocationCategory::get();
$filterCategories = Locator::getPageCategories($this->ID);
$allCategories = LocationCategory::get();

if ($allCategories->Count() > 0) {
$categories = ArrayList::create();
if ($filterCategories->Count() > 0) {
if ($filterCategories->Count() != 1) {
$categories = $filterCategories;
}
} else {
$categories = $allCategories;
}

if ($categories->count() > 0) {
$fields->push(
DropdownField::create(
'category',
'CategoryID',
'',
$categories->map('Title', 'Title')
)->setEmptyString('Select Category'));
$categories->map()
)->setEmptyString('All Categories'));
}
}

$actions = FieldList::create(
FormAction::create('', 'Search')
FormAction::create('index', 'Search')
);

return Form::create($this, 'LocationSearch', $fields, $actions);
if (class_exists('BootstrapForm')) {
$form = BootstrapForm::create($this, 'LocationSearch', $fields, $actions);
} else {
$form = Form::create($this, 'LocationSearch', $fields, $actions);
}

return $form
->setFormMethod('GET')
->setFormAction($this->Link())
->disableSecurityToken()
->loadDataFrom($this->request->getVars())
;
}
}
1 change: 1 addition & 0 deletions templates/Layout/Locator.ss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="content-container unit size3of4 lastUnit">
<h1>$Title</h1>
<% if $Content %><div class="typography">$Content</div><% end_if %>
<p>$Locations.Count locations</p>
<% if $AreLocations %>
<div id="form-container">
$LocationSearch
Expand Down
1 change: 1 addition & 0 deletions tests/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testFieldLabels()
'Category' => 'Category',
'ShowInLocator.NiceAsBoolean' => 'Show',
'Category.Name' => 'Category',
'Category.ID' => 'Category',
'Featured.NiceAsBoolean' => 'Featured',
'Coords' => 'Coords',
);
Expand Down
2 changes: 1 addition & 1 deletion tests/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public function testLocationSearch()

$form = $object->LocationSearch();
$fields = $form->Fields();
$this->assertNotNull($fields->fieldByName('category'));
$this->assertNotNull($fields->fieldByName('CategoryID'));
}
}

0 comments on commit 96ebf04

Please sign in to comment.