2012-07-16 22:39:01 +02:00
|
|
|
<?php
|
2012-07-18 14:23:51 +02:00
|
|
|
/**
|
2012-08-07 22:51:54 +02:00
|
|
|
* GridField component for uploading images in bulk
|
|
|
|
*
|
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
2012-07-18 14:23:51 +02:00
|
|
|
*/
|
2014-04-05 18:54:50 +02:00
|
|
|
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler {
|
2012-07-20 20:15:53 +02:00
|
|
|
|
2012-07-20 20:02:45 +02:00
|
|
|
/**
|
|
|
|
* component configuration
|
|
|
|
*
|
2013-06-26 18:46:46 +02:00
|
|
|
* 'fileRelationName' => field name of the $has_one File/Image relation
|
2012-07-20 20:15:53 +02:00
|
|
|
* 'editableFields' => fields editable on the Model
|
|
|
|
* 'fieldsClassBlacklist' => field types that will be removed from the automatic form generation
|
|
|
|
* 'fieldsNameBlacklist' => fields that will be removed from the automatic form generation
|
|
|
|
*
|
2012-07-20 20:02:45 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $config = array(
|
2013-06-26 18:46:46 +02:00
|
|
|
'fileRelationName' => null,
|
2012-07-20 20:02:45 +02:00
|
|
|
'editableFields' => null,
|
2012-08-09 19:24:33 +02:00
|
|
|
'fieldsClassBlacklist' => array(),
|
2012-08-07 21:40:12 +02:00
|
|
|
'fieldsNameBlacklist' => array(),
|
2013-01-10 18:23:36 +01:00
|
|
|
'folderName' => 'bulkUpload',
|
2013-03-26 09:48:12 +01:00
|
|
|
'maxFileSize' => null,
|
2013-01-10 18:23:36 +01:00
|
|
|
'sequentialUploads' => false
|
2012-07-20 20:02:45 +02:00
|
|
|
);
|
2012-08-09 19:24:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds any class that should not be used as they break the component
|
|
|
|
* These cannot be removed from the blacklist
|
|
|
|
*/
|
|
|
|
protected $forbiddenFieldsClasses = array( 'GridField', 'UploadField' );
|
2012-07-20 20:02:45 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
/**
|
|
|
|
*
|
2013-06-26 18:46:46 +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
|
|
|
*/
|
2013-06-26 18:46:46 +02:00
|
|
|
public function __construct($fileRelationName = null, $editableFields = null)
|
2012-08-07 22:51:54 +02:00
|
|
|
{
|
2013-06-26 18:46:46 +02:00
|
|
|
if ( $fileRelationName != null ) $this->setConfig ( 'fileRelationName', $fileRelationName );
|
2012-07-20 20:15:53 +02:00
|
|
|
if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields );
|
2012-08-09 19:24:33 +02:00
|
|
|
|
|
|
|
//init classes blacklist with forbidden classes
|
|
|
|
$this->config['fieldsClassBlacklist'] = $this->forbiddenFieldsClasses;
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:15:53 +02:00
|
|
|
/**
|
|
|
|
* Set a component configuration parameter
|
|
|
|
*
|
|
|
|
* @param string $reference
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2012-07-20 20:02:45 +02:00
|
|
|
function setConfig ( $reference, $value )
|
|
|
|
{
|
2012-11-23 03:09:11 +01:00
|
|
|
if (!key_exists($reference, $this->config) ) {
|
|
|
|
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ($reference == 'fieldsClassBlacklist' || $reference == 'fieldsClassBlacklist' || $reference == 'editableFields') && !is_array($value) )
|
|
|
|
{
|
|
|
|
$value = array($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//makes sure $forbiddenFieldsClasses are in no matter what
|
|
|
|
if ( $reference == 'fieldsClassBlacklist' )
|
2012-07-20 20:02:45 +02:00
|
|
|
{
|
2012-11-23 03:09:11 +01:00
|
|
|
$value = array_unique( array_merge($value, $this->forbiddenFieldsClasses) );
|
2012-07-20 20:02:45 +02:00
|
|
|
}
|
2013-03-26 09:48:12 +01:00
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
2013-01-10 18:23:36 +01:00
|
|
|
|
|
|
|
//sequentialUploads true/false
|
|
|
|
if ( $reference == 'sequentialUploads' && !is_bool($value) )
|
|
|
|
{
|
|
|
|
$value = false;
|
|
|
|
}
|
2012-11-23 03:09:11 +01:00
|
|
|
|
|
|
|
$this->config[$reference] = $value;
|
2012-07-20 20:02:45 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a field to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function addFieldNameToBlacklist ( $fieldName )
|
|
|
|
{
|
|
|
|
return array_push( $this->config['fieldsNameBlacklist'], $fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a class to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function addClassToBlacklist ( $className )
|
|
|
|
{
|
|
|
|
return array_push( $this->config['fieldsClassBlacklist'], $className);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a field to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function removeFieldNameFromBlacklist ( $fieldName )
|
|
|
|
{
|
|
|
|
if (key_exists($fieldName, $this->config['fieldsNameBlacklist'])) {
|
|
|
|
return delete( $this->config['fieldsNameBlacklist'][$fieldName] );
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a class to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function removeClassFromBlacklist ( $className )
|
|
|
|
{
|
2012-08-09 19:24:33 +02:00
|
|
|
if (key_exists($className, $this->config['fieldsNameBlacklist']) && !in_array($className, $this->forbiddenFieldsClasses)) {
|
2012-07-20 20:02:45 +02:00
|
|
|
return delete( $this->config['fieldsNameBlacklist'][$className] );
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
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);
|
2012-07-20 20:15:53 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ******************************************************************************** */
|
|
|
|
|
|
|
|
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'));
|
2012-08-11 00:18:00 +02:00
|
|
|
|
2014-04-05 18:54:50 +02:00
|
|
|
return $uploadField;
|
|
|
|
}
|
2012-08-11 00:18:00 +02:00
|
|
|
|
2014-04-05 18:54:50 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField)
|
|
|
|
{
|
|
|
|
// permission check
|
|
|
|
if( !singleton($gridField->getModelClass())->canEdit() )
|
2013-04-16 20:33:50 +02:00
|
|
|
{
|
2014-04-05 18:54:50 +02:00
|
|
|
return array();
|
2013-04-16 20:33:50 +02:00
|
|
|
}
|
|
|
|
|
2014-04-05 23:31:45 +02:00
|
|
|
// upload management buttons
|
|
|
|
$finishButton = FormAction::create('Finish', _t('GridFieldBulkTools.FINISH_BTN_LABEL', 'Finish'))
|
|
|
|
->addExtraClass('bulkUploadFinishButton')
|
|
|
|
->setAttribute('data-icon', 'accept')
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->setAttribute('src', '');//changes type to image so isn't hooked by default actions handlers
|
|
|
|
|
|
|
|
$cancelButton = FormAction::create('Cancel', _t('GridFieldBulkTools.CANCEL_BTN_LABEL', 'Cancel & delete all'))
|
|
|
|
->addExtraClass('bulkUploadCancelButton ss-ui-action-destructive')
|
|
|
|
->setAttribute('data-icon', 'decline')
|
|
|
|
->setAttribute('data-url', $gridField->Link('bulkupload/cancel'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->setAttribute('src', '');
|
|
|
|
|
|
|
|
if ( $gridField->getConfig()->getComponentsByType('GridFieldBulkManager') )
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
->setAttribute('src', '');
|
|
|
|
}else{
|
|
|
|
$editAllButton = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// get uploadField + inject extra buttons
|
|
|
|
$uploadField = $this->bulkUploadField($gridField);
|
|
|
|
$uploadField->FinishButton = $finishButton;
|
|
|
|
$uploadField->CancelButton = $cancelButton;
|
|
|
|
$uploadField->EditAllButton = $editAllButton;
|
|
|
|
|
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
|
2013-04-16 20:33:50 +02:00
|
|
|
));
|
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');
|
|
|
|
Requirements::add_i18n_javascript(BULKEDITTOOLS_UPLOAD_PATH . '/javascript/lang');
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return array
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function getURLHandlers($gridField) {
|
|
|
|
return array(
|
2014-04-05 18:54:50 +02:00
|
|
|
'bulkupload' => 'handleBulkUpload'
|
2012-07-16 22:39:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-18 14:23:51 +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)
|
|
|
|
{
|
|
|
|
$controller = $gridField->getForm()->Controller();
|
2014-04-05 18:54:50 +02:00
|
|
|
$handler = new GridFieldBulkUpload_Request($gridField, $this, $controller);
|
2012-07-16 22:39:01 +02:00
|
|
|
|
|
|
|
return $handler->handleRequest($request, DataModel::inst());
|
|
|
|
}
|
2014-04-05 18:54:50 +02:00
|
|
|
}
|
|
|
|
|