2013-12-01 23:54:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
|
|
|
*/
|
|
|
|
class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of action handling methods
|
|
|
|
*/
|
|
|
|
private static $allowed_actions = array('edit', 'update');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL handling rules.
|
|
|
|
*/
|
|
|
|
private static $url_handlers = array(
|
|
|
|
//'$Action!' => '$Action'
|
|
|
|
'bulkedit/update' => 'update',
|
|
|
|
'bulkedit' => 'edit'
|
|
|
|
);
|
2013-12-05 16:48:01 +01:00
|
|
|
|
|
|
|
|
2013-12-01 23:54:39 +01:00
|
|
|
/**
|
2013-12-05 16:48:01 +01:00
|
|
|
* Return a form for all the selected DataObject
|
|
|
|
* with their respective editable fields.
|
2013-12-01 23:54:39 +01:00
|
|
|
*
|
2013-12-05 16:48:01 +01:00
|
|
|
* @return form Selected DataObject editable fields
|
2013-12-01 23:54:39 +01:00
|
|
|
*/
|
2013-12-05 16:48:01 +01:00
|
|
|
public function editForm()
|
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(
|
|
|
|
FormAction::create('SaveAll', _t('GridFieldBulkTools.SAVE_BTN_LABEL', 'Save All'))
|
|
|
|
->setAttribute('id', 'bulkEditingUpdateBtn')
|
|
|
|
->addExtraClass('ss-ui-action-constructive cms-panel-link')
|
|
|
|
->setAttribute('data-icon', 'accept')
|
|
|
|
->setAttribute('data-url', $this->gridField->Link('bulkaction/bulkedit/update'))
|
|
|
|
->setUseButtonTag(true)
|
2013-12-05 17:29:47 +01:00
|
|
|
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
|
2013-12-01 23:54:39 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$actions->push(
|
2013-12-05 16:48:01 +01:00
|
|
|
FormAction::create('Cancel', _t('GridFieldBulkManager.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
|
|
|
);
|
|
|
|
|
2013-12-05 16:48:01 +01:00
|
|
|
$recordList = $this->getRecordIDList();
|
2013-12-01 23:54:39 +01:00
|
|
|
$editedRecordList = new FieldList();
|
|
|
|
$config = $this->component->getConfig();
|
|
|
|
|
|
|
|
foreach ( $recordList as $id )
|
|
|
|
{
|
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelCMSDataFields( $config, $this->gridField->list->dataClass );
|
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
|
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields( $recordCMSDataFields, $this->gridField->list->dataClass, $id );
|
|
|
|
|
|
|
|
$recordCMSDataFields['ID'] = new HiddenField('ID', '', $id);
|
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::escapeFormFieldsName( $recordCMSDataFields, $id );
|
|
|
|
|
|
|
|
$editedRecordList->push(
|
|
|
|
ToggleCompositeField::create(
|
|
|
|
'GFBM_'.$id,
|
2013-12-05 17:29:47 +01:00
|
|
|
DataObject::get_by_id($this->gridField->list->dataClass, $id)->getTitle(),
|
2013-12-01 23:54:39 +01:00
|
|
|
array_values($recordCMSDataFields)
|
|
|
|
)->setHeadingLevel(4)
|
|
|
|
->addExtraClass('bulkEditingFieldHolder')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'bulkEditingForm',
|
|
|
|
$editedRecordList,
|
|
|
|
$actions
|
2013-12-05 16:48:01 +01:00
|
|
|
);
|
2013-12-01 23:54:39 +01:00
|
|
|
|
|
|
|
if($crumbs && $crumbs->count()>=2){
|
|
|
|
$form->Backlink = $one_level_up->Link;
|
|
|
|
}
|
2013-12-05 16:48:01 +01:00
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and return the editing interface
|
|
|
|
*
|
|
|
|
* @return string Form's HTML
|
|
|
|
*/
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
$form = $this->editForm();
|
|
|
|
$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');
|
|
|
|
Requirements::add_i18n_javascript(BULKEDITTOOLS_MANAGER_PATH . '/javascript/lang');
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the changes made in the bulk edit into the dataObject
|
|
|
|
*
|
|
|
|
* @return JSON
|
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($this->request->requestVars());
|
|
|
|
$record = DataObject::get_by_id($this->gridField->list->dataClass, $data['ID']);
|
|
|
|
|
|
|
|
foreach($data as $field => $value)
|
|
|
|
{
|
|
|
|
if ( $record->hasMethod($field) )
|
|
|
|
{
|
|
|
|
$list = $record->$field();
|
|
|
|
$list->setByIDList( $value );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$record->setCastedField($field, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$record->write();
|
2013-12-05 17:29:47 +01:00
|
|
|
|
|
|
|
return json_encode(array(
|
|
|
|
'done' => 1,
|
|
|
|
'recordID' => $data['ID'],
|
|
|
|
'title' => $record->getTitle()
|
|
|
|
), JSON_NUMERIC_CHECK);
|
2013-12-01 23:54:39 +01:00
|
|
|
}
|
|
|
|
}
|