Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phriction/controller/PhrictionMoveController.php
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | public function processRequest() { | ||||
} | } | ||||
$target_slug = PhabricatorSlug::normalize( | $target_slug = PhabricatorSlug::normalize( | ||||
$request->getStr('new-slug', $slug)); | $request->getStr('new-slug', $slug)); | ||||
$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 | |||||
// 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()) | $editor = id(new PhrictionTransactionEditor()) | ||||
->setActor($user) | ->setActor($user) | ||||
->setContentSourceFromRequest($request) | ->setContentSourceFromRequest($request) | ||||
->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
->setDescription($request->getStr('description')); | ->setDescription($request->getStr('description')); | ||||
$xactions = array(); | $xactions = array(); | ||||
$xactions[] = id(new PhrictionTransaction()) | $xactions[] = id(new PhrictionTransaction()) | ||||
->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO) | ->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO) | ||||
->setNewValue($document); | ->setNewValue($document); | ||||
if (!$target_document) { | |||||
$target_document = PhrictionDocument::initializeNewDocument( | $target_document = PhrictionDocument::initializeNewDocument( | ||||
$user, | $user, | ||||
$target_slug); | $target_slug); | ||||
} | try { | ||||
$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( | ||||
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(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) | ||||
->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); | ||||
} | } | ||||
} | } |