2012-02-03 00:59:40 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\Storage\AssetContainer;
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\FileNameFilter;
|
|
|
|
use SilverStripe\Assets\Folder;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Object;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\ValidationException;
|
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
|
|
|
use SilverStripe\ORM\RelationList;
|
|
|
|
use SilverStripe\ORM\UnsavedRelationList;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\ViewableData;
|
|
|
|
use SilverStripe\View\ViewableData_Customised;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Exception;
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Field for uploading single or multiple files of all types, including images.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-17 07:12:56 +02:00
|
|
|
* <b>Features (some might not be available to old browsers):</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* - File Drag&Drop support
|
|
|
|
* - Progressbar
|
|
|
|
* - Image thumbnail/file icons even before upload finished
|
2013-05-27 05:22:59 +02:00
|
|
|
* - Saving into relations on form submit
|
2012-02-03 00:59:40 +01:00
|
|
|
* - Edit file
|
2012-09-26 23:34:00 +02:00
|
|
|
* - allowedExtensions is by default File::$allowed_extensions<li>maxFileSize the value of min(upload_max_filesize,
|
|
|
|
* post_max_size) from php.ini
|
2013-01-09 13:28:12 +01:00
|
|
|
*
|
|
|
|
* <>Usage</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @example <code>
|
2013-05-27 05:22:59 +02:00
|
|
|
* $UploadField = new UploadField('AttachedImages', 'Please upload some images <span>(max. 5 files)</span>');
|
|
|
|
* $UploadField->setAllowedFileCategories('image');
|
|
|
|
* $UploadField->setAllowedMaxFileNumber(5);
|
2012-02-03 00:59:40 +01:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @author Zauberfisch
|
|
|
|
*/
|
|
|
|
class UploadField extends FileField {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2012-02-03 00:59:40 +01:00
|
|
|
'upload',
|
2012-02-08 00:58:58 +01:00
|
|
|
'attach',
|
|
|
|
'handleItem',
|
|
|
|
'handleSelect',
|
2013-05-27 05:22:59 +02:00
|
|
|
'fileexists'
|
2012-02-03 00:59:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2012-02-03 00:59:40 +01:00
|
|
|
'item/$ID' => 'handleItem',
|
2012-02-08 00:58:58 +01:00
|
|
|
'select' => 'handleSelect',
|
2012-02-03 00:59:40 +01:00
|
|
|
'$Action!' => '$Action',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Template to use for the file button widget
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var string
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected $templateFileButtons = null;
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Template to use for the edit form
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var string
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected $templateFileEdit = null;
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Parent data record. Will be infered from parent form or controller if blank.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @var DataObject
|
|
|
|
*/
|
|
|
|
protected $record;
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Items loaded into this field. May be a RelationList, or any other SS_List
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @var SS_List
|
|
|
|
*/
|
|
|
|
protected $items;
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Config for this field used in the front-end javascript
|
2013-01-09 13:28:12 +01:00
|
|
|
* (will be merged into the config of the javascript file upload plugin).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var array
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2015-10-19 06:27:27 +02:00
|
|
|
protected $ufConfig = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Front end config defaults
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $defaultConfig = array(
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Automatically upload the file once selected
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
'autoUpload' => true,
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Restriction on number of files that may be set for this field. Set to null to allow
|
|
|
|
* unlimited. If record has a has_one and allowedMaxFileNumber is null, it will be set to 1.
|
|
|
|
* The resulting value will be set to maxNumberOfFiles
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var integer
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
'allowedMaxFileNumber' => null,
|
2013-01-09 11:34:02 +01:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Can the user upload new files, or just select from existing files.
|
2013-01-09 13:28:12 +01:00
|
|
|
* String values are interpreted as permission codes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var boolean|string
|
2013-01-09 11:34:02 +01:00
|
|
|
*/
|
|
|
|
'canUpload' => true,
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Can the user attach files from the assets archive on the site?
|
2013-01-11 05:33:06 +01:00
|
|
|
* String values are interpreted as permission codes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var boolean|string
|
2013-01-11 05:33:06 +01:00
|
|
|
*/
|
|
|
|
'canAttachExisting' => "CMS_ACCESS_AssetAdmin",
|
2013-05-13 16:53:02 +02:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Shows the target folder for new uploads in the field UI.
|
2013-05-13 16:53:02 +02:00
|
|
|
* Disable to keep the internal filesystem structure hidden from users.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var boolean|string
|
2013-05-13 16:53:02 +02:00
|
|
|
*/
|
|
|
|
'canPreviewFolder' => true,
|
2013-11-06 10:07:04 +01:00
|
|
|
/**
|
|
|
|
* Indicate a change event to the containing form if an upload
|
|
|
|
* or file edit/delete was performed.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
'changeDetection' => true,
|
2013-01-11 05:34:27 +01:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Maximum width of the preview thumbnail
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var integer
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
'previewMaxWidth' => 80,
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Maximum height of the preview thumbnail
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var integer
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
'previewMaxHeight' => 60,
|
|
|
|
/**
|
|
|
|
* javascript template used to display uploading files
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @see javascript/UploadField_uploadtemplate.js
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
'uploadTemplateName' => 'ss-uploadfield-uploadtemplate',
|
|
|
|
/**
|
|
|
|
* javascript template used to display already uploaded files
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @see javascript/UploadField_downloadtemplate.js
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
'downloadTemplateName' => 'ss-uploadfield-downloadtemplate',
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Show a warning when overwriting a file.
|
|
|
|
* This requires Upload->replaceFile config to be set to true, otherwise
|
2014-04-16 11:08:25 +02:00
|
|
|
* files will be renamed instead of overwritten
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @see Upload
|
|
|
|
* @var boolean
|
2013-03-08 04:02:38 +01:00
|
|
|
*/
|
2013-05-27 05:22:59 +02:00
|
|
|
'overwriteWarning' => true
|
2012-02-03 00:59:40 +01:00
|
|
|
);
|
|
|
|
|
2013-06-05 18:09:33 +02:00
|
|
|
/**
|
|
|
|
* @var String Folder to display in "Select files" list.
|
2014-08-15 08:53:05 +02:00
|
|
|
* Defaults to listing all files regardless of folder.
|
2013-06-05 18:09:33 +02:00
|
|
|
* The folder path should be relative to the webroot.
|
|
|
|
* See {@link FileField->folderName} to set the upload target instead.
|
|
|
|
* @example admin/folder/subfolder
|
|
|
|
*/
|
|
|
|
protected $displayFolderName;
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* FieldList $fields or string $name (of a method on File to provide a fields) for the EditForm
|
|
|
|
* @example 'getCMSFields'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var FieldList|string
|
|
|
|
*/
|
|
|
|
protected $fileEditFields = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm
|
|
|
|
* @example 'getCMSActions'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var FieldList|string
|
|
|
|
*/
|
|
|
|
protected $fileEditActions = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validator (eg RequiredFields) or string $name (of a method on File to provide a Validator) for the EditForm
|
|
|
|
* @example 'getCMSValidator'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @var RequiredFields|string
|
|
|
|
*/
|
|
|
|
protected $fileEditValidator = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new UploadField instance
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @param string $name The internal field name, passed to forms.
|
|
|
|
* @param string $title The field label.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on
|
|
|
|
* @link $record}, with the same name as the field name.
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function __construct($name, $title = null, SS_List $items = null) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
// TODO thats the first thing that came to my head, feel free to change it
|
|
|
|
$this->addExtraClass('ss-upload'); // class, used by js
|
|
|
|
$this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only
|
|
|
|
|
2015-10-19 06:27:27 +02:00
|
|
|
$this->ufConfig = self::config()->defaultConfig;
|
2013-01-09 13:28:12 +01:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
parent::__construct($name, $title);
|
|
|
|
|
|
|
|
if($items) $this->setItems($items);
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// filter out '' since this would be a regex problem on JS end
|
2013-03-21 19:48:54 +01:00
|
|
|
$this->getValidator()->setAllowedExtensions(
|
2016-08-19 00:51:35 +02:00
|
|
|
array_filter(File::config()->allowed_extensions)
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// get the lower max size
|
2013-05-27 05:22:59 +02:00
|
|
|
$maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
|
|
|
|
$maxPost = File::ini2bytes(ini_get('post_max_size'));
|
|
|
|
$this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost));
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set name of template used for Buttons on each file (replace, edit, remove, delete) (without path or extension)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $template
|
|
|
|
* @return $this
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function setTemplateFileButtons($template) {
|
|
|
|
$this->templateFileButtons = $template;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return string
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function getTemplateFileButtons() {
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this->_templates($this->templateFileButtons, '_FileButtons');
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set name of template used for the edit (inline & popup) of a file file (without path or extension)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $template
|
|
|
|
* @return $this
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function setTemplateFileEdit($template) {
|
|
|
|
$this->templateFileEdit = $template;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return string
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function getTemplateFileEdit() {
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this->_templates($this->templateFileEdit, '_FileEdit');
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Determine if the target folder for new uploads in is visible the field UI.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canPreviewFolder() {
|
|
|
|
if(!$this->isActive()) return false;
|
|
|
|
$can = $this->getConfig('canPreviewFolder');
|
|
|
|
return (is_bool($can)) ? $can : Permission::check($can);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the target folder for new uploads in is visible the field UI.
|
|
|
|
* Disable to keep the internal filesystem structure hidden from users.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param boolean|string $canPreviewFolder Either a boolean flag, or a
|
|
|
|
* required permission code
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setCanPreviewFolder($canPreviewFolder) {
|
|
|
|
return $this->setConfig('canPreviewFolder', $canPreviewFolder);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Determine if the field should show a warning when overwriting a file.
|
|
|
|
* This requires Upload->replaceFile config to be set to true, otherwise
|
|
|
|
* files will be renamed instead of overwritten (although the warning will
|
|
|
|
* still be displayed)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getOverwriteWarning() {
|
|
|
|
return $this->getConfig('overwriteWarning');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Determine if the field should show a warning when overwriting a file.
|
|
|
|
* This requires Upload->replaceFile config to be set to true, otherwise
|
|
|
|
* files will be renamed instead of overwritten (although the warning will
|
|
|
|
* still be displayed)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param boolean $overwriteWarning
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setOverwriteWarning($overwriteWarning) {
|
|
|
|
return $this->setConfig('overwriteWarning', $overwriteWarning);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:09:33 +02:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
2013-06-05 18:09:33 +02:00
|
|
|
*/
|
|
|
|
public function setDisplayFolderName($name) {
|
|
|
|
$this->displayFolderName = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getDisplayFolderName() {
|
|
|
|
return $this->displayFolderName;
|
|
|
|
}
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Force a record to be used as "Parent" for uploaded Files (eg a Page with a has_one to File)
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
2012-04-17 07:12:56 +02:00
|
|
|
* @param DataObject $record
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function setRecord($record) {
|
|
|
|
$this->record = $record;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Get the record to use as "Parent" for uploaded Files (eg a Page with a has_one to File) If none is set, it will
|
|
|
|
* use Form->getRecord() or Form->Controller()->data()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function getRecord() {
|
|
|
|
if (!$this->record && $this->form) {
|
2013-05-27 05:22:59 +02:00
|
|
|
if (($record = $this->form->getRecord()) && ($record instanceof DataObject)) {
|
|
|
|
$this->record = $record;
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif (($controller = $this->form->getController())
|
2014-08-15 08:53:05 +02:00
|
|
|
&& $controller->hasMethod('data')
|
2013-05-27 05:22:59 +02:00
|
|
|
&& ($record = $controller->data())
|
|
|
|
&& ($record instanceof DataObject)
|
|
|
|
) {
|
|
|
|
$this->record = $record;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->record;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Loads the related record values into this field. UploadField can be uploaded
|
|
|
|
* in one of three ways:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* - By passing in a list of file IDs in the $value parameter (an array with a single
|
|
|
|
* key 'Files', with the value being the actual array of IDs).
|
|
|
|
* - By passing in an explicit list of File objects in the $record parameter, and
|
|
|
|
* leaving $value blank.
|
|
|
|
* - By passing in a dataobject in the $record parameter, from which file objects
|
|
|
|
* will be extracting using the field name as the relation field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Each of these methods will update both the items (list of File objects) and the
|
2013-05-27 05:22:59 +02:00
|
|
|
* field value (list of file ID values).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param array $value Array of submitted form data, if submitting from a form
|
|
|
|
* @param array|DataObject|SS_List $record Full source record, either as a DataObject,
|
|
|
|
* SS_List of items, or an array of submitted form data
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return $this Self reference
|
2016-06-23 01:37:22 +02:00
|
|
|
* @throws ValidationException
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
public function setValue($value, $record = null) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// If we're not passed a value directly, we can attempt to infer the field
|
|
|
|
// value from the second parameter by inspecting its relations
|
|
|
|
$items = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Determine format of presented data
|
|
|
|
if(empty($value) && $record) {
|
|
|
|
// If a record is given as a second parameter, but no submitted values,
|
|
|
|
// then we should inspect this instead for the form values
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
if(($record instanceof DataObject) && $record->hasMethod($this->getName())) {
|
|
|
|
// If given a dataobject use reflection to extract details
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$data = $record->{$this->getName()}();
|
|
|
|
if($data instanceof DataObject) {
|
|
|
|
// If has_one, add sole item to default list
|
|
|
|
$items->push($data);
|
|
|
|
} elseif($data instanceof SS_List) {
|
|
|
|
// For many_many and has_many relations we can use the relation list directly
|
|
|
|
$items = $data;
|
|
|
|
}
|
|
|
|
} elseif($record instanceof SS_List) {
|
|
|
|
// If directly passing a list then save the items directly
|
|
|
|
$items = $record;
|
|
|
|
}
|
|
|
|
} elseif(!empty($value['Files'])) {
|
|
|
|
// If value is given as an array (such as a posted form), extract File IDs from this
|
|
|
|
$class = $this->getRelationAutosetClass();
|
|
|
|
$items = DataObject::get($class)->byIDs($value['Files']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// If javascript is disabled, direct file upload (non-html5 style) can
|
2013-05-27 05:22:59 +02:00
|
|
|
// trigger a single or multiple file submission. Note that this may be
|
|
|
|
// included in addition to re-submitted File IDs as above, so these
|
|
|
|
// should be added to the list instead of operated on independently.
|
|
|
|
if($uploadedFiles = $this->extractUploadedFileData($value)) {
|
|
|
|
foreach($uploadedFiles as $tempFile) {
|
|
|
|
$file = $this->saveTemporaryFile($tempFile, $error);
|
|
|
|
if($file) {
|
|
|
|
$items->add($file);
|
|
|
|
} else {
|
|
|
|
throw new ValidationException($error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Filter items by what's allowed to be viewed
|
|
|
|
$filteredItems = new ArrayList();
|
|
|
|
$fileIDs = array();
|
|
|
|
foreach($items as $file) {
|
|
|
|
if($file->exists() && $file->canView()) {
|
|
|
|
$filteredItems->push($file);
|
|
|
|
$fileIDs[] = $file->ID;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Filter and cache updated item list
|
|
|
|
$this->items = $filteredItems;
|
|
|
|
// Same format as posted form values for this field. Also ensures that
|
|
|
|
// $this->setValue($this->getValue()); is non-destructive
|
|
|
|
$value = $fileIDs ? array('Files' => $fileIDs) : null;
|
|
|
|
|
|
|
|
// Set value using parent
|
2016-08-19 00:51:35 +02:00
|
|
|
parent::setValue($value, $record);
|
|
|
|
return $this;
|
2013-05-27 05:22:59 +02:00
|
|
|
}
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Sets the items assigned to this field as an SS_List of File objects.
|
2014-08-15 08:53:05 +02:00
|
|
|
* Calling setItems will also update the value of this field, as well as
|
2013-05-27 05:22:59 +02:00
|
|
|
* updating the internal list of File items.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @param SS_List $items
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return UploadField self reference
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setItems(SS_List $items) {
|
2013-05-27 05:22:59 +02:00
|
|
|
return $this->setValue(null, $items);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Retrieves the current list of files
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
public function getItems() {
|
2013-05-27 05:22:59 +02:00
|
|
|
return $this->items ? $this->items : new ArrayList();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Retrieves a customised list of all File records to ensure they are
|
2013-05-27 05:22:59 +02:00
|
|
|
* properly viewable when rendered in the field template.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return SS_List[ViewableData_Customised]
|
|
|
|
*/
|
|
|
|
public function getCustomisedItems() {
|
|
|
|
$customised = new ArrayList();
|
|
|
|
foreach($this->getItems() as $file) {
|
|
|
|
$customised->push($this->customiseFile($file));
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2013-05-27 05:22:59 +02:00
|
|
|
return $customised;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the list of selected file IDs
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getItemIDs() {
|
|
|
|
$value = $this->Value();
|
|
|
|
return empty($value['Files']) ? array() : $value['Files'];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
public function Value() {
|
|
|
|
// Re-override FileField Value to use data value
|
|
|
|
return $this->dataValue();
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param DataObject|DataObjectInterface $record
|
|
|
|
* @return $this
|
|
|
|
*/
|
2013-05-27 05:22:59 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
|
|
|
// Check required relation details are available
|
|
|
|
$fieldname = $this->getName();
|
|
|
|
if(!$fieldname) return $this;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Get details to save
|
|
|
|
$idList = $this->getItemIDs();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check type of relation
|
|
|
|
$relation = $record->hasMethod($fieldname) ? $record->$fieldname() : null;
|
|
|
|
if($relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) {
|
|
|
|
// has_many or many_many
|
|
|
|
$relation->setByIDList($idList);
|
2015-02-25 16:21:33 +01:00
|
|
|
} elseif($record->hasOneComponent($fieldname)) {
|
2013-05-27 05:22:59 +02:00
|
|
|
// has_one
|
|
|
|
$record->{"{$fieldname}ID"} = $idList ? reset($idList) : 0;
|
|
|
|
}
|
|
|
|
return $this;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Customises a file with additional details suitable for rendering in the
|
|
|
|
* UploadField.ss template
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param ViewableData|AssetContainer $file
|
2012-02-03 00:59:40 +01:00
|
|
|
* @return ViewableData_Customised
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
protected function customiseFile(AssetContainer $file) {
|
2012-02-03 00:59:40 +01:00
|
|
|
$file = $file->customise(array(
|
|
|
|
'UploadFieldThumbnailURL' => $this->getThumbnailURLForFile($file),
|
|
|
|
'UploadFieldDeleteLink' => $this->getItemHandler($file->ID)->DeleteLink(),
|
2013-03-26 22:56:08 +01:00
|
|
|
'UploadFieldEditLink' => $this->getItemHandler($file->ID)->EditLink(),
|
|
|
|
'UploadField' => $this
|
2012-02-03 00:59:40 +01:00
|
|
|
));
|
|
|
|
// we do this in a second customise to have the access to the previous customisations
|
|
|
|
return $file->customise(array(
|
2016-07-18 08:33:44 +02:00
|
|
|
'UploadFieldFileButtons' => $file->renderWith($this->getTemplateFileButtons())
|
2012-02-03 00:59:40 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Assign a front-end config variable for the upload field
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-07 04:32:30 +02:00
|
|
|
* @see https://github.com/blueimp/jQuery-File-Upload/wiki/Options for the list of front end options available
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @param string $key
|
|
|
|
* @param mixed $val
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return UploadField self reference
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
public function setConfig($key, $val) {
|
2012-03-09 07:32:09 +01:00
|
|
|
$this->ufConfig[$key] = $val;
|
2012-02-03 00:59:40 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Gets a front-end config variable for the upload field
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-07 04:32:30 +02:00
|
|
|
* @see https://github.com/blueimp/jQuery-File-Upload/wiki/Options for the list of front end options available
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-03 00:59:40 +01:00
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getConfig($key) {
|
2013-06-07 04:32:30 +02:00
|
|
|
if(!isset($this->ufConfig[$key])) return null;
|
2012-03-09 07:32:09 +01:00
|
|
|
return $this->ufConfig[$key];
|
|
|
|
}
|
2013-05-27 05:22:59 +02:00
|
|
|
|
2012-03-09 07:32:09 +01:00
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Determine if the field should automatically upload the file.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return boolean
|
2012-03-09 07:32:09 +01:00
|
|
|
*/
|
|
|
|
public function getAutoUpload() {
|
|
|
|
return $this->getConfig('autoUpload');
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Determine if the field should automatically upload the file
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param boolean $autoUpload
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setAutoUpload($autoUpload) {
|
|
|
|
return $this->setConfig('autoUpload', $autoUpload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine maximum number of files allowed to be attached
|
2014-08-15 08:53:05 +02:00
|
|
|
* Defaults to 1 for has_one and null (unlimited) for
|
2013-05-27 05:22:59 +02:00
|
|
|
* many_many and has_many relations.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return integer|null Maximum limit, or null for no limit
|
|
|
|
*/
|
|
|
|
public function getAllowedMaxFileNumber() {
|
|
|
|
$allowedMaxFileNumber = $this->getConfig('allowedMaxFileNumber');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// if there is a has_one relation with that name on the record and
|
2013-05-27 05:22:59 +02:00
|
|
|
// allowedMaxFileNumber has not been set, it's wanted to be 1
|
|
|
|
if(empty($allowedMaxFileNumber)) {
|
|
|
|
$record = $this->getRecord();
|
|
|
|
$name = $this->getName();
|
2015-02-25 16:21:33 +01:00
|
|
|
if($record && $record->hasOneComponent($name)) {
|
2013-05-27 05:22:59 +02:00
|
|
|
return 1; // Default for has_one
|
|
|
|
} else {
|
|
|
|
return null; // Default for has_many and many_many
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $allowedMaxFileNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine maximum number of files allowed to be attached.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param integer|null $allowedMaxFileNumber Maximum limit. 0 or null will be treated as unlimited
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setAllowedMaxFileNumber($allowedMaxFileNumber) {
|
|
|
|
return $this->setConfig('allowedMaxFileNumber', $allowedMaxFileNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the user has permission to upload.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canUpload() {
|
|
|
|
if(!$this->isActive()) return false;
|
|
|
|
$can = $this->getConfig('canUpload');
|
|
|
|
return (is_bool($can)) ? $can : Permission::check($can);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify whether the user can upload files.
|
|
|
|
* String values will be treated as required permission codes
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param boolean|string $canUpload Either a boolean flag, or a required
|
|
|
|
* permission code
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setCanUpload($canUpload) {
|
|
|
|
return $this->setConfig('canUpload', $canUpload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the user has permission to attach existing files
|
|
|
|
* By default returns true if the user has the CMS_ACCESS_AssetAdmin permission
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canAttachExisting() {
|
|
|
|
if(!$this->isActive()) return false;
|
|
|
|
$can = $this->getConfig('canAttachExisting');
|
|
|
|
return (is_bool($can)) ? $can : Permission::check($can);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the field is neither readonly nor disabled
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isActive() {
|
|
|
|
return !$this->isDisabled() && !$this->isReadonly();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify whether the user can attach existing files
|
|
|
|
* String values will be treated as required permission codes
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param boolean|string $canAttachExisting Either a boolean flag, or a
|
|
|
|
* required permission code
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setCanAttachExisting($canAttachExisting) {
|
|
|
|
return $this->setConfig('canAttachExisting', $canAttachExisting);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets thumbnail width. Defaults to 80
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getPreviewMaxWidth() {
|
|
|
|
return $this->getConfig('previewMaxWidth');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see UploadField::getPreviewMaxWidth()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param integer $previewMaxWidth
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setPreviewMaxWidth($previewMaxWidth) {
|
|
|
|
return $this->setConfig('previewMaxWidth', $previewMaxWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets thumbnail height. Defaults to 60
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getPreviewMaxHeight() {
|
|
|
|
return $this->getConfig('previewMaxHeight');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see UploadField::getPreviewMaxHeight()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param integer $previewMaxHeight
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setPreviewMaxHeight($previewMaxHeight) {
|
|
|
|
return $this->setConfig('previewMaxHeight', $previewMaxHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* javascript template used to display uploading files
|
|
|
|
* Defaults to 'ss-uploadfield-uploadtemplate'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @see javascript/UploadField_uploadtemplate.js
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return string
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
public function getUploadTemplateName() {
|
|
|
|
return $this->getConfig('uploadTemplateName');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see UploadField::getUploadTemplateName()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param string $uploadTemplateName
|
|
|
|
* @return UploadField Self reference
|
|
|
|
*/
|
|
|
|
public function setUploadTemplateName($uploadTemplateName) {
|
|
|
|
return $this->setConfig('uploadTemplateName', $uploadTemplateName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* javascript template used to display already uploaded files
|
|
|
|
* Defaults to 'ss-downloadfield-downloadtemplate'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @see javascript/DownloadField_downloadtemplate.js
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return string
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
public function getDownloadTemplateName() {
|
|
|
|
return $this->getConfig('downloadTemplateName');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Uploadfield::getDownloadTemplateName()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param string $downloadTemplateName
|
|
|
|
* @return Uploadfield Self reference
|
|
|
|
*/
|
|
|
|
public function setDownloadTemplateName($downloadTemplateName) {
|
|
|
|
return $this->setConfig('downloadTemplateName', $downloadTemplateName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $fields for the EditForm
|
|
|
|
* @example 'getCMSFields'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param DataObject $file File context to generate fields for
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return FieldList List of form fields
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getFileEditFields(DataObject $file) {
|
2013-05-27 05:22:59 +02:00
|
|
|
// Empty actions, generate default
|
|
|
|
if(empty($this->fileEditFields)) {
|
|
|
|
$fields = $file->getCMSFields();
|
|
|
|
// Only display main tab, to avoid overly complex interface
|
|
|
|
if($fields->hasTabSet() && ($mainTab = $fields->findOrMakeTab('Root.Main'))) {
|
|
|
|
$fields = $mainTab->Fields();
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Fields instance
|
2015-09-15 04:52:02 +02:00
|
|
|
if ($this->fileEditFields instanceof FieldList) {
|
|
|
|
return $this->fileEditFields;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Method to call on the given file
|
|
|
|
if($file->hasMethod($this->fileEditFields)) {
|
|
|
|
return $file->{$this->fileEditFields}();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
throw new InvalidArgumentException("Invalid value for UploadField::fileEditFields");
|
2013-05-27 05:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $fields or string $name (of a method on File to provide a fields) for the EditForm
|
|
|
|
* @example 'getCMSFields'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param FieldList|string
|
|
|
|
* @return Uploadfield Self reference
|
|
|
|
*/
|
|
|
|
public function setFileEditFields($fileEditFields) {
|
|
|
|
$this->fileEditFields = $fileEditFields;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm
|
|
|
|
* @example 'getCMSActions'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param DataObject $file File context to generate form actions for
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return FieldList Field list containing FormAction
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getFileEditActions(DataObject $file) {
|
2013-05-27 05:22:59 +02:00
|
|
|
// Empty actions, generate default
|
|
|
|
if(empty($this->fileEditActions)) {
|
|
|
|
$actions = new FieldList($saveAction = new FormAction('doEdit', _t('UploadField.DOEDIT', 'Save')));
|
|
|
|
$saveAction->addExtraClass('ss-ui-action-constructive icon-accept');
|
|
|
|
return $actions;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Actions instance
|
2015-09-15 04:52:02 +02:00
|
|
|
if ($this->fileEditActions instanceof FieldList) {
|
|
|
|
return $this->fileEditActions;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Method to call on the given file
|
|
|
|
if($file->hasMethod($this->fileEditActions)) {
|
|
|
|
return $file->{$this->fileEditActions}();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
throw new InvalidArgumentException("Invalid value for UploadField::fileEditActions");
|
2013-05-27 05:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FieldList $actions or string $name (of a method on File to provide a actions) for the EditForm
|
|
|
|
* @example 'getCMSActions'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param FieldList|string
|
|
|
|
* @return Uploadfield Self reference
|
|
|
|
*/
|
|
|
|
public function setFileEditActions($fileEditActions) {
|
|
|
|
$this->fileEditActions = $fileEditActions;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the validator to use for the edit form
|
|
|
|
* @example 'getCMSValidator'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param DataObject $file File context to generate validator from
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return Validator Validator object
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getFileEditValidator(DataObject $file) {
|
2013-05-27 05:22:59 +02:00
|
|
|
// Empty validator
|
2015-09-15 04:52:02 +02:00
|
|
|
if(empty($this->fileEditValidator)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Validator instance
|
2015-09-15 04:52:02 +02:00
|
|
|
if($this->fileEditValidator instanceof Validator) {
|
|
|
|
return $this->fileEditValidator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Method to call on the given file
|
|
|
|
if($file->hasMethod($this->fileEditValidator)) {
|
|
|
|
return $file->{$this->fileEditValidator}();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
throw new InvalidArgumentException("Invalid value for UploadField::fileEditValidator");
|
2013-05-27 05:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validator (eg RequiredFields) or string $name (of a method on File to provide a Validator) for the EditForm
|
|
|
|
* @example 'getCMSValidator'
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param Validator|string
|
|
|
|
* @return Uploadfield Self reference
|
|
|
|
*/
|
|
|
|
public function setFileEditValidator($fileEditValidator) {
|
|
|
|
$this->fileEditValidator = $fileEditValidator;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param File|AssetContainer $file
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return string URL to thumbnail
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
protected function getThumbnailURLForFile(AssetContainer $file) {
|
|
|
|
if (!$file->exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to generate image at given size
|
|
|
|
$width = $this->getPreviewMaxWidth();
|
|
|
|
$height = $this->getPreviewMaxHeight();
|
|
|
|
if ($file->hasMethod('ThumbnailURL')) {
|
|
|
|
return $file->ThumbnailURL($width, $height);
|
|
|
|
}
|
|
|
|
if ($file->hasMethod('Thumbnail')) {
|
|
|
|
return $file->Thumbnail($width, $height)->getURL();
|
|
|
|
}
|
|
|
|
if ($file->hasMethod('Fit')) {
|
|
|
|
return $file->Fit($width, $height)->getURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if unsized icon is available
|
|
|
|
if($file->hasMethod('getIcon')) {
|
|
|
|
return $file->getIcon();
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 00:58:58 +01:00
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array('data-selectdialog-url', $this->Link('select'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:15:27 +01:00
|
|
|
public function extraClass() {
|
|
|
|
if($this->isDisabled()) {
|
|
|
|
$this->addExtraClass('disabled');
|
|
|
|
}
|
|
|
|
if($this->isReadonly()) {
|
|
|
|
$this->addExtraClass('readonly');
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-10-26 20:15:27 +01:00
|
|
|
return parent::extraClass();
|
|
|
|
}
|
|
|
|
|
2012-04-11 07:33:36 +02:00
|
|
|
public function Field($properties = array()) {
|
2013-05-27 05:22:59 +02:00
|
|
|
// Calculated config as per jquery.fileupload-ui.js
|
|
|
|
$allowedMaxFileNumber = $this->getAllowedMaxFileNumber();
|
2012-02-03 00:59:40 +01:00
|
|
|
$config = array(
|
|
|
|
'url' => $this->Link('upload'),
|
2012-02-08 00:58:58 +01:00
|
|
|
'urlSelectDialog' => $this->Link('select'),
|
|
|
|
'urlAttach' => $this->Link('attach'),
|
2016-08-19 00:51:35 +02:00
|
|
|
'urlFileExists' => $this->Link('fileexists'),
|
2012-02-03 00:59:40 +01:00
|
|
|
'acceptFileTypes' => '.+$',
|
2013-05-27 05:22:59 +02:00
|
|
|
// Fileupload treats maxNumberOfFiles as the max number of _additional_ items allowed
|
2014-04-16 11:08:25 +02:00
|
|
|
'maxNumberOfFiles' => $allowedMaxFileNumber ? ($allowedMaxFileNumber - count($this->getItemIDs())) : null,
|
|
|
|
'replaceFile' => $this->getUpload()->getReplaceFile(),
|
2012-02-03 00:59:40 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Validation: File extensions
|
|
|
|
if ($allowedExtensions = $this->getAllowedExtensions()) {
|
2012-02-03 00:59:40 +01:00
|
|
|
$config['acceptFileTypes'] = '(\.|\/)(' . implode('|', $allowedExtensions) . ')$';
|
2012-05-01 21:44:54 +02:00
|
|
|
$config['errorMessages']['acceptFileTypes'] = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'File.INVALIDEXTENSIONSHORT',
|
2012-05-09 08:38:21 +02:00
|
|
|
'Extension is not allowed'
|
2012-05-01 21:44:54 +02:00
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Validation: File size
|
|
|
|
if ($allowedMaxFileSize = $this->getValidator()->getAllowedMaxFileSize()) {
|
|
|
|
$config['maxFileSize'] = $allowedMaxFileSize;
|
2012-05-01 21:44:54 +02:00
|
|
|
$config['errorMessages']['maxFileSize'] = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'File.TOOLARGESHORT',
|
2016-03-29 02:38:24 +02:00
|
|
|
'File size exceeds {size}',
|
2012-05-01 21:44:54 +02:00
|
|
|
array('size' => File::format_size($config['maxFileSize']))
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Validation: Number of files
|
|
|
|
if ($allowedMaxFileNumber) {
|
|
|
|
if($allowedMaxFileNumber > 1) {
|
|
|
|
$config['errorMessages']['maxNumberOfFiles'] = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'UploadField.MAXNUMBEROFFILESSHORT',
|
2013-05-27 05:22:59 +02:00
|
|
|
'Can only upload {count} files',
|
|
|
|
array('count' => $allowedMaxFileNumber)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$config['errorMessages']['maxNumberOfFiles'] = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'UploadField.MAXNUMBEROFFILESONE',
|
2013-05-27 05:22:59 +02:00
|
|
|
'Can only upload one file'
|
|
|
|
);
|
|
|
|
}
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2013-03-08 04:02:38 +01:00
|
|
|
|
2014-04-16 11:08:25 +02:00
|
|
|
// add overwrite warning error message to the config object sent to Javascript
|
2013-05-27 05:22:59 +02:00
|
|
|
if ($this->getOverwriteWarning()) {
|
2013-03-08 04:02:38 +01:00
|
|
|
$config['errorMessages']['overwriteWarning'] =
|
2013-05-27 05:22:59 +02:00
|
|
|
_t('UploadField.OVERWRITEWARNING', 'File with the same name already exists');
|
2013-03-08 04:02:38 +01:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$mergedConfig = array_merge($config, $this->ufConfig);
|
2016-07-07 20:55:37 +02:00
|
|
|
return parent::Field(array(
|
2016-07-14 04:43:53 +02:00
|
|
|
'configString' => Convert::raw2json($mergedConfig),
|
2013-05-27 05:22:59 +02:00
|
|
|
'config' => new ArrayData($mergedConfig),
|
|
|
|
'multiple' => $allowedMaxFileNumber !== 1
|
2016-07-07 20:55:37 +02:00
|
|
|
));
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validation method for this field, called when the entire form is validated
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param Validator $validator
|
|
|
|
* @return boolean
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2013-05-27 05:22:59 +02:00
|
|
|
$name = $this->getName();
|
|
|
|
$files = $this->getItems();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// If there are no files then quit
|
|
|
|
if($files->count() == 0) return true;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// Check max number of files
|
2013-05-27 05:22:59 +02:00
|
|
|
$maxFiles = $this->getAllowedMaxFileNumber();
|
|
|
|
if($maxFiles && ($files->count() > $maxFiles)) {
|
|
|
|
$validator->validationError(
|
|
|
|
$name,
|
|
|
|
_t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'UploadField.MAXNUMBEROFFILES',
|
2013-05-27 05:22:59 +02:00
|
|
|
'Max number of {count} file(s) exceeded.',
|
|
|
|
array('count' => $maxFiles)
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Revalidate each file against nested validator
|
|
|
|
$this->upload->clearErrors();
|
|
|
|
foreach($files as $file) {
|
|
|
|
// Generate $_FILES style file attribute array for upload validator
|
|
|
|
$tmpFile = array(
|
|
|
|
'name' => $file->Name,
|
|
|
|
'type' => null, // Not used for type validation
|
|
|
|
'size' => $file->AbsoluteSize,
|
|
|
|
'tmp_name' => null, // Should bypass is_uploaded_file check
|
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
);
|
|
|
|
$this->upload->validate($tmpFile);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check all errors
|
|
|
|
if($errors = $this->upload->getErrors()) {
|
|
|
|
foreach($errors as $error) {
|
|
|
|
$validator->validationError($name, $error, "validation");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2012-02-03 00:59:40 +01:00
|
|
|
* @return UploadField_ItemHandler
|
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function handleItem(HTTPRequest $request) {
|
2012-02-03 00:59:40 +01:00
|
|
|
return $this->getItemHandler($request->param('ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $itemID
|
|
|
|
* @return UploadField_ItemHandler
|
|
|
|
*/
|
|
|
|
public function getItemHandler($itemID) {
|
2012-04-04 16:59:30 +02:00
|
|
|
return UploadField_ItemHandler::create($this, $itemID);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 00:58:58 +01:00
|
|
|
/**
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return UploadField_SelectHandler
|
2012-02-08 00:58:58 +01:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function handleSelect(HTTPRequest $request) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->canAttachExisting()) {
|
|
|
|
return $this->httpError(403);
|
|
|
|
}
|
2012-12-20 02:52:44 +01:00
|
|
|
return UploadField_SelectHandler::create($this, $this->getFolderName());
|
2012-02-08 00:58:58 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Given an array of post variables, extract all temporary file data into an array
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param array $postVars Array of posted form data
|
|
|
|
* @return array List of temporary file data
|
|
|
|
*/
|
|
|
|
protected function extractUploadedFileData($postVars) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Note: Format of posted file parameters in php is a feature of using
|
|
|
|
// <input name='{$Name}[Uploads][]' /> for multiple file uploads
|
|
|
|
$tmpFiles = array();
|
|
|
|
if( !empty($postVars['tmp_name'])
|
|
|
|
&& is_array($postVars['tmp_name'])
|
|
|
|
&& !empty($postVars['tmp_name']['Uploads'])
|
|
|
|
) {
|
|
|
|
for($i = 0; $i < count($postVars['tmp_name']['Uploads']); $i++) {
|
|
|
|
// Skip if "empty" file
|
|
|
|
if(empty($postVars['tmp_name']['Uploads'][$i])) continue;
|
|
|
|
$tmpFile = array();
|
|
|
|
foreach(array('name', 'type', 'tmp_name', 'error', 'size') as $field) {
|
|
|
|
$tmpFile[$field] = $postVars[$field]['Uploads'][$i];
|
|
|
|
}
|
|
|
|
$tmpFiles[] = $tmpFile;
|
|
|
|
}
|
|
|
|
} elseif(!empty($postVars['tmp_name'])) {
|
|
|
|
// Fallback to allow single file uploads (method used by AssetUploadField)
|
|
|
|
$tmpFiles[] = $postVars;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
return $tmpFiles;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Loads the temporary file data into a File object
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param array $tmpFile Temporary file data
|
|
|
|
* @param string $error Error message
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return AssetContainer File object, or null if error
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
protected function saveTemporaryFile($tmpFile, &$error = null) {
|
|
|
|
// Determine container object
|
|
|
|
$error = null;
|
|
|
|
$fileObject = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
if (empty($tmpFile)) {
|
|
|
|
$error = _t('UploadField.FIELDNOTSET', 'File information not found');
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
if($tmpFile['error']) {
|
|
|
|
$error = $tmpFile['error'];
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Search for relations that can hold the uploaded files, but don't fallback
|
|
|
|
// to default if there is no automatic relation
|
|
|
|
if ($relationClass = $this->getRelationAutosetClass(null)) {
|
2016-01-26 04:56:07 +01:00
|
|
|
// Allow File to be subclassed
|
2016-08-19 00:51:35 +02:00
|
|
|
if($relationClass === 'SilverStripe\\Assets\\File' && isset($tmpFile['name'])) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$relationClass = File::get_class_for_file_extension(
|
|
|
|
File::get_file_extension($tmpFile['name'])
|
|
|
|
);
|
|
|
|
}
|
2013-05-27 05:22:59 +02:00
|
|
|
// Create new object explicitly. Otherwise rely on Upload::load to choose the class.
|
|
|
|
$fileObject = Object::create($relationClass);
|
2015-09-15 04:52:02 +02:00
|
|
|
if(! ($fileObject instanceof DataObject) || !($fileObject instanceof AssetContainer)) {
|
|
|
|
throw new InvalidArgumentException("Invalid asset container $relationClass");
|
|
|
|
}
|
2013-05-27 05:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the uploaded file into a new file object.
|
|
|
|
try {
|
|
|
|
$this->upload->loadIntoFile($tmpFile, $fileObject, $this->getFolderName());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// we shouldn't get an error here, but just in case
|
|
|
|
$error = $e->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if upload field has an error
|
|
|
|
if ($this->upload->isError()) {
|
|
|
|
$error = implode(' ' . PHP_EOL, $this->upload->getErrors());
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// return file
|
|
|
|
return $this->upload->getFile();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Safely encodes the File object with all standard fields required
|
|
|
|
* by the front end
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param File|AssetContainer $file Object which contains a file
|
2013-05-27 05:22:59 +02:00
|
|
|
* @return array Array encoded list of file attributes
|
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
protected function encodeFileAttributes(AssetContainer $file) {
|
2013-05-27 05:22:59 +02:00
|
|
|
// Collect all output data.
|
2015-09-15 04:52:02 +02:00
|
|
|
$customised = $this->customiseFile($file);
|
2013-05-27 05:22:59 +02:00
|
|
|
return array(
|
|
|
|
'id' => $file->ID,
|
2015-09-15 04:52:02 +02:00
|
|
|
'name' => basename($file->getFilename()),
|
|
|
|
'url' => $file->getURL(),
|
|
|
|
'thumbnail_url' => $customised->UploadFieldThumbnailURL,
|
|
|
|
'edit_url' => $customised->UploadFieldEditLink,
|
|
|
|
'size' => $file->getAbsoluteSize(),
|
|
|
|
'type' => File::get_file_type($file->getFilename()),
|
2016-08-01 07:04:43 +02:00
|
|
|
'buttons' => (string)$customised->UploadFieldFileButtons,
|
2013-05-27 05:22:59 +02:00
|
|
|
'fieldname' => $this->getName()
|
|
|
|
);
|
|
|
|
}
|
2012-02-08 00:58:58 +01:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Action to handle upload of a single file
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
|
|
|
* @return HTTPResponse
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function upload(HTTPRequest $request) {
|
2013-01-09 11:34:02 +01:00
|
|
|
if($this->isDisabled() || $this->isReadonly() || !$this->canUpload()) {
|
|
|
|
return $this->httpError(403);
|
|
|
|
}
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
$token = $this->getForm()->getSecurityToken();
|
|
|
|
if(!$token->checkRequest($request)) return $this->httpError(400);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Get form details
|
|
|
|
$name = $this->getName();
|
|
|
|
$postVars = $request->postVar($name);
|
|
|
|
|
2014-09-20 16:09:48 +02:00
|
|
|
// Extract uploaded files from Form data
|
2013-05-27 05:22:59 +02:00
|
|
|
$uploadedFiles = $this->extractUploadedFileData($postVars);
|
2014-09-20 16:09:48 +02:00
|
|
|
$return = array();
|
|
|
|
|
|
|
|
// Save the temporary files into a File objects
|
|
|
|
// and save data/error on a per file basis
|
|
|
|
foreach ($uploadedFiles as $tempFile) {
|
|
|
|
$file = $this->saveTemporaryFile($tempFile, $error);
|
|
|
|
if(empty($file)) {
|
|
|
|
array_push($return, array('error' => $error));
|
|
|
|
} else {
|
|
|
|
array_push($return, $this->encodeFileAttributes($file));
|
|
|
|
}
|
|
|
|
$this->upload->clearErrors();
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Format response with json
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse(Convert::raw2json($return));
|
2012-02-03 00:59:40 +01:00
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2012-02-08 00:58:58 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Retrieves details for files that this field wishes to attache to the
|
2013-05-27 05:22:59 +02:00
|
|
|
* client-side form
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
2012-02-08 00:58:58 +01:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function attach(HTTPRequest $request) {
|
2012-02-08 00:58:58 +01:00
|
|
|
if(!$request->isPOST()) return $this->httpError(403);
|
2013-01-11 05:33:06 +01:00
|
|
|
if(!$this->canAttachExisting()) return $this->httpError(403);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Retrieve file attributes required by front end
|
2012-02-08 00:58:58 +01:00
|
|
|
$return = array();
|
2012-05-28 11:13:42 +02:00
|
|
|
$files = File::get()->byIDs($request->postVar('ids'));
|
2012-02-08 00:58:58 +01:00
|
|
|
foreach($files as $file) {
|
2013-05-27 05:22:59 +02:00
|
|
|
$return[] = $this->encodeFileAttributes($file);
|
2012-02-08 00:58:58 +01:00
|
|
|
}
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse(Convert::raw2json($return));
|
2012-02-08 00:58:58 +01:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
|
|
|
return $response;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-07 18:17:37 +01:00
|
|
|
/**
|
2014-05-05 04:49:25 +02:00
|
|
|
* Check if file exists, both checking filtered filename and exact filename
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-05-05 04:49:25 +02:00
|
|
|
* @param string $originalFile Filename
|
|
|
|
* @return bool
|
2012-02-07 18:17:37 +01:00
|
|
|
*/
|
2014-05-05 04:49:25 +02:00
|
|
|
protected function checkFileExists($originalFile) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check both original and safely filtered filename
|
|
|
|
$nameFilter = FileNameFilter::create();
|
2014-05-05 04:49:25 +02:00
|
|
|
$filteredFile = $nameFilter->filter($originalFile);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-20 02:03:40 +01:00
|
|
|
// Resolve expected folder name
|
|
|
|
$folderName = $this->getFolderName();
|
|
|
|
$folder = Folder::find_or_make($folderName);
|
2015-09-15 04:52:02 +02:00
|
|
|
$parentPath = $folder ? $folder->getFilename() : '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// check if either file exists
|
2015-09-15 04:52:02 +02:00
|
|
|
return File::find($parentPath.$originalFile) || File::find($parentPath.$filteredFile);
|
2014-05-05 04:49:25 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-05 04:49:25 +02:00
|
|
|
/**
|
|
|
|
* Determines if a specified file exists
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
2014-05-05 04:49:25 +02:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function fileexists(HTTPRequest $request) {
|
2014-05-05 04:49:25 +02:00
|
|
|
// Assert that requested filename doesn't attempt to escape the directory
|
|
|
|
$originalFile = $request->requestVar('filename');
|
|
|
|
if($originalFile !== basename($originalFile)) {
|
|
|
|
$return = array(
|
|
|
|
'error' => _t('File.NOVALIDUPLOAD', 'File is not a valid upload')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$return = array(
|
|
|
|
'exists' => $this->checkFileExists($originalFile)
|
|
|
|
);
|
2012-02-07 18:17:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Encode and present response
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse(Convert::raw2json($return));
|
2013-05-27 05:22:59 +02:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
2014-05-05 04:49:25 +02:00
|
|
|
if (!empty($return['error'])) $response->setStatusCode(400);
|
2013-05-27 05:22:59 +02:00
|
|
|
return $response;
|
2012-02-07 18:17:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function performReadonlyTransformation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->addExtraClass('readonly');
|
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
|
|
|
}
|
2012-02-07 18:17:37 +01:00
|
|
|
|
|
|
|
/**
|
2013-05-27 05:22:59 +02:00
|
|
|
* Gets the foreign class that needs to be created, or 'File' as default if there
|
|
|
|
* is no relationship, or it cannot be determined.
|
2012-04-20 06:20:00 +02:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $default Default value to return if no value could be calculated
|
2012-04-20 06:20:00 +02:00
|
|
|
* @return string Foreign class name.
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function getRelationAutosetClass($default = 'SilverStripe\\Assets\\File') {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Don't autodetermine relation if no relationship between parent record
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->relationAutoSetting) {
|
|
|
|
return $default;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check record and name
|
2012-04-20 06:20:00 +02:00
|
|
|
$name = $this->getName();
|
|
|
|
$record = $this->getRecord();
|
2013-05-27 05:22:59 +02:00
|
|
|
if(empty($name) || empty($record)) {
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
$class = $record->getRelationClass($name);
|
|
|
|
return empty($class) ? $default : $class;
|
|
|
|
}
|
2013-01-11 05:33:06 +01:00
|
|
|
}
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|