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))
|
|
|
|
->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))
|
|
|
|
->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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = id(new PhrictionContent())->load($document->getContentID());
|
|
|
|
|
|
|
|
if ($request->isFormPost() && !count($errors)) {
|
|
|
|
if (!count($errors)) { // First check if the target document exists
|
2014-05-19 21:41:12 +02: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))
|
|
|
|
->executeOne();
|
2013-03-06 22:12:04 +01:00
|
|
|
|
|
|
|
// Considering to overwrite existing docs? Nuke this!
|
|
|
|
if ($target_document && $target_document->getStatus() ==
|
|
|
|
PhrictionDocumentStatus::STATUS_EXISTS) {
|
|
|
|
|
|
|
|
$errors[] = pht('Can not overwrite existing target document.');
|
|
|
|
$e_url = pht('Already exists.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($errors)) { // I like to move it, move it!
|
|
|
|
$from_editor = id(PhrictionDocumentEditor::newForSlug($slug))
|
|
|
|
->setActor($user)
|
|
|
|
->setTitle($content->getTitle())
|
|
|
|
->setContent($content->getContent())
|
|
|
|
->setDescription($content->getDescription());
|
|
|
|
|
|
|
|
$target_editor = id(PhrictionDocumentEditor::newForSlug(
|
|
|
|
$target_slug))
|
|
|
|
->setActor($user)
|
|
|
|
->setTitle($content->getTitle())
|
|
|
|
->setContent($content->getContent())
|
|
|
|
->setDescription($content->getDescription());
|
|
|
|
|
|
|
|
// Move it!
|
2013-03-19 22:22:26 +01:00
|
|
|
$target_editor->moveHere($document->getID(), $document->getPHID());
|
2013-03-06 22:12:04 +01:00
|
|
|
|
|
|
|
// Retrieve the target doc directly from the editor
|
|
|
|
// No need to load it per Sql again
|
|
|
|
$target_document = $target_editor->getDocument();
|
|
|
|
$from_editor->moveAway($target_document->getID());
|
|
|
|
|
|
|
|
$redir_uri = PhrictionDocument::getSlugURI($target_document->getSlug());
|
|
|
|
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())
|
|
|
|
->setLabel(pht('Title'))
|
|
|
|
->setValue($content->getTitle()))
|
2013-03-06 22:12:04 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel(pht('New URI'))
|
|
|
|
->setValue($target_slug)
|
|
|
|
->setError($e_url)
|
|
|
|
->setName('new-slug')
|
|
|
|
->setCaption(pht('The new location of the document.')))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel(pht('Edit Notes'))
|
|
|
|
->setValue($content->getDescription())
|
|
|
|
->setError(null)
|
2014-04-19 02:51:46 +02:00
|
|
|
->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
|
|
|
}
|