GridFieldBulkEditingTools/bulkManager/code/GridFieldBulkActionEditHandler.php

199 lines
5.8 KiB
PHP
Raw Normal View History

<?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'
);
/**
* Return a form for all the selected DataObject
* with their respective editable fields.
*
* @return form Selected DataObject editable fields
*/
public function editForm()
{
$crumbs = $this->Breadcrumbs();
if($crumbs && $crumbs->count()>=2)
{
$one_level_up = $crumbs->offsetGet($crumbs->count()-2);
}
$actions = new FieldList();
$actions->push(
2014-04-13 18:56:12 +02:00
FormAction::create('SaveAll', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.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)
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
);
$actions->push(
2014-04-13 18:56:12 +02:00
FormAction::create('Cancel', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.CANCEL_BTN_LABEL', 'Cancel'))
->setAttribute('id', 'bulkEditingUpdateCancelBtn')
->addExtraClass('ss-ui-action-destructive cms-panel-link')
->setAttribute('data-icon', 'decline')
->setAttribute('href', $one_level_up->Link)
->setUseButtonTag(true)
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
);
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
$header = LiteralField::create(
'bulkEditHeader',
'<h1 id="bulkEditHeader">' . _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.HEADER',
'Editing {count} {class}',
array(
'count' => $editingCount,
2014-05-04 13:34:31 +02:00
'class' => $titleModelClass
2014-05-01 23:03:43 +02:00
)
) . '</h1>'
);
$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);
foreach ( $recordList as $id )
{
2014-05-04 13:34:31 +02:00
$record = DataObject::get_by_id($modelClass, $id);
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelCMSDataFields( $config, $this->gridField->list->dataClass );
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields( $recordCMSDataFields, $this->gridField->list->dataClass, $id );
2014-05-03 17:33:57 +02:00
//$recordCMSDataFields['ID'] = new HiddenField('ID', '', $id);
$recordCMSDataFields = GridFieldBulkEditingHelper::escapeFormFieldsName( $recordCMSDataFields, $id );
2014-05-01 23:03:43 +02:00
$recordsFieldList->push(
ToggleCompositeField::create(
2014-05-04 13:34:31 +02:00
'RecordFields_'.$id,
$record->getTitle(),
array_values($recordCMSDataFields)
2014-05-04 13:34:31 +02:00
)
->setHeadingLevel(4)
->setAttribute('data-id', $id)
->addExtraClass('bulkEditingFieldHolder')
);
}
$form = new Form(
$this,
2014-05-04 13:34:31 +02:00
'BulkEditingForm',
2014-05-01 23:03:43 +02:00
$recordsFieldList,
$actions
);
if($crumbs && $crumbs->count()>=2){
$form->Backlink = $one_level_up->Link;
}
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');
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');
if($this->request->isAjax())
{
$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');
return $response;
}
else {
$controller = $this->getToplevelController();
return $controller->customise(array( 'Content' => $form ));
}
}
/**
* Saves the changes made in the bulk edit into the dataObject
*
* @return JSON
*/
public function update()
{
2014-05-03 17:33:57 +02:00
$data = $this->request->requestVars();
$return = array();
$className = $this->gridField->list->dataClass;
if ( isset($data['url']) ) unset($data['url']);
if ( isset($data['cacheBuster']) ) unset($data['cacheBuster']);
foreach ($data as $recordID => $recordDataSet)
{
$record = DataObject::get_by_id($className, $recordID);
foreach($recordDataSet as $recordData)
{
$field = preg_replace('/record_(\d+)_(\w+)/i', '$2', $recordData['name']);
$value = $recordData['value'];
if ( $record->hasMethod($field) )
{
$list = $record->$field();
$list->setByIDList($value);
}
else{
$record->setCastedField($field, $value);
}
}
2014-05-03 17:33:57 +02:00
$done = $record->write();
array_push($return, array(
'id' => $done,
'title' => $record->getTitle()
));
}
return json_encode(array(
2014-05-03 17:33:57 +02:00
'done' => 1,
'records' => $return
), JSON_NUMERIC_CHECK);
}
}