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 ## 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`. 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()); $config->addComponent(new GridFieldBulkUpload());
## Usage 2 ## 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') $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')
$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));
$config->addComponent(new GridFieldBulkUpload($fileRelationName, $objectClassName));
## Configuration ## Configuration
### Component configuration ### Component configuration
The component's option can be configurated through the `setConfig` functions like this: 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);
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')
@ -37,4 +38,4 @@ Please see the [`UploadField` api](http://api.silverstripe.org/master/class-Uplo
## 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`.

View File

@ -7,14 +7,16 @@
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
{ {
/** /**
* component configuration. * Component configuration.
* *
* 'fileRelationName' => field name of the $has_one File/Image relation * '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 * @var array
*/ */
protected $config = array( protected $config = array(
'fileRelationName' => null, 'fileRelationName' => null,
'objectClassName' => null
); );
/** /**
@ -22,11 +24,11 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* These options are passed on directly to the UploadField * These options are passed on directly to the UploadField
* via {@link UploadField::setConfig()} api. * via {@link UploadField::setConfig()} api.
* *
* Defaults are: * * Defaults are: *
* 'sequentialUploads' => false : process uploads 1 after the other rather than all at once * 'sequentialUploads' => false : process uploads 1 after the other rather than all at once
* 'canAttachExisting' => true : displays "From files" button in the UploadField * 'canAttachExisting' => true : displays "From files" button in the UploadField
* 'canPreviewFolder' => true : displays the upload location in the UploadField * 'canPreviewFolder' => true : displays the upload location in the UploadField
* *
* @var array * @var array
*/ */
protected $ufConfig = array( protected $ufConfig = array(
@ -38,10 +40,10 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* UploadField setup function calls. * UploadField setup function calls.
* List of setup functions to call on {@link UploadField} with the value to pass. * List of setup functions to call on {@link UploadField} with the value to pass.
* *
* e.g. array('setFolderName' => 'bulkUpload') will result in: * e.g. array('setFolderName' => 'bulkUpload') will result in:
* $uploadField->setFolderName('bulkUpload') * $uploadField->setFolderName('bulkUpload')
* *
* @var array * @var array
*/ */
protected $ufSetup = array( protected $ufSetup = array(
@ -51,10 +53,10 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* UploadField Validator setup function calls. * UploadField Validator setup function calls.
* List of setup functions to call on {@link Upload::validator} with the value to pass. * List of setup functions to call on {@link Upload::validator} with the value to pass.
* *
* e.g. array('setAllowedMaxFileSize' => 10) will result in: * e.g. array('setAllowedMaxFileSize' => 10) will result in:
* $uploadField->getValidator()->setAllowedMaxFileSize(10) * $uploadField->getValidator()->setAllowedMaxFileSize(10)
* *
* @var array * @var array
*/ */
protected $ufValidatorSetup = array( protected $ufValidatorSetup = array(
@ -63,14 +65,18 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Component constructor. * Component constructor.
* *
* @param string $fileRelationName * @param string $fileRelationName
*/ */
public function __construct($fileRelationName = null) public function __construct($fileRelationName = null, $objectClassName = null)
{ {
if ($fileRelationName != null) { if ($fileRelationName != null) {
$this->setConfig('fileRelationName', $fileRelationName); $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. * Set a component configuration parameter.
* *
* @param string $reference * @param string $reference
* @param mixed $value * @param mixed $value
*/ */
@ -106,7 +112,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Set an UploadField configuration parameter. * Set an UploadField configuration parameter.
* *
* @param string $reference * @param string $reference
* @param mixed $value * @param mixed $value
*/ */
@ -119,7 +125,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Set an UploadField setup function call. * Set an UploadField setup function call.
* *
* @param string $function * @param string $function
* @param mixed $param * @param mixed $param
*/ */
@ -132,7 +138,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Set an UploadField Validator setup function call. * Set an UploadField Validator setup function call.
* *
* @param string $function * @param string $function
* @param mixed $param * @param mixed $param
*/ */
@ -145,7 +151,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Returns one $config reference or 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
@ -161,7 +167,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Returns one $ufConfig reference or the full config. * Returns one $ufConfig reference or the full config.
* *
* @param string $reference $ufConfig parameter to return * @param string $reference $ufConfig parameter to return
* *
* @return mixed * @return mixed
@ -177,7 +183,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Returns one $ufSetup reference or the full config. * Returns one $ufSetup reference or the full config.
* *
* @param string $reference $ufSetup parameter to return * @param string $reference $ufSetup parameter to return
* *
* @return mixed * @return mixed
@ -193,7 +199,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Returns one $ufValidatorSetup reference or the full config. * Returns one $ufValidatorSetup reference or the full config.
* *
* @param string $reference $ufValidatorSetup parameter to return * @param string $reference $ufValidatorSetup parameter to return
* *
* @return mixed * @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 * Get the first has_one Image/File relation from the GridField managed DataObject
* i.e. 'MyImage' => 'Image' will return 'MyImage'. * i.e. 'MyImage' => 'Image' will return 'MyImage'.
* *
* @return string Name of the $has_one relation * @return string Name of the $has_one relation
*/ */
public function getDefaultFileRelationName($gridField) 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 * Returns the name of the Image/File field name from the managed record
* Either as set in the component config or the default one. * Either as set in the component config or the default one.
* *
* @return string * @return string
*/ */
public function getFileRelationName($gridField) public function getFileRelationName($gridField)
@ -315,7 +321,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* HTML to be embedded into the GridField. * HTML to be embedded into the GridField.
* *
* @param GridField $gridField * @param GridField $gridField
* *
* @return array * @return array
@ -392,7 +398,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Component URL handlers. * Component URL handlers.
* *
* @param GridField $gridField * @param GridField $gridField
* *
* @return array * @return array
@ -406,7 +412,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
/** /**
* Pass control over to the RequestHandler. * Pass control over to the RequestHandler.
* *
* @param GridField $gridField * @param GridField $gridField
* @param SS_HTTPRequest $request * @param SS_HTTPRequest $request
* *

View File

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