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,26 +155,40 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
*/
public function update()
{
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($this->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 );
$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);
}
}
else{
$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()
'done' => 1,
'records' => $return
), JSON_NUMERIC_CHECK);
}
}

View File

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