diff --git a/bulkUpload/BULK_UPLOAD.md b/bulkUpload/BULK_UPLOAD.md index 3de12a9..6c53828 100644 --- a/bulkUpload/BULK_UPLOAD.md +++ b/bulkUpload/BULK_UPLOAD.md @@ -13,18 +13,28 @@ $fileRelationName (string, optional): The name of the `Image` or `File` has_one $config->addComponent(new GridFieldBulkUpload($fileRelationName)); ## Configuration -The component's option can be configurated individually or in bulk through the 'config' functions like this: +### Component configuration +The component's option can be configurated through the `setConfig` functions like this: $config->getComponentByType('GridFieldBulkUpload')->setConfig($reference, $value); -### $config overview The available configuration options are: * 'fileRelationName' : sets the name of the `Image` or `File` has_one field to use (i.e. 'MyImage') -* 'folderName' : name of the folder where the images or files should be uploaded -* 'maxFileSize' : integer, maximum upload file size in bytes -* 'sequentialUploads' : boolean, if true files will be uploaded one by one -* 'canAttachExisting' : boolean, if false the "From files" button will not be displayed in the UploadField (default: true) -* 'canPreviewFolder' : boolean, if false the upload location will not be displayed in the UploadField (default: true) + +### UploadField configuration +The underlying `UploadField` can be configured via a set of APIs: +* `setUfConfig($reference, $value)` is used to set an `UploadField::$ufConfig` settings +* `setUfSetup($function, $param)` is used to pass function calls on to the `UploadField` itself +* `setUfValidatorSetup($function, $param)` is used to pass function calls on to the `UploadField` `Validator` itself + +For example, to set the upload folder, which is set by calling `setFolderName` on the `UploadField`, and setting the upload method as sequential, you would use the following: + + $config->getComponentByType('GridFieldBulkUpload') + ->setUfSetup('setFolderName', 'myFolder') + ->setUfConfig('sequentialUploads', true); + +Please see the [`UploadField` api](http://api.silverstripe.org/master/class-UploadField.html) and the [`Upload` api](http://api.silverstripe.org/master/class-Upload.html) for more info. + ## Bulk Editing To get a quick edit shortcut to all the newly upload files, please also add the `GridFieldBulkManager` component to your `GridFieldConfig`. \ No newline at end of file diff --git a/bulkUpload/code/GridFieldBulkUpload.php b/bulkUpload/code/GridFieldBulkUpload.php index 6b0fa36..24c9f19 100644 --- a/bulkUpload/code/GridFieldBulkUpload.php +++ b/bulkUpload/code/GridFieldBulkUpload.php @@ -12,23 +12,60 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle * component configuration * * 'fileRelationName' => field name of the $has_one File/Image relation - * 'folderName' => where to upload the files - * 'maxFileSize' => maximum file size allowed per upload - * 'sequentialUploads' => process uploads 1 after the other rather than all at once - * 'canAttachExisting' => displays "From files" button in the UploadField - * 'canPreviewFolder' => displays the upload location in the UploadField * @var array */ protected $config = array( - 'fileRelationName' => null, - 'folderName' => 'bulkUpload', - 'maxFileSize' => null, - 'sequentialUploads' => false, + 'fileRelationName' => null + ); + + + /** + * UploadField configuration. + * These options are passed on directly to the UploadField + * via {@link UploadField::setConfig()} api + * + * Defaults are: * + * 'sequentialUploads' => false : process uploads 1 after the other rather than all at once + * 'canAttachExisting' => true : displays "From files" button in the UploadField + * 'canPreviewFolder' => true : displays the upload location in the UploadField + * + * @var array + */ + protected $ufConfig = array( + 'sequentialUploads' => false, 'canAttachExisting' => true, 'canPreviewFolder' => true ); + /** + * UploadField setup function calls. + * List of setup functions to call on {@link UploadField} with the value to pass + * + * e.g. array('setFolderName' => 'bulkUpload') will result in: + * $uploadField->setFolderName('bulkUpload') + * + * @var array + */ + protected $ufSetup = array( + 'setFolderName' => 'bulkUpload' + ); + + + /** + * UploadField Validator setup function calls. + * List of setup functions to call on {@link Upload::validator} with the value to pass + * + * e.g. array('setAllowedMaxFileSize' => 10) will result in: + * $uploadField->getValidator()->setAllowedMaxFileSize(10) + * + * @var array + */ + protected $ufValidatorSetup = array( + 'setAllowedMaxFileSize' => null + ); + + /** * Component constructor * @@ -57,37 +94,52 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle user_error("Unknown option reference: $reference", E_USER_ERROR); } - //makes sure maxFileSize is INT - if ( $reference == 'maxFileSize' && !is_int($value) ) - { - user_warning("maxFileSize should be an Integer. Setting it to Auto.", E_USER_ERROR); - $value = null; - } - - //sequentialUploads true/false - if ( $reference == 'sequentialUploads' && !is_bool($value) ) - { - $value = false; - } - - //canAttachExisting true/false - if ( $reference == 'canAttachExisting' && !is_bool($value) ) - { - $value = true; - } - - //canPreviewFolder true/false - if ( $reference == 'canPreviewFolder' && !is_bool($value) ) - { - $value = true; - } - $this->config[$reference] = $value; + return $this; + } + + + /** + * Set an UploadField configuration parameter + * + * @param string $reference + * @param mixed $value + */ + function setUfConfig ( $reference, $value ) + { + $this->ufConfig[$reference] = $value; + return $this; + } + + + /** + * Set an UploadField setup function call + * + * @param string $function + * @param mixed $param + */ + function setUfSetup ( $function, $param ) + { + $this->ufSetup[$function] = $param; + return $this; + } + + + /** + * Set an UploadField Validator setup function call + * + * @param string $function + * @param mixed $param + */ + function setUfValidatorSetup ( $function, $param ) + { + $this->ufValidatorSetup[$function] = $param; + return $this; } /** - * Returns one $config parameter of the full $config + * Returns one $config reference or the full $config * * @param string $reference $congif parameter to return * @return mixed @@ -97,6 +149,45 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle if ( $reference ) return $this->config[$reference]; else return $this->config; } + + + /** + * Returns one $ufConfig reference or the full config. + * + * @param string $reference $ufConfig parameter to return + * @return mixed + */ + function getUfConfig ( $reference = false ) + { + if ( $reference ) return $this->ufConfig[$reference]; + else return $this->ufConfig; + } + + + /** + * Returns one $ufSetup reference or the full config. + * + * @param string $reference $ufSetup parameter to return + * @return mixed + */ + function getUfSetup ( $reference = false ) + { + if ( $reference ) return $this->ufSetup[$reference]; + else return $this->ufSetup; + } + + + /** + * Returns one $ufValidatorSetup reference or the full config. + * + * @param string $reference $ufValidatorSetup parameter to return + * @return mixed + */ + function getUfValidatorSetup ( $reference = false ) + { + if ( $reference ) return $this->ufValidatorSetup[$reference]; + else return $this->ufValidatorSetup; + } /** @@ -175,8 +266,6 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle ->setConfig('previewMaxWidth', 20) ->setConfig('previewMaxHeight', 20) ->setConfig('changeDetection', false) - ->setConfig('canPreviewFolder', $this->getConfig('canPreviewFolder')) - ->setConfig('canAttachExisting', $this->getConfig('canAttachExisting')) ->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber) @@ -189,22 +278,23 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle ->setConfig('urlFileExists', $gridField->Link('bulkupload/fileexists')) ; - //max file size - $maxFileSize = $this->getConfig('maxFileSize'); - if ( $maxFileSize !== null ) - { - $uploadField->getValidator()->setAllowedMaxFileSize( $maxFileSize ); - } + //set UploadField config + foreach ($this->ufConfig as $key => $val) + { + $uploadField->setConfig($key, $val); + } - //upload dir - $uploadDir = $this->getConfig('folderName'); - if ( $uploadDir !== null ) - { - $uploadField->setFolderName($uploadDir); - } + //UploadField setup + foreach ($this->ufSetup as $fn => $param) + { + $uploadField->{$fn}($param); + } - //sequential upload - $uploadField->setConfig('sequentialUploads', $this->getConfig('sequentialUploads')); + //UploadField Validator setup + foreach ($this->ufValidatorSetup as $fn => $param) + { + $uploadField->getValidator()->{$fn}($param); + } return $uploadField; }