Skip to content

Commit

Permalink
add filter by LocationCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirish committed Sep 11, 2024
1 parent 482463b commit 7b7325c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 36 additions & 9 deletions src/Element/ElementLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Element/ElementLocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
27 changes: 26 additions & 1 deletion tests/Element/ElementLocationsTest.yml
Original file line number Diff line number Diff line change
@@ -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"
Title: "Locations"
two:
Title: "Locations 2"
CategoryID: =>Dynamic\Locations\Model\LocationCategory.one

0 comments on commit 7b7325c

Please sign in to comment.