Rename Uploadable -> UploadReceiver and FileUploadable -> FileUploadReceiver

This commit is contained in:
Damian Mooyman 2016-11-25 11:33:33 +13:00
parent 7cba50e3a5
commit d4abfea4eb
5 changed files with 56 additions and 91 deletions

View File

@ -26,9 +26,8 @@ use Exception;
* - Files can't be edited once uploaded. * - Files can't be edited once uploaded.
* - Attached files can only be removed, not deleted. * - Attached files can only be removed, not deleted.
*/ */
class AssetField extends FormField class AssetField extends FormField {
{ use UploadReceiver;
use Uploadable;
/** /**
* @var array * @var array
@ -158,14 +157,13 @@ class AssetField extends FormField
* @param string $name The internal field name, passed to forms. * @param string $name The internal field name, passed to forms.
* @param string $title The field label. * @param string $title The field label.
*/ */
public function __construct($name, $title = null) public function __construct($name, $title = null) {
{
$this->addExtraClass('ss-upload'); // class, used by js $this->addExtraClass('ss-upload'); // class, used by js
$this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only $this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only
$this->ufConfig = array_merge($this->ufConfig, self::config()->defaultConfig); $this->ufConfig = array_merge($this->ufConfig, self::config()->defaultConfig);
$this->constructUploadable(); $this->constructUploadReceiver();
parent::__construct($name, $title); parent::__construct($name, $title);
// AssetField always uses rename replacement method // AssetField always uses rename replacement method
@ -188,8 +186,7 @@ class AssetField extends FormField
* @param string * @param string
* @return $this * @return $this
*/ */
public function setTemplateFileButtons($template) public function setTemplateFileButtons($template) {
{
$this->templateFileButtons = $template; $this->templateFileButtons = $template;
return $this; return $this;
} }
@ -197,8 +194,7 @@ class AssetField extends FormField
/** /**
* @return string * @return string
*/ */
public function getTemplateFileButtons() public function getTemplateFileButtons() {
{
return $this->_templates($this->templateFileButtons, '_FileButtons'); return $this->_templates($this->templateFileButtons, '_FileButtons');
} }
@ -207,8 +203,7 @@ class AssetField extends FormField
* *
* @return boolean * @return boolean
*/ */
public function canPreviewFolder() public function canPreviewFolder() {
{
if(!$this->isActive()) { if(!$this->isActive()) {
return false; return false;
} }
@ -227,8 +222,7 @@ class AssetField extends FormField
* required permission code * required permission code
* @return $this Self reference * @return $this Self reference
*/ */
public function setCanPreviewFolder($canPreviewFolder) public function setCanPreviewFolder($canPreviewFolder) {
{
return $this->setConfig('canPreviewFolder', $canPreviewFolder); return $this->setConfig('canPreviewFolder', $canPreviewFolder);
} }
@ -236,8 +230,7 @@ class AssetField extends FormField
* @param string * @param string
* @return $this * @return $this
*/ */
public function setDisplayFolderName($name) public function setDisplayFolderName($name) {
{
$this->displayFolderName = $name; $this->displayFolderName = $name;
return $this; return $this;
} }
@ -245,8 +238,7 @@ class AssetField extends FormField
/** /**
* @return string * @return string
*/ */
public function getDisplayFolderName() public function getDisplayFolderName() {
{
return $this->displayFolderName; return $this->displayFolderName;
} }
@ -256,8 +248,7 @@ class AssetField extends FormField
* @param DataObject $record * @param DataObject $record
* @return $this * @return $this
*/ */
public function setRecord($record) public function setRecord($record) {
{
$this->record = $record; $this->record = $record;
return $this; return $this;
} }
@ -268,8 +259,7 @@ class AssetField extends FormField
* *
* @return DataObject * @return DataObject
*/ */
public function getRecord() public function getRecord() {
{
if (!$this->record if (!$this->record
&& $this->form && $this->form
&& ($record = $this->form->getRecord()) && ($record = $this->form->getRecord())
@ -280,8 +270,7 @@ class AssetField extends FormField
return $this->record; return $this->record;
} }
public function setValue($value, $record = null) public function setValue($value, $record = null) {
{
// Extract value from underlying record // Extract value from underlying record
if(empty($value) && $this->getName() && $record instanceof DataObject) { if(empty($value) && $this->getName() && $record instanceof DataObject) {
$name = $this->getName(); $name = $this->getName();
@ -316,14 +305,12 @@ class AssetField extends FormField
return parent::setValue($value, $record); return parent::setValue($value, $record);
} }
public function Value() public function Value() {
{
// Re-override FileField Value to use data value // Re-override FileField Value to use data value
return $this->dataValue(); return $this->dataValue();
} }
public function saveInto(DataObjectInterface $record) public function saveInto(DataObjectInterface $record) {
{
// Check required relation details are available // Check required relation details are available
$name = $this->getName(); $name = $this->getName();
if(!$name) { if(!$name) {
@ -348,8 +335,7 @@ class AssetField extends FormField
* @param mixed $val * @param mixed $val
* @return $this self reference * @return $this self reference
*/ */
public function setConfig($key, $val) public function setConfig($key, $val) {
{
$this->ufConfig[$key] = $val; $this->ufConfig[$key] = $val;
return $this; return $this;
} }
@ -362,8 +348,7 @@ class AssetField extends FormField
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function getConfig($key) public function getConfig($key) {
{
if(isset($this->ufConfig[$key])) { if(isset($this->ufConfig[$key])) {
return $this->ufConfig[$key]; return $this->ufConfig[$key];
} }
@ -374,8 +359,7 @@ class AssetField extends FormField
* *
* @return boolean * @return boolean
*/ */
public function getAutoUpload() public function getAutoUpload() {
{
return $this->getConfig('autoUpload'); return $this->getConfig('autoUpload');
} }
@ -385,8 +369,7 @@ class AssetField extends FormField
* @param boolean $autoUpload * @param boolean $autoUpload
* @return $this Self reference * @return $this Self reference
*/ */
public function setAutoUpload($autoUpload) public function setAutoUpload($autoUpload) {
{
return $this->setConfig('autoUpload', $autoUpload); return $this->setConfig('autoUpload', $autoUpload);
} }
@ -395,8 +378,7 @@ class AssetField extends FormField
* *
* @return boolean * @return boolean
*/ */
public function canUpload() public function canUpload() {
{
if(!$this->isActive()) { if(!$this->isActive()) {
return false; return false;
} }
@ -415,8 +397,7 @@ class AssetField extends FormField
* permission code * permission code
* @return $this Self reference * @return $this Self reference
*/ */
public function setCanUpload($canUpload) public function setCanUpload($canUpload) {
{
return $this->setConfig('canUpload', $canUpload); return $this->setConfig('canUpload', $canUpload);
} }
@ -425,8 +406,7 @@ class AssetField extends FormField
* *
* @return bool * @return bool
*/ */
public function isActive() public function isActive() {
{
return !$this->isDisabled() && !$this->isReadonly(); return !$this->isDisabled() && !$this->isReadonly();
} }
@ -435,8 +415,7 @@ class AssetField extends FormField
* *
* @return int * @return int
*/ */
public function getPreviewMaxWidth() public function getPreviewMaxWidth() {
{
return $this->getConfig('previewMaxWidth'); return $this->getConfig('previewMaxWidth');
} }
@ -446,8 +425,7 @@ class AssetField extends FormField
* @param int $previewMaxWidth * @param int $previewMaxWidth
* @return $this Self reference * @return $this Self reference
*/ */
public function setPreviewMaxWidth($previewMaxWidth) public function setPreviewMaxWidth($previewMaxWidth) {
{
return $this->setConfig('previewMaxWidth', $previewMaxWidth); return $this->setConfig('previewMaxWidth', $previewMaxWidth);
} }
@ -456,8 +434,7 @@ class AssetField extends FormField
* *
* @return int * @return int
*/ */
public function getPreviewMaxHeight() public function getPreviewMaxHeight() {
{
return $this->getConfig('previewMaxHeight'); return $this->getConfig('previewMaxHeight');
} }
@ -467,8 +444,7 @@ class AssetField extends FormField
* @param int $previewMaxHeight * @param int $previewMaxHeight
* @return $this Self reference * @return $this Self reference
*/ */
public function setPreviewMaxHeight($previewMaxHeight) public function setPreviewMaxHeight($previewMaxHeight) {
{
return $this->setConfig('previewMaxHeight', $previewMaxHeight); return $this->setConfig('previewMaxHeight', $previewMaxHeight);
} }
@ -479,8 +455,7 @@ class AssetField extends FormField
* @see javascript/UploadField_uploadtemplate.js * @see javascript/UploadField_uploadtemplate.js
* @return string * @return string
*/ */
public function getUploadTemplateName() public function getUploadTemplateName() {
{
return $this->getConfig('uploadTemplateName'); return $this->getConfig('uploadTemplateName');
} }
@ -490,8 +465,7 @@ class AssetField extends FormField
* @param string $uploadTemplateName * @param string $uploadTemplateName
* @return $this Self reference * @return $this Self reference
*/ */
public function setUploadTemplateName($uploadTemplateName) public function setUploadTemplateName($uploadTemplateName) {
{
return $this->setConfig('uploadTemplateName', $uploadTemplateName); return $this->setConfig('uploadTemplateName', $uploadTemplateName);
} }
@ -502,8 +476,7 @@ class AssetField extends FormField
* @see javascript/DownloadField_downloadtemplate.js * @see javascript/DownloadField_downloadtemplate.js
* @return string * @return string
*/ */
public function getDownloadTemplateName() public function getDownloadTemplateName() {
{
return $this->getConfig('downloadTemplateName'); return $this->getConfig('downloadTemplateName');
} }
@ -513,13 +486,11 @@ class AssetField extends FormField
* @param string $downloadTemplateName * @param string $downloadTemplateName
* @return $this Self reference * @return $this Self reference
*/ */
public function setDownloadTemplateName($downloadTemplateName) public function setDownloadTemplateName($downloadTemplateName) {
{
return $this->setConfig('downloadTemplateName', $downloadTemplateName); return $this->setConfig('downloadTemplateName', $downloadTemplateName);
} }
public function extraClass() public function extraClass() {
{
if($this->isDisabled()) { if($this->isDisabled()) {
$this->addExtraClass('disabled'); $this->addExtraClass('disabled');
} }
@ -530,8 +501,7 @@ class AssetField extends FormField
return parent::extraClass(); return parent::extraClass();
} }
public function Field($properties = array()) public function Field($properties = array()) {
{
// Calculated config as per jquery.fileupload-ui.js // Calculated config as per jquery.fileupload-ui.js
$config = array( $config = array(
'allowedMaxFileNumber' => 1, // Only one file allowed for AssetField 'allowedMaxFileNumber' => 1, // Only one file allowed for AssetField
@ -577,8 +547,7 @@ class AssetField extends FormField
* @param Validator $validator * @param Validator $validator
* @return boolean * @return boolean
*/ */
public function validate($validator) public function validate($validator) {
{
$name = $this->getName(); $name = $this->getName();
$value = $this->Value(); $value = $this->Value();
@ -620,8 +589,7 @@ class AssetField extends FormField
* @param array $postVars Array of posted form data * @param array $postVars Array of posted form data
* @return array data for uploaded file * @return array data for uploaded file
*/ */
protected function extractUploadedFileData($postVars) protected function extractUploadedFileData($postVars) {
{
// Note: Format of posted file parameters in php is a feature of using // Note: Format of posted file parameters in php is a feature of using
// <input name='{$Name}[Upload]' /> for multiple file uploads // <input name='{$Name}[Upload]' /> for multiple file uploads
@ -653,8 +621,7 @@ class AssetField extends FormField
* @param string $error Error message * @param string $error Error message
* @return array Result of saved file, or null if error * @return array Result of saved file, or null if error
*/ */
protected function saveTemporaryFile($tmpFile, &$error = null) protected function saveTemporaryFile($tmpFile, &$error = null) {
{
$error = null; $error = null;
if (empty($tmpFile)) { if (empty($tmpFile)) {
$error = _t('UploadField.FIELDNOTSET', 'File information not found'); $error = _t('UploadField.FIELDNOTSET', 'File information not found');
@ -696,8 +663,7 @@ class AssetField extends FormField
* @param string $variant * @param string $variant
* @return array Encoded list of file attributes * @return array Encoded list of file attributes
*/ */
protected function encodeAssetAttributes($filename, $hash, $variant) protected function encodeAssetAttributes($filename, $hash, $variant) {
{
// Force regeneration of file thumbnail for this tuple (without saving into db) // Force regeneration of file thumbnail for this tuple (without saving into db)
$object = DBFile::create(); $object = DBFile::create();
$object->setValue(array('Filename' => $filename, 'Hash' => $hash, 'Variant' => $variant)); $object->setValue(array('Filename' => $filename, 'Hash' => $hash, 'Variant' => $variant));
@ -725,8 +691,7 @@ class AssetField extends FormField
* @param HTTPRequest $request * @param HTTPRequest $request
* @return HTTPResponse * @return HTTPResponse
*/ */
public function upload(HTTPRequest $request) public function upload(HTTPRequest $request) {
{
if($this->isDisabled() || $this->isReadonly() || !$this->canUpload()) { if($this->isDisabled() || $this->isReadonly() || !$this->canUpload()) {
return $this->httpError(403); return $this->httpError(403);
} }
@ -767,8 +732,7 @@ class AssetField extends FormField
return $response; return $response;
} }
public function performReadonlyTransformation() public function performReadonlyTransformation() {
{
$clone = clone $this; $clone = clone $this;
$clone->addExtraClass('readonly'); $clone->addExtraClass('readonly');
$clone->setReadonly(true); $clone->setReadonly(true);
@ -782,13 +746,10 @@ class AssetField extends FormField
* @param string $default Default value to return if no value could be calculated * @param string $default Default value to return if no value could be calculated
* @return string Foreign class name. * @return string Foreign class name.
*/ */
public function getRelationAutosetClass($default = 'SilverStripe\\Assets\\File') public function getRelationAutosetClass($default = 'SilverStripe\\Assets\\File') {
{
// Don't autodetermine relation if no relationship between parent record // Don't autodetermine relation if no relationship between parent record
if (!$this->relationAutoSetting) { if(!$this->relationAutoSetting) return $default;
return $default;
}
// Check record and name // Check record and name
$name = $this->getName(); $name = $this->getName();
@ -804,10 +765,10 @@ class AssetField extends FormField
/** /**
* @return AssetStore * @return AssetStore
*/ */
protected function getAssetStore() protected function getAssetStore() {
{
return Injector::inst()->get('AssetStore'); return Injector::inst()->get('AssetStore');
} }
public function getAttributes() { public function getAttributes() {
return array_merge( return array_merge(
parent::getAttributes(), parent::getAttributes(),

View File

@ -47,7 +47,7 @@ use SilverStripe\Core\Object;
* </code> * </code>
*/ */
class FileField extends FormField { class FileField extends FormField {
use Uploadable; use UploadReceiver;
/** /**
* Flag to automatically determine and save a has_one-relationship * Flag to automatically determine and save a has_one-relationship
@ -67,7 +67,7 @@ class FileField extends FormField {
* @param int $value The value of the field. * @param int $value The value of the field.
*/ */
public function __construct($name, $title = null, $value = null) { public function __construct($name, $title = null, $value = null) {
$this->constructUploadable(); $this->constructUploadReceiver();
parent::__construct($name, $title, $value); parent::__construct($name, $title, $value);
} }

View File

@ -25,10 +25,12 @@ use SilverStripe\ORM\ValidationException;
* Additionally supports writing directly to the File table not attached * Additionally supports writing directly to the File table not attached
* to any parent record. * to any parent record.
* *
* Note that this trait expects to be applied to a {@see FormField} class
*
* @mixin FormField * @mixin FormField
*/ */
trait FileUploadable { trait FileUploadReceiver {
use Uploadable; use UploadReceiver;
/** /**
* Flag to automatically determine and save a has_one-relationship * Flag to automatically determine and save a has_one-relationship
@ -54,8 +56,8 @@ trait FileUploadable {
*/ */
protected $items; protected $items;
protected function constructFileUploadable() { protected function constructFileUploadReceiver() {
$this->constructUploadable(); $this->constructUploadReceiver();
} }

View File

@ -46,7 +46,7 @@ use Exception;
* since the required frontend dependencies are included through CMS bundling. * since the required frontend dependencies are included through CMS bundling.
*/ */
class UploadField extends FormField { class UploadField extends FormField {
use FileUploadable; use FileUploadReceiver;
/** /**
* @var array * @var array
@ -223,7 +223,7 @@ class UploadField extends FormField {
$this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only $this->addExtraClass('ss-uploadfield'); // class, used by css for uploadfield only
$this->ufConfig = self::config()->defaultConfig; $this->ufConfig = self::config()->defaultConfig;
$this->constructFileUploadable(); $this->constructFileUploadReceiver();
parent::__construct($name, $title); parent::__construct($name, $title);

View File

@ -10,9 +10,11 @@ use SilverStripe\Assets\Upload_Validator;
/** /**
* Represents a form field which has an Upload() instance and can upload to a folder * Represents a form field which has an Upload() instance and can upload to a folder
* *
* Note that this trait expects to be applied to a {@see FormField} class
*
* @mixin FormField * @mixin FormField
*/ */
trait Uploadable trait UploadReceiver
{ {
/** /**
* Upload object (needed for validation * Upload object (needed for validation
@ -34,7 +36,7 @@ trait Uploadable
/** /**
* Bootstrap Uploadable field * Bootstrap Uploadable field
*/ */
protected function constructUploadable() { protected function constructUploadReceiver() {
// Set Upload instance // Set Upload instance
$this->setUpload(Upload::create()); $this->setUpload(Upload::create());