updated fields can now be saved

changes made in bulk editing are now saved: db fields + relations.
Fixing some DOM elements IDs and URLs
This commit is contained in:
colymba 2012-08-26 23:59:25 +03:00
parent 8f7a753d7f
commit fd4f046ad5
3 changed files with 143 additions and 8 deletions

View File

@ -180,7 +180,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
->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">
<a id="doBulkActionButton" href="'.$gridField->Link('bulkediting').'/edit'.'" data-url="'.$gridField->Link('bulkediting').'" class="action ss-ui-button cms-panel-link" data-icon="pencil">
GO
</a>';
@ -202,7 +202,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
*/
public function getURLHandlers($gridField) {
return array(
'bulkEdit' => 'handlebulkEdit'
'bulkediting' => 'handlebulkEdit'
);
}

View File

@ -69,7 +69,7 @@ class GridFieldBulkManager_Request extends RequestHandler {
$actions->push(
FormAction::create('SaveAll', 'Save All')
->setAttribute('id', 'bulkImageUploadUpdateBtn')
->setAttribute('id', 'bulkEditingUpdateBtn')
->addExtraClass('ss-ui-action-constructive cms-panel-link')
->setAttribute('data-icon', 'accept')
->setAttribute('data-url', $this->Link('update'))
@ -80,7 +80,7 @@ class GridFieldBulkManager_Request extends RequestHandler {
{
$actions->push(
FormAction::create('SaveAndFinish', 'Save All & Finish')
->setAttribute('id', 'bulkImageUploadUpdateFinishBtn')
->setAttribute('id', 'bulkEditingUpdateFinishBtn')
->addExtraClass('ss-ui-action-constructive cms-panel-link')
->setAttribute('data-icon', 'accept')
->setAttribute('data-url', $this->Link('update'))
@ -91,7 +91,7 @@ class GridFieldBulkManager_Request extends RequestHandler {
$actions->push(
FormAction::create('Cancel', 'Cancel & Delete All')
->setAttribute('id', 'bulkImageUploadUpdateCancelBtn')
->setAttribute('id', 'bulkEditingUpdateCancelBtn')
->addExtraClass('ss-ui-action-destructive cms-panel-link')
->setAttribute('data-icon', 'decline')
->setAttribute('data-url', $this->Link('cancel'))
@ -111,11 +111,16 @@ class GridFieldBulkManager_Request extends RequestHandler {
$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, '#'.$id.' > '.DataObject::get_by_id($this->gridField->list->dataClass, $id)->getTitle(),
array_values($recordCMSDataFields)
)->setHeadingLevel(4)
ToggleCompositeField::create(
'GFBM_'.$id,
'#'.$id.': '.DataObject::get_by_id($this->gridField->list->dataClass, $id)->getTitle(),
array_values($recordCMSDataFields)
)->setHeadingLevel(4)
->addExtraClass('bulkEditingFieldHolder')
);
}
@ -149,6 +154,31 @@ class GridFieldBulkManager_Request extends RequestHandler {
return $response;
}
/**
* Saves the changes made in the bulk edit into the dataObject
*
* @param SS_HTTPRequest $request
* @return JSON
*/
public function update(SS_HTTPRequest $request)
{
$data = $this->getParsedPostData($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();
return '{done:1,recordID:'.$data['ID'].'}';
}
/**
*
* @param SS_HTTPRequest $request
@ -204,6 +234,27 @@ class GridFieldBulkManager_Request extends RequestHandler {
return $recordList['records'];
}
/**
* Simple function that replace the 'record_XX_' off of the ID field name
* prefix needed since it was taken for a pageID if sent as is as well as fixing other things
*
* @param array $data
* @return array
*/
function getParsedPostData(array $data)
{
$return = array();
foreach( $data as $key => $val)
{
$return[ preg_replace( '/record_(\d+)_(\w+)/i', '$2', $key) ] = $val;
}
if ( isset($return['url']) ) unset($return['url']);
if ( isset($return['cacheBuster']) ) unset($return['cacheBuster']);
return $return;
}
/**
* Edited version of the GridFieldEditForm function

View File

@ -127,6 +127,90 @@
}
});
$('.bulkEditingFieldHolder').entwine({
onmatch: function(){
var id, name = 'bulkEditingForm';
id = $(this).attr('id').split('_')[3];
$(this).wrap('<form name="'+name+'_'+id+'" id="'+name+'_'+id+'" class="'+name+'"/>');
},
onunmatch: function(){
}
});
$('.bulkEditingForm input, .bulkEditingForm select, .bulkEditingForm textarea').entwine({
onchange: function(){
var form;
form = this.parents('form.bulkEditingForm');
if ( !$(form).hasClass('hasUpdate') ) {
$(form).addClass('hasUpdate');
}
$('#bulkEditingUpdateFinishBtn').addClass('dirty');
}
});
$('#bulkEditingUpdateBtn').entwine({
onmatch: function(){
$(this).data('completedForms', 0);
},
onunmatch: function(){
},
onclick: function(e){
var formsWithUpadtes, url, data, cacheBuster;
formsWithUpadtes = $('form.bulkEditingForm.hasUpdate');
$(this).data('formsToUpdate', $(formsWithUpadtes).length);
url = $(this).data('url');
if ( $(formsWithUpadtes).length > 0 ) $(this).addClass('loading');
//@TODO execute 'doFinish' even when no form have been changed
$(formsWithUpadtes).each(function(){
cacheBuster = new Date().getTime() + '_' + $(this).attr('name');
data = $(this).serialize();
$.ajax({
url: url + '?cacheBuster=' + cacheBuster,
data: data,
type: "POST",
context: $(this)
}).done(function() {
var btn = $('#bulkEditingUpdateBtn');
var totalForms = parseInt( $(btn).data('formsToUpdate') );
var counter = parseInt( $(btn).data('completedForms') );
counter = counter + 1;
$(btn).data('completedForms', counter);
$(this).removeClass('hasUpdate');
if ( counter == totalForms ) {
$('#bulkEditingUpdateFinishBtn').removeClass('dirty');
$('#bulkEditingUpdateBtn').data('completedForms', 0);
$('#bulkEditingUpdateBtn').removeClass('loading');
if ( $('#bulkEditingUpdateBtn').hasClass('doFinish') ) {
//@TODO find a way to pass this as CMS navigation through AJAX
window.location = $('#bulkEditingUpdateFinishBtn').data('return-url');
}
}
});
})
}
});
$('#bulkEditingUpdateFinishBtn').entwine({
onclick: function(e){
if ( $(this).hasClass('dirty') ) {
$('#bulkEditingUpdateBtn').addClass('doFinish');
$('#bulkEditingUpdateBtn').click();
}
}
});
});
}(jQuery));