From 622eaac9ed40bc9b690cd6e81f6c9200d53f2dba Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Fri, 7 Nov 2014 10:03:20 -0800 Subject: [PATCH] Phriction - move delete business logic from controlller to editor Summary: Ref T4029. More cleanup and code consolidation for the long terms benefits. Test Plan: found a document and opened up two browser tabs. Loaded delete dialog on both. Completed delete in one tab and noted document was properly deleted. Tried to complete delete in tab 2 and got an error message. Reviewers: chad, epriestley Reviewed By: epriestley Subscribers: Korvin, epriestley Maniphest Tasks: T4029 Differential Revision: https://secure.phabricator.com/D10809 --- .../controller/PhrictionDeleteController.php | 27 +++++++------------ .../editor/PhrictionTransactionEditor.php | 23 ++++++++++++++++ 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/applications/phriction/controller/PhrictionDeleteController.php b/src/applications/phriction/controller/PhrictionDeleteController.php index 5f773834fe..236ad81dd7 100644 --- a/src/applications/phriction/controller/PhrictionDeleteController.php +++ b/src/applications/phriction/controller/PhrictionDeleteController.php @@ -26,20 +26,10 @@ final class PhrictionDeleteController extends PhrictionController { return new Aphront404Response(); } - $e_text = null; - $disallowed_states = array( - PhrictionDocumentStatus::STATUS_DELETED => true, // Silly - PhrictionDocumentStatus::STATUS_MOVED => true, // Makes no sense - PhrictionDocumentStatus::STATUS_STUB => true, // How could they? - ); - if (isset($disallowed_states[$document->getStatus()])) { - $e_text = pht( - 'An already moved or deleted document can not be deleted.'); - } - $document_uri = PhrictionDocument::getSlugURI($document->getSlug()); - if (!$e_text && $request->isFormPost()) { + $e_text = null; + if ($request->isFormPost()) { $xactions = array(); $xactions[] = id(new PhrictionTransaction()) ->setTransactionType(PhrictionTransaction::TYPE_DELETE) @@ -48,16 +38,19 @@ final class PhrictionDeleteController extends PhrictionController { $editor = id(new PhrictionTransactionEditor()) ->setActor($user) ->setContentSourceFromRequest($request) - ->setContinueOnNoEffect(true) - ->applyTransactions($document, $xactions); - - return id(new AphrontRedirectResponse())->setURI($document_uri); + ->setContinueOnNoEffect(true); + try { + $editor->applyTransactions($document, $xactions); + return id(new AphrontRedirectResponse())->setURI($document_uri); + } catch (PhabricatorApplicationTransactionValidationException $ex) { + $e_text = phutil_implode_html("\n", $ex->getErrorMessages()); + } } if ($e_text) { $dialog = id(new AphrontDialogView()) ->setUser($user) - ->setTitle(pht('Can not delete document!')) + ->setTitle(pht('Can Not Delete Document!')) ->appendChild($e_text) ->addCancelButton($document_uri); } else { diff --git a/src/applications/phriction/editor/PhrictionTransactionEditor.php b/src/applications/phriction/editor/PhrictionTransactionEditor.php index a9e3aae896..49d18e7b51 100644 --- a/src/applications/phriction/editor/PhrictionTransactionEditor.php +++ b/src/applications/phriction/editor/PhrictionTransactionEditor.php @@ -476,6 +476,29 @@ final class PhrictionTransactionEditor $errors[] = $error; } } + break; + case PhrictionTransaction::TYPE_DELETE: + switch ($object->getStatus()) { + case PhrictionDocumentStatus::STATUS_DELETED: + $e_text = pht('An already deleted document can not be deleted.'); + break; + case PhrictionDocumentStatus::STATUS_MOVED: + $e_text = pht('A moved document can not be deleted.'); + break; + case PhrictionDocumentStatus::STATUS_STUB: + $e_text = pht('A stub document can not be deleted.'); + break; + default: + break 2; + } + + $error = new PhabricatorApplicationTransactionValidationError( + $type, + pht('Can not delete document.'), + $e_text, + $xaction); + $errors[] = $error; + break; } }