1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-22 05:20:56 +01:00

Phriction - move "move" business logic to the editor

Summary: Ref T4029. Even more code consolidation and cleanup for the long term benefits!

Test Plan: moved a page successfully. tried to move a page to an existing page and got an error. did the two tab trick to try to move a deleted page and got an error.

Reviewers: chad, epriestley

Reviewed By: epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T4029

Differential Revision: https://secure.phabricator.com/D10812
This commit is contained in:
Bob Trahan 2014-11-07 11:45:49 -08:00
parent 3b26fe8c05
commit b538611e29
2 changed files with 68 additions and 60 deletions

View file

@ -56,75 +56,36 @@ final class PhrictionMoveController extends PhrictionController {
$submit_uri = $request->getRequestURI()->getPath(); $submit_uri = $request->getRequestURI()->getPath();
$cancel_uri = PhrictionDocument::getSlugURI($slug); $cancel_uri = PhrictionDocument::getSlugURI($slug);
$errors = array(); $e_url = true;
$error_view = null; $validation_exception = 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);
}
$content = $document->getContent(); $content = $document->getContent();
if ($request->isFormPost() && !count($errors)) { if ($request->isFormPost()) {
// NOTE: We use the ominpotent user because we can't let users overwrite $editor = id(new PhrictionTransactionEditor())
// documents even if they can't see them. ->setActor($user)
$target_document = id(new PhrictionDocumentQuery()) ->setContentSourceFromRequest($request)
->setViewer(PhabricatorUser::getOmnipotentUser()) ->setContinueOnNoEffect(true)
->withSlugs(array($target_slug)) ->setDescription($request->getStr('description'));
->needContent(true)
->executeOne();
// Considering to overwrite existing docs? Nuke this! $xactions = array();
$exists = PhrictionDocumentStatus::STATUS_EXISTS; $xactions[] = id(new PhrictionTransaction())
if ($target_document && $target_document->getStatus() == $exists) { ->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO)
$errors[] = pht('Can not overwrite existing target document.'); ->setNewValue($document);
$e_url = pht('Already exists.'); $target_document = PhrictionDocument::initializeNewDocument(
} $user,
$target_slug);
if (!count($errors)) { try {
$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);
}
$editor->applyTransactions($target_document, $xactions); $editor->applyTransactions($target_document, $xactions);
$redir_uri = PhrictionDocument::getSlugURI( $redir_uri = PhrictionDocument::getSlugURI(
$target_document->getSlug()); $target_document->getSlug());
return id(new AphrontRedirectResponse())->setURI($redir_uri); 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()) $form = id(new PHUIFormLayoutView())
->setUser($user) ->setUser($user)
->appendChild( ->appendChild(
@ -141,12 +102,13 @@ final class PhrictionMoveController extends PhrictionController {
->appendChild( ->appendChild(
id(new AphrontFormTextControl()) id(new AphrontFormTextControl())
->setLabel(pht('Edit Notes')) ->setLabel(pht('Edit Notes'))
->setValue($content->getDescription()) ->setValue(pht('Moving document to a new location.'))
->setError(null) ->setError(null)
->setName('description')); ->setName('description'));
$dialog = id(new AphrontDialogView()) $dialog = id(new AphrontDialogView())
->setUser($user) ->setUser($user)
->setValidationException($validation_exception)
->setTitle(pht('Move Document')) ->setTitle(pht('Move Document'))
->appendChild($form) ->appendChild($form)
->setSubmitURI($submit_uri) ->setSubmitURI($submit_uri)

View file

@ -477,6 +477,53 @@ final class PhrictionTransactionEditor
} }
} }
break; 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: case PhrictionTransaction::TYPE_DELETE:
switch ($object->getStatus()) { switch ($object->getStatus()) {
case PhrictionDocumentStatus::STATUS_DELETED: case PhrictionDocumentStatus::STATUS_DELETED:
@ -498,7 +545,6 @@ final class PhrictionTransactionEditor
$e_text, $e_text,
$xaction); $xaction);
$errors[] = $error; $errors[] = $error;
break; break;
} }
} }