From 574c53d5ba522f43650d9cd4de2d51dd57b53a32 Mon Sep 17 00:00:00 2001 From: jean Date: Mon, 8 Oct 2012 09:18:13 +1300 Subject: [PATCH] FIX 7927 Redirect to the parent controller after deleting an item in a gridfield (edit form) --- forms/gridfield/GridFieldDetailForm.php | 55 ++++++++++++++++--------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/forms/gridfield/GridFieldDetailForm.php b/forms/gridfield/GridFieldDetailForm.php index 69a15a551..a32acbe14 100644 --- a/forms/gridfield/GridFieldDetailForm.php +++ b/forms/gridfield/GridFieldDetailForm.php @@ -326,7 +326,7 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler { $this->component->getValidator() ); if($this->record->ID !== 0) { - $form->loadDataFrom($this->record); + $form->loadDataFrom($this->record); } // TODO Coupling with CMS @@ -341,16 +341,9 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler { if($form->Fields()->hasTabset()) { $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); $form->addExtraClass('ss-tabset cms-tabset'); - } - - if($toplevelController->hasMethod('Backlink')) { - $form->Backlink = $toplevelController->Backlink(); - } elseif($this->popupController->hasMethod('Breadcrumbs')) { - $parents = $this->popupController->Breadcrumbs(false)->items; - $form->Backlink = array_pop($parents)->Link; - } else { - $form->Backlink = $toplevelController->Link(); } + + $form->Backlink = $this->getBackLink(); } $cb = $this->component->getItemEditFormCallback(); @@ -373,6 +366,25 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler { } return $c; } + + protected function getBackLink(){ + // TODO Coupling with CMS + $backlink = ''; + $toplevelController = $this->getToplevelController(); + if($toplevelController && $toplevelController instanceof LeftAndMain) { + if($toplevelController->hasMethod('Backlink')) { + $backlink = $toplevelController->Backlink(); + } elseif($this->popupController->hasMethod('Breadcrumbs')) { + $parents = $this->popupController->Breadcrumbs(false)->items; + $backlink = array_pop($parents)->Link; + } else { + $backlink = $toplevelController->Link(); + } + } + return $backlink; + } + + public function doSave($data, $form) { $new_record = $this->record->ID == 0; @@ -424,14 +436,14 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler { } public function doDelete($data, $form) { + $title = $this->record->Title; try { - $toDelete = $this->record; - if (!$toDelete->canDelete()) { + if (!$this->record->canDelete()) { throw new ValidationException( _t('GridFieldDetailForm.DeletePermissionsFailure',"No delete permissions"),0); } - $toDelete->delete(); + $this->record->delete(); } catch(ValidationException $e) { $form->sessionMessage($e->getResult()->message(), 'bad'); return Controller::curr()->redirectBack(); @@ -440,17 +452,22 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler { $message = sprintf( _t('GridFieldDetailForm.Deleted', 'Deleted %s %s'), $this->record->singular_name(), - '"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"' + htmlspecialchars($title, ENT_QUOTES) ); + + $toplevelController = $this->getToplevelController(); + if($toplevelController && $toplevelController instanceof LeftAndMain) { + $backForm = $toplevelController->getEditForm(); + $backForm->sessionMessage($message, 'good'); + } else { + $form->sessionMessage($message, 'good'); + } - $form->sessionMessage($message, 'good'); - - //when an item is deleted, redirect to the revelant admin section without the action parameter + //when an item is deleted, redirect to the parent controller $controller = Controller::curr(); - $noActionURL = $controller->removeAction($data['url']); $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh - return $controller->redirect($noActionURL, 302); //redirect back to admin section + return $controller->redirect($this->getBacklink(), 302); //redirect back to admin section } /**