GridFieldBulkEditingTools/bulkUpload/code/GridFieldBulkUpload.php

301 lines
8.3 KiB
PHP
Raw Normal View History

2012-07-16 22:39:01 +02:00
<?php
/**
* GridField component for uploading images in bulk
*
* @author colymba
* @package GridFieldBulkEditingTools
2014-05-04 16:12:05 +02:00
* @subpackage BulkUpload
*/
2014-05-04 16:12:05 +02:00
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
{
/**
* component configuration
*
* 'fileRelationName' => field name of the $has_one File/Image relation
2014-04-06 19:04:12 +02:00
* '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(
2014-04-06 19:04:12 +02:00
'fileRelationName' => null,
'folderName' => 'bulkUpload',
'maxFileSize' => null,
'sequentialUploads' => false
);
2014-05-11 12:32:21 +02:00
2012-07-16 22:39:01 +02:00
/**
2014-05-11 12:32:21 +02:00
* Component constructor
2012-07-16 22:39:01 +02:00
*
* @param string $fileRelationName
2014-04-05 18:54:50 +02:00
* @param string/array $editableFields
2012-07-16 22:39:01 +02:00
*/
2014-04-06 19:04:12 +02:00
public function __construct($fileRelationName = null)
{
if ( $fileRelationName != null ) $this->setConfig ( 'fileRelationName', $fileRelationName );
2012-07-16 22:39:01 +02:00
}
2014-05-04 16:12:05 +02:00
/* **********************************************************************
* Components settings and custom methodes
* */
/**
* 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;
}
2014-05-11 12:32:21 +02:00
/**
* 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;
}
2014-04-05 18:54:50 +02:00
/**
* 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);
2014-04-05 18:54:50 +02:00
$imageField = null;
foreach( $hasOneFields as $field => $type )
{
if( $type === 'Image' || $type === 'File' || is_subclass_of($type, 'File') )
{
$imageField = $field;
break;
}
}
return $imageField;
}
2014-05-04 16:12:05 +02:00
2014-04-05 18:54:50 +02:00
/**
* 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);
}
2014-05-11 12:32:21 +02:00
2012-07-16 22:39:01 +02:00
/**
2014-04-05 18:54:50 +02:00
* Return the ClassName of the fileRelation
* i.e. 'MyImage' => 'Image' will return 'Image'
* i.e. 'MyImage' => 'File' will return 'File'
2012-07-16 22:39:01 +02:00
*
2014-04-05 18:54:50 +02:00
* @return string file relation className
2012-07-16 22:39:01 +02:00
*/
2014-04-05 18:54:50 +02:00
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';
}
}
2014-05-04 16:12:05 +02:00
2014-04-05 18:54:50 +02:00
2014-04-13 18:38:08 +02:00
/**
* Returned a configured UploadField instance
* embedded in the gridfield heard
* @param GridField $gridField Current GridField
* @return UploadField Configured UploadField instance
*/
2014-04-05 18:54:50 +02:00
public function bulkUploadField($gridField)
{
$fileRelationName = $this->getFileRelationName($gridField);
$uploadField = UploadField::create($fileRelationName, '')
->setForm($gridField->getForm())
->setConfig('previewMaxWidth', 20)
->setConfig('previewMaxHeight', 20)
->setConfig('changeDetection', false)
2014-04-05 23:31:45 +02:00
->setTemplate('GridFieldBulkUploadField')
2014-04-05 18:54:50 +02:00
->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'));
2014-04-05 18:54:50 +02:00
return $uploadField;
}
2014-04-13 18:38:08 +02:00
2014-05-04 16:12:05 +02:00
/* **********************************************************************
* GridField_HTMLProvider
* */
2014-04-05 18:54:50 +02:00
/**
2014-04-13 18:38:08 +02:00
* HTML to be embedded into the GridField
*
2014-04-05 18:54:50 +02:00
* @param GridField $gridField
* @return array
*/
public function getHTMLFragments($gridField)
{
// permission check
if( !singleton($gridField->getModelClass())->canEdit() )
{
2014-04-05 18:54:50 +02:00
return array();
}
// check BulkManager exists
$bulkManager = $gridField->getConfig()->getComponentsByType('GridFieldBulkManager');
2014-04-05 23:31:45 +02:00
// upload management buttons
2014-04-13 18:56:12 +02:00
$finishButton = FormAction::create('Finish', _t('GRIDFIELD_BULK_UPLOAD.FINISH_BTN_LABEL', 'Finish'))
2014-04-05 23:31:45 +02:00
->addExtraClass('bulkUploadFinishButton')
->setAttribute('data-icon', 'accept')
2014-04-06 14:51:15 +02:00
->setUseButtonTag(true);
2014-04-05 23:31:45 +02:00
2014-04-13 18:56:12 +02:00
$clearErrorButton = FormAction::create('ClearError', _t('GRIDFIELD_BULK_UPLOAD.CLEAR_ERROR_BTN_LABEL', 'Clear errors'))
2014-04-06 13:30:54 +02:00
->addExtraClass('bulkUploadClearErrorButton')
->setAttribute('data-icon', 'arrow-circle-double')
2014-04-06 14:51:15 +02:00
->setUseButtonTag(true);
2014-04-06 13:30:54 +02:00
if ( count($bulkManager) )
2014-04-05 23:31:45 +02:00
{
2014-04-13 18:56:12 +02:00
$cancelButton = FormAction::create('Cancel', _t('GRIDFIELD_BULK_UPLOAD.CANCEL_BTN_LABEL', 'Cancel'))
->addExtraClass('bulkUploadCancelButton ss-ui-action-destructive')
->setAttribute('data-icon', 'decline')
->setAttribute('data-url', $gridField->Link('bulkupload/cancel'))
->setUseButtonTag(true);
2014-04-13 18:56:12 +02:00
$editAllButton = FormAction::create('EditAll', _t('GRIDFIELD_BULK_UPLOAD.EDIT_ALL_BTN_LABEL', 'Edit all'))
->addExtraClass('bulkUploadEditButton')
->setAttribute('data-icon', 'pencil')
->setAttribute('data-url', $gridField->Link('bulkupload/edit'))
->setUseButtonTag(true);
}
else{
$cancelButton = '';
2014-04-05 23:31:45 +02:00
$editAllButton = '';
}
// get uploadField + inject extra buttons
$uploadField = $this->bulkUploadField($gridField);
2014-04-06 13:30:54 +02:00
$uploadField->FinishButton = $finishButton;
$uploadField->CancelButton = $cancelButton;
$uploadField->EditAllButton = $editAllButton;
$uploadField->ClearErrorButton = $clearErrorButton;
2014-04-05 23:31:45 +02:00
2014-04-05 18:54:50 +02:00
$data = ArrayData::create(array(
2014-04-05 23:31:45 +02:00
'Colspan' => count($gridField->getColumns()),
'UploadField' => $uploadField->Field() // call ->Field() to get requirements in right order
));
2014-04-05 18:54:50 +02:00
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');
2014-04-13 18:56:12 +02:00
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
2012-07-16 22:39:01 +02:00
return array(
2014-04-05 18:54:50 +02:00
'header' => $data->renderWith('GridFieldBulkUpload')
2012-07-16 22:39:01 +02:00
);
}
2014-04-13 18:38:08 +02:00
2014-05-04 16:12:05 +02:00
/* **********************************************************************
* GridField_URLHandler
* */
2012-07-16 22:39:01 +02:00
/**
2014-04-13 18:38:08 +02:00
* Component URL handlers
*
2012-07-16 22:39:01 +02:00
* @param GridField $gridField
* @return array
2012-07-16 22:39:01 +02:00
*/
public function getURLHandlers($gridField) {
2014-04-06 19:04:12 +02:00
return array(
'bulkupload' => 'handleBulkUpload'
);
2012-07-16 22:39:01 +02:00
}
2014-05-11 12:32:21 +02:00
2012-07-16 22:39:01 +02:00
/**
* Pass control over to the RequestHandler
*
* @param GridField $gridField
* @param SS_HTTPRequest $request
* @return mixed
2012-07-16 22:39:01 +02:00
*/
public function handleBulkUpload($gridField, $request)
{
2014-04-06 19:04:12 +02:00
$controller = $gridField->getForm()->Controller();
$handler = new GridFieldBulkUpload_Request($gridField, $this, $controller);
2012-07-16 22:39:01 +02:00
return $handler->handleRequest($request, DataModel::inst());
}
2014-05-11 12:32:21 +02:00
}