mirror of
https://github.com/a2nt/silverstripe-elemental-basics.git
synced 2024-10-22 17:05:54 +02:00
IMPR: minor
This commit is contained in:
parent
e56511d1e6
commit
7eaf7436b5
160
src/Extensions/SliderElement.php
Executable file
160
src/Extensions/SliderElement.php
Executable file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: tony
|
||||
* Date: 6/30/18
|
||||
* Time: 11:54 PM
|
||||
*/
|
||||
|
||||
namespace A2nt\ElementalBasics\Elements;
|
||||
|
||||
use Colymba\BulkUpload\BulkUploader;
|
||||
use Dynamic\Elements\Flexslider\Elements\ElementSlideshow;
|
||||
use Dynamic\FlexSlider\Model\SlideImage;
|
||||
use Dynamic\FlexSlider\ORM\FlexSlider;
|
||||
use SilverStripe\Assets\Image;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Forms\CheckboxField;
|
||||
use SilverStripe\Forms\LiteralField;
|
||||
use SilverStripe\Forms\NumericField;
|
||||
use SilverStripe\Forms\ReadonlyField;
|
||||
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
||||
|
||||
class SliderElement extends ElementSlideshow
|
||||
{
|
||||
private static $singular_name = 'Slider';
|
||||
|
||||
private static $plural_name = 'Sliders';
|
||||
|
||||
private static $description = 'Displays slide show';
|
||||
|
||||
private static $table_name = 'SliderElement';
|
||||
|
||||
private static $slide_width = 2140;
|
||||
private static $slide_height = 700;
|
||||
|
||||
private static $db = [
|
||||
'Interval' => '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) {
|
||||
$config = $grid->getConfig();
|
||||
|
||||
$bulk = new BulkUploader('Image', SlideImage::class, false);
|
||||
$bulk
|
||||
->setUfSetup('setFolderName', 'Uploads/SlideImages')
|
||||
->setUfSetup('getValidator.setAllowedExtensions', ['jpg', 'jpeg', 'png', 'gif']);
|
||||
$config->addComponent($bulk);
|
||||
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user