2012-08-12 16:48:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
|
|
|
*/
|
|
|
|
class GridFieldBulkManager_Request extends RequestHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var GridField
|
|
|
|
*/
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var GridField_URLHandler
|
|
|
|
*/
|
|
|
|
protected $component;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2013-04-04 21:58:09 +02:00
|
|
|
private static $url_handlers = array(
|
2012-08-12 16:48:34 +02:00
|
|
|
'$Action!' => '$Action'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridFIeld $gridField
|
|
|
|
* @param GridField_URLHandler $component
|
|
|
|
* @param Controller $controller
|
|
|
|
*/
|
|
|
|
public function __construct($gridField, $component, $controller) {
|
|
|
|
$this->gridField = $gridField;
|
|
|
|
$this->component = $component;
|
|
|
|
$this->controller = $controller;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL for this RequestHandler
|
|
|
|
*
|
|
|
|
* @author SilverStripe
|
|
|
|
* @see GridFieldDetailForm_ItemRequest
|
|
|
|
* @param string $action
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Link($action = null) {
|
2012-08-18 14:44:09 +02:00
|
|
|
return Controller::join_links($this->gridField->Link(), 'bulkediting', $action);
|
2012-08-12 16:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function edit(SS_HTTPRequest $request)
|
|
|
|
{
|
2012-08-16 22:29:35 +02:00
|
|
|
$recordList = $this->getPOSTRecordList($request);
|
|
|
|
|
|
|
|
$crumbs = $this->Breadcrumbs();
|
|
|
|
if($crumbs && $crumbs->count()>=2) $one_level_up = $crumbs->offsetGet($crumbs->count()-2);
|
|
|
|
|
|
|
|
$actions = new FieldList();
|
|
|
|
|
|
|
|
$actions->push(
|
2013-06-23 18:10:22 +02:00
|
|
|
FormAction::create('SaveAll', _t('GridFieldBulkTools.SAVE_BTN_LABEL', 'Save All'))
|
2012-08-26 22:59:25 +02:00
|
|
|
->setAttribute('id', 'bulkEditingUpdateBtn')
|
2012-08-16 22:29:35 +02:00
|
|
|
->addExtraClass('ss-ui-action-constructive cms-panel-link')
|
|
|
|
->setAttribute('data-icon', 'accept')
|
|
|
|
->setAttribute('data-url', $this->Link('update'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
);
|
2013-03-04 10:50:41 +01:00
|
|
|
/*
|
2012-08-16 22:29:35 +02:00
|
|
|
if($crumbs && $crumbs->count()>=2)
|
|
|
|
{
|
|
|
|
$actions->push(
|
|
|
|
FormAction::create('SaveAndFinish', 'Save All & Finish')
|
2012-08-26 22:59:25 +02:00
|
|
|
->setAttribute('id', 'bulkEditingUpdateFinishBtn')
|
2012-08-16 22:29:35 +02:00
|
|
|
->addExtraClass('ss-ui-action-constructive cms-panel-link')
|
|
|
|
->setAttribute('data-icon', 'accept')
|
|
|
|
->setAttribute('data-url', $this->Link('update'))
|
|
|
|
->setAttribute('data-return-url', $one_level_up->Link)
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
);
|
2013-03-04 10:50:41 +01:00
|
|
|
} */
|
2012-08-16 22:29:35 +02:00
|
|
|
|
|
|
|
$actions->push(
|
2013-06-23 18:10:22 +02:00
|
|
|
FormAction::create('Cancel', _t('GridFieldBulkTools.CANCEL_BTN_LABEL', 'Cancel & Delete All'))
|
2012-08-26 22:59:25 +02:00
|
|
|
->setAttribute('id', 'bulkEditingUpdateCancelBtn')
|
2012-08-16 22:29:35 +02:00
|
|
|
->addExtraClass('ss-ui-action-destructive cms-panel-link')
|
|
|
|
->setAttribute('data-icon', 'decline')
|
|
|
|
->setAttribute('data-url', $this->Link('cancel'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
$editedRecordList = new FieldList();
|
|
|
|
$config = $this->component->getConfig();
|
|
|
|
|
|
|
|
foreach ( $recordList as $id )
|
|
|
|
{
|
2012-08-18 00:05:19 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelCMSDataFields( $config, $this->gridField->list->dataClass );
|
2012-08-16 22:29:35 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
|
2012-08-18 00:05:19 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields( $recordCMSDataFields, $this->gridField->list->dataClass, $id );
|
2012-08-16 22:29:35 +02:00
|
|
|
|
2012-08-26 22:59:25 +02:00
|
|
|
$recordCMSDataFields['ID'] = new HiddenField('ID', '', $id);
|
2012-08-16 22:29:35 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::escapeFormFieldsName( $recordCMSDataFields, $id );
|
2012-08-26 22:59:25 +02:00
|
|
|
|
2012-08-16 22:29:35 +02:00
|
|
|
$editedRecordList->push(
|
2012-08-26 22:59:25 +02:00
|
|
|
ToggleCompositeField::create(
|
|
|
|
'GFBM_'.$id,
|
|
|
|
'#'.$id.': '.DataObject::get_by_id($this->gridField->list->dataClass, $id)->getTitle(),
|
|
|
|
array_values($recordCMSDataFields)
|
|
|
|
)->setHeadingLevel(4)
|
|
|
|
->addExtraClass('bulkEditingFieldHolder')
|
2012-08-16 22:29:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'bulkEditingForm',
|
|
|
|
$editedRecordList,
|
|
|
|
$actions
|
|
|
|
);
|
|
|
|
|
|
|
|
$form->setTemplate('LeftAndMain_EditForm');
|
|
|
|
$form->addExtraClass('center cms-content');
|
|
|
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
|
|
|
|
|
|
|
|
if($crumbs && $crumbs->count()>=2){
|
|
|
|
$form->Backlink = $one_level_up->Link;
|
|
|
|
}
|
|
|
|
|
|
|
|
$formHTML = $form->forTemplate();
|
|
|
|
|
|
|
|
Requirements::javascript(BULK_EDIT_TOOLS_PATH . '/javascript/GridFieldBulkManager.js');
|
2013-06-23 18:10:22 +02:00
|
|
|
Requirements::css(BULK_EDIT_TOOLS_PATH . '/css/GridFieldBulkManager.css');
|
|
|
|
Requirements::add_i18n_javascript(BULK_EDIT_TOOLS_PATH . '/javascript/lang');
|
2012-08-16 22:29:35 +02:00
|
|
|
|
|
|
|
$response = new SS_HTTPResponse($formHTML);
|
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
$response->addHeader('X-Title', 'SilverStripe - Bulk '.$this->gridField->list->dataClass.' Editing');
|
2013-05-19 19:01:41 +02:00
|
|
|
|
|
|
|
if($request->isAjax())
|
|
|
|
{
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$controller = $this->getToplevelController();
|
|
|
|
// If not requested by ajax, we need to render it within the controller context+template
|
|
|
|
return $controller->customise(array(
|
|
|
|
'Content' => $response->getBody(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse up nested requests until we reach the first that's not a GridFieldDetailForm or GridFieldDetailForm_ItemRequest.
|
|
|
|
* The opposite of {@link Controller::curr()}, required because
|
|
|
|
* Controller::$controller_stack is not directly accessible.
|
|
|
|
*
|
|
|
|
* @return Controller
|
|
|
|
*/
|
|
|
|
protected function getToplevelController() {
|
|
|
|
$c = $this->controller;
|
|
|
|
while($c && ($c instanceof GridFieldDetailForm_ItemRequest || $c instanceof GridFieldDetailForm)) {
|
|
|
|
$c = $c->getController();
|
|
|
|
}
|
|
|
|
return $c;
|
2012-08-12 16:48:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-26 22:59:25 +02:00
|
|
|
/**
|
|
|
|
* Saves the changes made in the bulk edit into the dataObject
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return JSON
|
|
|
|
*/
|
|
|
|
public function update(SS_HTTPRequest $request)
|
|
|
|
{
|
2012-09-05 19:17:19 +02:00
|
|
|
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($request->requestVars());
|
2012-08-26 22:59:25 +02:00
|
|
|
$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();
|
|
|
|
|
|
|
|
return '{done:1,recordID:'.$data['ID'].'}';
|
|
|
|
}
|
|
|
|
|
2012-08-16 22:29:35 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return \SS_HTTPResponse
|
|
|
|
*/
|
2012-08-12 16:48:34 +02:00
|
|
|
public function unlink(SS_HTTPRequest $request)
|
|
|
|
{
|
2012-08-15 20:16:41 +02:00
|
|
|
$recordList = $this->getPOSTRecordList($request);
|
2013-03-04 10:44:53 +01:00
|
|
|
$this->gridField->list->removeMany($recordList);
|
2012-08-15 20:16:41 +02:00
|
|
|
|
2013-03-04 10:44:53 +01:00
|
|
|
$response = new SS_HTTPResponse(Convert::raw2json(array($recordList)));
|
2012-08-15 20:16:41 +02:00
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
return $response;
|
2012-08-12 16:48:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-16 22:29:35 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return \SS_HTTPResponse
|
|
|
|
*/
|
2012-08-12 16:48:34 +02:00
|
|
|
public function delete(SS_HTTPRequest $request)
|
|
|
|
{
|
|
|
|
$recordList = $this->getPOSTRecordList($request);
|
|
|
|
$recordClass = $this->gridField->list->dataClass;
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ( $recordList as $id )
|
|
|
|
{
|
|
|
|
$res = DataObject::delete_by_id($recordClass, $id);
|
|
|
|
array_push($result, array($id => $res));
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = new SS_HTTPResponse(Convert::raw2json(array($result)));
|
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getPOSTRecordList(SS_HTTPRequest $request)
|
|
|
|
{
|
|
|
|
$recordList = $request->requestVars();
|
|
|
|
return $recordList['records'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edited version of the GridFieldEditForm function
|
|
|
|
* adds the 'Bulk Upload' at the end of the crums
|
|
|
|
*
|
|
|
|
* CMS-specific functionality: Passes through navigation breadcrumbs
|
|
|
|
* to the template, and includes the currently edited record (if any).
|
|
|
|
* see {@link LeftAndMain->Breadcrumbs()} for details.
|
|
|
|
*
|
|
|
|
* @author SilverStripe original Breadcrumbs() method
|
|
|
|
* @see GridFieldDetailForm_ItemRequest
|
|
|
|
* @param boolean $unlinked
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
|
|
|
function Breadcrumbs($unlinked = false) {
|
|
|
|
if(!$this->controller->hasMethod('Breadcrumbs')) return;
|
|
|
|
|
|
|
|
$items = $this->controller->Breadcrumbs($unlinked);
|
|
|
|
$items->push(new ArrayData(array(
|
2012-08-18 00:27:36 +02:00
|
|
|
'Title' => 'Bulk Editing',
|
2012-08-12 16:48:34 +02:00
|
|
|
'Link' => false
|
|
|
|
)));
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
}
|