silverstripe-webpack/app/src/Elements/SliderElement.php

103 lines
2.4 KiB
PHP
Raw Normal View History

2018-07-25 11:03:50 +02:00
<?php
/**
* Created by PhpStorm.
* User: tony
* Date: 6/30/18
* Time: 11:54 PM
*/
namespace Site\Elements;
use Dynamic\Elements\Flexslider\Elements\ElementSlideshow;
2018-12-19 19:48:04 +01:00
use Dynamic\FlexSlider\Model\SlideImage;
2018-07-25 11:03:50 +02:00
use Dynamic\FlexSlider\ORM\FlexSlider;
2018-12-19 19:48:04 +01:00
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\ReadonlyField;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
2018-07-25 11:03:50 +02:00
class SliderElement extends ElementSlideshow
{
private static $singular_name = 'Slider';
private static $plural_name = 'Sliders';
private static $description = 'Displays slideshow';
2018-07-26 08:28:49 +02:00
private static $table_name = 'SliderElement';
2018-07-25 11:03:50 +02:00
private static $extensions = [
FlexSlider::class,
];
2018-12-19 19:48:04 +01:00
private static $owns = [
'Slides',
];
private $items;
2018-07-25 11:03:50 +02:00
public function getType()
{
return self::$singular_name;
}
2018-12-19 19:48:04 +01:00
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',
]);
$grid = $fields->dataFieldByName('Slides');
2019-04-11 00:15:29 +02:00
if ($grid) {
$config = $grid->getConfig();
$columns = new GridFieldEditableColumns();
$columns->setDisplayFields([
'Hide' => [
'title' => 'Hide it?',
'field' => CheckboxField::class,
],
]);
$config->addComponent($columns);
}
2018-12-19 19:48:04 +01:00
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(function($item, $list) use ($date) {
$on = $item->getField('DateOn');
$off = $item->getField('DateOff');
return ($on <= $date) && (!$off || $off > $date);
})->sort('SortOrder');
return $this->items;
}
2018-07-25 11:03:50 +02:00
}