diff --git a/src/applications/phriction/controller/PhrictionMoveController.php b/src/applications/phriction/controller/PhrictionMoveController.php --- a/src/applications/phriction/controller/PhrictionMoveController.php +++ b/src/applications/phriction/controller/PhrictionMoveController.php @@ -56,75 +56,36 @@ $submit_uri = $request->getRequestURI()->getPath(); $cancel_uri = PhrictionDocument::getSlugURI($slug); - $errors = array(); - $error_view = null; - $e_url = null; - - $disallowed_statuses = array( - PhrictionDocumentStatus::STATUS_DELETED => true, // Silly - PhrictionDocumentStatus::STATUS_MOVED => true, // Plain silly - PhrictionDocumentStatus::STATUS_STUB => true, // Utterly silly - ); - if (isset($disallowed_statuses[$document->getStatus()])) { - $error_dialog = id(new AphrontDialogView()) - ->setUser($user) - ->setTitle('Can not move page!') - ->appendChild(pht('An already moved or deleted document '. - 'can not be moved again.')) - ->addCancelButton($cancel_uri); - - return id(new AphrontDialogResponse())->setDialog($error_dialog); - } - + $e_url = true; + $validation_exception = null; $content = $document->getContent(); - if ($request->isFormPost() && !count($errors)) { - - // NOTE: We use the ominpotent user because we can't let users overwrite - // documents even if they can't see them. - $target_document = id(new PhrictionDocumentQuery()) - ->setViewer(PhabricatorUser::getOmnipotentUser()) - ->withSlugs(array($target_slug)) - ->needContent(true) - ->executeOne(); - - // Considering to overwrite existing docs? Nuke this! - $exists = PhrictionDocumentStatus::STATUS_EXISTS; - if ($target_document && $target_document->getStatus() == $exists) { - $errors[] = pht('Can not overwrite existing target document.'); - $e_url = pht('Already exists.'); - } - - if (!count($errors)) { - - $editor = id(new PhrictionTransactionEditor()) - ->setActor($user) - ->setContentSourceFromRequest($request) - ->setContinueOnNoEffect(true) - ->setDescription($request->getStr('description')); - - $xactions = array(); - $xactions[] = id(new PhrictionTransaction()) - ->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO) - ->setNewValue($document); - if (!$target_document) { - $target_document = PhrictionDocument::initializeNewDocument( - $user, - $target_slug); - } + if ($request->isFormPost()) { + + $editor = id(new PhrictionTransactionEditor()) + ->setActor($user) + ->setContentSourceFromRequest($request) + ->setContinueOnNoEffect(true) + ->setDescription($request->getStr('description')); + + $xactions = array(); + $xactions[] = id(new PhrictionTransaction()) + ->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO) + ->setNewValue($document); + $target_document = PhrictionDocument::initializeNewDocument( + $user, + $target_slug); + try { $editor->applyTransactions($target_document, $xactions); - $redir_uri = PhrictionDocument::getSlugURI( $target_document->getSlug()); return id(new AphrontRedirectResponse())->setURI($redir_uri); + } catch (PhabricatorApplicationTransactionValidationException $ex) { + $validation_exception = $ex; + $e_url = $ex->getShortMessage(PhrictionTransaction::TYPE_MOVE_TO); } } - if ($errors) { - $error_view = id(new AphrontErrorView()) - ->setErrors($errors); - } - $form = id(new PHUIFormLayoutView()) ->setUser($user) ->appendChild( @@ -141,12 +102,13 @@ ->appendChild( id(new AphrontFormTextControl()) ->setLabel(pht('Edit Notes')) - ->setValue($content->getDescription()) + ->setValue(pht('Moving document to a new location.')) ->setError(null) ->setName('description')); $dialog = id(new AphrontDialogView()) ->setUser($user) + ->setValidationException($validation_exception) ->setTitle(pht('Move Document')) ->appendChild($form) ->setSubmitURI($submit_uri) diff --git a/src/applications/phriction/editor/PhrictionTransactionEditor.php b/src/applications/phriction/editor/PhrictionTransactionEditor.php --- a/src/applications/phriction/editor/PhrictionTransactionEditor.php +++ b/src/applications/phriction/editor/PhrictionTransactionEditor.php @@ -477,6 +477,53 @@ } } break; + + case PhrictionTransaction::TYPE_MOVE_TO: + $source_document = $xaction->getNewValue(); + switch ($source_document->getStatus()) { + case PhrictionDocumentStatus::STATUS_DELETED: + $e_text = pht('A deleted document can not be moved.'); + break; + case PhrictionDocumentStatus::STATUS_MOVED: + $e_text = pht('A moved document can not be moved again.'); + break; + case PhrictionDocumentStatus::STATUS_STUB: + $e_text = pht('A stub document can not be moved.'); + break; + default: + $e_text = null; + break; + } + + if ($e_text) { + $error = new PhabricatorApplicationTransactionValidationError( + $type, + pht('Can not move document.'), + $e_text, + $xaction); + $errors[] = $error; + } + + // NOTE: We use the ominpotent user because we can't let users + // overwrite documents even if they can't see them. + $target_document = id(new PhrictionDocumentQuery()) + ->setViewer(PhabricatorUser::getOmnipotentUser()) + ->withSlugs(array($object->getSlug())) + ->needContent(true) + ->executeOne(); + + // Considering to overwrite existing docs? Nuke this! + $exists = PhrictionDocumentStatus::STATUS_EXISTS; + if ($target_document && $target_document->getStatus() == $exists) { + $error = new PhabricatorApplicationTransactionValidationError( + $type, + pht('Can not move document.'), + pht('Can not overwrite existing target document.'), + $xaction); + $errors[] = $error; + } + break; + case PhrictionTransaction::TYPE_DELETE: switch ($object->getStatus()) { case PhrictionDocumentStatus::STATUS_DELETED: @@ -498,7 +545,6 @@ $e_text, $xaction); $errors[] = $error; - break; } }