diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19f7e63..7a973e2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,4 @@ on: jobs: ci: name: CI - uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 - with: - phpcoverage: true + uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 \ No newline at end of file diff --git a/_config/config.yml b/_config/config.yml index e69de29..2f8912d 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -0,0 +1,3 @@ +Dynamic\Locations\Model\LocationCategory: + extensions: + - Dynamic\Elements\Locaitons\Extension\LocationCategoryDataExtension \ No newline at end of file diff --git a/composer.json b/composer.json index e74ea30..079838f 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,9 @@ "CMS" ], "require": { - "dnadesign/silverstripe-elemental": "^5.0" + "dnadesign/silverstripe-elemental": "^5.0", + "dynamic/silverstripe-locations": "^1.0", + "silverstripe/tagfield": "^3.0" }, "require-dev": { "silverstripe/recipe-testing": "^3" diff --git a/src/Element/ElementLocations.php b/src/Element/ElementLocations.php new file mode 100644 index 0000000..58d0bf7 --- /dev/null +++ b/src/Element/ElementLocations.php @@ -0,0 +1,110 @@ + LocationCategory::class, + ]; + + /** + * @return FieldList + */ + public function getCMSFields(): FieldList + { + $this->beforeUpdateCMSFields(function (FieldList $fields) { + + $fields->removeByName('Categories'); + + $fields->addFieldToTab( + 'Root.Main', + TagField::create( + 'Categories', + 'Categories', + LocationCategory::get(), + $this->Categories() + ) + ); + }); + + return parent::getCMSFields(); + } + + /** + * return ArrayList + */ + public function getLocationsList() + { + $locations = ArrayList::create(); + + if ($this->Categories()->count()) { + $locations = Location::get()->filter('Categories.ID', $this->Categories()->column()); + } else { + $locations = Location::get(); + } + + $this->extend('updateGetLocationsList', $locations); + + return $locations; + } + + /** + * @return string + */ + public function getSummary(): string + { + $count = $this->getLocationsList()->count(); + $label = _t( + Location::class . '.PLURALS', + '1 Location|{count} Locations', + [ 'count' => $count ] + ); + return DBField::create_field('HTMLText', $label)->Summary(20); + } + + /** + * @return string + */ + public function getType(): string + { + return _t(__CLASS__ . '.BlockType', 'Locations'); + } +} diff --git a/src/Extension/LocationCategoryDataExtension.php b/src/Extension/LocationCategoryDataExtension.php new file mode 100644 index 0000000..6a5f1da --- /dev/null +++ b/src/Extension/LocationCategoryDataExtension.php @@ -0,0 +1,23 @@ + ElementLocations::class, + ]; +} diff --git a/templates/Dynamic/Elements/Locations/Elements/ElementLocations.ss b/templates/Dynamic/Elements/Locations/Elements/ElementLocations.ss new file mode 100644 index 0000000..dbd042d --- /dev/null +++ b/templates/Dynamic/Elements/Locations/Elements/ElementLocations.ss @@ -0,0 +1,2 @@ +<% if $Title && $ShowTitle %>

$Title

<% end_if %> +<% if $Content %>
$Content
<% end_if %> \ No newline at end of file diff --git a/tests/Element/ElementLocationsTest.php b/tests/Element/ElementLocationsTest.php new file mode 100644 index 0000000..22da426 --- /dev/null +++ b/tests/Element/ElementLocationsTest.php @@ -0,0 +1,74 @@ +objFromFixture(ElementLocations::class, 'one'); + $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, 'one'); + $this->compareList( + DataList::create(Location::class), + $object->getLocationsList(), + 'Should return all locations as not being filtered by location category' + ); + + $object = $this->objFromFixture(ElementLocations::class, 'two'); + $this->compareList( + DataList::create(Location::class)->filter('Categories.ID', $object->Categories()->column()), + $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 new file mode 100644 index 0000000..2210845 --- /dev/null +++ b/tests/Element/ElementLocationsTest.yml @@ -0,0 +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" + Categories: =>Dynamic\Locations\Model\LocationCategory.one + +Dynamic\Elements\Locations\Elements\ElementLocations: + one: + Title: "Locations" + two: + Title: "Locations 2" + Categories: =>Dynamic\Locations\Model\LocationCategory.one +