2013-12-01 23:54:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-04 16:12:05 +02:00
|
|
|
* Bulk action handler for editing records.
|
|
|
|
*
|
2013-12-01 23:54:39 +01:00
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
2014-05-04 16:12:05 +02:00
|
|
|
* @subpackage BulkManager
|
2013-12-01 23:54:39 +01:00
|
|
|
*/
|
|
|
|
class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
|
|
|
|
{
|
|
|
|
/**
|
2014-05-11 12:32:21 +02:00
|
|
|
* RequestHandler allowed actions
|
|
|
|
* @var array
|
2013-12-01 23:54:39 +01:00
|
|
|
*/
|
2014-09-07 19:18:32 +02:00
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'bulkEditForm',
|
|
|
|
'recordEditForm'
|
|
|
|
);
|
2013-12-01 23:54:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-11 12:32:21 +02:00
|
|
|
* RequestHandler url => action map
|
|
|
|
* @var array
|
2013-12-01 23:54:39 +01:00
|
|
|
*/
|
|
|
|
private static $url_handlers = array(
|
2014-09-07 19:18:32 +02:00
|
|
|
'bulkEdit/bulkEditForm' => 'bulkEditForm',
|
|
|
|
'bulkEdit/recordEditForm' => 'recordEditForm',
|
|
|
|
'bulkEdit' => 'index'
|
2013-12-01 23:54:39 +01:00
|
|
|
);
|
2013-12-05 16:48:01 +01:00
|
|
|
|
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
/**
|
|
|
|
* Return URL to this RequestHandler
|
|
|
|
* @param string $action Action to append to URL
|
|
|
|
* @return string URL
|
|
|
|
*/
|
|
|
|
public function Link($action = null)
|
|
|
|
{
|
|
|
|
return Controller::join_links(parent::Link(), 'bulkEdit', $action);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-01 23:54:39 +01:00
|
|
|
/**
|
2014-05-04 15:24:30 +02:00
|
|
|
* Return a form for all the selected DataObjects
|
2013-12-05 16:48:01 +01:00
|
|
|
* with their respective editable fields.
|
2013-12-01 23:54:39 +01:00
|
|
|
*
|
2014-05-04 15:24:30 +02:00
|
|
|
* @return Form Selected DataObjects editable fields
|
2013-12-01 23:54:39 +01:00
|
|
|
*/
|
2014-09-07 19:18:32 +02:00
|
|
|
public function bulkEditForm()
|
2013-12-01 23:54:39 +01:00
|
|
|
{
|
|
|
|
$crumbs = $this->Breadcrumbs();
|
2013-12-05 16:48:01 +01:00
|
|
|
if($crumbs && $crumbs->count()>=2)
|
|
|
|
{
|
|
|
|
$one_level_up = $crumbs->offsetGet($crumbs->count()-2);
|
|
|
|
}
|
2013-12-01 23:54:39 +01:00
|
|
|
|
2013-12-05 16:48:01 +01:00
|
|
|
$actions = new FieldList();
|
2013-12-01 23:54:39 +01:00
|
|
|
|
|
|
|
$actions->push(
|
2014-09-07 19:18:32 +02:00
|
|
|
FormAction::create('doSave', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_BTN_LABEL', 'Save all'))
|
|
|
|
->setAttribute('id', 'bulkEditingSaveBtn')
|
|
|
|
->addExtraClass('ss-ui-action-constructive')
|
2013-12-01 23:54:39 +01:00
|
|
|
->setAttribute('data-icon', 'accept')
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
$actions->push(
|
2014-04-13 18:56:12 +02:00
|
|
|
FormAction::create('Cancel', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.CANCEL_BTN_LABEL', 'Cancel'))
|
2013-12-01 23:54:39 +01:00
|
|
|
->setAttribute('id', 'bulkEditingUpdateCancelBtn')
|
|
|
|
->addExtraClass('ss-ui-action-destructive cms-panel-link')
|
|
|
|
->setAttribute('data-icon', 'decline')
|
2013-12-05 16:48:01 +01:00
|
|
|
->setAttribute('href', $one_level_up->Link)
|
2013-12-01 23:54:39 +01:00
|
|
|
->setUseButtonTag(true)
|
2013-12-05 16:48:01 +01:00
|
|
|
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
|
2013-12-01 23:54:39 +01:00
|
|
|
);
|
|
|
|
|
2014-05-01 23:03:43 +02:00
|
|
|
$recordList = $this->getRecordIDList();
|
|
|
|
$recordsFieldList = new FieldList();
|
|
|
|
$config = $this->component->getConfig();
|
|
|
|
|
|
|
|
$editingCount = count($recordList);
|
2014-05-04 13:34:31 +02:00
|
|
|
$modelClass = $this->gridField->getModelClass();
|
|
|
|
$singleton = singleton($modelClass);
|
|
|
|
$titleModelClass = (($editingCount > 1) ? $singleton->i18n_plural_name() : $singleton->i18n_singular_name());
|
2014-05-01 23:03:43 +02:00
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
//some cosmetics
|
2014-05-10 13:34:40 +02:00
|
|
|
$headerText = _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.HEADER_TEXT',
|
|
|
|
'Editing {count} {class}',
|
|
|
|
array(
|
|
|
|
'count' => $editingCount,
|
|
|
|
'class' => $titleModelClass
|
|
|
|
)
|
|
|
|
);
|
2014-05-01 23:03:43 +02:00
|
|
|
$header = LiteralField::create(
|
|
|
|
'bulkEditHeader',
|
2014-05-10 13:34:40 +02:00
|
|
|
'<h1 id="bulkEditHeader">' . $headerText . '</h1>'
|
2014-05-01 23:03:43 +02:00
|
|
|
);
|
|
|
|
$recordsFieldList->push($header);
|
|
|
|
|
|
|
|
$toggle = LiteralField::create('bulkEditToggle', '<span id="bulkEditToggle">' . _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.TOGGLE_ALL_LINK', 'Show/Hide all') . '</span>');
|
|
|
|
$recordsFieldList->push($toggle);
|
2014-09-07 19:18:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
//fetch fields for each record and push to fieldList
|
2013-12-01 23:54:39 +01:00
|
|
|
foreach ( $recordList as $id )
|
2014-09-07 19:18:32 +02:00
|
|
|
{
|
|
|
|
$record = DataObject::get_by_id($modelClass, $id);
|
2014-05-04 15:24:30 +02:00
|
|
|
$recordEditingFields = $this->getRecordEditingFields($record);
|
|
|
|
|
|
|
|
$toggleField = ToggleCompositeField::create(
|
|
|
|
'RecordFields_'.$id,
|
|
|
|
$record->getTitle(),
|
|
|
|
$recordEditingFields
|
|
|
|
)
|
|
|
|
->setHeadingLevel(4)
|
|
|
|
->setAttribute('data-id', $id)
|
|
|
|
->addExtraClass('bulkEditingFieldHolder');
|
|
|
|
|
|
|
|
$recordsFieldList->push($toggleField);
|
2013-12-01 23:54:39 +01:00
|
|
|
}
|
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
$bulkEditForm = Form::create(
|
2013-12-01 23:54:39 +01:00
|
|
|
$this,
|
2014-09-07 19:18:32 +02:00
|
|
|
'recordEditForm', //recordEditForm name is here to trick SS to pass all subform request to recordEditForm()
|
2014-05-01 23:03:43 +02:00
|
|
|
$recordsFieldList,
|
2013-12-01 23:54:39 +01:00
|
|
|
$actions
|
2014-09-07 19:18:32 +02:00
|
|
|
);
|
2013-12-01 23:54:39 +01:00
|
|
|
|
|
|
|
if($crumbs && $crumbs->count()>=2){
|
2014-09-07 19:18:32 +02:00
|
|
|
$bulkEditForm->Backlink = $one_level_up->Link;
|
|
|
|
}
|
|
|
|
|
|
|
|
//override form action URL back to bulkEditForm
|
|
|
|
//and add record ids GET var
|
|
|
|
$bulkEditForm->setAttribute(
|
|
|
|
'action',
|
|
|
|
$this->Link('bulkEditForm?records[]='.implode('&', $recordList))
|
|
|
|
);
|
|
|
|
|
|
|
|
return $bulkEditForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return's a form with only one record's fields
|
|
|
|
* Used for bulkEditForm subForm requests via ajax
|
|
|
|
*
|
|
|
|
* @return Form Currently being edited form
|
|
|
|
*/
|
|
|
|
public function recordEditForm()
|
|
|
|
{
|
|
|
|
//clone current request : used to figure out what record we are asking
|
|
|
|
$request = clone $this->request;
|
|
|
|
$recordInfo = $request->shift();
|
|
|
|
|
|
|
|
//shift request till we find the requested field
|
|
|
|
while ($recordInfo)
|
|
|
|
{
|
|
|
|
if ( $unescapedRecordInfo = $this->unEscapeFieldName($recordInfo) )
|
|
|
|
{
|
|
|
|
$id = $unescapedRecordInfo['id'];
|
|
|
|
$fieldName = $unescapedRecordInfo['name'];
|
|
|
|
|
|
|
|
$action = $request->shift();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$recordInfo = $request->shift();
|
|
|
|
}
|
2013-12-01 23:54:39 +01:00
|
|
|
}
|
2013-12-05 16:48:01 +01:00
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
//generate a form with only that requested record's fields
|
|
|
|
if ( $id )
|
|
|
|
{
|
|
|
|
$modelClass = $this->gridField->getModelClass();
|
|
|
|
$record = DataObject::get_by_id($modelClass, $id);
|
|
|
|
|
|
|
|
$cmsFields = $record->getCMSFields();
|
|
|
|
$recordEditingFields = $this->getRecordEditingFields($record);
|
|
|
|
|
|
|
|
return Form::create(
|
|
|
|
$this->gridField,
|
|
|
|
'recordEditForm',
|
|
|
|
FieldList::create($recordEditingFields),
|
|
|
|
FieldList::create()
|
|
|
|
);
|
|
|
|
}
|
2013-12-05 16:48:01 +01:00
|
|
|
}
|
2014-05-04 15:24:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a record's populated form fields
|
|
|
|
* with all filtering done ready to be included in the main form
|
|
|
|
*
|
|
|
|
* @uses DataObject::getCMSFields()
|
|
|
|
*
|
|
|
|
* @param DataObject $record The record to get the fields from
|
|
|
|
* @return array The record's editable fields
|
|
|
|
*/
|
|
|
|
private function getRecordEditingFields(DataObject $record)
|
|
|
|
{
|
|
|
|
$tempForm = Form::create(
|
|
|
|
$this, "TempEditForm",
|
|
|
|
$record->getCMSFields(),
|
|
|
|
FieldList::create()
|
|
|
|
);
|
|
|
|
|
|
|
|
$tempForm->loadDataFrom($record);
|
|
|
|
$fields = $tempForm->Fields();
|
|
|
|
|
|
|
|
$fields = $this->filterRecordEditingFields($fields, $record->ID);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters a records editable fields
|
|
|
|
* based on component's config
|
|
|
|
* and escape each field with unique name.
|
|
|
|
*
|
|
|
|
* See {@link GridFieldBulkManager} component for filtering config.
|
|
|
|
*
|
|
|
|
* @param FieldList $fields Record's CMS Fields
|
|
|
|
* @param integer $id Record's ID, used fir unique name
|
|
|
|
* @return array Filtered record's fields
|
|
|
|
*/
|
|
|
|
private function filterRecordEditingFields(FieldList $fields, $id)
|
|
|
|
{
|
|
|
|
$config = $this->component->getConfig();
|
|
|
|
$editableFields = $config['editableFields'];
|
|
|
|
|
|
|
|
// get all dataFields or just the ones allowed in config
|
|
|
|
if ( $editableFields )
|
|
|
|
{
|
|
|
|
$dataFields = array();
|
|
|
|
|
|
|
|
foreach ($editableFields as $fieldName)
|
|
|
|
{
|
|
|
|
array_push(
|
|
|
|
$dataFields,
|
|
|
|
$fields->dataFieldByName($fieldName)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$dataFields = $fields->dataFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
// escape field names with unique prefix
|
|
|
|
foreach ( $dataFields as $name => $field )
|
|
|
|
{
|
2014-09-07 19:18:32 +02:00
|
|
|
$field->Name = $this->escapeFieldName($id, $name);
|
2014-05-04 15:24:30 +02:00
|
|
|
$dataFields[$name] = $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dataFields;
|
|
|
|
}
|
2014-09-07 19:18:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a fieldName with a unique prefix
|
|
|
|
*
|
|
|
|
* @param integer $recordID Record id from who the field belongs
|
|
|
|
* @param string $name Field name
|
|
|
|
* @return string Escaped field name
|
|
|
|
*/
|
|
|
|
protected function escapeFieldName($recordID, $name)
|
|
|
|
{
|
|
|
|
return 'record_' . $recordID . '_' . $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Un-escape a previously escaped field name
|
|
|
|
*
|
|
|
|
* @param string $fieldName Escaped field name
|
|
|
|
* @return array|false Fasle if the fieldName was not escaped. Or Array map with record 'id' and field 'name'
|
|
|
|
*/
|
|
|
|
protected function unEscapeFieldName($fieldName)
|
|
|
|
{
|
|
|
|
$parts = array();
|
|
|
|
$match = preg_match('/record_(\d+)_(\w+)/i', $fieldName, $parts);
|
|
|
|
|
|
|
|
if ( !$match )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return array(
|
|
|
|
'id' => $parts[1],
|
|
|
|
'name' => $parts[2],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 16:48:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-09-07 19:18:32 +02:00
|
|
|
* Creates and return the bulk editing interface
|
2013-12-05 16:48:01 +01:00
|
|
|
*
|
|
|
|
* @return string Form's HTML
|
|
|
|
*/
|
2014-09-07 19:18:32 +02:00
|
|
|
public function index()
|
2013-12-05 16:48:01 +01:00
|
|
|
{
|
2014-09-07 19:18:32 +02:00
|
|
|
$form = $this->bulkEditForm();
|
2013-12-05 16:48:01 +01:00
|
|
|
$form->setTemplate('LeftAndMain_EditForm');
|
|
|
|
$form->addExtraClass('center cms-content');
|
|
|
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
|
2013-12-01 23:54:39 +01:00
|
|
|
|
2014-04-05 18:54:50 +02:00
|
|
|
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH . '/javascript/GridFieldBulkEditingForm.js');
|
|
|
|
Requirements::css(BULKEDITTOOLS_MANAGER_PATH . '/css/GridFieldBulkEditingForm.css');
|
2014-04-13 18:56:12 +02:00
|
|
|
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
|
2013-12-01 23:54:39 +01:00
|
|
|
|
|
|
|
if($this->request->isAjax())
|
|
|
|
{
|
2013-12-05 16:48:01 +01:00
|
|
|
$response = new SS_HTTPResponse(
|
|
|
|
Convert::raw2json(array( 'Content' => $form->forAjaxTemplate()->getValue() ))
|
|
|
|
);
|
|
|
|
$response->addHeader('X-Pjax', 'Content');
|
|
|
|
$response->addHeader('Content-Type', 'text/json');
|
|
|
|
$response->addHeader('X-Title', 'SilverStripe - Bulk '.$this->gridField->list->dataClass.' Editing');
|
2013-12-01 23:54:39 +01:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$controller = $this->getToplevelController();
|
2013-12-05 16:48:01 +01:00
|
|
|
return $controller->customise(array( 'Content' => $form ));
|
2013-12-01 23:54:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
|
2013-12-01 23:54:39 +01:00
|
|
|
/**
|
2014-09-07 19:18:32 +02:00
|
|
|
* Handles bulkEditForm submission
|
|
|
|
* and parses and saves each records data
|
2013-12-01 23:54:39 +01:00
|
|
|
*
|
2014-09-07 19:18:32 +02:00
|
|
|
* @param array $data Sumitted form data
|
|
|
|
* @param Form $form Form
|
2013-12-01 23:54:39 +01:00
|
|
|
*/
|
2014-09-07 19:18:32 +02:00
|
|
|
public function doSave($data, $form)
|
|
|
|
{
|
|
|
|
$className = $this->gridField->list->dataClass;
|
|
|
|
$singleton = singleton($className);
|
2014-05-03 17:33:57 +02:00
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
$formsData = array();
|
|
|
|
$ids = array();
|
|
|
|
$done = 0;
|
2014-05-03 17:33:57 +02:00
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
//unescape and sort form data per record ID
|
|
|
|
foreach ($data as $fieldName => $value)
|
2014-05-03 17:33:57 +02:00
|
|
|
{
|
2014-09-07 19:18:32 +02:00
|
|
|
if ( $fieldInfo = $this->unEscapeFieldName($fieldName) )
|
2014-05-03 17:33:57 +02:00
|
|
|
{
|
2014-09-07 19:18:32 +02:00
|
|
|
if ( !isset($formsData[$fieldInfo['id']]) )
|
|
|
|
{
|
|
|
|
$formsData[$fieldInfo['id']] = array();
|
2014-05-03 17:33:57 +02:00
|
|
|
}
|
2014-09-07 19:18:32 +02:00
|
|
|
|
|
|
|
$formsData[$fieldInfo['id']][$fieldInfo['name']] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//process each record's form data and save
|
|
|
|
foreach ($formsData as $recordID => $recordData)
|
|
|
|
{
|
|
|
|
$record = DataObject::get_by_id($className, $recordID);
|
|
|
|
$recordForm = Form::create(
|
|
|
|
$this, "RecordForm",
|
|
|
|
$record->getCMSFields(),
|
|
|
|
FieldList::create()
|
|
|
|
);
|
|
|
|
|
|
|
|
$recordForm->loadDataFrom($recordData);
|
|
|
|
$recordForm->saveInto($record);
|
|
|
|
$id = $record->write();
|
|
|
|
|
|
|
|
array_push($ids, $record->ID);
|
|
|
|
|
|
|
|
if ( $id )
|
|
|
|
{
|
|
|
|
$done++;
|
2013-12-01 23:54:39 +01:00
|
|
|
}
|
2014-05-03 17:33:57 +02:00
|
|
|
}
|
2013-12-05 17:29:47 +01:00
|
|
|
|
2014-09-07 19:18:32 +02:00
|
|
|
//compose form message
|
|
|
|
$messageModelClass = (($editingCount > 1) ? $singleton->i18n_plural_name() : $singleton->i18n_singular_name());
|
|
|
|
$message = _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_RESULT_TEXT',
|
|
|
|
'{count} {class} saved successfully.',
|
|
|
|
array(
|
|
|
|
'count' => $done,
|
|
|
|
'class' => $messageModelClass
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$form->sessionMessage($message, 'good');
|
|
|
|
|
|
|
|
//return back to form
|
|
|
|
return Controller::curr()->redirect($this->Link('?records[]='.implode('&records[]=', $ids)));
|
|
|
|
//return Controller::curr()->redirect($form->Backlink); //returns to gridField
|
2013-12-01 23:54:39 +01:00
|
|
|
}
|
2014-05-22 15:52:19 +02:00
|
|
|
}
|