silverstripe-elemental-basics/src/Extensions/SlideImageEx.php

149 lines
3.9 KiB
PHP
Raw Normal View History

2022-04-19 13:50:36 +02:00
<?php
/**
* Created by PhpStorm.
* User: tony
* Date: 6/23/18
* Time: 1:23 PM
*/
namespace A2nt\ElementalBasics\Extensions;
2022-07-08 20:04:48 +02:00
use LeKoala\FilePond\FilePondField;
2022-05-02 10:07:10 +02:00
use A2nt\ElementalBasics\Elements\SliderElement;
2022-07-08 19:41:13 +02:00
use SilverStripe\AssetAdmin\Forms\UploadField;
2022-07-08 19:19:50 +02:00
use SilverStripe\Assets\File;
2022-04-19 16:31:17 +02:00
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\ToggleCompositeField;
2022-04-19 13:50:36 +02:00
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\ValidationResult;
2022-05-10 13:08:53 +02:00
/**
* Class \A2nt\ElementalBasics\Extensions\SlideImageEx
*
* @property \A2nt\ElementalBasics\Extensions\SlideImageEx $owner
* @property boolean $Hide
* @property string $DateOn
* @property string $DateOff
*/
2022-04-19 13:50:36 +02:00
class SlideImageEx extends DataExtension
{
2022-07-08 19:19:50 +02:00
private static $db = [
2022-04-19 16:31:17 +02:00
'Hide' => 'Boolean(0)',
'DateOn' => 'Datetime',
'DateOff' => 'Datetime',
];
2022-07-08 19:19:50 +02:00
private static $has_one = [
'VideoFile' => File::class,
];
2022-07-08 19:41:13 +02:00
private static $owns = [
'VideoFile',
];
2022-07-08 19:19:50 +02:00
private $_cache = [
'element' => [],
];
2022-04-19 16:31:17 +02:00
2022-07-08 19:19:50 +02:00
public function getElement()
2022-04-19 16:31:17 +02:00
{
2022-07-08 19:19:50 +02:00
if (!isset($this->_cache['element'][$this->owner->ID])) {
2022-04-19 16:31:17 +02:00
$this->_cache['element'][$this->owner->ID] = $this->owner->SlideshowElement();
}
return $this->_cache['element'][$this->owner->ID];
}
public function ImageURL()
{
$el = $this->getElement();
$img = $this->owner->Image();
2022-07-08 19:19:50 +02:00
if (!$img) {
2022-04-19 16:31:17 +02:00
return null;
}
2022-07-08 19:19:50 +02:00
if ($el->getField('ImageOriginalSize')) {
2022-04-19 16:31:17 +02:00
return $img->Link();
}
return $img->FocusFill($this->getSlideWidth(), $this->getSlideHeight())->Link();
}
public function getSlideWidth()
{
$element = $this->getElement();
2022-07-08 19:19:50 +02:00
if (!$element->ID) {
return SliderElement::config()->get('slide_width');
2022-04-19 16:31:17 +02:00
}
return $element->getSlideWidth();
}
public function getSlideHeight()
{
$element = $this->getElement();
2022-07-08 19:19:50 +02:00
if (!$element->ID) {
return SliderElement::config()->get('slide_height');
2022-04-19 16:31:17 +02:00
}
return $element->getSlideHeight();
}
2022-07-08 19:41:13 +02:00
public static function formatBytes($size, $precision = 2)
{
$base = log($size, 1024);
$suffixes = array('', 'K', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}
2022-04-19 16:31:17 +02:00
public function updateCMSFields(FieldList $fields)
{
parent::updateCMSFields($fields);
$fields->removeByName([
'PageLinkID',
'Hide',
'DateOn',
'DateOff',
]);
2022-07-08 20:04:48 +02:00
$videoUpload = FilePondField::create('VideoFile')
->setChunkUploads(true)
2022-07-08 19:41:13 +02:00
->setAllowedExtensions(['mp4'])
->setFolderName('Uploads/SlideVideos');
$videoUpload->setTitle(
'Video File (max size: '
.self::formatBytes($videoUpload->getValidator()->getAllowedMaxFileSize('mp4'))
.')'
);
$fields->insertAfter('Headline', $videoUpload);
2022-04-19 16:31:17 +02:00
$fields->dataFieldByName('Image')
->setTitle('Image ('.$this->getSlideWidth().' x '.$this->getSlideHeight().' px)');
$fields->addFieldToTab('Root.Main', ToggleCompositeField::create(
'ConfigHD',
'Slide Settings',
[
CheckboxField::create('Hide', 'Hide this slide? (That will hide the slide regardless of start/end fields)'),
DatetimeField::create('DateOn', 'When would you like to start showing the slide?'),
DatetimeField::create('DateOff', 'When would you like to stop showing the slide?'),
]
));
}
2022-04-19 13:50:36 +02:00
public function validate(ValidationResult $validationResult)
{
if (!$this->owner->Name) {
$this->owner->Name = rand();
}
2022-04-19 15:19:49 +02:00
}
2022-04-19 13:50:36 +02:00
}