NEW Handle Versioned DataObject

This commit is contained in:
Thierry François 2018-03-02 11:36:15 +02:00
parent 9e9cb46d74
commit 2eb86854d7
3 changed files with 77 additions and 32 deletions

View File

@ -37,6 +37,18 @@ Upload multiple images or files at once into DataObjects. Perfect for galleries
$config->addComponent(new \Colymba\BulkUpload\BulkUploader()); $config->addComponent(new \Colymba\BulkUpload\BulkUploader());
``` ```
### Versioned
By default `BulkUploader` will write to the current stage (most likely Draft). To auto publish your `DataObject`, use the following param or config:
```php
$config->addComponent(new \Colymba\BulkUpload\BulkUploader(null, null, true));
```
OR
```php
$config->getComponentByType('Colymba\\BulkUpload\\BulkUploader')->setAutoPublishDataObject(true);
```
Your `DataObject` should `own` the `Image`/`File` relation so it is published at the same time: [See SilverStripe DataObject ownership DOC](https://github.com/silverstripe/silverstripe-framework/blob/4.0/docs/en/02_Developer_Guides/00_Model/10_Versioning.md#dataobject-ownership)
See [BULK_UPLOAD.md](docs/en/BULK_UPLOAD.md) for detailed configuration. See [BULK_UPLOAD.md](docs/en/BULK_UPLOAD.md) for detailed configuration.
## Bulk Manager ## Bulk Manager

View File

@ -84,6 +84,37 @@ class BulkUploadHandler extends RequestHandler
return $this->component->bulkUploadField($this->gridField); return $this->component->bulkUploadField($this->gridField);
} }
/**
* Creates a new DataObject
* Add uploaded file to the Dataobject
* Add DataObject to Gridfield list
* Publish DataObject if enabled
*
* @param integer $fileID The newly uploaded/attached file ID
*
* @return DataObject The new DataObject
*/
protected function createDataObject($fileID)
{
$recordClass = $this->component->getRecordClassName($this->gridField);
$record = $recordClass::create();
$record->write();
$record->extend('onBulkUpload', $this->gridField);
$fileRelationName = $this->component->getFileRelationName($this->gridField);
$record->{"{$fileRelationName}ID"} = $fileID;
$this->gridField->list->add($record);
if ($this->component->getAutoPublishDataObject() && $record->hasExtension('Versioned'))
{
$record->publishRecursive();
}
return $record;
}
/** /**
* Process upload through UploadField, * Process upload through UploadField,
* creates new record and link newly uploaded file * creates new record and link newly uploaded file
@ -132,19 +163,8 @@ class BulkUploadHandler extends RequestHandler
public function upload(HTTPRequest $request) public function upload(HTTPRequest $request)
{ {
// 1. DataObject
//create record
$recordClass = $this->component->getRecordClassName($this->gridField);
$record = $recordClass::create();
$record->write();
// passes the current gridfield-instance to a call-back method on the new object
$record->extend('onBulkUpload', $this->gridField);
// 2. File Upload
$assetAdmin = AssetAdmin::singleton(); $assetAdmin = AssetAdmin::singleton();
$uploadResponse = $assetAdmin->apiCreateFile($request); $uploadResponse = $assetAdmin->apiCreateFile($request);
$file = null;
if ($uploadResponse->getStatusCode() == 200) if ($uploadResponse->getStatusCode() == 200)
{ {
@ -152,12 +172,7 @@ class BulkUploadHandler extends RequestHandler
$responseData = array_shift($responseData); $responseData = array_shift($responseData);
} }
// 3. Add File to Record $this->createDataObject($responseData['id']);
$fileRelationName = $this->component->getFileRelationName($this->gridField);
$record->{"{$fileRelationName}ID"} = $responseData['id'];
// 4. Add to Gridfield List
$this->gridField->list->add($record);
return $uploadResponse; return $uploadResponse;
} }
@ -256,21 +271,11 @@ class BulkUploadHandler extends RequestHandler
$attachResponses = $uploadField->attach($request); $attachResponses = $uploadField->attach($request);
$attachResponses = json_decode($attachResponses->getBody(), true); $attachResponses = json_decode($attachResponses->getBody(), true);
$fileRelationName = $uploadField->getName(); $return = array();
$recordClass = $this->component->getRecordClassName($this->gridField);
$return = array();
foreach ($attachResponses as $attachResponse) { foreach ($attachResponses as $attachResponse)
// create record {
$record = Injector::inst()->create($recordClass); $record = $this->createDataObject($attachResponse['id']);
$record->write();
$record->extend('onBulkUpload', $this->gridField);
// attach file
$record->{"{$fileRelationName}ID"} = $attachResponse['id'];
// attached record to gridField relation
$this->gridField->list->add($record);
// JS Template Data // JS Template Data
$responseData = $this->newRecordJSTemplateData($record, $attachResponse); $responseData = $this->newRecordJSTemplateData($record, $attachResponse);

View File

@ -34,6 +34,13 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
'recordClassName' => null 'recordClassName' => null
); );
/**
* If true, the component will Publish Versioned DataObject
* if fasle they will be left as draft.
* @var boolean
*/
protected $autoPublishDataObject = false;
/** /**
* UploadField configuration. * UploadField configuration.
* These options are passed on directly to the UploadField * These options are passed on directly to the UploadField
@ -84,7 +91,7 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
* @param string $fileRelationName * @param string $fileRelationName
* @param string $recordClassName * @param string $recordClassName
*/ */
public function __construct($fileRelationName = null, $recordClassName = null) public function __construct($fileRelationName = null, $recordClassName = null, $autoPublishDataObject = false)
{ {
if ($fileRelationName != null) { if ($fileRelationName != null) {
$this->setConfig('fileRelationName', $fileRelationName); $this->setConfig('fileRelationName', $fileRelationName);
@ -93,6 +100,8 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
if ($recordClassName != null) { if ($recordClassName != null) {
$this->setConfig('recordClassName', $recordClassName); $this->setConfig('recordClassName', $recordClassName);
} }
$this->setAutoPublishDataObject($autoPublish);
} }
/* ********************************************************************** /* **********************************************************************
@ -116,6 +125,25 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
return $this; return $this;
} }
/**
* Set Versioned DataObject auto publish config
* @param boolean $autoPublish True to auto publish versioned dataobjects
*/
public function setAutoPublishDataObject($autoPublish)
{
$this->autoPublishDataObject = $autoPublish;
return $this;
}
/**
* Get Versioned DataObject auto publish config
* @return boolean auto publish config value
*/
public function getAutoPublishDataObject()
{
return $this->autoPublishDataObject;
}
/** /**
* Set an UploadField configuration parameter. * Set an UploadField configuration parameter.
* *