diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 488aba4..c6c1eee 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -8,6 +8,6 @@ - + diff --git a/src/Extension/CarouselPageExtension.php b/src/Extension/CarouselPageExtension.php index eb8c1bb..329f2a6 100644 --- a/src/Extension/CarouselPageExtension.php +++ b/src/Extension/CarouselPageExtension.php @@ -5,23 +5,42 @@ use SilverStripe\View\SSViewer; use Dynamic\Carousel\Model\Slide; use SilverStripe\ORM\DataExtension; +use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\GridField\GridField; -use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; +use SilverStripe\Forms\ToggleCompositeField; use SilverStripe\Forms\GridField\GridFieldAddNewButton; +use SilverStripe\Forms\GridField\GridFieldFilterHeader; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; -use SilverStripe\Forms\GridField\GridFieldFilterHeader; use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; +use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; +use SilverStripe\Forms\NumericField; /** * Class \Dynamic\Carousel\Extension\CarouselPageExtension * * @property BlockPage|CampaignLandingPage|HomePage|CarouselPageExtension $owner + * @property string $Controls + * @property string $Indicators + * @property string $Transitions + * @property string $Autoplay + * @property int $Interval * @method ManyManyList|Slide[] Slides() */ class CarouselPageExtension extends DataExtension { + /** + * @var array + */ + private static $db = [ + 'Controls' => 'Enum("Off,On", "On")', + 'Indicators' => 'Enum("Off,On", "On")', + 'Transitions' => 'Enum("Slide,Fade", "Slide")', + 'Autoplay' => 'Enum("Off,Autoplay after interaction,On","Off")', + 'Interval' => 'Int' + ]; + /** * @var array */ @@ -38,6 +57,13 @@ class CarouselPageExtension extends DataExtension ], ]; + /** + * @var array + */ + private static $defaults = [ + 'Interval' => 5 + ]; + /** * @param \SilverStripe\Forms\FieldList $fields * @return void @@ -62,7 +88,44 @@ public function updateCMSFields(\SilverStripe\Forms\FieldList $fields) new GridFieldAddExistingSearchButton(), ]); - $fields->addFieldToTab('Root.Hero', $grid); + $settings = ToggleCompositeField::create('CarouselSettingsHD', 'Settings', [ + DropdownField::create('Controls', 'Show Controls', $this->owner->dbObject('Controls')->enumValues()) + ->setDescription('Previous/next arrows. Hidden if only one slide'), + DropdownField::create('Indicators', 'Show Indicators', $this->owner->dbObject('Indicators')->enumValues()) + ->setDescription(' Let users jump directly to a particular slide. Hidden if only one slide'), + DropdownField::create('Transitions', 'Transitions', $this->owner->dbObject('Transitions')->enumValues()), + DropdownField::create('Autoplay', 'Autoplay', $this->owner->dbObject('Autoplay')->enumValues()), + NumericField::create('Interval') + ->setDescription('Time in seconds'), + ])->setHeadingLevel(4); + + $fields->addFieldsToTab('Root.Hero', [ + $grid, + $settings, + ]); + } + + /** + * @return string + */ + public function onBeforeWrite() + { + if (!$this->owner->Interval || $this->owner->Interval < 0) { + $this->owner->Interval = self::$defaults['Interval']; + } + parent::onBeforeWrite(); + } + + /** + * @return string + */ + public function IntervalInMilliseconds(): int + { + $interval = $this->owner->Interval; + if (!$this->owner->Interval || $this->owner->Interval < 0) { + $interval = self::$defaults['Inverval']; + } + return (int) $interval * 1000; } /** diff --git a/templates/Includes/Carousel.ss b/templates/Includes/Carousel.ss index 40ba7b0..01d761c 100644 --- a/templates/Includes/Carousel.ss +++ b/templates/Includes/Carousel.ss @@ -1,6 +1,9 @@ <% if $Slides %> -