mirror of
https://github.com/a2nt/silverstripe-elemental-basics.git
synced 2024-10-22 15:05:54 +00:00
FIX: Minor improvements
This commit is contained in:
parent
890694d5ed
commit
1e880adea9
@ -81,7 +81,7 @@ class SliderElement extends ElementSlideshow
|
|||||||
|
|
||||||
public function getSlideWidth()
|
public function getSlideWidth()
|
||||||
{
|
{
|
||||||
if($this->getField('ImageOriginalSize')){
|
if ($this->getField('ImageOriginalSize')) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ class SliderElement extends ElementSlideshow
|
|||||||
|
|
||||||
public function getSlideHeight()
|
public function getSlideHeight()
|
||||||
{
|
{
|
||||||
if($this->getField('ImageOriginalSize')){
|
if ($this->getField('ImageOriginalSize')) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,6 +123,11 @@ class SliderElement extends ElementSlideshow
|
|||||||
|
|
||||||
$grid = $fields->dataFieldByName('Slides');
|
$grid = $fields->dataFieldByName('Slides');
|
||||||
if ($grid) {
|
if ($grid) {
|
||||||
|
$fields->insertBefore('Slides', LiteralField::create(
|
||||||
|
'SlidesNote',
|
||||||
|
'<div class="alert alert-info">Note: to show hidden slide open slide item and uncheck "Hide" checkbox</div>'
|
||||||
|
));
|
||||||
|
|
||||||
$config = $grid->getConfig();
|
$config = $grid->getConfig();
|
||||||
|
|
||||||
$bulk = new BulkUploader('Image', SlideImage::class, false);
|
$bulk = new BulkUploader('Image', SlideImage::class, false);
|
||||||
|
@ -2,183 +2,150 @@
|
|||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: tony
|
* User: tony
|
||||||
* Date: 6/30/18
|
* Date: 6/23/18
|
||||||
* Time: 11:54 PM
|
* Time: 1:23 PM
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace A2nt\ElementalBasics\Elements;
|
namespace A2nt\ElementalBasics\Extensions;
|
||||||
|
|
||||||
use Colymba\BulkUpload\BulkUploader;
|
use A2nt\ElementalBasics\Elements\SliderElement;
|
||||||
use Dynamic\Elements\Flexslider\Elements\ElementSlideshow;
|
|
||||||
use Dynamic\FlexSlider\Model\SlideImage;
|
use Dynamic\FlexSlider\Model\SlideImage;
|
||||||
use Dynamic\FlexSlider\ORM\FlexSlider;
|
use LeKoala\FilePond\FilePondField;
|
||||||
use SilverStripe\Assets\Image;
|
use SilverStripe\Assets\File;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Forms\CheckboxField;
|
use SilverStripe\Forms\CheckboxField;
|
||||||
use SilverStripe\Forms\LiteralField;
|
use SilverStripe\Forms\DatetimeField;
|
||||||
use SilverStripe\Forms\NumericField;
|
use SilverStripe\Forms\ToggleCompositeField;
|
||||||
use SilverStripe\Forms\ReadonlyField;
|
use SilverStripe\ORM\DataExtension;
|
||||||
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\ORM\ValidationResult;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class \A2nt\ElementalBasics\Elements\SliderElement
|
* Class \A2nt\ElementalBasics\Extensions\SlideImageEx
|
||||||
*
|
*
|
||||||
* @property string $Animation
|
* @property \A2nt\ElementalBasics\Extensions\SlideImageEx $owner
|
||||||
* @property boolean $Loop
|
* @property boolean $Hide
|
||||||
* @property boolean $Animate
|
* @property string $DateOn
|
||||||
* @property boolean $ThumbnailNav
|
* @property string $DateOff
|
||||||
* @property boolean $SliderControlNav
|
|
||||||
* @property boolean $SliderDirectionNav
|
|
||||||
* @property boolean $CarouselControlNav
|
|
||||||
* @property boolean $CarouselDirectionNav
|
|
||||||
* @property int $CarouselThumbnailCt
|
|
||||||
* @property float $FlexSliderSpeed
|
|
||||||
* @property int $Interval
|
|
||||||
* @property int $SlidesInRow
|
|
||||||
* @property boolean $ImageOriginalSize
|
|
||||||
* @method \SilverStripe\ORM\DataList|\Dynamic\FlexSlider\Model\SlideImage[] Slides()
|
|
||||||
* @mixin \Dynamic\FlexSlider\ORM\FlexSlider
|
|
||||||
*/
|
*/
|
||||||
class SliderElement extends ElementSlideshow
|
class SlideImageEx extends DataExtension
|
||||||
{
|
{
|
||||||
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 = [
|
private static $db = [
|
||||||
'Interval' => 'Int',
|
'Hide' => 'Boolean(0)',
|
||||||
'SlidesInRow' => 'Int',
|
'DateOn' => 'Datetime',
|
||||||
'ImageOriginalSize' => 'Boolean(0)',
|
'DateOff' => 'Datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
private static $extensions = [
|
private static $has_one = [
|
||||||
FlexSlider::class,
|
'VideoFile' => File::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
private static $owns = [
|
private static $owns = [
|
||||||
'Slides',
|
'VideoFile',
|
||||||
];
|
];
|
||||||
|
|
||||||
private $items;
|
private $_cache = [
|
||||||
|
'element' => [],
|
||||||
|
];
|
||||||
|
|
||||||
public function getType()
|
public function getElement()
|
||||||
{
|
{
|
||||||
return self::$singular_name;
|
if (!isset($this->_cache['element'][$this->owner->ID])) {
|
||||||
|
$this->_cache['element'][$this->owner->ID] = $this->owner->SlideshowElement();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function ratioSize($size)
|
return $this->_cache['element'][$this->owner->ID];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function ImageURL()
|
||||||
{
|
{
|
||||||
$count = $this->SlidesInRow;
|
$el = $this->getElement();
|
||||||
return ($count > 1) ? $size / $count : $size;
|
$img = $this->owner->Image();
|
||||||
|
|
||||||
|
if (!$img) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($el->getField('ImageOriginalSize')) {
|
||||||
|
return $img->Link();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $img->FocusFill($this->getSlideWidth(), $this->getSlideHeight())->Link();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSlideWidth()
|
public function getSlideWidth()
|
||||||
{
|
{
|
||||||
if ($this->getField('ImageOriginalSize')) {
|
$element = $this->getElement();
|
||||||
return null;
|
if (!$element->ID) {
|
||||||
|
return SliderElement::config()->get('slide_width');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->ratioSize(self::config()->get('slide_width'));
|
return $element->getSlideWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSlideHeight()
|
public function getSlideHeight()
|
||||||
{
|
{
|
||||||
if ($this->getField('ImageOriginalSize')) {
|
$element = $this->getElement();
|
||||||
return null;
|
if (!$element->ID) {
|
||||||
|
return SliderElement::config()->get('slide_height');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->ratioSize(self::config()->get('slide_height'));
|
return $element->getSlideHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCMSFields()
|
public static function formatBytes($size, $precision = 2)
|
||||||
{
|
{
|
||||||
$fields = parent::getCMSFields();
|
$base = log($size, 1024);
|
||||||
|
$suffixes = array('', 'K', 'M', 'G', 'T');
|
||||||
|
|
||||||
|
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateCMSFields(FieldList $fields)
|
||||||
|
{
|
||||||
|
parent::updateCMSFields($fields);
|
||||||
|
|
||||||
// remove in case you don't need to provide this functionality
|
|
||||||
$fields->removeByName([
|
$fields->removeByName([
|
||||||
|
'PageLinkID',
|
||||||
|
'Hide',
|
||||||
|
'DateOn',
|
||||||
|
'DateOff',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$videoUpload = FilePondField::create('VideoFile')
|
||||||
|
->setChunkUploads(true)
|
||||||
|
->setAllowedExtensions(['mp4'])
|
||||||
|
->setFolderName('Uploads/SlideVideos');
|
||||||
|
|
||||||
|
$validator = $videoUpload->getValidator();
|
||||||
|
$validator->setAllowedMaxFileSize(['mp4' => Config::inst()->get(SlideImage::class, 'max_video_size')]);
|
||||||
|
|
||||||
|
$maxFileSize = $validator->getAllowedMaxFileSize('mp4');
|
||||||
|
$videoUpload->setTitle('Video File (max size: '.self::formatBytes($maxFileSize).')');
|
||||||
|
|
||||||
|
$fields->insertAfter('Headline', $videoUpload);
|
||||||
|
|
||||||
|
$fields->dataFieldByName('Image')
|
||||||
|
->setTitle('Image ('.$this->getSlideWidth().' x '.$this->getSlideHeight().' px)');
|
||||||
|
|
||||||
|
$fields->addFieldsToTab('Root.Main', [
|
||||||
|
CheckboxField::create('Hide', 'Hide this slide? (That will hide the slide regardless of start/end fields)'),
|
||||||
|
ToggleCompositeField::create(
|
||||||
'ConfigHD',
|
'ConfigHD',
|
||||||
'Animation',
|
'Slide Date Settings',
|
||||||
'Loop',
|
[
|
||||||
'Animate',
|
DatetimeField::create('DateOn', 'When would you like to start showing the slide?'),
|
||||||
'ThumbnailNav',
|
DatetimeField::create('DateOff', 'When would you like to stop showing the slide?'),
|
||||||
'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',
|
|
||||||
'<div class="alert alert-info">Note: to show hidden slide open slide item and uncheck "Hide" checkbox</div>'
|
|
||||||
));
|
|
||||||
|
|
||||||
$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);
|
|
||||||
$config->addComponent(new \Colymba\BulkManager\BulkManager());
|
|
||||||
|
|
||||||
$columns = new GridFieldEditableColumns();
|
|
||||||
$columns->setDisplayFields([
|
|
||||||
'Hide' => [
|
|
||||||
'title' => 'Hide it?',
|
|
||||||
'field' => CheckboxField::class,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config->addComponent($columns);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $fields;
|
public function validate(ValidationResult $validationResult)
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getSlideShow()
|
|
||||||
{
|
{
|
||||||
if ($this->items) {
|
if (!$this->owner->Name) {
|
||||||
return $this->items;
|
$this->owner->Name = rand();
|
||||||
}
|
|
||||||
|
|
||||||
$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…
x
Reference in New Issue
Block a user