2012-08-11 00:19:13 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GridField component for editing attached models in bulk
|
|
|
|
*
|
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
|
|
|
*/
|
|
|
|
class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler {
|
|
|
|
|
2012-08-16 22:29:35 +02:00
|
|
|
/**
|
|
|
|
* component configuration
|
|
|
|
*
|
|
|
|
* 'imageFieldName' => field name of the $has_one Model Image relation
|
|
|
|
* '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
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $config = array(
|
|
|
|
'editableFields' => null,
|
|
|
|
'fieldsClassBlacklist' => array(),
|
2013-11-30 18:30:32 +01:00
|
|
|
'fieldsNameBlacklist' => array(),
|
|
|
|
'actions' => array()
|
2012-08-16 22:29:35 +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' );
|
|
|
|
|
|
|
|
|
2013-11-30 18:30:32 +01:00
|
|
|
public function __construct($editableFields = null, $defaultActions = true)
|
2012-08-11 00:19:13 +02:00
|
|
|
{
|
2012-08-16 22:29:35 +02:00
|
|
|
if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields );
|
|
|
|
$this->config['fieldsClassBlacklist'] = $this->forbiddenFieldsClasses;
|
2013-11-30 18:30:32 +01:00
|
|
|
|
|
|
|
if ( $defaultActions )
|
|
|
|
{
|
|
|
|
$this->config['actions'] = array(
|
|
|
|
'edit' => array(
|
|
|
|
'label' => _t('GridFieldBulkTools.EDIT_SELECT_LABEL', 'Edit'),
|
2013-12-01 12:31:35 +01:00
|
|
|
'handler' => 'GridFieldBulkManager_Request',
|
|
|
|
'config' => array(
|
|
|
|
'isAjax' => false,
|
|
|
|
'icon' => 'pencil',
|
|
|
|
'isDestructive' => false
|
|
|
|
)
|
2013-11-30 18:30:32 +01:00
|
|
|
),
|
|
|
|
'unlink' => array(
|
|
|
|
'label' => _t('GridFieldBulkTools.UNLINK_SELECT_LABEL', 'UnLink'),
|
2013-12-01 12:31:35 +01:00
|
|
|
'handler' => 'GridFieldBulkManager_Request',
|
|
|
|
'config' => array(
|
|
|
|
'isAjax' => true,
|
|
|
|
'icon' => 'chain--minus',
|
|
|
|
'isDestructive' => false
|
|
|
|
)
|
2013-11-30 18:30:32 +01:00
|
|
|
),
|
|
|
|
'delete' => array(
|
|
|
|
'label' => _t('GridFieldBulkTools.DELETE_SELECT_LABEL', 'Delete'),
|
2013-12-01 12:31:35 +01:00
|
|
|
'handler' => 'GridFieldBulkManager_Request',
|
|
|
|
'config' => array(
|
|
|
|
'isAjax' => true,
|
|
|
|
'icon' => 'decline',
|
|
|
|
'isDestructive' => true
|
|
|
|
)
|
2013-11-30 18:30:32 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2012-08-16 22:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a component configuration parameter
|
|
|
|
*
|
|
|
|
* @param string $reference
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
function setConfig ( $reference, $value )
|
|
|
|
{
|
2013-11-30 18:30:32 +01:00
|
|
|
if (!key_exists($reference, $this->config) )
|
|
|
|
{
|
2012-11-23 03:17:16 +01:00
|
|
|
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
|
|
|
}
|
2013-11-30 18:30:32 +01:00
|
|
|
|
|
|
|
if ( $reference == 'actions' )
|
|
|
|
{
|
|
|
|
user_error("Bulk actions must be edited via addBulkAction() and removeBulkAction()", E_USER_ERROR);
|
|
|
|
}
|
2012-11-23 03:17:16 +01:00
|
|
|
|
|
|
|
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-08-16 22:29:35 +02:00
|
|
|
{
|
2012-11-23 03:17:16 +01:00
|
|
|
$value = array_unique( array_merge($value, $this->forbiddenFieldsClasses) );
|
2012-08-16 22:29:35 +02:00
|
|
|
}
|
2012-11-23 03:17:16 +01:00
|
|
|
|
|
|
|
$this->config[$reference] = $value;
|
2013-11-30 18:30:32 +01:00
|
|
|
|
|
|
|
return $this;
|
2012-08-16 22:29:35 +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 )
|
|
|
|
{
|
|
|
|
if (key_exists($className, $this->config['fieldsNameBlacklist']) && !in_array($className, $this->forbiddenFieldsClasses)) {
|
|
|
|
return delete( $this->config['fieldsNameBlacklist'][$className] );
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-11 00:19:13 +02:00
|
|
|
}
|
2013-11-30 18:30:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lets you add custom bulk actions to the bulk manager interface
|
|
|
|
*
|
|
|
|
* @todo add config options for front-end: isAjax, icon
|
|
|
|
*
|
|
|
|
* @param string $name Bulk action's name. Used by RequestHandler.
|
|
|
|
* @param string $label Dropdown menu action's label. Default to ucfirst($name).
|
|
|
|
* @param string $handler RequestHandler class name for this action. Default to 'GridFieldBulkAction'.ucfirst($name).'Handler'
|
2013-12-01 12:31:35 +01:00
|
|
|
* @param array $config Front-end configuration array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )
|
2013-11-30 18:30:32 +01:00
|
|
|
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
|
|
|
*/
|
2013-12-01 12:31:35 +01:00
|
|
|
function addBulkAction(string $name, $label = null, $handler = null, $config = null)
|
2013-11-30 18:30:32 +01:00
|
|
|
{
|
|
|
|
if ( array_key_exists($name, $this->config['actions']) )
|
|
|
|
{
|
|
|
|
user_error("Bulk action $name already exists.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = strtolower($name);
|
|
|
|
|
|
|
|
if ( !$label )
|
|
|
|
{
|
|
|
|
$label = ucfirst($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$handler )
|
|
|
|
{
|
|
|
|
$handler = 'GridFieldBulkAction'.ucfirst($name).'Handler';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !ClassInfo::exists( $handler ) )
|
|
|
|
{
|
|
|
|
user_error("Bulk action handler for $name not found: $handler", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-12-01 12:31:35 +01:00
|
|
|
if ( $config && !is_array($config) )
|
|
|
|
{
|
|
|
|
user_error("Bulk action front-end config should be an array of key => value pairs.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$config = array(
|
|
|
|
'isAjax' => true,
|
|
|
|
'icon' => 'accept',
|
|
|
|
'isDestructive' => false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-30 18:30:32 +01:00
|
|
|
$this->config['actions'][$name] = array(
|
2013-12-01 12:31:35 +01:00
|
|
|
'label' => $label,
|
|
|
|
'handler' => $handler,
|
|
|
|
'config' => $config
|
2013-11-30 18:30:32 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a bulk actions from the bulk manager interface
|
|
|
|
*
|
|
|
|
* @param string $name Bulk action's name
|
|
|
|
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
|
|
|
*/
|
|
|
|
function removeBulkAction(string $name)
|
|
|
|
{
|
|
|
|
if ( !array_key_exists($name, $this->config['actions']) )
|
|
|
|
{
|
|
|
|
user_error("Bulk action $name doesn't exists.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset( $this->config['actions'][$name] );
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-08-11 00:19:13 +02:00
|
|
|
|
|
|
|
/* GridField_ColumnProvider */
|
|
|
|
|
|
|
|
function augmentColumns($gridField, &$columns)
|
|
|
|
{
|
|
|
|
if(!in_array('BulkSelect', $columns)) $columns[] = 'BulkSelect';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumnsHandled($gridField)
|
|
|
|
{
|
|
|
|
return array('BulkSelect');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumnContent($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
$cb = CheckboxField::create('bulkSelect_'.$record->ID)
|
2013-11-30 18:30:32 +01:00
|
|
|
->addExtraClass('bulkSelect no-change-track');
|
2012-08-11 00:19:13 +02:00
|
|
|
return $cb->Field();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumnAttributes($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
return array('class' => 'col-bulkSelect');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumnMetadata($gridField, $columnName)
|
|
|
|
{
|
2012-08-15 20:16:41 +02:00
|
|
|
if($columnName == 'BulkSelect') {
|
|
|
|
return array('title' => 'Select');
|
|
|
|
}
|
2012-08-11 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* // GridField_ColumnProvider */
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-30 18:30:32 +01:00
|
|
|
public function getHTMLFragments($gridField)
|
|
|
|
{
|
2012-08-11 00:19:13 +02:00
|
|
|
Requirements::css(BULK_EDIT_TOOLS_PATH . '/css/GridFieldBulkManager.css');
|
|
|
|
Requirements::javascript(BULK_EDIT_TOOLS_PATH . '/javascript/GridFieldBulkManager.js');
|
|
|
|
|
2013-11-30 18:30:32 +01:00
|
|
|
if ( !count($this->config['actions']) )
|
|
|
|
{
|
|
|
|
user_error("Trying to use GridFieldBulkManager without any bulk action.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$actionsListSource = array();
|
2013-12-01 12:31:35 +01:00
|
|
|
$actionsConfig = array();
|
|
|
|
|
2013-11-30 18:30:32 +01:00
|
|
|
foreach ($this->config['actions'] as $action => $actionData)
|
|
|
|
{
|
2013-12-01 12:31:35 +01:00
|
|
|
$actionsListSource[$action] = $actionData['label'];
|
|
|
|
$actionsConfig[$action] = $actionData['config'];
|
2013-11-30 18:30:32 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 12:31:35 +01:00
|
|
|
reset($this->config['actions']);
|
|
|
|
$firstAction = key($this->config['actions']);
|
|
|
|
|
2013-11-30 18:30:32 +01:00
|
|
|
$dropDownActionsList = DropdownField::create('bulkActionName', '')
|
|
|
|
->setSource( $actionsListSource )
|
|
|
|
->setAttribute('class', 'bulkActionName no-change-track')
|
2013-04-29 18:01:32 +02:00
|
|
|
->setAttribute('id', '');
|
2013-04-16 22:38:35 +02:00
|
|
|
|
2013-12-01 12:31:35 +01:00
|
|
|
$templateData = array(
|
2013-11-30 18:30:32 +01:00
|
|
|
'Menu' => $dropDownActionsList->FieldHolder(),
|
2013-04-16 22:38:35 +02:00
|
|
|
'Button' => array(
|
2013-12-01 12:31:35 +01:00
|
|
|
'Label' => _t('GridFieldBulkTools.ACTION_BTN_LABEL', 'Go'),
|
|
|
|
'DataURL' => $gridField->Link('bulkaction'),
|
|
|
|
'Icon' => $this->config['actions'][$firstAction]['config']['icon'],
|
|
|
|
'DataConfig' => htmlspecialchars(json_encode($actionsConfig), ENT_QUOTES, 'UTF-8')
|
2013-04-16 22:38:35 +02:00
|
|
|
),
|
|
|
|
'Select' => array(
|
2013-06-23 18:10:22 +02:00
|
|
|
'Label' => _t('GridFieldBulkTools.SELECT_ALL_LABEL', 'Select all')
|
2013-12-01 12:31:35 +01:00
|
|
|
),
|
|
|
|
'Colspan' => (count($gridField->getColumns()) - 1)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$this->config['actions'][$firstAction]['config']['isAjax'] )
|
|
|
|
{
|
|
|
|
$templateData['Button']['href'] = $gridField->Link('bulkaction') . '/' . $firstAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
$templateData = new ArrayData($templateData);
|
2013-04-16 22:38:35 +02:00
|
|
|
|
2012-08-11 00:19:13 +02:00
|
|
|
return array(
|
2013-12-01 12:31:35 +01:00
|
|
|
'header' => $templateData->renderWith('BulkManagerButtons')
|
2012-08-11 00:19:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getURLHandlers($gridField) {
|
|
|
|
return array(
|
2013-11-30 18:30:32 +01:00
|
|
|
'bulkaction' => 'handlebulkaction'
|
2012-08-11 00:19:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass control over to the RequestHandler
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-11-30 18:30:32 +01:00
|
|
|
public function handlebulkaction($gridField, $request)
|
2012-08-11 00:19:13 +02:00
|
|
|
{
|
2013-11-30 18:30:32 +01:00
|
|
|
$bulkAction = strtolower( $request->remaining() );
|
2012-08-11 00:19:13 +02:00
|
|
|
$controller = $gridField->getForm()->Controller();
|
2013-11-30 18:30:32 +01:00
|
|
|
|
|
|
|
if ( isset($this->config['actions'][$bulkAction]) )
|
|
|
|
{
|
|
|
|
$handlerClass = $this->config['actions'][$bulkAction]['handler'];
|
|
|
|
$handler = Injector::inst()->create($handlerClass, $gridField, $this, $controller);
|
|
|
|
|
|
|
|
return $handler->handleRequest($request, DataModel::inst());
|
|
|
|
}
|
2012-08-11 00:19:13 +02:00
|
|
|
}
|
|
|
|
}
|