2012-08-24 22:19:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|
|
|
|
2012-08-24 22:19:30 +02:00
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-24 22:19:14 +02:00
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
$parent = null;
|
2012-08-24 22:19:30 +02:00
|
|
|
$parent_id = null;
|
|
|
|
if (!$this->id) {
|
|
|
|
$is_create = true;
|
|
|
|
|
|
|
|
$paste = new PhabricatorPaste();
|
|
|
|
|
|
|
|
$parent_id = $request->getStr('parent');
|
|
|
|
if ($parent_id) {
|
|
|
|
// NOTE: If the Paste is forked from a paste which the user no longer
|
|
|
|
// has permission to see, we still let them edit it.
|
|
|
|
$parent = id(new PhabricatorPasteQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($parent_id))
|
2012-08-24 22:20:20 +02:00
|
|
|
->needContent(true)
|
2012-12-20 21:24:34 +01:00
|
|
|
->needRawContent(true)
|
2012-08-24 22:19:30 +02:00
|
|
|
->execute();
|
|
|
|
$parent = head($parent);
|
|
|
|
|
|
|
|
if ($parent) {
|
|
|
|
$paste->setParentPHID($parent->getPHID());
|
2012-10-01 05:08:37 +02:00
|
|
|
$paste->setViewPolicy($parent->getViewPolicy());
|
2012-08-24 22:19:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$paste->setAuthorPHID($user->getPHID());
|
|
|
|
} else {
|
|
|
|
$is_create = false;
|
2012-08-24 22:19:14 +02:00
|
|
|
|
2012-08-24 22:19:30 +02:00
|
|
|
$paste = id(new PhabricatorPasteQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->executeOne();
|
|
|
|
if (!$paste) {
|
|
|
|
return new Aphront404Response();
|
2012-08-24 22:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = null;
|
|
|
|
$e_text = true;
|
|
|
|
$errors = array();
|
|
|
|
if ($request->isFormPost()) {
|
2012-08-24 22:19:30 +02:00
|
|
|
|
|
|
|
if ($is_create) {
|
|
|
|
$text = $request->getStr('text');
|
|
|
|
if (!strlen($text)) {
|
2013-02-13 20:47:31 +01:00
|
|
|
$e_text = pht('Required');
|
|
|
|
$errors[] = pht('The paste may not be blank.');
|
2012-08-24 22:19:30 +02:00
|
|
|
} else {
|
|
|
|
$e_text = null;
|
|
|
|
}
|
2012-08-24 22:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$paste->setTitle($request->getStr('title'));
|
|
|
|
$paste->setLanguage($request->getStr('language'));
|
2012-09-13 19:15:08 +02:00
|
|
|
$paste->setViewPolicy($request->getStr('can_view'));
|
|
|
|
|
|
|
|
// NOTE: The author is the only editor and can always view the paste,
|
|
|
|
// so it's impossible for them to choose an invalid policy.
|
2012-08-24 22:19:14 +02:00
|
|
|
|
|
|
|
if (!$errors) {
|
2012-08-24 22:19:30 +02:00
|
|
|
if ($is_create) {
|
|
|
|
$paste_file = PhabricatorFile::newFromFileData(
|
|
|
|
$text,
|
|
|
|
array(
|
|
|
|
'name' => $paste->getTitle(),
|
|
|
|
'mime-type' => 'text/plain; charset=utf-8',
|
|
|
|
'authorPHID' => $user->getPHID(),
|
|
|
|
));
|
|
|
|
$paste->setFilePHID($paste_file->getPHID());
|
|
|
|
}
|
2012-08-24 22:19:14 +02:00
|
|
|
$paste->save();
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($paste->getURI());
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-24 22:19:30 +02:00
|
|
|
if ($is_create && $parent) {
|
2013-02-13 20:47:31 +01:00
|
|
|
$paste->setTitle(pht('Fork of %s', $parent->getFullName()));
|
2012-08-24 22:19:14 +02:00
|
|
|
$paste->setLanguage($parent->getLanguage());
|
2012-12-20 21:24:34 +01:00
|
|
|
$text = $parent->getRawContent();
|
2012-08-24 22:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
2013-02-13 20:47:31 +01:00
|
|
|
->setTitle(pht('A Fatal Omission!'))
|
2012-08-24 22:19:14 +02:00
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new AphrontFormView();
|
|
|
|
$form->setFlexible(true);
|
|
|
|
|
|
|
|
$langs = array(
|
2013-02-13 20:47:31 +01:00
|
|
|
'' => pht('(Detect From Filename in Title)'),
|
2012-08-24 22:19:14 +02:00
|
|
|
) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
|
|
|
|
|
|
|
|
$form
|
|
|
|
->setUser($user)
|
|
|
|
->addHiddenInput('parent', $parent_id)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-02-13 20:47:31 +01:00
|
|
|
->setLabel(pht('Title'))
|
2012-08-24 22:19:14 +02:00
|
|
|
->setValue($paste->getTitle())
|
|
|
|
->setName('title'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
2013-02-13 20:47:31 +01:00
|
|
|
->setLabel(pht('Language'))
|
2012-08-24 22:19:14 +02:00
|
|
|
->setName('language')
|
|
|
|
->setValue($paste->getLanguage())
|
2012-08-24 22:19:30 +02:00
|
|
|
->setOptions($langs));
|
|
|
|
|
2012-09-13 19:15:08 +02:00
|
|
|
$policies = id(new PhabricatorPolicyQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->setObject($paste)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormPolicyControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
|
|
|
->setPolicyObject($paste)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_view'));
|
|
|
|
|
2012-08-24 22:19:30 +02:00
|
|
|
if ($is_create) {
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
2013-02-13 20:47:31 +01:00
|
|
|
->setLabel(pht('Text'))
|
2012-08-24 22:19:30 +02:00
|
|
|
->setError($e_text)
|
|
|
|
->setValue($text)
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
|
|
|
->setCustomClass('PhabricatorMonospaced')
|
|
|
|
->setName('text'));
|
2012-10-02 01:49:27 +02:00
|
|
|
} else {
|
2013-01-18 03:57:09 +01:00
|
|
|
$fork_link = phutil_tag(
|
2012-10-02 01:49:27 +02:00
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $this->getApplicationURI('?parent='.$paste->getID())
|
|
|
|
),
|
2013-02-19 22:33:10 +01:00
|
|
|
pht('Fork'));
|
2012-10-02 01:49:27 +02:00
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
2013-02-13 20:47:31 +01:00
|
|
|
->setLabel(pht('Text'))
|
|
|
|
->setValue(pht(
|
2013-02-05 23:30:29 +01:00
|
|
|
'Paste text can not be edited. %s to create a new paste.',
|
|
|
|
$fork_link)));
|
2012-08-24 22:19:30 +02:00
|
|
|
}
|
2012-08-24 22:19:14 +02:00
|
|
|
|
2012-08-24 22:19:30 +02:00
|
|
|
$submit = new AphrontFormSubmitControl();
|
|
|
|
|
|
|
|
if (!$is_create) {
|
|
|
|
$submit->addCancelButton($paste->getURI());
|
2012-12-07 22:35:17 +01:00
|
|
|
$submit->setValue(pht('Save Paste'));
|
|
|
|
$title = pht('Edit %s', $paste->getFullName());
|
|
|
|
$short = pht('Edit');
|
2012-08-24 22:19:30 +02:00
|
|
|
} else {
|
2012-12-07 22:35:17 +01:00
|
|
|
$submit->setValue(pht('Create Paste'));
|
|
|
|
$title = pht('Create Paste');
|
|
|
|
$short = pht('Create');
|
2012-08-24 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
2012-08-24 22:19:14 +02:00
|
|
|
->appendChild($submit);
|
|
|
|
|
2012-12-07 22:35:17 +01:00
|
|
|
$crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
|
|
|
|
if (!$is_create) {
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName('P'.$paste->getID())
|
|
|
|
->setHref('/P'.$paste->getID()));
|
|
|
|
}
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())->setName($short));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
2012-08-24 22:19:14 +02:00
|
|
|
array(
|
2012-12-07 22:35:17 +01:00
|
|
|
$crumbs,
|
2012-08-24 22:19:30 +02:00
|
|
|
id(new PhabricatorHeaderView())->setHeader($title),
|
2012-08-24 22:19:14 +02:00
|
|
|
$error_view,
|
|
|
|
$form,
|
2012-12-07 22:35:17 +01:00
|
|
|
),
|
2012-08-24 22:19:14 +02:00
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'device' => true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|