1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-15 19:32:40 +01:00
phorge-phorge/src/applications/differential/controller/inlinecommentedit/DifferentialInlineCommentEditController.php

249 lines
7.2 KiB
PHP
Raw Normal View History

2011-02-02 01:42:36 +01:00
<?php
/*
* Copyright 2012 Facebook, Inc.
2011-02-02 01:42:36 +01:00
*
* 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.
*/
class DifferentialInlineCommentEditController extends DifferentialController {
private $revisionID;
public function willProcessRequest(array $data) {
$this->revisionID = $data['id'];
}
public function processRequest() {
$request = $this->getRequest();
$changeset = $request->getInt('changeset');
$is_new = $request->getBool('is_new');
$on_right = $request->getBool('on_right');
2011-02-02 01:42:36 +01:00
$number = $request->getInt('number');
$length = $request->getInt('length');
$text = $request->getStr('text');
$op = $request->getStr('op');
2011-02-02 19:10:25 +01:00
$inline_id = $request->getInt('id');
2011-02-02 01:42:36 +01:00
$user = $request->getUser();
$submit_uri = '/differential/comment/inline/edit/'.$this->revisionID.'/';
$edit_dialog = new DifferentialInlineCommentEditView();
2011-02-02 19:10:25 +01:00
$edit_dialog->setUser($user);
$edit_dialog->setSubmitURI($submit_uri);
$edit_dialog->setOnRight($on_right);
2011-02-02 19:10:25 +01:00
2011-02-02 01:42:36 +01:00
switch ($op) {
case 'delete':
$inline = $this->loadInlineCommentForEditing($inline_id);
2011-02-02 19:10:25 +01:00
2011-02-02 01:42:36 +01:00
if ($request->isFormPost()) {
2011-02-02 19:10:25 +01:00
$inline->delete();
return $this->buildEmptyResponse();
2011-02-02 01:42:36 +01:00
}
$dialog = new AphrontDialogView();
$dialog->setUser($user);
$dialog->setSubmitURI($submit_uri);
2011-02-02 01:42:36 +01:00
$dialog->setTitle('Really delete this comment?');
$dialog->addHiddenInput('id', $inline_id);
$dialog->addHiddenInput('op', 'delete');
$dialog->appendChild('<p>Delete this inline comment?</p>');
$dialog->addCancelButton('#');
$dialog->addSubmitButton();
return id(new AphrontDialogResponse())->setDialog($dialog);
2011-02-02 01:42:36 +01:00
case 'edit':
$inline = $this->loadInlineCommentForEditing($inline_id);
2011-02-02 19:10:25 +01:00
if ($request->isFormPost()) {
if (strlen($text)) {
$inline->setContent($text);
2011-02-03 04:38:43 +01:00
$inline->setCache(null);
2011-02-02 19:10:25 +01:00
$inline->save();
return $this->buildRenderedCommentResponse(
$inline,
$on_right);
} else {
$inline->delete();
return $this->buildEmptyResponse();
2011-02-02 19:10:25 +01:00
}
}
$edit_dialog->setTitle('Edit Inline Comment');
2011-02-02 01:42:36 +01:00
2011-02-02 19:10:25 +01:00
$edit_dialog->addHiddenInput('id', $inline_id);
$edit_dialog->addHiddenInput('op', 'edit');
$edit_dialog->appendChild(
$this->renderTextArea(
nonempty($text, $inline->getContent())));
2011-02-02 19:10:25 +01:00
return id(new AphrontAjaxResponse())
->setContent($edit_dialog->render());
2011-02-02 01:42:36 +01:00
case 'create':
if (!$request->isFormPost() || !strlen($text)) {
return $this->buildEmptyResponse();
2011-02-02 01:42:36 +01:00
}
// Verify revision and changeset correspond to actual objects.
$revision_obj = id(new DifferentialRevision())->load($this->revisionID);
$changeset_obj = id(new DifferentialChangeset())->load($changeset);
if (!$revision_obj || !$changeset_obj) {
throw new Exception("Invalid revision ID or changeset ID!");
}
2011-02-02 01:42:36 +01:00
$inline = id(new DifferentialInlineComment())
->setRevisionID($this->revisionID)
->setChangesetID($changeset)
->setCommentID(null)
->setAuthorPHID($user->getPHID())
->setLineNumber($number)
->setLineLength($length)
->setIsNewFile($is_new)
2011-02-02 01:42:36 +01:00
->setContent($text)
->save();
2011-02-02 19:10:25 +01:00
return $this->buildRenderedCommentResponse($inline, $on_right);
case 'reply':
2011-02-02 01:42:36 +01:00
default:
if ($op == 'reply') {
$inline = $this->loadInlineComment($inline_id);
// Override defaults.
$changeset = $inline->getChangesetID();
$is_new = $inline->getIsNewFile();
$number = $inline->getLineNumber();
$length = $inline->getLineLength();
$edit_dialog->setTitle('Reply to Inline Comment');
} else {
$edit_dialog->setTitle('New Inline Comment');
}
2011-02-02 19:10:25 +01:00
$edit_dialog->addHiddenInput('op', 'create');
$edit_dialog->addHiddenInput('changeset', $changeset);
$edit_dialog->addHiddenInput('is_new', $is_new);
$edit_dialog->addHiddenInput('number', $number);
$edit_dialog->addHiddenInput('length', $length);
$edit_dialog->appendChild($this->renderTextArea($text));
2011-02-02 19:10:25 +01:00
return id(new AphrontAjaxResponse())
->setContent($edit_dialog->render());
2011-02-02 01:42:36 +01:00
}
}
2011-02-02 19:10:25 +01:00
private function buildRenderedCommentResponse(
DifferentialInlineComment $inline,
$on_right) {
$request = $this->getRequest();
$user = $request->getUser();
$engine = PhabricatorMarkupEngine::newDifferentialMarkupEngine();
2011-02-02 19:10:25 +01:00
$phids = array($user->getPHID());
$handles = id(new PhabricatorObjectHandleData($phids))
->loadHandles();
$view = new DifferentialInlineCommentView();
$view->setInlineComment($inline);
$view->setOnRight($on_right);
$view->setBuildScaffolding(true);
$view->setMarkupEngine($engine);
$view->setHandles($handles);
$view->setEditable(true);
return id(new AphrontAjaxResponse())
->setContent(
array(
'inlineCommentID' => $inline->getID(),
'markup' => $view->render(),
));
}
private function buildEmptyResponse() {
2011-02-02 19:10:25 +01:00
return id(new AphrontAjaxResponse())
->setContent(
array(
'markup' => '',
));
}
private function renderTextArea($text) {
return javelin_render_tag(
2011-02-02 19:10:25 +01:00
'textarea',
array(
'class' => 'differential-inline-comment-edit-textarea',
'sigil' => 'differential-inline-comment-edit-textarea',
2011-02-02 19:10:25 +01:00
'name' => 'text',
),
phutil_escape_html($text));
2011-02-02 19:10:25 +01:00
}
private function loadInlineComment($id) {
$inline = null;
if ($id) {
$inline = id(new DifferentialInlineComment())->load($id);
}
if (!$inline) {
throw new Exception("No such inline comment!");
}
return $inline;
}
private function loadInlineCommentForEditing($id) {
$inline = $this->loadInlineComment($id);
$user = $this->getRequest()->getUser();
if (!$this->canEditInlineComment($user, $inline, $this->revisionID)) {
throw new Exception("That comment is not editable!");
}
return $inline;
}
private function canEditInlineComment(
PhabricatorUser $user,
DifferentialInlineComment $inline,
$revision_id) {
// Only the author may edit a comment.
if ($inline->getAuthorPHID() != $user->getPHID()) {
return false;
}
// Saved comments may not be edited.
if ($inline->getCommentID()) {
return false;
}
// Inline must be attached to the active revision.
if ($inline->getRevisionID() != $revision_id) {
return false;
}
return true;
}
2011-02-02 01:42:36 +01:00
}