diff --git a/src/Elements/SliderElement.php b/src/Elements/SliderElement.php new file mode 100755 index 0000000..032c26c --- /dev/null +++ b/src/Elements/SliderElement.php @@ -0,0 +1,186 @@ + 'Int', + 'SlidesInRow' => 'Int', + 'ImageOriginalSize' => 'Boolean(0)', + ]; + + private static $extensions = [ + FlexSlider::class, + ]; + + private static $owns = [ + 'Slides', + ]; + + private $items; + + public function getType() + { + return self::$singular_name; + } + + protected function ratioSize($size) + { + $count = $this->SlidesInRow; + return ($count > 1) ? $size / $count : $size; + } + + public function getSlideWidth() + { + if ($this->getField('ImageOriginalSize')) { + return null; + } + + return $this->ratioSize(self::config()->get('slide_width')); + } + + public function getSlideHeight() + { + if ($this->getField('ImageOriginalSize')) { + return null; + } + + return $this->ratioSize(self::config()->get('slide_height')); + } + + public function getCMSFields() + { + $fields = parent::getCMSFields(); + + // remove in case you don't need to provide this functionality + $fields->removeByName([ + 'ConfigHD', + 'Animation', + 'Loop', + 'Animate', + 'ThumbnailNav', + 'SliderControlNav', + 'SliderDirectionNav', + 'CarouselControlNav', + 'CarouselDirectionNav', + 'CarouselThumbnailCt', + ]); + + $fields->addFieldsToTab('Root.Settings', [ + CheckboxField::create('ImageOriginalSize', 'Use original image size'), + NumericField::create('Interval', 'Auto-play Interval (sec)'), + NumericField::create('SlidesInRow'), + ]); + + $grid = $fields->dataFieldByName('Slides'); + if ($grid) { + $fields->insertBefore('Slides', LiteralField::create( + 'SlidesNote', + '
Note: to show hidden slide open slide item and uncheck "Hide" checkbox
' + )); + + $config = $grid->getConfig(); + $config->removeComponentsByType(GridField_ActionMenu::class); + + $bulk = new BulkUploader('Image', SlideImage::class, false); + $bulk + ->setUfSetup('setFolderName', 'Uploads/SlideImages'); + //->setUfSetup('getValidator.setAllowedExtensions', ['jpg', 'jpeg', 'png', 'gif']); + $config->addComponent($bulk); + $config->addComponent(new \Colymba\BulkManager\BulkManager()); + + $columns = new GridFieldEditableColumns(); + $columns->setDisplayFields([ + 'Hide' => [ + 'title' => 'Hide it?', + 'field' => CheckboxField::class, + ], + ]); + + $config->addComponent($columns); + } + + return $fields; + } + + /** + * @return mixed + */ + public function getSlideShow() + { + if ($this->items) { + return $this->items; + } + + $date = date('Y-m-d H:i:s'); + $this->items = $this->Slides()->filter([ + 'Hide' => '0', + ])->filterByCallback(static function ($item, $list) use ($date) { + $on = $item->getField('DateOn'); + $off = $item->getField('DateOff'); + + return ($on <= $date) && (!$off || $off > $date); + })->sort('SortOrder'); + + return $this->items; + } + + public function onBeforeWrite() + { + parent::onBeforeWrite(); + + if (!$this->getField('Interval')) { + $this->setField('Interval', 5000); + } + } +}