From f39fcf38e365d176ea36eba5fac5a1b7445ad0a4 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Tue, 15 Mar 2016 20:53:14 -0500 Subject: [PATCH 1/3] Locator - allow filter by LocationCategory 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 --- code/LocationCategory.php | 8 +++-- code/Locator.php | 75 +++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/code/LocationCategory.php b/code/LocationCategory.php index 6be5bbb..283ce3d 100644 --- a/code/LocationCategory.php +++ b/code/LocationCategory.php @@ -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'; } diff --git a/code/Locator.php b/code/Locator.php index b9439a4..15dc317 100644 --- a/code/Locator.php +++ b/code/Locator.php @@ -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), @@ -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() @@ -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 @@ -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, @@ -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( From db1019a20b52dff2de0187f0bb1d96a53ea6143d Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Thu, 7 Apr 2016 16:35:17 -0500 Subject: [PATCH 2/3] Codecov check --- code/Locator.php | 2 +- tests/LocatorTest.php | 42 ++++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/code/Locator.php b/code/Locator.php index 15dc317..e11ad76 100644 --- a/code/Locator.php +++ b/code/Locator.php @@ -207,7 +207,7 @@ public function LocationSearch() $locatorCategories = Locator::getPageCategories($this->ID); - if (LocationCategory::get()->Count() > 0 && $locatorCategories && $locatorCategories->Count() < 1) { + if (LocationCategory::get()->Count() > 0 && $locatorCategories && $locatorCategories->Count() != 1) { $categories = LocationCategory::get(); if ($categories->count() > 0) { diff --git a/tests/LocatorTest.php b/tests/LocatorTest.php index b0fd968..e7169f2 100644 --- a/tests/LocatorTest.php +++ b/tests/LocatorTest.php @@ -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); } @@ -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() @@ -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')); + } } From 5bd411635246cf4f455c23a1d2532076ed13cd21 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Thu, 7 Apr 2016 16:48:44 -0500 Subject: [PATCH 3/3] Travis tweak --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4138cd3..da9a622 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,8 +29,6 @@ matrix: env: DB=SQLITE - php: hhvm env: DB=MYSQL - - php: 5.5 - env: DB=MYSQL allow_failures: - php: 7.0