Source of file SelectUploadField.php
Size: 7,411 Bytes - Last Modified: 2021-12-23T10:34:15+00:00
/var/www/docs.ssmods.com/process/src/src/SelectUploadField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 | <?php namespace SilverStripe\SelectUpload; use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Security\Permission; use SilverStripe\ORM\SS_List; use SilverStripe\View\Requirements; use SilverStripe\Assets\Folder; use SilverStripe\Assets\File; use SilverStripe\AssetAdmin\Controller\AssetAdmin; use SilverStripe\Forms\Form; /** * A composite form field which allows users to select a folder into which files may be uploaded */ class SelectUploadField extends UploadField { private static $url_handlers = [ 'folder/tree/$ID' => 'tree' ]; /** * @config * @var array */ private static $allowed_actions = [ 'upload', 'tree', 'changeFolder' ]; /** * Folder selector field * * @var FolderDropdownField */ protected $selectField; /** * @var bool|string */ protected $canSelectFolder = true; public function __construct($name, $title = null, SS_List $items = null) { parent::__construct($name, $title, $items); $this->selectField = FolderDropdownField::create("{$name}/folder"); // If we haven't uploaded to a folder yet, set to the default foldername if (!$this->selectField->Value()) { $folderID = $this->folderIDFromPath($this->getDefaultFolderName()); if ($folderID) { $this->selectField->setValue($folderID); } } } /** * @param array $properties * @return \SilverStripe\ORM\FieldType\DBHTMLText */ public function Field($properties = []) { $this->setAttribute('data-folder-link', $this->Link('changeFolder')); $field = parent::Field($properties); Requirements::javascript("silverstripe/selectupload:/js/SelectUploadField.js"); Requirements::css("silverstripe/selectupload:/css/SelectUploadField.css"); return $field; } /** * Get the folder selector field * * @return FolderDropdownField */ public function FolderSelector() { return $this->selectField; } /** * Return the subtree for a selected folder id * * @param HTTPRequest $request * @return string HTTP Response */ public function tree(HTTPRequest $request) { return $this->FolderSelector()->tree($request); } /** * process HTTP request to change the upload folder * * @param HTTPRequest $request * @throws HTTPResponse_Exception */ public function changeFolder(HTTPRequest $request) { // CSRF check $token = $this->getForm()->getSecurityToken(); if (!$token->checkRequest($request)) { return $this->httpError(400); } $folderID = $request->postVar('FolderID'); if ($folderID) { $this->FolderSelector()->setLastFolderID($folderID); } } /** * @param Form $form * @return UploadField */ public function setForm($form) { $this->selectField->setForm($form); return parent::setForm($form); } /** * @return string */ public function Type() { return 'selectupload entwine-uploadfield uploadfield'; } /** * Given a request, ensure that the current field is aware of the selected folder * * @param HTTPRequest $request */ protected function updateFolderName(HTTPRequest $request) { // check if allowed to select folder if (!$this->getCanSelectFolder()) { return; } // Get path from upload $folderID = $request->requestVar("{$this->Name}/folder"); $path = $this->folderPathFromID($folderID); if ($path !== false) { $this->setFolderName($path); $this->selectField->setValue($folderID); } } /** * Get path of a folder relative to /assets/ by id. * This will be a format appropriate for setting setFolderName to * * @param int $folderID * @return string|bool Relative path to the assets directory, or false if not found */ protected function folderPathFromID($folderID) { if (empty($folderID)) { return false; } $folder = Folder::get()->byID($folderID); if (!$folder) { return false; } // Translate path $path = $folder->getFilename(); if (stripos($path, ASSETS_DIR) === 0) { $path = substr($path, strlen(ASSETS_DIR) + 1); } return $path; } /** * Gets the ID of a folder given a path relative to /assets/. * * @param string $path * @return int Folder ID */ protected function folderIDFromPath($path) { $folder = File::find($path); if ($folder) { return $folder->ID; } } /** * @param HTTPRequest $request * @return array|HTTPResponse|\SilverStripe\Control\RequestHandler|string */ public function handleRequest(HTTPRequest $request) { $this->updateFolderName($request); return parent::handleRequest($request); } /** * Specify whether the user can select an upload folder. * String values will be treated as required permission codes * * @param boolean|string $canSelectFolder Either a boolean flag, or a required * permission code * @return $this */ public function setCanSelectFolder($canSelectFolder) { $this->canSelectFolder = $canSelectFolder; return $this; } /** * Determine if the current member is allowed to change the folder * * @return boolean */ public function getCanSelectFolder() { if (!$this->isActive()) { return false; } if ($this->template && in_array($this->template, self::config()->disable_for_templates)) { return false; } // Check config $can = $this->canSelectFolder; return (is_bool($can)) ? $can : Permission::check($can); } /** * @return string */ public function getFolderName() { // Ensure that, if this member is allowed, the persistant folder overrides any default set if ($this->getCanSelectFolder()) { $path = $this->folderPathFromID($this->selectField->Value()); if ($path !== false) { return $path; } } return $this->getDefaultFolderName(); } /** * Get the 'default' folder name * * @return string */ public function getDefaultFolderName() { return parent::getFolderName(); } /** * Improve readability of folder name and directory structure. * add a space before and after each / * 'foo/bar' will be displayed as 'foo / bar' * * @return null|string|string[] */ public function getDisplayFolderName() { $name = $this->getFolderName(); return preg_replace('/\s*\\/\s*/', ' / ', trim($name, '/')); } /** * Returns true if the field is neither readonly nor disabled * * @return boolean */ public function isActive() { return !$this->isDisabled() && !$this->isReadonly(); } } |