Skip to content

Commit

Permalink
FEATURE initial class creation (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirish authored Sep 12, 2024
1 parent 657a800 commit 42c4e16
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 4 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dynamic\Locations\Model\LocationCategory:
extensions:
- Dynamic\Elements\Locaitons\Extension\LocationCategoryDataExtension
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
110 changes: 110 additions & 0 deletions src/Element/ElementLocations.php
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');
}
}
23 changes: 23 additions & 0 deletions src/Extension/LocationCategoryDataExtension.php
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,
];
}
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 %>
74 changes: 74 additions & 0 deletions tests/Element/ElementLocationsTest.php
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);
}
}
28 changes: 28 additions & 0 deletions tests/Element/ElementLocationsTest.yml
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

0 comments on commit 42c4e16

Please sign in to comment.