Basic operations for bulk editing in place

Create Request_handler edit function returning the form, and JS
manipulation of the UI buttons etc..
This commit is contained in:
colymba 2012-08-16 23:29:35 +03:00
parent 471a1c1331
commit 92d269bac3
3 changed files with 268 additions and 19 deletions

View File

@ -7,8 +7,122 @@
*/
class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler {
public function __construct()
/**
* 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(),
'fieldsNameBlacklist' => array()
);
/**
* 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' );
public function __construct($editableFields = null)
{
if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields );
$this->config['fieldsClassBlacklist'] = $this->forbiddenFieldsClasses;
}
/**
* Set a component configuration parameter
*
* @param string $reference
* @param mixed $value
*/
function setConfig ( $reference, $value )
{
if ( isset( $this->config[$reference] ) )
{
if ( ($reference == 'fieldsClassBlacklist' || $reference == 'fieldsClassBlacklist' || $reference == 'editableFields') && !is_array($value) )
{
$value = array($value);
}
//makes sure $forbiddenFieldsClasses are in no matter what
if ( $reference == 'fieldsClassBlacklist' )
{
$value = array_unique( array_merge($value, $this->forbiddenFieldsClasses) );
}
$this->config[$reference] = $value;
}
}
/**
* 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;
}
}
/* GridField_ColumnProvider */
@ -56,17 +170,24 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
$dropDownActionList = DropdownField::create('bulkActionName', '')
->setSource( array('edit' => 'Edit','unlink' => 'UnLink','delete' => 'Delete') );
/*
$actionButton = FormAction::create('doBulkAction', 'GO')
->setAttribute('id', 'doBulkActionButton')
//->addExtraClass('cms-panel-link')
->setAttribute('data-icon', 'pencil')
->setAttribute('data-url', $gridField->Link('bulkEdit'))
->setAttribute('href', $gridField->Link('bulkEdit').'/edit')
->setUseButtonTag(true);
*/
$actionButtonHTML = '
<a id="doBulkActionButton" href="'.$gridField->Link('bulkEdit').'/edit'.'" data-url="'.$gridField->Link('bulkEdit').'" class="action ss-ui-button cms-panel-link" data-icon="pencil">
GO
</a>';
$html = '<div id="bulkManagerOptions">'.
$dropDownActionList->FieldHolder().
$actionButton->Field().
//$actionButton->Field().
$actionButtonHTML.
'</div>';
return array(

View File

@ -60,9 +60,99 @@ class GridFieldBulkManager_Request extends RequestHandler {
public function edit(SS_HTTPRequest $request)
{
$recordList = $request->requestVars();
$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(
FormAction::create('SaveAll', 'Save All')
->setAttribute('id', 'bulkImageUploadUpdateBtn')
->addExtraClass('ss-ui-action-constructive cms-panel-link')
->setAttribute('data-icon', 'accept')
->setAttribute('data-url', $this->Link('update'))
->setUseButtonTag(true)
);
if($crumbs && $crumbs->count()>=2)
{
$actions->push(
FormAction::create('SaveAndFinish', 'Save All & Finish')
->setAttribute('id', 'bulkImageUploadUpdateFinishBtn')
->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)
);
}
$actions->push(
FormAction::create('Cancel', 'Cancel & Delete All')
->setAttribute('id', 'bulkImageUploadUpdateCancelBtn')
->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 )
{
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelCMSDataFields( $config, $this->gridField->list->dataClass, $id );
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
$recordCMSDataFields = GridFieldBulkEditingHelper::escapeFormFieldsName( $recordCMSDataFields, $id );
$editedRecordList->push(
ToggleCompositeField::create('GFBM_'.$id, '#'.$id,
array_values($recordCMSDataFields)
)->setHeadingLevel(4)
);
}
/*
* ********************************************************************
*/
$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');
Requirements::css(BULK_EDIT_TOOLS_PATH . '/css/GridFieldBulkManager.css');
$response = new SS_HTTPResponse($formHTML);
$response->addHeader('Content-Type', 'text/plain');
$response->addHeader('X-Title', 'SilverStripe - Bulk '.$this->gridField->list->dataClass.' Editing');
return $response;
}
/**
*
* @param SS_HTTPRequest $request
* @return \SS_HTTPResponse
*/
public function unlink(SS_HTTPRequest $request)
{
$recordList = $this->getPOSTRecordList($request);
@ -84,6 +174,11 @@ class GridFieldBulkManager_Request extends RequestHandler {
return $response;
}
/**
*
* @param SS_HTTPRequest $request
* @return \SS_HTTPResponse
*/
public function delete(SS_HTTPRequest $request)
{
$recordList = $this->getPOSTRecordList($request);

View File

@ -29,7 +29,20 @@
onunmatch: function(){
},
onclick: function(e) {
}
}/*,
onchange: function(){
var idList, id;
idList = $('#doBulkActionButton').data('selection');
if ( !idList ) idList = '';
id = $(this).attr('name').split('_')[1];
if ( !$(this).prop('checked') ) idList.replace( '#' + id, '');
else idList = idList + '#' + id;
$('#doBulkActionButton').data('selection', idList);
}*/
});
$('select#bulkActionName').entwine({
@ -48,18 +61,22 @@
$(btn).removeClass('ss-ui-action-destructive');
$(btn).attr('data-icon', 'pencil');
$(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-pencil');
$(btn).attr('href', $(btn).data('url')+'/edit');
break;
case 'unlink':
$(btn).removeClass('ss-ui-action-destructive');
$(btn).attr('data-icon', 'chain--minus');
$(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-chain--minus');
$(btn).removeAttr('href');
break;
case 'delete':
$(btn).addClass('ss-ui-action-destructive');
$(btn).attr('data-icon', 'decline');
$(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-decline');
$(btn).removeAttr('href');
break;
}
@ -72,25 +89,41 @@
},
onunmatch: function(){
},
onmouseover: function(){
var action, ids = [];
action = $('select#bulkActionName').val();
if ( action == 'edit' )
{
$('.col-bulkSelect input:checked').each(function(){
ids.push( parseInt( $(this).attr('name').split('_')[1] ) );
});
if(ids.length > 0) $(this).attr('href', $(this).data('url')+'/'+action+'?records[]='+ids.join('&records[]=') );
}
},
onclick: function(e) {
var action, url, data = {}, ids = [], cacheBuster;
action = $('select#bulkActionName').val();
url = $(this).data('url');
cacheBuster = new Date().getTime();
$('.col-bulkSelect input:checked').each(function(){
ids.push( parseInt( $(this).attr('name').split('_')[1] ) );
});
data.records = ids;
if ( action != 'edit' )
{
url = $(this).data('url');
cacheBuster = new Date().getTime();
$('.col-bulkSelect input:checked').each(function(){
ids.push( parseInt( $(this).attr('name').split('_')[1] ) );
});
data.records = ids;
$.ajax({
url: url + '/' + action + '?cacheBuster=' + cacheBuster,
data: data,
type: "POST",
context: $(this)
}).done(function() {
//@TODO refresh GridField
});
}
$.ajax({
url: url + '/' + action + '?cacheBuster=' + cacheBuster,
data: data,
type: "POST",
context: $(this)
}).done(function() {
//@TODO refresh GridField
});
}
});