mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
273 lines
7.8 KiB
PHP
273 lines
7.8 KiB
PHP
<?php
|
|
/**
|
|
* GridField component for uploading images in bulk
|
|
*
|
|
* @author colymba
|
|
* @package GridFieldBulkEditingTools
|
|
*/
|
|
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler {
|
|
|
|
/**
|
|
* component configuration
|
|
*
|
|
* 'fileRelationName' => field name of the $has_one File/Image relation
|
|
* 'folderName' => where to upload the files
|
|
* 'maxFileSize' => maximum file size allowed per upload
|
|
* 'sequentialUploads' => process uploads 1 after the other rather than all at once
|
|
* @var array
|
|
*/
|
|
protected $config = array(
|
|
'fileRelationName' => null,
|
|
'folderName' => 'bulkUpload',
|
|
'maxFileSize' => null,
|
|
'sequentialUploads' => false
|
|
);
|
|
|
|
/**
|
|
*
|
|
* @param string $fileRelationName
|
|
* @param string/array $editableFields
|
|
*/
|
|
public function __construct($fileRelationName = null)
|
|
{
|
|
if ( $fileRelationName != null ) $this->setConfig ( 'fileRelationName', $fileRelationName );
|
|
}
|
|
|
|
/**
|
|
* Set a component configuration parameter
|
|
*
|
|
* @param string $reference
|
|
* @param mixed $value
|
|
*/
|
|
function setConfig ( $reference, $value )
|
|
{
|
|
if (!key_exists($reference, $this->config) ) {
|
|
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
|
}
|
|
|
|
//makes sure maxFileSize is INT
|
|
if ( $reference == 'maxFileSize' && !is_int($value) )
|
|
{
|
|
user_warning("maxFileSize should be an Integer. Setting it to Auto.", E_USER_ERROR);
|
|
$value = null;
|
|
}
|
|
|
|
//sequentialUploads true/false
|
|
if ( $reference == 'sequentialUploads' && !is_bool($value) )
|
|
{
|
|
$value = false;
|
|
}
|
|
|
|
$this->config[$reference] = $value;
|
|
}
|
|
|
|
/**
|
|
* Returns one $config parameter of the full $config
|
|
*
|
|
* @param string $reference $congif parameter to return
|
|
* @return mixed
|
|
*/
|
|
function getConfig ( $reference = false )
|
|
{
|
|
if ( $reference ) return $this->config[$reference];
|
|
else return $this->config;
|
|
}
|
|
|
|
/* ******************************************************************************** */
|
|
|
|
/**
|
|
* 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;
|
|
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
|
|
|
$imageField = null;
|
|
foreach( $hasOneFields as $field => $type )
|
|
{
|
|
if( $type === 'Image' || $type === 'File' || is_subclass_of($type, 'File') )
|
|
{
|
|
$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
|
|
*
|
|
* @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'
|
|
*
|
|
* @return string file relation className
|
|
*/
|
|
public function getFileRelationClassName($gridField)
|
|
{
|
|
$recordClass = $gridField->list->dataClass;
|
|
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
|
|
|
$fieldName = $this->getFileRelationName($gridField);
|
|
if($fieldName)
|
|
{
|
|
return $hasOneFields[$fieldName];
|
|
}
|
|
else{
|
|
return 'File';
|
|
}
|
|
}
|
|
|
|
|
|
/* ******************************************************************************** */
|
|
|
|
public function bulkUploadField($gridField)
|
|
{
|
|
$fileRelationName = $this->getFileRelationName($gridField);
|
|
$uploadField = UploadField::create($fileRelationName, '')
|
|
->setForm($gridField->getForm())
|
|
|
|
->setConfig('previewMaxWidth', 20)
|
|
->setConfig('previewMaxHeight', 20)
|
|
->setConfig('changeDetection', false)
|
|
|
|
->setTemplate('GridFieldBulkUploadField')
|
|
->setDownloadTemplateName('colymba-bulkuploaddownloadtemplate')
|
|
|
|
->setConfig('url', $gridField->Link('bulkupload/upload'))
|
|
->setConfig('urlSelectDialog', $gridField->Link('bulkupload/select'))
|
|
->setConfig('urlAttach', $gridField->Link('bulkupload/attach'))
|
|
->setConfig('urlFileExists', $gridField->Link('bulkupload/fileexists'))
|
|
;
|
|
|
|
//max file size
|
|
$maxFileSize = $this->getConfig('maxFileSize');
|
|
if ( $maxFileSize !== null )
|
|
{
|
|
$uploadField->getValidator()->setAllowedMaxFileSize( $maxFileSize );
|
|
}
|
|
|
|
//upload dir
|
|
$uploadDir = $this->getConfig('folderName');
|
|
if ( $uploadDir !== null )
|
|
{
|
|
$uploadField->setFolderName($uploadDir);
|
|
}
|
|
|
|
//sequential upload
|
|
$uploadField->setConfig('sequentialUploads', $this->getConfig('sequentialUploads'));
|
|
|
|
return $uploadField;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param GridField $gridField
|
|
* @return array
|
|
*/
|
|
public function getHTMLFragments($gridField)
|
|
{
|
|
// permission check
|
|
if( !singleton($gridField->getModelClass())->canEdit() )
|
|
{
|
|
return array();
|
|
}
|
|
|
|
// check BulkManager exists
|
|
$bulkManager = $gridField->getConfig()->getComponentsByType('GridFieldBulkManager');
|
|
|
|
// upload management buttons
|
|
$finishButton = FormAction::create('Finish', _t('GridFieldBulkTools.FINISH_BTN_LABEL', 'Finish'))
|
|
->addExtraClass('bulkUploadFinishButton')
|
|
->setAttribute('data-icon', 'accept')
|
|
->setUseButtonTag(true);
|
|
|
|
$clearErrorButton = FormAction::create('ClearError', _t('GridFieldBulkTools.CLEAR_ERROR_BTN_LABEL', 'Clear errors'))
|
|
->addExtraClass('bulkUploadClearErrorButton')
|
|
->setAttribute('data-icon', 'arrow-circle-double')
|
|
->setUseButtonTag(true);
|
|
|
|
if ( !$bulkManager )
|
|
{
|
|
$cancelButton = FormAction::create('Cancel', _t('GridFieldBulkTools.CANCEL_BTN_LABEL', 'Cancel'))
|
|
->addExtraClass('bulkUploadCancelButton ss-ui-action-destructive')
|
|
->setAttribute('data-icon', 'decline')
|
|
->setAttribute('data-url', $gridField->Link('bulkupload/cancel'))
|
|
->setUseButtonTag(true);
|
|
|
|
$editAllButton = FormAction::create('EditAll', _t('GridFieldBulkTools.EDIT_ALL_BTN_LABEL', 'Edit all'))
|
|
->addExtraClass('bulkUploadEditButton')
|
|
->setAttribute('data-icon', 'pencil')
|
|
->setAttribute('data-url', $gridField->Link('bulkupload/edit'))
|
|
->setUseButtonTag(true);
|
|
}
|
|
else{
|
|
$cancelButton = '';
|
|
$editAllButton = '';
|
|
}
|
|
|
|
// get uploadField + inject extra buttons
|
|
$uploadField = $this->bulkUploadField($gridField);
|
|
$uploadField->FinishButton = $finishButton;
|
|
$uploadField->CancelButton = $cancelButton;
|
|
$uploadField->EditAllButton = $editAllButton;
|
|
$uploadField->ClearErrorButton = $clearErrorButton;
|
|
|
|
$data = ArrayData::create(array(
|
|
'Colspan' => count($gridField->getColumns()),
|
|
'UploadField' => $uploadField->Field() // call ->Field() to get requirements in right order
|
|
));
|
|
|
|
Requirements::css(BULKEDITTOOLS_UPLOAD_PATH . '/css/GridFieldBulkUpload.css');
|
|
Requirements::javascript(BULKEDITTOOLS_UPLOAD_PATH . '/javascript/GridFieldBulkUpload.js');
|
|
Requirements::javascript(BULKEDITTOOLS_UPLOAD_PATH . '/javascript/GridFieldBulkUpload_downloadtemplate.js');
|
|
Requirements::add_i18n_javascript(BULKEDITTOOLS_UPLOAD_PATH . '/javascript/lang');
|
|
|
|
return array(
|
|
'header' => $data->renderWith('GridFieldBulkUpload')
|
|
);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param GridField $gridField
|
|
* @return array
|
|
*/
|
|
public function getURLHandlers($gridField) {
|
|
return array(
|
|
'bulkupload' => 'handleBulkUpload'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Pass control over to the RequestHandler
|
|
*
|
|
* @param GridField $gridField
|
|
* @param SS_HTTPRequest $request
|
|
* @return mixed
|
|
*/
|
|
public function handleBulkUpload($gridField, $request)
|
|
{
|
|
$controller = $gridField->getForm()->Controller();
|
|
$handler = new GridFieldBulkUpload_Request($gridField, $this, $controller);
|
|
|
|
return $handler->handleRequest($request, DataModel::inst());
|
|
}
|
|
}
|
|
|