Skip to content

Commit

Permalink
Locator - allow filter by LocationCategory
Browse files Browse the repository at this point in the history
In CMS Settings tab on Locator page, added following:

many_many LocationCategory

If a user selects one or more categories, list is filtered by those categories on load, and category drop down is removed.

Allows module to have multiple Locator pages in a site and show different locations on each.

fixes #57
  • Loading branch information
jsirish committed Apr 7, 2016
1 parent 5c89951 commit f39fcf3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
8 changes: 6 additions & 2 deletions code/LocationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ class LocationCategory extends DataObject
'Locations' => 'Location',
);

private static $singular_name = 'Category';
private static $plural_name = 'Categories';
private static $belogs_many_many = array(
'Locators' => 'Locator'
);

private static $singular_name = "Category";
Private static $plural_name = "Categories";

private static $default_sort = 'Name';
}
75 changes: 52 additions & 23 deletions code/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@ class Locator extends Page
'Unit' => 'Enum("m,km","m")',
);

private static $many_many = array(
'Categories' => 'LocationCategory',
);

private static $defaults = array(
'AutoGeocode' => true,
);

private static $singular_name = 'Locator';
private static $plural_name = 'Locators';
private static $description = 'Show locations on a map';
private static $description = 'Find locations on a map';

public function getCMSFields()
{
$fields = parent::getCMSFields();

// Locations Grid Field
$config = GridFieldConfig_RecordEditor::create();
$locations = Location::get();
$fields->addFieldToTab('Root.Locations', GridField::create('Locations', 'Locations', $locations, $config));

// Location categories
$config = GridFieldConfig_RecordEditor::create();
$fields->addFieldToTab('Root.Categories', GridField::create('Categories', 'Categories', LocationCategory::get(), $config));

// Settings
$fields->addFieldsToTab('Root.Settings', array(
HeaderField::create('DisplayOptions', 'Display Options', 3),
Expand All @@ -38,19 +33,35 @@ public function getCMSFields()
CheckboxField::create('ModalWindow', 'Modal Window - Show Map results in a modal window'),
));

// Filter categories
$config = GridFieldConfig_RelationEditor::create();
if (class_exists('GridFieldAddExistingSearchButton')) {
$config->removeComponentsByType('GridFieldAddExistingAutocompleter');
$config->addComponent(new GridFieldAddExistingSearchButton());
}
$categories = $this->Categories();
$categoriesField = GridField::create('Categories', 'Categories', $categories, $config)
->setDescription('only show locations from the selected category');

// Filter
$fields->addFieldsToTab('Root.Filter', array(
HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3),
$categoriesField,
));

$this->extend('updateCMSFields', $fields);

return $fields;
}

public static function getLocations($filter = array(), $exclude = array())
public static function getLocations($filter = array(), $exclude = array(), $filterAny = array())
{
$filter['ShowInLocator'] = true;
$exclude['Lat'] = 0;

return Location::get()
->exclude($exclude)
->exclude('Lat', 0)
->filter($filter);
$Locations = Location::get()->exclude($exclude)->filter($filter)->filterAny($filterAny);

return $Locations;
}

public function getAreLocations()
Expand All @@ -62,6 +73,17 @@ public function getAllCategories()
{
return LocationCategory::get();
}

public static function getPageCategories($id = null)
{
if ($id) {
if ($locator = Locator::get()->byID($id)) {
return $locator->Categories();
}
return false;
}
return false;
}
}

class Locator_Controller extends Page_Controller
Expand Down Expand Up @@ -151,7 +173,18 @@ public function init()
public function xml(SS_HTTPRequest $request)
{
$filter = array();
$Locations = Locator::getLocations($filter);
$exclude = array();
$filterAny = array();

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

$Locations = Locator::getLocations($filter, $exclude, $filterAny);

return $this->customise(array(
'Locations' => $Locations,
Expand All @@ -172,14 +205,10 @@ public function LocationSearch()
);
$address->setAttribute('placeholder', 'address or zip code');

if (LocationCategory::get()->Count() > 0) {
$filter = array();
$locals = Locator::getLocations($filter, $exclude = array('CategoryID' => 0));
$categories = ArrayList::create();
$locatorCategories = Locator::getPageCategories($this->ID);

foreach ($locals as $local) {
$categories->add($local->Category());
}
if (LocationCategory::get()->Count() > 0 && $locatorCategories && $locatorCategories->Count() < 1) {
$categories = LocationCategory::get();

if ($categories->count() > 0) {
$fields->push(
Expand Down

0 comments on commit f39fcf3

Please sign in to comment.