From 78967304a2200c6256972cf6690f0e3cc0b27c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thierry=20Fran=C3=A7ois?= Date: Mon, 18 Jan 2016 16:49:00 +0200 Subject: [PATCH] NEW Override automatic DataObject class Specify which class to use when creating the DataObject that contains the image. --- bulkUpload/BULK_UPLOAD.md | 13 ++-- bulkUpload/code/GridFieldBulkUpload.php | 67 ++++++++++++------- .../code/GridFieldBulkUpload_Request.php | 22 +++--- 3 files changed, 60 insertions(+), 42 deletions(-) diff --git a/bulkUpload/BULK_UPLOAD.md b/bulkUpload/BULK_UPLOAD.md index 6c53828..65dced9 100644 --- a/bulkUpload/BULK_UPLOAD.md +++ b/bulkUpload/BULK_UPLOAD.md @@ -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)); +$recordClassName (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, $recordClassName)); ## 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`. \ No newline at end of file +To get a quick edit shortcut to all the newly upload files, please also add the `GridFieldBulkManager` component to your `GridFieldConfig`. diff --git a/bulkUpload/code/GridFieldBulkUpload.php b/bulkUpload/code/GridFieldBulkUpload.php index 975784f..5d2b73c 100644 --- a/bulkUpload/code/GridFieldBulkUpload.php +++ b/bulkUpload/code/GridFieldBulkUpload.php @@ -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 + * 'recordClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name * * @var array */ protected $config = array( 'fileRelationName' => null, + 'recordClassName' => 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, $recordClassName = null) { if ($fileRelationName != null) { $this->setConfig('fileRelationName', $fileRelationName); } + + if ($recordClassName != null) { + $this->setConfig('recordClassName', $recordClassName); + } } /* ********************************************************************** @@ -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 @@ -207,15 +213,26 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle } } + /** + * Returns the class name of container `DataObject` record. + * Either as set in the component config or fron the `Gridfield` `dataClass. + * + * @return string + */ + public function getRecordClassName($gridField) + { + return $this->getConfig('recordClassName') ? $this->getConfig('recordClassName') : $gridField->list->dataClass; + } + /** * 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) { - $recordClass = $gridField->list->dataClass; + $recordClass = $this->getRecordClassName($gridField); $hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED); $imageField = null; @@ -232,7 +249,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) @@ -251,7 +268,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle */ public function getFileRelationClassName($gridField) { - $recordClass = $gridField->list->dataClass; + $recordClass = $this->getRecordClassName($gridField); $hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED); $fieldName = $this->getFileRelationName($gridField); @@ -315,7 +332,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle /** * HTML to be embedded into the GridField. - * + * * @param GridField $gridField * * @return array @@ -392,7 +409,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle /** * Component URL handlers. - * + * * @param GridField $gridField * * @return array @@ -406,7 +423,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle /** * Pass control over to the RequestHandler. - * + * * @param GridField $gridField * @param SS_HTTPRequest $request * diff --git a/bulkUpload/code/GridFieldBulkUpload_Request.php b/bulkUpload/code/GridFieldBulkUpload_Request.php index 7d1e2be..3cabd31 100644 --- a/bulkUpload/code/GridFieldBulkUpload_Request.php +++ b/bulkUpload/code/GridFieldBulkUpload_Request.php @@ -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,7 @@ class GridFieldBulkUpload_Request extends RequestHandler public function upload(SS_HTTPRequest $request) { //create record - $recordClass = $this->gridField->list->dataClass; + $recordClass = $this->component->getRecordClassName($this->gridField); $record = Object::create($recordClass); $record->write(); @@ -105,7 +105,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 +124,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 +171,7 @@ class GridFieldBulkUpload_Request extends RequestHandler /** * Pass select request to UploadField. - * + * * @link UploadField->select() */ public function select(SS_HTTPRequest $request) @@ -188,7 +188,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 +201,7 @@ class GridFieldBulkUpload_Request extends RequestHandler /** * Pass getAllowedMaxFileNumber request to UploadField * Used by select dialog. - * + * * @link UploadField->getAllowedMaxFileNumber() */ public function getAllowedMaxFileNumber() @@ -214,7 +214,7 @@ class GridFieldBulkUpload_Request extends RequestHandler /** * Retrieve Files to be attached * and generated DataObjects for each one. - * + * * @param SS_HTTPRequest $request * * @return SS_HTTPResponse @@ -226,7 +226,7 @@ class GridFieldBulkUpload_Request extends RequestHandler $attachResponses = json_decode($attachResponses->getBody(), true); $fileRelationName = $uploadField->getName(); - $recordClass = $this->gridField->list->dataClass; + $recordClass = $this->component->getRecordClassName($this->gridField); $return = array(); foreach ($attachResponses as $attachResponse) { @@ -257,7 +257,7 @@ class GridFieldBulkUpload_Request extends RequestHandler /** * Pass fileexists request to UploadField. - * + * * @link UploadField->fileexists() */ public function fileexists(SS_HTTPRequest $request)