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;
|
|
|
|
|
2013-10-16 19:35:52 +02:00
|
|
|
$paste = PhabricatorPaste::initializeNewPaste($user);
|
2012-08-24 22:19:30 +02:00
|
|
|
|
|
|
|
$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());
|
2014-05-04 20:11:46 +02:00
|
|
|
$paste->attachRawContent('');
|
2012-08-24 22:19:30 +02:00
|
|
|
} 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))
|
2014-05-04 20:11:46 +02:00
|
|
|
->needRawContent(true)
|
2012-08-24 22:19:30 +02:00
|
|
|
->executeOne();
|
|
|
|
if (!$paste) {
|
|
|
|
return new Aphront404Response();
|
2012-08-24 22:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = null;
|
|
|
|
$e_text = true;
|
|
|
|
$errors = array();
|
2013-08-02 21:56:58 +02:00
|
|
|
if ($is_create && $parent) {
|
|
|
|
$v_title = pht('Fork of %s', $parent->getFullName());
|
|
|
|
$v_language = $parent->getLanguage();
|
|
|
|
$v_text = $parent->getRawContent();
|
|
|
|
} else {
|
|
|
|
$v_title = $paste->getTitle();
|
|
|
|
$v_language = $paste->getLanguage();
|
2014-05-04 20:11:46 +02:00
|
|
|
$v_text = $paste->getRawContent();
|
2013-08-02 21:56:58 +02:00
|
|
|
}
|
|
|
|
$v_policy = $paste->getViewPolicy();
|
|
|
|
|
2014-06-04 02:22:09 +02:00
|
|
|
if ($is_create) {
|
|
|
|
$v_projects = array();
|
|
|
|
} else {
|
|
|
|
$v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
|
|
|
|
$paste->getPHID(),
|
|
|
|
PhabricatorEdgeConfig::TYPE_OBJECT_HAS_PROJECT);
|
|
|
|
$v_projects = array_reverse($v_projects);
|
|
|
|
}
|
|
|
|
|
2012-08-24 22:19:14 +02:00
|
|
|
if ($request->isFormPost()) {
|
2013-08-02 21:56:58 +02:00
|
|
|
$xactions = array();
|
2012-08-24 22:19:30 +02:00
|
|
|
|
2014-05-04 20:11:46 +02:00
|
|
|
$v_text = $request->getStr('text');
|
|
|
|
if (!strlen($v_text)) {
|
|
|
|
$e_text = pht('Required');
|
|
|
|
$errors[] = pht('The paste may not be blank.');
|
|
|
|
} else {
|
|
|
|
$e_text = null;
|
|
|
|
}
|
2012-08-24 22:19:14 +02:00
|
|
|
|
2013-08-02 21:56:58 +02:00
|
|
|
$v_title = $request->getStr('title');
|
|
|
|
$v_language = $request->getStr('language');
|
|
|
|
$v_policy = $request->getStr('can_view');
|
2014-06-04 02:22:09 +02:00
|
|
|
$v_projects = $request->getArr('projects');
|
2012-09-13 19:15:08 +02:00
|
|
|
|
|
|
|
// 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) {
|
2014-05-04 20:11:46 +02:00
|
|
|
if ($is_create || ($v_text !== $paste->getRawContent())) {
|
2014-05-04 20:11:34 +02:00
|
|
|
$file = PhabricatorPasteEditor::initializeFileForPaste(
|
|
|
|
$user,
|
|
|
|
$v_title,
|
|
|
|
$v_text);
|
|
|
|
|
2013-08-02 21:56:58 +02:00
|
|
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
2014-05-04 20:11:46 +02:00
|
|
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)
|
2014-05-04 20:11:34 +02:00
|
|
|
->setNewValue($file->getPHID());
|
2012-08-24 22:19:30 +02:00
|
|
|
}
|
2014-05-04 20:11:34 +02:00
|
|
|
|
2013-08-02 21:56:58 +02:00
|
|
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
|
|
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
|
|
|
|
->setNewValue($v_title);
|
|
|
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
|
|
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)
|
|
|
|
->setNewValue($v_language);
|
|
|
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
|
|
|
|
->setNewValue($v_policy);
|
2014-06-04 02:22:09 +02:00
|
|
|
|
|
|
|
$proj_edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_PROJECT;
|
|
|
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
|
|
|
|
->setMetadataValue('edge:type', $proj_edge_type)
|
|
|
|
->setNewValue(array('=' => array_fuse($v_projects)));
|
|
|
|
|
2013-08-02 21:56:58 +02:00
|
|
|
$editor = id(new PhabricatorPasteEditor())
|
|
|
|
->setActor($user)
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
$xactions = $editor->applyTransactions($paste, $xactions);
|
2012-08-24 22:19:14 +02:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($paste->getURI());
|
2013-08-02 21:56:58 +02:00
|
|
|
} else {
|
|
|
|
// make sure we update policy so its correctly populated to what
|
|
|
|
// the user chose
|
|
|
|
$paste->setViewPolicy($v_policy);
|
2012-08-24 22:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new AphrontFormView();
|
|
|
|
|
|
|
|
$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'))
|
2013-08-02 21:56:58 +02:00
|
|
|
->setValue($v_title)
|
2012-08-24 22:19:14 +02:00
|
|
|
->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')
|
2013-08-02 21:56:58 +02:00
|
|
|
->setValue($v_language)
|
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'));
|
|
|
|
|
2014-06-04 02:22:09 +02:00
|
|
|
|
|
|
|
if ($v_projects) {
|
|
|
|
$project_handles = $this->loadViewerHandles($v_projects);
|
|
|
|
} else {
|
|
|
|
$project_handles = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormTokenizerControl())
|
|
|
|
->setLabel(pht('Projects'))
|
|
|
|
->setName('projects')
|
|
|
|
->setValue($project_handles)
|
|
|
|
->setDatasource('/typeahead/common/projects/'));
|
|
|
|
|
2014-05-04 20:11:46 +02:00
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
2013-02-13 20:47:31 +01:00
|
|
|
->setLabel(pht('Text'))
|
2014-05-04 20:11:46 +02:00
|
|
|
->setError($e_text)
|
|
|
|
->setValue($v_text)
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
|
|
|
->setCustomClass('PhabricatorMonospaced')
|
|
|
|
->setName('text'));
|
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'));
|
2013-08-26 20:53:11 +02:00
|
|
|
$title = pht('Create New Paste');
|
2012-12-07 22:35:17 +01:00
|
|
|
$short = pht('Create');
|
2012-08-24 22:19:30 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 20:53:11 +02:00
|
|
|
$form->appendChild($submit);
|
|
|
|
|
2013-09-25 20:23:29 +02:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2013-08-26 20:53:11 +02:00
|
|
|
->setHeaderText($title)
|
2014-01-10 18:17:37 +01:00
|
|
|
->setFormErrors($errors)
|
2013-08-26 20:53:11 +02:00
|
|
|
->setForm($form);
|
2012-08-24 22:19:14 +02:00
|
|
|
|
2012-12-07 22:35:17 +01:00
|
|
|
$crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
|
|
|
|
if (!$is_create) {
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb('P'.$paste->getID(), '/P'.$paste->getID());
|
2012-12-07 22:35:17 +01:00
|
|
|
}
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb($short);
|
2012-12-07 22:35:17 +01:00
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
2012-08-24 22:19:14 +02:00
|
|
|
array(
|
2012-12-07 22:35:17 +01:00
|
|
|
$crumbs,
|
2013-08-26 20:53:11 +02:00
|
|
|
$form_box,
|
2012-12-07 22:35:17 +01:00
|
|
|
),
|
2012-08-24 22:19:14 +02:00
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|