generated from silverstripe/silverstripe-module
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
244 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Dynamic\Locations\Model\LocationCategory: | ||
extensions: | ||
- Dynamic\Elements\Locaitons\Extension\LocationCategoryDataExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Dynamic\Elements\Locations\Elements; | ||
|
||
use SilverStripe\ORM\ArrayList; | ||
use SilverStripe\Forms\FieldList; | ||
use Dynamic\Locations\Model\Location; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
use DNADesign\Elemental\Models\BaseElement; | ||
use Dynamic\Locations\Model\LocationCategory; | ||
use SilverStripe\TagField\TagField; | ||
|
||
/** | ||
* Class \Dynamic\Elements\Locations\Elements\ElementLocations | ||
* | ||
* @method ManyManyList|LocationCategory[] Categories() | ||
*/ | ||
class ElementLocations extends BaseElement | ||
{ | ||
/** | ||
* @var string | ||
* @config | ||
*/ | ||
private static string $table_name = 'ElementLocations'; | ||
|
||
/** | ||
* @var string | ||
* @config | ||
*/ | ||
private static string $icon = 'font-icon-globe'; | ||
|
||
/** | ||
* @var array | ||
* @config | ||
*/ | ||
private static array $db = [ | ||
|
||
]; | ||
|
||
/** | ||
* @var array | ||
* @config | ||
*/ | ||
private static array $many_many = [ | ||
'Categories' => 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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Dynamic\Elements\Locaitons\Extension; | ||
|
||
use Dynamic\Elements\Locations\Elements\ElementLocations; | ||
use SilverStripe\ORM\DataExtension; | ||
|
||
/** | ||
* Class \Dynamic\Elements\Locaitons\Extension\LocationCategoryDataExtension | ||
* | ||
* @property LocationCategory|LocationCategoryDataExtension $owner | ||
* @method ManyManyList|ElementLocations[] ElementLocations() | ||
*/ | ||
class LocationCategoryDataExtension extends DataExtension | ||
{ | ||
/** | ||
* @var array | ||
* @config | ||
*/ | ||
private static array $belongs_many_many = [ | ||
'ElementLocations' => ElementLocations::class, | ||
]; | ||
} |
2 changes: 2 additions & 0 deletions
2
templates/Dynamic/Elements/Locations/Elements/ElementLocations.ss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<% if $Title && $ShowTitle %><h2 class="element__title">$Title</h2><% end_if %> | ||
<% if $Content %><div class="element__content">$Content</div><% end_if %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
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 | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected static $fixture_file = 'ElementLocationsTest.yml'; | ||
|
||
/** | ||
* | ||
*/ | ||
public function testGetCMSFields(): void | ||
{ | ||
$object = $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|