2013-03-06 22:12:04 +01:00
|
|
|
<?php
|
|
|
|
|
2014-07-10 00:12:48 +02:00
|
|
|
final class PhrictionMoveController extends PhrictionController {
|
2013-03-06 22:12:04 +01:00
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
if ($this->id) {
|
2014-05-19 21:41:12 +02:00
|
|
|
$document = id(new PhrictionDocumentQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($this->id))
|
2014-11-05 21:48:17 +01:00
|
|
|
->needContent(true)
|
2014-05-19 21:41:12 +02:00
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
2013-03-06 22:12:04 +01:00
|
|
|
} else {
|
|
|
|
$slug = PhabricatorSlug::normalize(
|
|
|
|
$request->getStr('slug'));
|
|
|
|
if (!$slug) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2014-05-19 21:41:12 +02:00
|
|
|
$document = id(new PhrictionDocumentQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withSlugs(array($slug))
|
2014-11-05 21:48:17 +01:00
|
|
|
->needContent(true)
|
2014-05-19 21:41:12 +02:00
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
2013-03-06 22:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$document) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($slug)) {
|
|
|
|
$slug = $document->getSlug();
|
|
|
|
}
|
|
|
|
|
|
|
|
$target_slug = PhabricatorSlug::normalize(
|
|
|
|
$request->getStr('new-slug', $slug));
|
|
|
|
|
|
|
|
$submit_uri = $request->getRequestURI()->getPath();
|
|
|
|
$cancel_uri = PhrictionDocument::getSlugURI($slug);
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
$error_view = null;
|
|
|
|
$e_url = null;
|
|
|
|
|
|
|
|
$disallowed_statuses = array(
|
2013-03-21 17:09:30 +01:00
|
|
|
PhrictionDocumentStatus::STATUS_DELETED => true, // Silly
|
|
|
|
PhrictionDocumentStatus::STATUS_MOVED => true, // Plain silly
|
|
|
|
PhrictionDocumentStatus::STATUS_STUB => true, // Utterly silly
|
2013-03-06 22:12:04 +01:00
|
|
|
);
|
2013-03-21 17:09:30 +01:00
|
|
|
if (isset($disallowed_statuses[$document->getStatus()])) {
|
|
|
|
$error_dialog = id(new AphrontDialogView())
|
|
|
|
->setUser($user)
|
2014-06-09 20:36:49 +02:00
|
|
|
->setTitle('Can not move page!')
|
2013-03-21 17:09:30 +01:00
|
|
|
->appendChild(pht('An already moved or deleted document '.
|
|
|
|
'can not be moved again.'))
|
|
|
|
->addCancelButton($cancel_uri);
|
2013-03-06 22:12:04 +01:00
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($error_dialog);
|
|
|
|
}
|
|
|
|
|
2014-11-05 21:48:17 +01:00
|
|
|
$content = $document->getContent();
|
2013-03-06 22:12:04 +01:00
|
|
|
|
|
|
|
if ($request->isFormPost() && !count($errors)) {
|
2014-05-19 21:41:12 +02:00
|
|
|
|
2014-11-05 21:48:17 +01:00
|
|
|
// 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();
|
2013-03-06 22:12:04 +01:00
|
|
|
|
2014-11-05 21:48:17 +01:00
|
|
|
// 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.');
|
2013-03-06 22:12:04 +01:00
|
|
|
}
|
|
|
|
|
2014-11-05 21:48:17 +01:00
|
|
|
if (!count($errors)) {
|
2013-03-06 22:12:04 +01:00
|
|
|
|
2014-11-05 21:48:17 +01:00
|
|
|
$editor = id(new PhrictionTransactionEditor())
|
2013-03-06 22:12:04 +01:00
|
|
|
->setActor($user)
|
2014-11-05 21:48:17 +01:00
|
|
|
->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);
|
2013-03-06 22:12:04 +01:00
|
|
|
|
2014-11-05 21:48:17 +01:00
|
|
|
$redir_uri = PhrictionDocument::getSlugURI(
|
|
|
|
$target_document->getSlug());
|
2013-03-06 22:12:04 +01:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($redir_uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
2013-08-26 20:53:11 +02:00
|
|
|
$form = id(new PHUIFormLayoutView())
|
2013-03-06 22:12:04 +01:00
|
|
|
->setUser($user)
|
2013-03-07 17:20:13 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormStaticControl())
|
2014-11-05 21:48:17 +01:00
|
|
|
->setLabel(pht('Title'))
|
|
|
|
->setValue($content->getTitle()))
|
2013-03-06 22:12:04 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2014-11-05 21:48:17 +01:00
|
|
|
->setLabel(pht('New URI'))
|
|
|
|
->setValue($target_slug)
|
|
|
|
->setError($e_url)
|
|
|
|
->setName('new-slug')
|
|
|
|
->setCaption(pht('The new location of the document.')))
|
2013-03-06 22:12:04 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2014-11-05 21:48:17 +01:00
|
|
|
->setLabel(pht('Edit Notes'))
|
|
|
|
->setValue($content->getDescription())
|
|
|
|
->setError(null)
|
|
|
|
->setName('description'));
|
2013-03-06 22:12:04 +01:00
|
|
|
|
2013-03-07 17:20:13 +01:00
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
->setUser($user)
|
|
|
|
->setTitle(pht('Move Document'))
|
|
|
|
->appendChild($form)
|
|
|
|
->setSubmitURI($submit_uri)
|
|
|
|
->addSubmitButton(pht('Move Document'))
|
|
|
|
->addCancelButton($cancel_uri);
|
2013-03-06 22:12:04 +01:00
|
|
|
|
2013-03-07 17:20:13 +01:00
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
2013-03-06 22:12:04 +01:00
|
|
|
}
|
2014-07-10 00:12:48 +02:00
|
|
|
|
2013-03-06 22:12:04 +01:00
|
|
|
}
|