NEW Override automatic DataObject class

Specify which class to use when creating the DataObject that contains
the image.
This commit is contained in:
Thierry François 2016-01-18 16:49:00 +02:00
parent ef0f9fbcb8
commit af7524efd1
3 changed files with 50 additions and 39 deletions

View File

@ -3,21 +3,22 @@ A component for uploading images and/or files in bulk into `DataObject` managed
## Usage 1
Simplest usage, add the component to your `GridFieldConfig` as below. The component will find the first `Image` or `File` has_one relation to use on the managed `DataObject`.
$config->addComponent(new GridFieldBulkUpload());
## Usage 2
You can specify which `Image` or `File` field to use.
You can specify which `Image` or `File` field to use and a specific `DataObject` class name to use.
$fileRelationName (string, optional): The name of the `Image` or `File` has_one field to use (If your relation is set has 'MyImage' => 'Image', the parameter should be 'MyImage')
$config->addComponent(new GridFieldBulkUpload($fileRelationName));
$objectClassName (string, optional): The class name of the `DataObject` to create (Usefull if for example your `GridField` holds `DataObject`s of different classes, like when used with the `GridFieldAddNewMultiClass` component.)
$config->addComponent(new GridFieldBulkUpload($fileRelationName, $objectClassName));
## Configuration
### Component configuration
The component's option can be configurated through the `setConfig` functions like this:
$config->getComponentByType('GridFieldBulkUpload')->setConfig($reference, $value);
The available configuration options are:
* 'fileRelationName' : sets the name of the `Image` or `File` has_one field to use (i.e. 'MyImage')
@ -37,4 +38,4 @@ Please see the [`UploadField` api](http://api.silverstripe.org/master/class-Uplo
## 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`.

View File

@ -7,14 +7,16 @@
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
{
/**
* component configuration.
*
* Component configuration.
*
* 'fileRelationName' => field name of the $has_one File/Image relation
* 'objectClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name
*
* @var array
*/
protected $config = array(
'fileRelationName' => null,
'objectClassName' => null
);
/**
@ -22,11 +24,11 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* These options are passed on directly to the UploadField
* via {@link UploadField::setConfig()} api.
*
* Defaults are: *
* 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(
@ -38,10 +40,10 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* 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(
@ -51,10 +53,10 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* 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(
@ -63,14 +65,18 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Component constructor.
*
*
* @param string $fileRelationName
*/
public function __construct($fileRelationName = null)
public function __construct($fileRelationName = null, $objectClassName = null)
{
if ($fileRelationName != null) {
$this->setConfig('fileRelationName', $fileRelationName);
}
if ($objectClassName != null) {
$this->setConfig('objectClassName', $objectClassName);
}
}
/* **********************************************************************
@ -79,7 +85,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Set a component configuration parameter.
*
*
* @param string $reference
* @param mixed $value
*/
@ -106,7 +112,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Set an UploadField configuration parameter.
*
*
* @param string $reference
* @param mixed $value
*/
@ -119,7 +125,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Set an UploadField setup function call.
*
*
* @param string $function
* @param mixed $param
*/
@ -132,7 +138,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Set an UploadField Validator setup function call.
*
*
* @param string $function
* @param mixed $param
*/
@ -145,7 +151,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Returns one $config reference or the full $config.
*
*
* @param string $reference $congif parameter to return
*
* @return mixed
@ -161,7 +167,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Returns one $ufConfig reference or the full config.
*
*
* @param string $reference $ufConfig parameter to return
*
* @return mixed
@ -177,7 +183,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Returns one $ufSetup reference or the full config.
*
*
* @param string $reference $ufSetup parameter to return
*
* @return mixed
@ -193,7 +199,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Returns one $ufValidatorSetup reference or the full config.
*
*
* @param string $reference $ufValidatorSetup parameter to return
*
* @return mixed
@ -210,7 +216,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Get the first has_one Image/File relation from the GridField managed DataObject
* i.e. 'MyImage' => 'Image' will return 'MyImage'.
*
*
* @return string Name of the $has_one relation
*/
public function getDefaultFileRelationName($gridField)
@ -232,7 +238,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Returns the name of the Image/File field name from the managed record
* Either as set in the component config or the default one.
*
*
* @return string
*/
public function getFileRelationName($gridField)
@ -315,7 +321,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* HTML to be embedded into the GridField.
*
*
* @param GridField $gridField
*
* @return array
@ -392,7 +398,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Component URL handlers.
*
*
* @param GridField $gridField
*
* @return array
@ -406,7 +412,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/**
* Pass control over to the RequestHandler.
*
*
* @param GridField $gridField
* @param SS_HTTPRequest $request
*

View File

@ -47,7 +47,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Handler's constructor.
*
*
* @param GridFIeld $gridField
* @param GridField_URLHandler $component
* @param Controller $controller
@ -62,7 +62,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Return the original component's UploadField.
*
*
* @return UploadField UploadField instance as defined in the component
*/
public function getUploadField()
@ -83,7 +83,11 @@ class GridFieldBulkUpload_Request extends RequestHandler
public function upload(SS_HTTPRequest $request)
{
//create record
$recordClass = $this->gridField->list->dataClass;
if ($this->component->getConfig('objectClassName')) {
$recordClass = $this->component->getConfig('objectClassName');
} else {
$recordClass = $this->gridField->list->dataClass;
}
$record = Object::create($recordClass);
$record->write();
@ -105,7 +109,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
$uploadResponse = Convert::json2array($uploadResponse->getBody());
$uploadResponse = array_shift($uploadResponse);
// Attach the file to record.
// Attach the file to record.
$record->{"{$fileRelationName}ID"} = $uploadResponse['id'];
$record->write();
@ -124,7 +128,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Updates the Upload/Attach response from the UploadField
* with the new DataObject records for the JS template.
*
*
* @param DataObject $record Newly create DataObject record
* @param array $uploadResponse Upload or Attach response from UploadField
*
@ -171,7 +175,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Pass select request to UploadField.
*
*
* @link UploadField->select()
*/
public function select(SS_HTTPRequest $request)
@ -188,7 +192,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Pass getRelationAutosetClass request to UploadField
* Used by select dialog.
*
*
* @link UploadField->getRelationAutosetClass()
*/
public function getRelationAutosetClass($default = 'File')
@ -201,7 +205,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Pass getAllowedMaxFileNumber request to UploadField
* Used by select dialog.
*
*
* @link UploadField->getAllowedMaxFileNumber()
*/
public function getAllowedMaxFileNumber()
@ -214,7 +218,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Retrieve Files to be attached
* and generated DataObjects for each one.
*
*
* @param SS_HTTPRequest $request
*
* @return SS_HTTPResponse
@ -257,7 +261,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Pass fileexists request to UploadField.
*
*
* @link UploadField->fileexists()
*/
public function fileexists(SS_HTTPRequest $request)