From 7b7325c598baaed23f0ddf24eb4a207e09f09efa Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Wed, 11 Sep 2024 11:18:51 -0500 Subject: [PATCH] add filter by LocationCategory --- composer.json | 3 +- src/Element/ElementLocations.php | 45 ++++++++++++++++++++------ tests/Element/ElementLocationsTest.php | 42 ++++++++++++++++++++++++ tests/Element/ElementLocationsTest.yml | 27 +++++++++++++++- 4 files changed, 106 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index e74ea30..c0a565f 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,8 @@ "CMS" ], "require": { - "dnadesign/silverstripe-elemental": "^5.0" + "dnadesign/silverstripe-elemental": "^5.0", + "dynamic/silverstripe-locations": "^1.0" }, "require-dev": { "silverstripe/recipe-testing": "^3" diff --git a/src/Element/ElementLocations.php b/src/Element/ElementLocations.php index 986447e..d0085da 100644 --- a/src/Element/ElementLocations.php +++ b/src/Element/ElementLocations.php @@ -2,12 +2,17 @@ namespace Dynamic\Elements\Locations\Elements; +use SilverStripe\ORM\ArrayList; +use Dynamic\Locations\Model\Location; use SilverStripe\ORM\FieldType\DBField; use DNADesign\Elemental\Models\BaseElement; +use Dynamic\Locations\Model\LocationCategory; /** * Class \Dynamic\Elements\Locations\Elements\ElementLocations * + * @property int $CategoryID + * @method LocationCategory Category() */ class ElementLocations extends BaseElement { @@ -21,32 +26,54 @@ class ElementLocations extends BaseElement * @var string * @config */ - private static string $singular_name = 'Locations'; + private static string $icon = 'font-icon-globe'; /** - * @var string + * @var array * @config */ - private static string $plural_name = 'Locations'; + private static array $db = [ + + ]; /** - * @var string + * @var array * @config */ - private static string $description = 'A locations element'; + private static array $has_one = [ + 'Category' => LocationCategory::class, + ]; /** - * @var string - * @config + * return ArrayList */ - private static string $icon = 'font-icon-globe'; + public function getLocationsList() + { + $locations = ArrayList::create(); + + if ($this->CategoryID && $category = LocationCategory::get()->byID($this->CategoryID)) { + $locations = $category->Locations(); + } else { + $locations = Location::get(); + } + + $this->extend('updateGetLocationsList', $locations); + + return $locations; + } /** * @return string */ public function getSummary(): string { - return DBField::create_field('HTMLText', 'Locations')->Summary(20); + $count = $this->getLocationsList()->count(); + $label = _t( + Location::class . '.PLURALS', + '1 Location|{count} Locations', + [ 'count' => $count ] + ); + return DBField::create_field('HTMLText', $label)->Summary(20); } /** diff --git a/tests/Element/ElementLocationsTest.php b/tests/Element/ElementLocationsTest.php index 3d7bae3..11350db 100644 --- a/tests/Element/ElementLocationsTest.php +++ b/tests/Element/ElementLocationsTest.php @@ -2,8 +2,10 @@ namespace Dynamic\Elements\Locations\Test\Elements; +use SilverStripe\ORM\DataList; use SilverStripe\Forms\FieldList; use SilverStripe\Dev\SapphireTest; +use Dynamic\Locations\Model\Location; use Dynamic\Elements\Locations\Elements\ElementLocations; class ElementLocationsTest extends SapphireTest @@ -22,4 +24,44 @@ public function testGetCMSFields(): void $fields = $object->getCMSFields(); $this->assertInstanceOf(FieldList::class, $fields); } + + /** + * + */ + public function testGetSummary() + { + $object = $this->objFromFixture(ElementLocations::class, 'one'); + $count = $object->getLocationsList()->count(); + $this->assertEquals( + $object->getSummary(), + _t( + Location::class . 'PLURALS', + 'A Location|{count} Locations', + ['count' => $count] + ) + ); + } + + /** + * + */ + public function testGetLocationsList(): void + { + $object = $this->objFromFixture(ElementLocations::class, 'two'); + $this->compareList( + DataList::create(Location::class)->filter('Categories.ID', $object->CategoryID), + $object->getLocationsList(), + 'Should only return locations assigned to location category' + ); + } + + /** + * + */ + private function compareList(DataList $expected, DataList $actual, $message = ''): void + { + $expectedArray = $expected->map('ID', 'ClassName')->toArray(); + $actualArray = $expected->map('ID', 'ClassName')->toArray(); + $this->assertEquals($expectedArray, $actualArray, $message); + } } diff --git a/tests/Element/ElementLocationsTest.yml b/tests/Element/ElementLocationsTest.yml index 03852cb..30d7619 100644 --- a/tests/Element/ElementLocationsTest.yml +++ b/tests/Element/ElementLocationsTest.yml @@ -1,3 +1,28 @@ +Dynamic\Locations\Model\LocationCategory: + one: + Title: "Category 1" + +Dynamic\Locations\Model\Location: + one: + Title: "Location 1" + Address: "123 Fake St" + City: "Springfield" + State: "IL" + Zip: "62701" + Country: "US" + two: + Title: "Location 2" + Address: "456 Fake St" + City: "Springfield" + State: "IL" + Zip: "62701" + Country: "US" + CategoryID: =>Dynamic\Locations\Model\LocationCategory.one + Dynamic\Elements\Locations\Elements\ElementLocations: one: - Title: "Locations" \ No newline at end of file + Title: "Locations" + two: + Title: "Locations 2" + CategoryID: =>Dynamic\Locations\Model\LocationCategory.one +