Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phriction/controller/PhrictionMoveController.php
Show All 10 Lines | final class PhrictionMoveController extends PhrictionController { | ||||
public function processRequest() { | public function processRequest() { | ||||
$request = $this->getRequest(); | $request = $this->getRequest(); | ||||
$user = $request->getUser(); | $user = $request->getUser(); | ||||
if ($this->id) { | if ($this->id) { | ||||
$document = id(new PhrictionDocumentQuery()) | $document = id(new PhrictionDocumentQuery()) | ||||
->setViewer($user) | ->setViewer($user) | ||||
->withIDs(array($this->id)) | ->withIDs(array($this->id)) | ||||
->needContent(true) | |||||
->requireCapabilities( | ->requireCapabilities( | ||||
array( | array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
)) | )) | ||||
->executeOne(); | ->executeOne(); | ||||
} else { | } else { | ||||
$slug = PhabricatorSlug::normalize( | $slug = PhabricatorSlug::normalize( | ||||
$request->getStr('slug')); | $request->getStr('slug')); | ||||
if (!$slug) { | if (!$slug) { | ||||
return new Aphront404Response(); | return new Aphront404Response(); | ||||
} | } | ||||
$document = id(new PhrictionDocumentQuery()) | $document = id(new PhrictionDocumentQuery()) | ||||
->setViewer($user) | ->setViewer($user) | ||||
->withSlugs(array($slug)) | ->withSlugs(array($slug)) | ||||
->needContent(true) | |||||
->requireCapabilities( | ->requireCapabilities( | ||||
array( | array( | ||||
PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
)) | )) | ||||
->executeOne(); | ->executeOne(); | ||||
} | } | ||||
Show All 26 Lines | if (isset($disallowed_statuses[$document->getStatus()])) { | ||||
->setTitle('Can not move page!') | ->setTitle('Can not move page!') | ||||
->appendChild(pht('An already moved or deleted document '. | ->appendChild(pht('An already moved or deleted document '. | ||||
'can not be moved again.')) | 'can not be moved again.')) | ||||
->addCancelButton($cancel_uri); | ->addCancelButton($cancel_uri); | ||||
return id(new AphrontDialogResponse())->setDialog($error_dialog); | return id(new AphrontDialogResponse())->setDialog($error_dialog); | ||||
} | } | ||||
$content = id(new PhrictionContent())->load($document->getContentID()); | $content = $document->getContent(); | ||||
if ($request->isFormPost() && !count($errors)) { | if ($request->isFormPost() && !count($errors)) { | ||||
if (!count($errors)) { // First check if the target document exists | |||||
// NOTE: We use the ominpotent user because we can't let users overwrite | // NOTE: We use the ominpotent user because we can't let users overwrite | ||||
// documents even if they can't see them. | // documents even if they can't see them. | ||||
$target_document = id(new PhrictionDocumentQuery()) | $target_document = id(new PhrictionDocumentQuery()) | ||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | ->setViewer(PhabricatorUser::getOmnipotentUser()) | ||||
->withSlugs(array($target_slug)) | ->withSlugs(array($target_slug)) | ||||
->needContent(true) | |||||
->executeOne(); | ->executeOne(); | ||||
// Considering to overwrite existing docs? Nuke this! | // Considering to overwrite existing docs? Nuke this! | ||||
if ($target_document && $target_document->getStatus() == | $exists = PhrictionDocumentStatus::STATUS_EXISTS; | ||||
PhrictionDocumentStatus::STATUS_EXISTS) { | if ($target_document && $target_document->getStatus() == $exists) { | ||||
$errors[] = pht('Can not overwrite existing target document.'); | $errors[] = pht('Can not overwrite existing target document.'); | ||||
$e_url = pht('Already exists.'); | $e_url = pht('Already exists.'); | ||||
} | } | ||||
} | |||||
if (!count($errors)) { // I like to move it, move it! | if (!count($errors)) { | ||||
$from_editor = id(PhrictionDocumentEditor::newForSlug($slug)) | |||||
->setActor($user) | |||||
->setTitle($content->getTitle()) | |||||
->setContent($content->getContent()) | |||||
->setDescription($content->getDescription()); | |||||
$target_editor = id(PhrictionDocumentEditor::newForSlug( | $editor = id(new PhrictionTransactionEditor()) | ||||
$target_slug)) | |||||
->setActor($user) | ->setActor($user) | ||||
->setTitle($content->getTitle()) | ->setContentSourceFromRequest($request) | ||||
->setContent($content->getContent()) | ->setContinueOnNoEffect(true) | ||||
->setDescription($content->getDescription()); | ->setDescription($request->getStr('description')); | ||||
// Move it! | $xactions = array(); | ||||
$target_editor->moveHere($document->getID(), $document->getPHID()); | $xactions[] = id(new PhrictionTransaction()) | ||||
->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO) | |||||
// Retrieve the target doc directly from the editor | ->setNewValue($document); | ||||
// No need to load it per Sql again | if (!$target_document) { | ||||
$target_document = $target_editor->getDocument(); | $target_document = PhrictionDocument::initializeNewDocument( | ||||
$from_editor->moveAway($target_document->getID()); | $user, | ||||
$target_slug); | |||||
} | |||||
$editor->applyTransactions($target_document, $xactions); | |||||
$redir_uri = PhrictionDocument::getSlugURI($target_document->getSlug()); | $redir_uri = PhrictionDocument::getSlugURI( | ||||
$target_document->getSlug()); | |||||
return id(new AphrontRedirectResponse())->setURI($redir_uri); | return id(new AphrontRedirectResponse())->setURI($redir_uri); | ||||
} | } | ||||
} | } | ||||
if ($errors) { | if ($errors) { | ||||
$error_view = id(new AphrontErrorView()) | $error_view = id(new AphrontErrorView()) | ||||
->setErrors($errors); | ->setErrors($errors); | ||||
} | } | ||||
$form = id(new PHUIFormLayoutView()) | $form = id(new PHUIFormLayoutView()) | ||||
->setUser($user) | ->setUser($user) | ||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormStaticControl()) | id(new AphrontFormStaticControl()) | ||||
->setLabel(pht('Title')) | ->setLabel(pht('Title')) | ||||
->setValue($content->getTitle())) | ->setValue($content->getTitle())) | ||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
->setLabel(pht('New URI')) | ->setLabel(pht('New URI')) | ||||
->setValue($target_slug) | ->setValue($target_slug) | ||||
->setError($e_url) | ->setError($e_url) | ||||
->setName('new-slug') | ->setName('new-slug') | ||||
->setCaption(pht('The new location of the document.'))) | ->setCaption(pht('The new location of the document.'))) | ||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
->setLabel(pht('Edit Notes')) | ->setLabel(pht('Edit Notes')) | ||||
->setValue($content->getDescription()) | ->setValue($content->getDescription()) | ||||
->setError(null) | ->setError(null) | ||||
->setName('description')); | ->setName('description')); | ||||
$dialog = id(new AphrontDialogView()) | $dialog = id(new AphrontDialogView()) | ||||
->setUser($user) | ->setUser($user) | ||||
->setTitle(pht('Move Document')) | ->setTitle(pht('Move Document')) | ||||
->appendChild($form) | ->appendChild($form) | ||||
->setSubmitURI($submit_uri) | ->setSubmitURI($submit_uri) | ||||
->addSubmitButton(pht('Move Document')) | ->addSubmitButton(pht('Move Document')) | ||||
->addCancelButton($cancel_uri); | ->addCancelButton($cancel_uri); | ||||
return id(new AphrontDialogResponse())->setDialog($dialog); | return id(new AphrontDialogResponse())->setDialog($dialog); | ||||
} | } | ||||
} | } |