Process updates in bulk

This commit is contained in:
Thierry Francois 2014-05-03 18:33:57 +03:00
parent 00b936baa6
commit e621f4895f
2 changed files with 142 additions and 136 deletions

View File

@ -87,7 +87,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields( $recordCMSDataFields, $this->gridField->list->dataClass, $id );
$recordCMSDataFields['ID'] = new HiddenField('ID', '', $id);
//$recordCMSDataFields['ID'] = new HiddenField('ID', '', $id);
$recordCMSDataFields = GridFieldBulkEditingHelper::escapeFormFieldsName( $recordCMSDataFields, $id );
$recordsFieldList->push(
@ -155,11 +155,21 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
*/
public function update()
{
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($this->request->requestVars());
$record = DataObject::get_by_id($this->gridField->list->dataClass, $data['ID']);
$data = $this->request->requestVars();
$return = array();
$className = $this->gridField->list->dataClass;
foreach($data as $field => $value)
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();
@ -169,12 +179,16 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$record->setCastedField($field, $value);
}
}
$record->write();
$done = $record->write();
array_push($return, array(
'id' => $done,
'title' => $record->getTitle()
));
}
return json_encode(array(
'done' => 1,
'recordID' => $data['ID'],
'title' => $record->getTitle()
'records' => $return
), JSON_NUMERIC_CHECK);
}
}

View File

@ -40,18 +40,18 @@
$('.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+'"/>');
var id = this.attr('id').split('_')[3],
name = 'bulkEditingForm',
$wrap = $('<div/>')
;
$wrap.attr('id', name + '_' + id).addClass(name).data('id', id);
this.wrap($wrap);
},
onunmatch: function(){
}
onunmatch: function(){}
});
$('.bulkEditingForm').entwine({
onsubmit: function(){
return false;
},
onchange: function(){
this.removeClass('updated');
if ( !this.hasClass('hasUpdate') )
@ -62,70 +62,62 @@
});
$('#bulkEditingUpdateBtn').entwine({
onmatch: function(){
$(this).data('completedForms', 0);
},
onunmatch: function(){
},
onmatch: function(){},
onunmatch: function(){},
onclick: function(e){
e.stopImmediatePropagation();
var $formsWithUpadtes = $('form.bulkEditingForm.hasUpdate'),
url = $(this).data('url'),
data,
cacheBuster
var $formsWithUpadtes = $('div.bulkEditingForm.hasUpdate'),
url = this.data('url'),
data = {},
cacheBuster = new Date().getTime() + '_' + this.attr('name')
;
$(this).data('formsToUpdate', $formsWithUpadtes.length);
if ( $formsWithUpadtes.length > 0 )
{
this.addClass('loading');
}
else{
return;
}
if ( $formsWithUpadtes.length > 0 ) $(this).addClass('loading');
if ( url.indexOf('?') !== -1 )
{
cacheBuster = '&cacheBuster=' + cacheBuster;
}
else{
cacheBuster = '?cacheBuster=' + cacheBuster;
}
$formsWithUpadtes.each(function(){
cacheBuster = new Date().getTime() + '_' + $(this).attr('name');
data = $(this).serialize();
if ( url.indexOf('?') !== -1 ) cacheBuster = '&cacheBuster=' + cacheBuster;
else cacheBuster = '?cacheBuster=' + cacheBuster;
var $this = $(this);
data[$this.data('id')] = $this.find(':input').serializeArray();
});
$.ajax({
url: url + cacheBuster,
data: data,
type: "POST",
context: $(this)
context: this
}).success(function(data, textStatus, jqXHR){
var $btn = $('#bulkEditingUpdateBtn'),
totalForms = parseInt( $btn.data('formsToUpdate') ),
counter = parseInt( $btn.data('completedForms') ),
title
;
counter = counter + 1;
$btn.data('completedForms', counter);
$(this).removeClass('hasUpdate');
$(this).addClass('updated');
try{
data = $.parseJSON(data);
}catch(er){}
if ( data.title )
{
$(this).find('.ui-accordion-header a').html(data.title);
}
$(this).find('.ui-accordion-header').click();
$.each(data.records, function(index, record){
var $form = $('#bulkEditingForm_'+record.id)
$formHeader = $form.find('.ui-accordion-header')
;
if ( counter == totalForms )
{
$('#bulkEditingUpdateBtn').data('completedForms', 0);
$('#bulkEditingUpdateBtn').removeClass('loading');
}
});
})
}
$form.removeClass('hasUpdate').addClass('updated');
$formHeader.find('a').html(record.title);
$formHeader.click();
});
this.removeClass('loading');
});
}
});
});
}(jQuery));