Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locator - allow filter by LocationCategory #67

Merged
merged 3 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ matrix:
env: DB=SQLITE
- php: hhvm
env: DB=MYSQL
- php: 5.5
env: DB=MYSQL
allow_failures:
- php: 7.0

Expand Down
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
42 changes: 24 additions & 18 deletions tests/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,8 @@ public function testGetCMSFields()

public function testGetLocations()
{
$object = $this->objFromFixture('Location', 'dynamic');
$object->write();

$object2 = $this->objFromFixture('Location', 'silverstripe');
$object2->write();

$object3 = $this->objFromFixture('Location', '3sheeps');
$object3->write();

$locator = singleton('Locator');
$count = Location::get()->filter('ShowInLocator', 1)->exclude('Lat', 0)->Count();

$this->assertEquals($locator->getLocations()->Count(), $count);
}

Expand All @@ -35,16 +25,21 @@ public function testGetAreLocations()
public function testGetAllCategories()
{
$locator = singleton('Locator');
$count = LocationCategory::get();
$this->assertEquals($locator->getAllCategories(), $count);
}

$object = $this->objFromFixture('LocationCategory', 'service');
$object->write();

$object2 = $this->objFromFixture('LocationCategory', 'manufacturing');
$object2->write();
public function testGetPageCategories()
{
$locator = Locator::create();
$this->assertFalse($locator->getPageCategories());

$count = LocationCategory::get();
$this->assertFalse($locator->getPageCategories(500));

$this->assertEquals($locator->getAllCategories(), $count);
$locator->write();
$category = $this->objFromFixture('LocationCategory', 'service');
$locator->Categories()->add($category);
$this->assertEquals($locator->getPageCategories($locator->ID), $locator->Categories());
}

public function testInit()
Expand All @@ -57,8 +52,19 @@ public function testXml()

public function testLocationSearch()
{
$object = Locator_Controller::create();
$locator = $this->objFromFixture('Locator', 'locator1');
$object = Locator_Controller::create($locator);
$form = $object->LocationSearch();
$this->assertTrue(is_a($form, 'Form'));

$category = $this->objFromFixture('LocationCategory', 'service');
$category2 = $this->objFromFixture('LocationCategory', 'manufacturing');
$locator->Categories()->add($category);
$locator->Categories()->add($category2);

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

}
}