Source of file PerfectCmsImagesUploadField.php
Size: 4,070 Bytes - Last Modified: 2021-12-23T10:45:59+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/PerfectCmsImagesUploadField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 | <?php namespace Sunnysideup\PerfectCmsImages\Forms; use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Assets\Folder; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\SS_List; use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; use Sunnysideup\PerfectCmsImages\Filesystem\PerfectCmsImageValidator; /** * image-friendly upload field. * * Usage: * $field = PerfectCmsImagesUploadFielde::create( * "ImageField", * "Add Image", * ); */ class PerfectCmsImagesUploadField extends UploadField { private static $max_size_in_kilobytes = 2048; private static $folder_prefix = ''; /** * @config * * @var array */ private static $allowed_actions = [ 'upload', ]; private $afterUpload; /** * @param string $name the internal field name, passed to forms * @param null|string $title the field label * @param null|SS_List $items If no items are defined, the field will try to auto-detect an existing relation */ public function __construct( string $name, ?string $title = null, ?SS_List $items = null, ?string $alternativeName = null ) { parent::__construct( $name, $title, $items ); $perfectCMSImageValidator = new PerfectCMSImageValidator(); $this->setValidator($perfectCMSImageValidator); if (null === $alternativeName) { $alternativeName = $name; } $this->selectFormattingStandard($alternativeName); } public function setDescription($string): self { parent::setDescription( DBField::create_field('HTMLText', $string . '<br />' . $this->RightTitle()) ); //important! return $this; } /** * @param string $name Formatting Standard */ public function selectFormattingStandard(string $name): self { $this->setPerfectFolderName($name); $this->setDescription(PerfectCMSImages::get_description_for_cms($name)); $this->setAllowedFileCategories('image'); $alreadyAllowed = $this->getAllowedExtensions(); $this->setAllowedExtensions($alreadyAllowed + ['svg']); //keep the size reasonable $maxSizeInKilobytes = PerfectCMSImages::max_size_in_kilobytes($name); $this->getValidator()->setAllowedMaxFileSize(1 * 1024 * $maxSizeInKilobytes); $this->getValidator()->setFieldName($name); return $this; } /** * Creates a single file based on a form-urlencoded upload. * Allows for hooking AfterUpload. * * @return HTTPResponse */ public function upload(HTTPRequest $request) { $response = parent::upload($request); // If afterUpload is a function .. return is_callable($this->afterUpload) ? // .. then return the results from that .. ($this->afterUpload)($response) : // .. else return the original $response $response; } /** * Add an anonymous functions to run after upload completes. * * @param callable $func */ public function setAfterUpload($func): self { $this->afterUpload = $func; return $this; } protected function setPerfectFolderName(string $name) { $folderPrefix = $this->Config()->get('folder_prefix'); $folderName = $this->folderName; if ('' === $folderName) { //folder related stuff ... $folderName = PerfectCMSImages::get_folder($name); if ('' === $folderName) { $folderName = 'other-images'; } $folderName = implode( '/', array_filter([$folderPrefix, $folderName]) ); } //create folder Folder::find_or_make($folderName); //set folder $this->setFolderName($folderName); } } |