Permalink
Browse files

ENHANCEMENT: working delete button

  • Loading branch information...
1 parent 550f754 commit 393690998005cbe98c46dfcfcac68c6262d7a5fd Julian Seidenberg committed with chillu Feb 27, 2012
Showing with 49 additions and 2 deletions.
  1. +17 −0 control/Controller.php
  2. +32 −2 forms/gridfield/GridFieldPopupForms.php
View
@@ -298,6 +298,23 @@ function getViewer($action) {
public function hasAction($action) {
return parent::hasAction($action) || $this->hasActionTemplate($action);
}
+
+ /**
+ * Removes all the "action" part of the current URL and returns the result.
+ * If no action parameter is present, returns the full URL
+ * @static
+ * @return String
+ */
+ public function removeAction($fullURL, $action = null) {
+ if (!$action) $action = $this->getAction(); //default to current action
+ $returnURL = $fullURL;
+
+ if (($pos = strpos($fullURL, $action)) !== false) {
+ $returnURL = substr($fullURL,0,$pos);
+ }
+
+ return $returnURL;
+ }
/**
* Returns TRUE if this controller has a template that is specifically designed to handle a specific action.
@@ -183,10 +183,12 @@ function ItemEditForm() {
// WARNING: The arguments passed here are a little arbitrary. This API will need cleanup
$this->record->getCMSFields($this->popupController, $this->popupFormName),
new FieldList(
- $saveAction = new FormAction('doSave', _t('GridFieldDetailsForm.Save', 'Save'))
+ $saveAction = new FormAction('doSave', _t('GridFieldDetailsForm.Save', 'Save')),
+ $deleteAction = new FormAction('doDelete', _t('GridFieldDetailsForm.Delete', 'Delete'))
)
);
$saveAction->addExtraClass('ss-ui-action-constructive');
+ $deleteAction->addExtraClass('ss-ui-action-destructive');
$form->loadDataFrom($this->record);
return $form;
}
@@ -201,7 +203,7 @@ function doSave($data, $form) {
$this->gridField->getList()->add($this->record);
} catch(ValidationException $e) {
$form->sessionMessage($e->getResult()->message(), 'bad');
- return Director::redirectBack();
+ return Controller::curr()->redirectBack();
}
// TODO Save this item into the given relationship
@@ -217,6 +219,34 @@ function doSave($data, $form) {
return $this->popupController->redirectBack();
}
+ function doDelete($data, $form) {
+ try {
+ $toDelete = $this->record;
+ if (!$toDelete->canDelete()) {
+ throw new ValidationException(_t('GridFieldDetailsForm.DeletePermissionsFailure',"No delete permissions"),0);
+ }
+
+ $toDelete->delete();
+ } catch(ValidationException $e) {
+ $form->sessionMessage($e->getResult()->message(), 'bad');
+ return Director::redirectBack();
+ }
+
+ $message = sprintf(
+ _t('ComplexTableField.SUCCESSEDIT2', 'Deleted %s %s'),
+ $this->record->singular_name(),
+ '<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'
+ );
+
+ $form->sessionMessage($message, 'good');
+
+ //when an item is deleted, redirect to the revelant admin section without the action parameter
+ $controller = Controller::curr();
+ $noActionURL = $controller->removeAction($data['url']);
+
+ return Director::redirect($noActionURL, 302); //redirect back to admin section
+ }
+
/**
* @param String
*/

0 comments on commit 3936909

Please sign in to comment.