2012-07-16 22:39:01 +02:00
|
|
|
<?php
|
2017-01-25 13:27:11 +01:00
|
|
|
|
|
|
|
namespace Colymba\BulkUpload;
|
|
|
|
|
2018-09-30 23:05:36 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\View\ArrayData;
|
2018-02-18 16:23:44 +01:00
|
|
|
|
2017-01-25 13:27:11 +01:00
|
|
|
use SilverStripe\View\Requirements;
|
2018-09-30 23:05:36 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use Colymba\BulkUpload\BulkUploadField;
|
|
|
|
use Colymba\BulkUpload\BulkUploadHandler;
|
2023-01-25 01:47:52 +01:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2018-09-30 23:05:36 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
2017-01-25 13:27:11 +01:00
|
|
|
|
2012-07-18 14:23:51 +02:00
|
|
|
/**
|
2015-12-15 13:08:57 +01:00
|
|
|
* GridField component for uploading images in bulk.
|
2012-08-07 22:51:54 +02:00
|
|
|
*
|
|
|
|
* @author colymba
|
2012-07-18 14:23:51 +02:00
|
|
|
*/
|
2017-01-25 13:27:11 +01:00
|
|
|
class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
2015-12-15 13:08:57 +01:00
|
|
|
{
|
|
|
|
/**
|
2016-01-18 15:49:00 +01:00
|
|
|
* Component configuration.
|
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* 'fileRelationName' => field name of the $has_one File/Image relation
|
2016-01-18 16:38:01 +01:00
|
|
|
* 'recordClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name
|
2015-12-15 13:08:57 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $config = array(
|
2017-01-25 13:27:11 +01:00
|
|
|
'fileRelationName' => null,
|
|
|
|
'recordClassName' => null
|
2015-12-15 13:08:57 +01:00
|
|
|
);
|
|
|
|
|
2018-03-02 10:36:15 +01:00
|
|
|
/**
|
|
|
|
* If true, the component will Publish Versioned DataObject
|
|
|
|
* if fasle they will be left as draft.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $autoPublishDataObject = false;
|
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/**
|
|
|
|
* UploadField setup function calls.
|
|
|
|
* List of setup functions to call on {@link UploadField} with the value to pass.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* e.g. array('setFolderName' => 'bulkUpload') will result in:
|
|
|
|
* $uploadField->setFolderName('bulkUpload')
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $ufSetup = array(
|
2017-01-25 13:27:11 +01:00
|
|
|
'setFolderName' => 'bulkUpload',
|
2015-12-15 13:08:57 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component constructor.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param string $fileRelationName
|
2017-01-25 13:27:11 +01:00
|
|
|
* @param string $recordClassName
|
2015-12-15 13:08:57 +01:00
|
|
|
*/
|
2018-03-06 13:08:09 +01:00
|
|
|
public function __construct($fileRelationName = null, $recordClassName = null, $autoPublish = false)
|
2015-12-15 13:08:57 +01:00
|
|
|
{
|
|
|
|
if ($fileRelationName != null) {
|
|
|
|
$this->setConfig('fileRelationName', $fileRelationName);
|
|
|
|
}
|
2016-01-18 15:49:00 +01:00
|
|
|
|
2016-01-18 16:38:01 +01:00
|
|
|
if ($recordClassName != null) {
|
|
|
|
$this->setConfig('recordClassName', $recordClassName);
|
2016-01-18 15:49:00 +01:00
|
|
|
}
|
2018-03-02 10:36:15 +01:00
|
|
|
|
|
|
|
$this->setAutoPublishDataObject($autoPublish);
|
2015-12-15 13:08:57 +01:00
|
|
|
}
|
2014-04-05 18:54:50 +02:00
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/* **********************************************************************
|
|
|
|
* Components settings and custom methodes
|
|
|
|
* */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a component configuration parameter.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param string $reference
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setConfig($reference, $value)
|
2014-09-06 12:39:39 +02:00
|
|
|
{
|
2022-04-13 04:08:15 +02:00
|
|
|
if (!array_key_exists($reference, $this->config ?? [])) {
|
2015-12-15 13:08:57 +01:00
|
|
|
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config[$reference] = $value;
|
|
|
|
|
|
|
|
return $this;
|
2014-09-06 12:39:39 +02:00
|
|
|
}
|
2014-04-05 18:54:50 +02:00
|
|
|
|
2018-03-02 10:36:15 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
2014-09-06 12:39:39 +02:00
|
|
|
}
|
2014-04-05 18:54:50 +02:00
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/**
|
|
|
|
* Set an UploadField setup function call.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param string $function
|
|
|
|
* @param mixed $param
|
|
|
|
*/
|
|
|
|
public function setUfSetup($function, $param)
|
|
|
|
{
|
|
|
|
$this->ufSetup[$function] = $param;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one $config reference or the full $config.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param string $reference $congif parameter to return
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getConfig($reference = false)
|
|
|
|
{
|
|
|
|
if ($reference) {
|
|
|
|
return $this->config[$reference];
|
|
|
|
} else {
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one $ufSetup reference or the full config.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param string $reference $ufSetup parameter to return
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getUfSetup($reference = false)
|
|
|
|
{
|
|
|
|
if ($reference) {
|
|
|
|
return $this->ufSetup[$reference];
|
|
|
|
} else {
|
|
|
|
return $this->ufSetup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 16:38:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the class name of container `DataObject` record.
|
2017-01-25 13:14:04 +01:00
|
|
|
* Either as set in the component config or from the `Gridfield` `dataClass`.
|
2016-01-18 16:38:01 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRecordClassName($gridField)
|
|
|
|
{
|
|
|
|
return $this->getConfig('recordClassName') ? $this->getConfig('recordClassName') : $gridField->list->dataClass;
|
|
|
|
}
|
|
|
|
|
2015-12-15 13:08:57 +01:00
|
|
|
/**
|
|
|
|
* Get the first has_one Image/File relation from the GridField managed DataObject
|
|
|
|
* i.e. 'MyImage' => 'Image' will return 'MyImage'.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2017-01-25 13:27:11 +01:00
|
|
|
* @param GridField $gridField
|
2015-12-15 13:08:57 +01:00
|
|
|
* @return string Name of the $has_one relation
|
|
|
|
*/
|
|
|
|
public function getDefaultFileRelationName($gridField)
|
|
|
|
{
|
2016-01-18 16:38:01 +01:00
|
|
|
$recordClass = $this->getRecordClassName($gridField);
|
2017-05-28 19:58:12 +02:00
|
|
|
$hasOneFields = Config::inst()->get($recordClass, 'has_one');
|
2015-12-15 13:08:57 +01:00
|
|
|
|
|
|
|
$imageField = null;
|
|
|
|
foreach ($hasOneFields as $field => $type) {
|
2017-01-25 13:27:11 +01:00
|
|
|
if ($type === 'SilverStripe\\Assets\\Image'
|
|
|
|
|| $type === 'SilverStripe\\Assets\\File'
|
|
|
|
|| is_subclass_of($type, 'SilverStripe\\Assets\\File')
|
|
|
|
) {
|
2015-12-15 13:08:57 +01:00
|
|
|
$imageField = $field;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $imageField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the Image/File field name from the managed record
|
|
|
|
* Either as set in the component config or the default one.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2017-01-25 13:27:11 +01:00
|
|
|
* @param GridField $gridField
|
2015-12-15 13:08:57 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileRelationName($gridField)
|
|
|
|
{
|
|
|
|
$configFileRelationName = $this->getConfig('fileRelationName');
|
|
|
|
|
|
|
|
return $configFileRelationName ? $configFileRelationName : $this->getDefaultFileRelationName($gridField);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the ClassName of the fileRelation
|
|
|
|
* i.e. 'MyImage' => 'Image' will return 'Image'
|
|
|
|
* i.e. 'MyImage' => 'File' will return 'File'.
|
|
|
|
*
|
2017-01-25 13:27:11 +01:00
|
|
|
* @param GridField $gridField
|
2015-12-15 13:08:57 +01:00
|
|
|
* @return string file relation className
|
|
|
|
*/
|
|
|
|
public function getFileRelationClassName($gridField)
|
|
|
|
{
|
2016-01-18 16:38:01 +01:00
|
|
|
$recordClass = $this->getRecordClassName($gridField);
|
2017-05-28 19:58:12 +02:00
|
|
|
$hasOneFields = Config::inst()->get($recordClass, 'has_one');
|
2015-12-15 13:08:57 +01:00
|
|
|
|
|
|
|
$fieldName = $this->getFileRelationName($gridField);
|
|
|
|
if ($fieldName) {
|
|
|
|
return $hasOneFields[$fieldName];
|
|
|
|
} else {
|
2017-01-25 13:27:11 +01:00
|
|
|
return 'SilverStripe\\Assets\\File';
|
2015-12-15 13:08:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returned a configured UploadField instance
|
|
|
|
* embedded in the gridfield heard.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField Current GridField
|
|
|
|
*
|
|
|
|
* @return UploadField Configured UploadField instance
|
|
|
|
*/
|
|
|
|
public function bulkUploadField($gridField)
|
2014-09-06 12:39:39 +02:00
|
|
|
{
|
2015-12-15 13:08:57 +01:00
|
|
|
$fileRelationName = $this->getFileRelationName($gridField);
|
2018-03-05 09:44:23 +01:00
|
|
|
$fieldName = $fileRelationName . '_' . $this->getRecordClassName($gridField) . '_BU';
|
|
|
|
$uploadField = BulkUploadField::create($gridField, $fieldName, '')
|
2015-12-15 13:08:57 +01:00
|
|
|
->setForm($gridField->getForm())
|
|
|
|
->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber)
|
|
|
|
;
|
|
|
|
|
2017-01-25 13:27:11 +01:00
|
|
|
//UploadField setup
|
|
|
|
foreach ($this->ufSetup as $fn => $param) {
|
|
|
|
$uploadField->{$fn}($param);
|
|
|
|
}
|
2015-12-15 13:08:57 +01:00
|
|
|
|
2017-01-25 13:14:04 +01:00
|
|
|
$schema['data']['createFileEndpoint'] = [
|
2018-03-05 23:03:12 +01:00
|
|
|
'url' => $gridField->Link('bulkupload/upload'),
|
2017-01-25 13:14:04 +01:00
|
|
|
'method' => 'post',
|
|
|
|
'payloadFormat' => 'urlencoded',
|
|
|
|
];
|
2018-03-05 23:03:12 +01:00
|
|
|
|
|
|
|
$schema['data']['attachFileEndpoint'] = [
|
|
|
|
'url' => $gridField->Link('bulkupload/attach'),
|
|
|
|
'method' => 'post'
|
|
|
|
];
|
|
|
|
|
2017-01-25 13:14:04 +01:00
|
|
|
$uploadField->setSchemaData($schema);
|
2015-12-15 13:08:57 +01:00
|
|
|
|
|
|
|
return $uploadField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **********************************************************************
|
|
|
|
* GridField_HTMLProvider
|
|
|
|
* */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTML to be embedded into the GridField.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param GridField $gridField
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField)
|
|
|
|
{
|
|
|
|
// permission check
|
|
|
|
if (!singleton($gridField->getModelClass())->canEdit()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2018-02-22 09:15:42 +01:00
|
|
|
// get uploadField
|
2015-12-15 13:08:57 +01:00
|
|
|
$uploadField = $this->bulkUploadField($gridField);
|
|
|
|
|
|
|
|
$data = ArrayData::create(array(
|
2022-04-13 04:08:15 +02:00
|
|
|
'Colspan' => (count($gridField->getColumns() ?? [])),
|
2017-01-25 13:14:04 +01:00
|
|
|
'UploadField' => $uploadField->Field() // call ->Field() to get requirements in right order
|
2015-12-15 13:08:57 +01:00
|
|
|
));
|
|
|
|
|
2018-03-05 23:03:12 +01:00
|
|
|
Requirements::javascript('colymba/gridfield-bulk-editing-tools:client/dist/js/main.js');
|
|
|
|
Requirements::css('colymba/gridfield-bulk-editing-tools:client/dist/styles/main.css');
|
2018-02-28 20:26:30 +01:00
|
|
|
Requirements::add_i18n_javascript('colymba/gridfield-bulk-editing-tools:client/lang');
|
2015-12-15 13:08:57 +01:00
|
|
|
|
|
|
|
return array(
|
2018-02-27 14:14:19 +01:00
|
|
|
'before' => $data->renderWith('Colymba\\BulkUpload\\BulkUploader'),
|
2015-12-15 13:08:57 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* **********************************************************************
|
|
|
|
* GridField_URLHandler
|
|
|
|
* */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component URL handlers.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param GridField $gridField
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getURLHandlers($gridField)
|
|
|
|
{
|
|
|
|
return array(
|
2016-04-11 21:31:21 +02:00
|
|
|
'bulkupload' => 'handleBulkUpload'
|
2015-12-15 13:08:57 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass control over to the RequestHandler.
|
2016-01-18 15:49:00 +01:00
|
|
|
*
|
2015-12-15 13:08:57 +01:00
|
|
|
* @param GridField $gridField
|
2017-01-25 13:27:11 +01:00
|
|
|
* @param HTTPRequest $request
|
2015-12-15 13:08:57 +01:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handleBulkUpload($gridField, $request)
|
|
|
|
{
|
2018-09-30 23:05:36 +02:00
|
|
|
$controller = Controller::curr();
|
|
|
|
$controller->pushCurrent();
|
2023-01-25 01:47:52 +01:00
|
|
|
$handler = new BulkUploadHandler($gridField, $this);
|
2015-12-15 13:08:57 +01:00
|
|
|
|
2017-08-03 11:12:21 +02:00
|
|
|
return $handler->handleRequest($request);
|
2015-12-15 13:08:57 +01:00
|
|
|
}
|
2014-05-22 15:57:09 +02:00
|
|
|
}
|