mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
NEW Handle Versioned DataObject
This commit is contained in:
parent
9e9cb46d74
commit
2eb86854d7
12
README.md
12
README.md
@ -37,6 +37,18 @@ Upload multiple images or files at once into DataObjects. Perfect for galleries
|
||||
$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.
|
||||
|
||||
## Bulk Manager
|
||||
|
@ -84,6 +84,37 @@ class BulkUploadHandler extends RequestHandler
|
||||
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,
|
||||
* creates new record and link newly uploaded file
|
||||
@ -132,19 +163,8 @@ class BulkUploadHandler extends RequestHandler
|
||||
|
||||
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();
|
||||
$uploadResponse = $assetAdmin->apiCreateFile($request);
|
||||
$file = null;
|
||||
|
||||
if ($uploadResponse->getStatusCode() == 200)
|
||||
{
|
||||
@ -152,12 +172,7 @@ class BulkUploadHandler extends RequestHandler
|
||||
$responseData = array_shift($responseData);
|
||||
}
|
||||
|
||||
// 3. Add File to Record
|
||||
$fileRelationName = $this->component->getFileRelationName($this->gridField);
|
||||
$record->{"{$fileRelationName}ID"} = $responseData['id'];
|
||||
|
||||
// 4. Add to Gridfield List
|
||||
$this->gridField->list->add($record);
|
||||
$this->createDataObject($responseData['id']);
|
||||
|
||||
return $uploadResponse;
|
||||
}
|
||||
@ -256,21 +271,11 @@ class BulkUploadHandler extends RequestHandler
|
||||
$attachResponses = $uploadField->attach($request);
|
||||
$attachResponses = json_decode($attachResponses->getBody(), true);
|
||||
|
||||
$fileRelationName = $uploadField->getName();
|
||||
$recordClass = $this->component->getRecordClassName($this->gridField);
|
||||
$return = array();
|
||||
$return = array();
|
||||
|
||||
foreach ($attachResponses as $attachResponse) {
|
||||
// create record
|
||||
$record = Injector::inst()->create($recordClass);
|
||||
$record->write();
|
||||
$record->extend('onBulkUpload', $this->gridField);
|
||||
|
||||
// attach file
|
||||
$record->{"{$fileRelationName}ID"} = $attachResponse['id'];
|
||||
|
||||
// attached record to gridField relation
|
||||
$this->gridField->list->add($record);
|
||||
foreach ($attachResponses as $attachResponse)
|
||||
{
|
||||
$record = $this->createDataObject($attachResponse['id']);
|
||||
|
||||
// JS Template Data
|
||||
$responseData = $this->newRecordJSTemplateData($record, $attachResponse);
|
||||
|
@ -34,6 +34,13 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
'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.
|
||||
* 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 $recordClassName
|
||||
*/
|
||||
public function __construct($fileRelationName = null, $recordClassName = null)
|
||||
public function __construct($fileRelationName = null, $recordClassName = null, $autoPublishDataObject = false)
|
||||
{
|
||||
if ($fileRelationName != null) {
|
||||
$this->setConfig('fileRelationName', $fileRelationName);
|
||||
@ -93,6 +100,8 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
if ($recordClassName != null) {
|
||||
$this->setConfig('recordClassName', $recordClassName);
|
||||
}
|
||||
|
||||
$this->setAutoPublishDataObject($autoPublish);
|
||||
}
|
||||
|
||||
/* **********************************************************************
|
||||
@ -116,6 +125,25 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user