mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-22 14:52:41 +01:00
Allow pastes to be edited
Summary: Permits the name and langauge of a paste to be edited. This will eventually allow the visibility policy to be edited as well. Test Plan: Edited name/langauge of some pastes. Tried to edit a paste I didn't own, was harshly rebuffed. Reviewers: vrana, btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T1690 Differential Revision: https://secure.phabricator.com/D3376
This commit is contained in:
parent
fed30dfb4c
commit
36e71a0601
9 changed files with 144 additions and 49 deletions
|
@ -161,6 +161,7 @@ $action_template = id(new PhutilSprite())
|
|||
$action_map = array(
|
||||
'file' => 'icon/page_white_text.png',
|
||||
'fork' => 'icon/arrow_branch.png',
|
||||
'edit' => 'icon/page_white_edit.png',
|
||||
);
|
||||
|
||||
foreach ($action_map as $icon => $source) {
|
||||
|
|
|
@ -35,6 +35,7 @@ final class PhabricatorApplicationPaste extends PhabricatorApplication {
|
|||
'/P(?P<id>\d+)' => 'PhabricatorPasteViewController',
|
||||
'/paste/' => array(
|
||||
'' => 'PhabricatorPasteEditController',
|
||||
'edit/(?P<id>\d+)/' => 'PhabricatorPasteEditController',
|
||||
'filter/(?P<filter>\w+)/' => 'PhabricatorPasteListController',
|
||||
),
|
||||
);
|
||||
|
|
|
@ -18,15 +18,25 @@
|
|||
|
||||
final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||
|
||||
private $id;
|
||||
|
||||
public function willProcessRequest(array $data) {
|
||||
$this->id = idx($data, 'id');
|
||||
}
|
||||
|
||||
|
||||
public function processRequest() {
|
||||
$request = $this->getRequest();
|
||||
$user = $request->getUser();
|
||||
|
||||
$parent = null;
|
||||
$parent_id = null;
|
||||
if (!$this->id) {
|
||||
$is_create = true;
|
||||
|
||||
$paste = new PhabricatorPaste();
|
||||
$title = 'Create Paste';
|
||||
|
||||
$parent_id = $request->getStr('parent');
|
||||
$parent = null;
|
||||
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.
|
||||
|
@ -41,10 +51,30 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
}
|
||||
}
|
||||
|
||||
$paste->setAuthorPHID($user->getPHID());
|
||||
} else {
|
||||
$is_create = false;
|
||||
|
||||
$paste = id(new PhabricatorPasteQuery())
|
||||
->setViewer($user)
|
||||
->requireCapabilities(
|
||||
array(
|
||||
PhabricatorPolicyCapability::CAN_VIEW,
|
||||
PhabricatorPolicyCapability::CAN_EDIT,
|
||||
))
|
||||
->withIDs(array($this->id))
|
||||
->executeOne();
|
||||
if (!$paste) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
}
|
||||
|
||||
$text = null;
|
||||
$e_text = true;
|
||||
$errors = array();
|
||||
if ($request->isFormPost()) {
|
||||
|
||||
if ($is_create) {
|
||||
$text = $request->getStr('text');
|
||||
if (!strlen($text)) {
|
||||
$e_text = 'Required';
|
||||
|
@ -52,26 +82,27 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
} else {
|
||||
$e_text = null;
|
||||
}
|
||||
}
|
||||
|
||||
$paste->setTitle($request->getStr('title'));
|
||||
$paste->setLanguage($request->getStr('language'));
|
||||
|
||||
if (!$errors) {
|
||||
if ($is_create) {
|
||||
$paste_file = PhabricatorFile::newFromFileData(
|
||||
$text,
|
||||
array(
|
||||
'name' => $title,
|
||||
'name' => $paste->getTitle(),
|
||||
'mime-type' => 'text/plain; charset=utf-8',
|
||||
'authorPHID' => $user->getPHID(),
|
||||
));
|
||||
$paste->setFilePHID($paste_file->getPHID());
|
||||
$paste->setAuthorPHID($user->getPHID());
|
||||
}
|
||||
$paste->save();
|
||||
|
||||
return id(new AphrontRedirectResponse())->setURI($paste->getURI());
|
||||
}
|
||||
} else {
|
||||
if ($parent) {
|
||||
if ($is_create && $parent) {
|
||||
$paste->setTitle('Fork of '.$parent->getFullName());
|
||||
$paste->setLanguage($parent->getLanguage());
|
||||
|
||||
|
@ -96,9 +127,6 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
'' => '(Detect With Wizardly Powers)',
|
||||
) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
|
||||
|
||||
$submit = id(new AphrontFormSubmitControl())
|
||||
->setValue('Create Paste');
|
||||
|
||||
$form
|
||||
->setUser($user)
|
||||
->addHiddenInput('parent', $parent_id)
|
||||
|
@ -112,7 +140,10 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
->setLabel('Language')
|
||||
->setName('language')
|
||||
->setValue($paste->getLanguage())
|
||||
->setOptions($langs))
|
||||
->setOptions($langs));
|
||||
|
||||
if ($is_create) {
|
||||
$form
|
||||
->appendChild(
|
||||
id(new AphrontFormTextAreaControl())
|
||||
->setLabel('Text')
|
||||
|
@ -120,7 +151,8 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
->setValue($text)
|
||||
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
||||
->setCustomClass('PhabricatorMonospaced')
|
||||
->setName('text'))
|
||||
->setName('text'));
|
||||
}
|
||||
|
||||
/* TODO: Doesn't have any useful options yet.
|
||||
->appendChild(
|
||||
|
@ -132,13 +164,25 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
->setName('policy'))
|
||||
*/
|
||||
|
||||
$submit = new AphrontFormSubmitControl();
|
||||
|
||||
if (!$is_create) {
|
||||
$submit->addCancelButton($paste->getURI());
|
||||
$submit->setValue('Save Paste');
|
||||
$title = 'Edit '.$paste->getFullName();
|
||||
} else {
|
||||
$submit->setValue('Create Paste');
|
||||
$title = 'Create Paste';
|
||||
}
|
||||
|
||||
$form
|
||||
->appendChild($submit);
|
||||
|
||||
$nav = $this->buildSideNavView();
|
||||
$nav->selectFilter('edit');
|
||||
$nav->appendChild(
|
||||
array(
|
||||
id(new PhabricatorHeaderView())->setHeader('Create Paste'),
|
||||
id(new PhabricatorHeaderView())->setHeader($title),
|
||||
$error_view,
|
||||
$form,
|
||||
));
|
||||
|
|
|
@ -59,7 +59,7 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
|||
$fork_phids));
|
||||
|
||||
$header = $this->buildHeaderView($paste);
|
||||
$actions = $this->buildActionView($paste, $file);
|
||||
$actions = $this->buildActionView($user, $paste, $file);
|
||||
$properties = $this->buildPropertyView($paste, $fork_phids);
|
||||
$source_code = $this->buildSourceCodeView($paste, $file);
|
||||
|
||||
|
@ -89,9 +89,15 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
|||
}
|
||||
|
||||
private function buildActionView(
|
||||
PhabricatorUser $user,
|
||||
PhabricatorPaste $paste,
|
||||
PhabricatorFile $file) {
|
||||
|
||||
$can_edit = PhabricatorPolicyFilter::hasCapability(
|
||||
$user,
|
||||
$paste,
|
||||
PhabricatorPolicyCapability::CAN_EDIT);
|
||||
|
||||
return id(new PhabricatorActionListView())
|
||||
->addAction(
|
||||
id(new PhabricatorActionView())
|
||||
|
@ -102,7 +108,14 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
|||
id(new PhabricatorActionView())
|
||||
->setName(pht('View Raw File'))
|
||||
->setIcon('file')
|
||||
->setHref($file->getBestURI()));
|
||||
->setHref($file->getBestURI()))
|
||||
->addAction(
|
||||
id(new PhabricatorActionView())
|
||||
->setName(pht('Edit Paste'))
|
||||
->setIcon('edit')
|
||||
->setDisabled(!$can_edit)
|
||||
->setWorkflow(!$can_edit)
|
||||
->setHref($this->getApplicationURI('/edit/'.$paste->getID().'/')));
|
||||
}
|
||||
|
||||
private function buildPropertyView(
|
||||
|
|
|
@ -44,12 +44,16 @@ final class PhabricatorPaste extends PhabricatorPasteDAO
|
|||
public function getCapabilities() {
|
||||
return array(
|
||||
PhabricatorPolicyCapability::CAN_VIEW,
|
||||
PhabricatorPolicyCapability::CAN_EDIT,
|
||||
);
|
||||
}
|
||||
|
||||
public function getPolicy($capability) {
|
||||
if ($capability == PhabricatorPolicyCapability::CAN_VIEW) {
|
||||
return PhabricatorPolicies::POLICY_USER;
|
||||
}
|
||||
return PhabricatorPolicies::POLICY_NOONE;
|
||||
}
|
||||
|
||||
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
|
||||
return ($user->getPHID() == $this->getAuthorPHID());
|
||||
|
|
|
@ -21,6 +21,8 @@ final class PhabricatorActionView extends AphrontView {
|
|||
private $name;
|
||||
private $icon;
|
||||
private $href;
|
||||
private $disabled;
|
||||
private $workflow;
|
||||
|
||||
public function setHref($href) {
|
||||
$this->href = $href;
|
||||
|
@ -37,6 +39,16 @@ final class PhabricatorActionView extends AphrontView {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function setDisabled($disabled) {
|
||||
$this->disabled = $disabled;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setWorkflow($workflow) {
|
||||
$this->workflow = $workflow;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
|
||||
$icon = null;
|
||||
|
@ -51,11 +63,12 @@ final class PhabricatorActionView extends AphrontView {
|
|||
}
|
||||
|
||||
if ($this->href) {
|
||||
$item = phutil_render_tag(
|
||||
$item = javelin_render_tag(
|
||||
'a',
|
||||
array(
|
||||
'href' => $this->href,
|
||||
'class' => 'phabricator-action-view-item',
|
||||
'sigil' => $this->workflow ? 'workflow' : null,
|
||||
),
|
||||
phutil_escape_html($this->name));
|
||||
} else {
|
||||
|
@ -67,10 +80,16 @@ final class PhabricatorActionView extends AphrontView {
|
|||
phutil_escape_html($this->name));
|
||||
}
|
||||
|
||||
$classes = array();
|
||||
$classes[] = 'phabricator-action-view';
|
||||
if ($this->disabled) {
|
||||
$classes[] = 'phabricator-action-view-disabled';
|
||||
}
|
||||
|
||||
return phutil_render_tag(
|
||||
'li',
|
||||
array(
|
||||
'class' => 'phabricator-action-view',
|
||||
'class' => implode(' ', $classes),
|
||||
),
|
||||
$icon.$item);
|
||||
}
|
||||
|
|
|
@ -258,3 +258,7 @@
|
|||
.action-fork {
|
||||
background-position: 0px -2538px;
|
||||
}
|
||||
|
||||
.action-edit {
|
||||
background-position: 0px -2555px;
|
||||
}
|
||||
|
|
|
@ -52,3 +52,12 @@
|
|||
color: #ffffff;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.phabricator-action-view-disabled .phabricator-action-view-item {
|
||||
color: #888888;
|
||||
}
|
||||
|
||||
.phabricator-action-view-disabled .phabricator-action-view-item:hover {
|
||||
background-color: #dfdfdf;
|
||||
color: #888888;
|
||||
}
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 83 KiB |
Loading…
Reference in a new issue