2012-08-24 22:19:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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-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)) {
|
|
|
|
$e_text = 'Required';
|
|
|
|
$errors[] = 'The paste may not be blank.';
|
|
|
|
} 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) {
|
2012-08-24 22:19:14 +02:00
|
|
|
$paste->setTitle('Fork of '.$parent->getFullName());
|
|
|
|
$paste->setLanguage($parent->getLanguage());
|
2012-08-24 22:20:20 +02:00
|
|
|
$text = $parent->getContent();
|
2012-08-24 22:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
|
|
|
->setTitle('A fatal omission!')
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new AphrontFormView();
|
|
|
|
$form->setFlexible(true);
|
|
|
|
|
|
|
|
$langs = array(
|
|
|
|
'' => '(Detect With Wizardly Powers)',
|
|
|
|
) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
|
|
|
|
|
|
|
|
$form
|
|
|
|
->setUser($user)
|
|
|
|
->addHiddenInput('parent', $parent_id)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel('Title')
|
|
|
|
->setValue($paste->getTitle())
|
|
|
|
->setName('title'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
|
|
|
->setLabel('Language')
|
|
|
|
->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())
|
|
|
|
->setLabel('Text')
|
|
|
|
->setError($e_text)
|
|
|
|
->setValue($text)
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
|
|
|
->setCustomClass('PhabricatorMonospaced')
|
|
|
|
->setName('text'));
|
2012-10-02 01:49:27 +02:00
|
|
|
} else {
|
|
|
|
$fork_link = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $this->getApplicationURI('?parent='.$paste->getID())
|
|
|
|
),
|
|
|
|
'Fork'
|
|
|
|
);
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
|
|
|
->setLabel('Text')
|
|
|
|
->setValue(
|
|
|
|
'Paste text can not be edited. '.
|
|
|
|
$fork_link.' to create a new paste.'
|
|
|
|
));
|
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());
|
|
|
|
$submit->setValue('Save Paste');
|
|
|
|
$title = 'Edit '.$paste->getFullName();
|
|
|
|
} else {
|
|
|
|
$submit->setValue('Create Paste');
|
|
|
|
$title = 'Create Paste';
|
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
2012-08-24 22:19:14 +02:00
|
|
|
->appendChild($submit);
|
|
|
|
|
|
|
|
$nav = $this->buildSideNavView();
|
|
|
|
$nav->selectFilter('edit');
|
|
|
|
$nav->appendChild(
|
|
|
|
array(
|
2012-08-24 22:19:30 +02:00
|
|
|
id(new PhabricatorHeaderView())->setHeader($title),
|
2012-08-24 22:19:14 +02:00
|
|
|
$error_view,
|
|
|
|
$form,
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'device' => true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|