Skip to content

Commit

Permalink
ENHANCEMENT: working delete button
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Seidenberg authored and chillu committed Feb 27, 2012
1 parent 550f754 commit 3936909
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
17 changes: 17 additions & 0 deletions control/Controller.php
Expand Up @@ -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.
Expand Down
34 changes: 32 additions & 2 deletions forms/gridfield/GridFieldPopupForms.php
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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
*/
Expand Down

0 comments on commit 3936909

Please sign in to comment.