mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
FIX #79 New API for the UploadField configuration
Exposes `setUfConfig`, `setUfSetup` and `setUfValidatorSetup` on the component
This commit is contained in:
parent
0cbbe2b1fc
commit
1010c9be6f
@ -13,18 +13,28 @@ $fileRelationName (string, optional): The name of the `Image` or `File` has_one
|
|||||||
$config->addComponent(new GridFieldBulkUpload($fileRelationName));
|
$config->addComponent(new GridFieldBulkUpload($fileRelationName));
|
||||||
|
|
||||||
## Configuration
|
## 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->getComponentByType('GridFieldBulkUpload')->setConfig($reference, $value);
|
||||||
|
|
||||||
### $config overview
|
|
||||||
The available configuration options are:
|
The available configuration options are:
|
||||||
* 'fileRelationName' : sets the name of the `Image` or `File` has_one field to use (i.e. 'MyImage')
|
* '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
|
### UploadField configuration
|
||||||
* 'sequentialUploads' : boolean, if true files will be uploaded one by one
|
The underlying `UploadField` can be configured via a set of APIs:
|
||||||
* 'canAttachExisting' : boolean, if false the "From files" button will not be displayed in the UploadField (default: true)
|
* `setUfConfig($reference, $value)` is used to set an `UploadField::$ufConfig` settings
|
||||||
* 'canPreviewFolder' : boolean, if false the upload location will not be displayed in the UploadField (default: true)
|
* `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
|
## Bulk Editing
|
||||||
To get a quick edit shortcut to all the newly upload files, please also add the `GridFieldBulkManager` component to your `GridFieldConfig`.
|
To get a quick edit shortcut to all the newly upload files, please also add the `GridFieldBulkManager` component to your `GridFieldConfig`.
|
@ -12,23 +12,60 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
|
|||||||
* component configuration
|
* component configuration
|
||||||
*
|
*
|
||||||
* 'fileRelationName' => field name of the $has_one File/Image relation
|
* '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
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $config = array(
|
protected $config = array(
|
||||||
'fileRelationName' => null,
|
'fileRelationName' => null
|
||||||
'folderName' => 'bulkUpload',
|
);
|
||||||
'maxFileSize' => null,
|
|
||||||
'sequentialUploads' => false,
|
|
||||||
|
/**
|
||||||
|
* 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,
|
'canAttachExisting' => true,
|
||||||
'canPreviewFolder' => 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
|
* Component constructor
|
||||||
*
|
*
|
||||||
@ -57,37 +94,52 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
|
|||||||
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
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;
|
$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
|
* @param string $reference $congif parameter to return
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@ -97,6 +149,45 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
|
|||||||
if ( $reference ) return $this->config[$reference];
|
if ( $reference ) return $this->config[$reference];
|
||||||
else return $this->config;
|
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('previewMaxWidth', 20)
|
||||||
->setConfig('previewMaxHeight', 20)
|
->setConfig('previewMaxHeight', 20)
|
||||||
->setConfig('changeDetection', false)
|
->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)
|
->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'))
|
->setConfig('urlFileExists', $gridField->Link('bulkupload/fileexists'))
|
||||||
;
|
;
|
||||||
|
|
||||||
//max file size
|
//set UploadField config
|
||||||
$maxFileSize = $this->getConfig('maxFileSize');
|
foreach ($this->ufConfig as $key => $val)
|
||||||
if ( $maxFileSize !== null )
|
{
|
||||||
{
|
$uploadField->setConfig($key, $val);
|
||||||
$uploadField->getValidator()->setAllowedMaxFileSize( $maxFileSize );
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//upload dir
|
//UploadField setup
|
||||||
$uploadDir = $this->getConfig('folderName');
|
foreach ($this->ufSetup as $fn => $param)
|
||||||
if ( $uploadDir !== null )
|
{
|
||||||
{
|
$uploadField->{$fn}($param);
|
||||||
$uploadField->setFolderName($uploadDir);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//sequential upload
|
//UploadField Validator setup
|
||||||
$uploadField->setConfig('sequentialUploads', $this->getConfig('sequentialUploads'));
|
foreach ($this->ufValidatorSetup as $fn => $param)
|
||||||
|
{
|
||||||
|
$uploadField->getValidator()->{$fn}($param);
|
||||||
|
}
|
||||||
|
|
||||||
return $uploadField;
|
return $uploadField;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user