Permalink
Browse files

Merge pull request #9 from jim2mullered/master

Small modifications and bug fixes to improve Model Admin usability
  • Loading branch information...
2 parents 001726d + 301f845 commit 0e52c3861a3331cbe2774a62656800ebc99ae092 Aram Balakjian committed Mar 6, 2012
@@ -103,6 +103,10 @@ public function getCMSActions()
$PublishAction->describe("Publish this item");
$Actions->insertFirst($PublishAction);
}
+
+ $SaveAction = FormAction::create('doSaveToDraft', 'Save');
+ $SaveAction->describe("Save a draft of this item");
+ $Actions->insertFirst($SaveAction);
if ($this->canCreate())
{
@@ -122,7 +126,13 @@ public function getCMSActions()
$Actions->insertFirst($unPublishAction);
}
-
+ $DeleteAction = FormAction::create('doDeleteItem', 'Delete this item');
+ $DeleteAction->describe("Delete this item");
+ $Actions->insertFirst($DeleteAction);
+
+ $ListViewAction = FormAction::create('listview', 'Go back to list');
+ $ListViewAction->describe("Return to the list");
+ $Actions->insertFirst($ListViewAction);
return $Actions;
}
@@ -33,13 +33,30 @@ public function doPublish($data, $form, $request)
} else {
Director::redirectBack();
}
- }
+ }
+
+ public function doSaveToDraft($data, $form, $request) {
+ $form->saveInto($this->currentRecord);
+
+ try {
+ $this->currentRecord->write();
+ } catch(ValidationException $e) {
+ $form->sessionMessage($e->getResult()->message(), 'bad');
+ }
+
+ // Behaviour switched on ajax.
+ if(Director::is_ajax()) {
+ return $this->edit($request);
+ } else {
+ Director::redirectBack();
+ }
+ }
public function doUnpublish($data, $form, $request)
{
$record = $this->currentRecord;
- if($record && !$record->canDeleteFromLive())
+ if($record && !$record->canUnPublish())
return Security::permissionFailure($this);
$record->doUnpublish();
@@ -51,7 +68,7 @@ public function doUnpublish($data, $form, $request)
}
}
- public function doDelete($data, $form, $request)
+ public function doDeleteItem($data, $form, $request)
{
$record = $this->currentRecord;
@@ -61,7 +78,7 @@ public function doDelete($data, $form, $request)
$record->doDelete();
if(Director::is_ajax()) {
- $this->edit($request);
+ $this->edit($request);
} else {
Director::redirectBack();
}
@@ -80,5 +97,16 @@ public function duplicate($data, $form, $request) {
} else {
Director::redirectBack();
}
- }
+ }
+
+ public function EditForm() {
+ $form = parent::EditForm();
+ $fields = $form->Actions();
+ $fields->removeByName('action_doSave');
+ $fields->removeByName('action_doDelete');
+ $fields->removeByName('action_goForward');
+ $fields->removeByName('action_goBack');
+ $form->setActions ($fields);
+ return $form;
+ }
}
@@ -1,3 +1,6 @@
+form p.checkbox {
+ margin: 0px;
+}
.doapToolbar{
border: 1px solid #ccc;
background: #fff;
@@ -8,13 +8,72 @@ $(document).ready(function() {
return false;
}
- $('#ModelClassSelector').live("change",doList);
- $('#list_view').live("click",doList);
- if($('#list_view_loading').length) {
+ $('#ModelClassSelector').live("change",doList);
+ $('#list_view').live("click",doList);
+ if($('#list_view_loading').length) {
doList();
- }
- $('button[name=action_clearsearch]').click(doList);
+ }
+ $('button[name=action_clearsearch]').click(doList);
+
+ //Go back to list button
+ $('#Form_EditForm_action_listview').live('click',doList);
+
+ //Save to draft Button
+ $('#right input[name=action_doSaveToDraft],#right input[name=action_doCreate]').live('click', function(){
+ var form = $('#right form');
+ var formAction = form.attr('action') + '?' + $(this).fieldSerialize();
+
+ // @todo TinyMCE coupling
+ if(typeof tinyMCE != 'undefined') tinyMCE.triggerSave();
+
+ // Post the data to save
+ $.post(formAction, form.formToArray(), function(result){
+ // @todo TinyMCE coupling
+ tinymce_removeAll();
+
+ $('#right #ModelAdminPanel').html(result);
+ if($('#right #ModelAdminPanel form').hasClass('validationerror')) {
+ statusMessage(ss.i18n._t('ModelAdmin.VALIDATIONERROR', 'Validation Error'), 'bad');
+ } else {
+ statusMessage(ss.i18n._t('ModelAdmin.SAVED', 'Saved'), 'good');
+ }
+
+ // TODO/SAM: It seems a bit of a hack to have to list all the little updaters here.
+ // Is jQuery.live a solution?
+ Behaviour.apply(); // refreshes ComplexTableField
+ if(window.onresize) window.onresize();
+ }, 'html');
+
+ return false;
+ });
+
+ //Do Delete Item button
+ $('#right input[name=action_doDeleteItem]').live('click', function(){
+ var confirmed = confirm(ss.i18n._t('ModelAdmin.REALLYDELETE', 'Really delete?'));
+ if(!confirmed) {
+ $(this).removeClass('loading')
+ return false;
+ }
+
+ var form = $('#right form');
+ var formAction = form.attr('action') + '?' + $(this).fieldSerialize();
+
+ // The POST actually handles the delete
+ $.post(formAction, form.formToArray(), function(result){
+ // On success, the panel is refreshed and a status message shown.
+ $('#right #ModelAdminPanel').html(result);
+
+ statusMessage(ss.i18n._t('ModelAdmin.DELETED', 'Successfully deleted'));
+ $('#form_actions_right').remove();
+ // To do - convert everything to jQuery so that this isn't needed
+ Behaviour.apply(); // refreshes ComplexTableField
+
+ return doList();
+ });
+ return false();
+ });
+
//Duplicate Button
$('#right input[name=action_duplicate],#right input[name=action_doPublish],#right input[name=action_doUnpublish]').live('click', function(){
var form = $('#right form');

0 comments on commit 0e52c38

Please sign in to comment.