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::getModelFilteredDataFields($config, $recordCMSDataFields);
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields( $recordCMSDataFields, $this->gridField->list->dataClass, $id ); $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 ); $recordCMSDataFields = GridFieldBulkEditingHelper::escapeFormFieldsName( $recordCMSDataFields, $id );
$recordsFieldList->push( $recordsFieldList->push(
@ -155,26 +155,40 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
*/ */
public function update() public function update()
{ {
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($this->request->requestVars()); $data = $this->request->requestVars();
$record = DataObject::get_by_id($this->gridField->list->dataClass, $data['ID']); $return = array();
$className = $this->gridField->list->dataClass;
foreach($data as $field => $value)
{ if ( isset($data['url']) ) unset($data['url']);
if ( $record->hasMethod($field) ) if ( isset($data['cacheBuster']) ) unset($data['cacheBuster']);
{
$list = $record->$field(); foreach ($data as $recordID => $recordDataSet)
$list->setByIDList( $value ); {
$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{ $done = $record->write();
$record->setCastedField($field, $value); array_push($return, array(
} 'id' => $done,
} 'title' => $record->getTitle()
$record->write(); ));
}
return json_encode(array( return json_encode(array(
'done' => 1, 'done' => 1,
'recordID' => $data['ID'], 'records' => $return
'title' => $record->getTitle()
), JSON_NUMERIC_CHECK); ), JSON_NUMERIC_CHECK);
} }
} }

View File

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