[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/diff/ -> PhabricatorInlineCommentController.php (source)

   1  <?php
   2  
   3  abstract class PhabricatorInlineCommentController
   4    extends PhabricatorController {
   5  
   6    abstract protected function createComment();
   7    abstract protected function loadComment($id);
   8    abstract protected function loadCommentForEdit($id);
   9    abstract protected function deleteComment(
  10      PhabricatorInlineCommentInterface $inline);
  11    abstract protected function saveComment(
  12      PhabricatorInlineCommentInterface $inline);
  13  
  14    private $changesetID;
  15    private $isNewFile;
  16    private $isOnRight;
  17    private $lineNumber;
  18    private $lineLength;
  19    private $commentText;
  20    private $operation;
  21    private $commentID;
  22  
  23    public function getCommentID() {
  24      return $this->commentID;
  25    }
  26  
  27    public function getOperation() {
  28      return $this->operation;
  29    }
  30  
  31    public function getCommentText() {
  32      return $this->commentText;
  33    }
  34  
  35    public function getLineLength() {
  36      return $this->lineLength;
  37    }
  38  
  39    public function getLineNumber() {
  40      return $this->lineNumber;
  41    }
  42  
  43    public function getIsOnRight() {
  44      return $this->isOnRight;
  45    }
  46  
  47    public function getChangesetID() {
  48      return $this->changesetID;
  49    }
  50  
  51    public function getIsNewFile() {
  52      return $this->isNewFile;
  53    }
  54  
  55  
  56    public function processRequest() {
  57      $request = $this->getRequest();
  58      $user = $request->getUser();
  59  
  60      $this->readRequestParameters();
  61  
  62      switch ($this->getOperation()) {
  63        case 'delete':
  64          $inline = $this->loadCommentForEdit($this->getCommentID());
  65  
  66          if ($request->isFormPost()) {
  67            $this->deleteComment($inline);
  68            return $this->buildEmptyResponse();
  69          }
  70  
  71          $dialog = new AphrontDialogView();
  72          $dialog->setUser($user);
  73          $dialog->setSubmitURI($request->getRequestURI());
  74  
  75          $dialog->setTitle(pht('Really delete this comment?'));
  76          $dialog->addHiddenInput('id', $this->getCommentID());
  77          $dialog->addHiddenInput('op', 'delete');
  78          $dialog->appendChild(
  79            phutil_tag('p', array(), pht('Delete this inline comment?')));
  80  
  81          $dialog->addCancelButton('#');
  82          $dialog->addSubmitButton(pht('Delete'));
  83  
  84          return id(new AphrontDialogResponse())->setDialog($dialog);
  85        case 'edit':
  86          $inline = $this->loadCommentForEdit($this->getCommentID());
  87  
  88          $text = $this->getCommentText();
  89  
  90          if ($request->isFormPost()) {
  91            if (strlen($text)) {
  92              $inline->setContent($text);
  93              $this->saveComment($inline);
  94              return $this->buildRenderedCommentResponse(
  95                $inline,
  96                $this->getIsOnRight());
  97            } else {
  98              $this->deleteComment($inline);
  99              return $this->buildEmptyResponse();
 100            }
 101          }
 102  
 103          $edit_dialog = $this->buildEditDialog();
 104          $edit_dialog->setTitle(pht('Edit Inline Comment'));
 105  
 106          $edit_dialog->addHiddenInput('id', $this->getCommentID());
 107          $edit_dialog->addHiddenInput('op', 'edit');
 108  
 109          $edit_dialog->appendChild(
 110            $this->renderTextArea(
 111              nonempty($text, $inline->getContent())));
 112  
 113          return id(new AphrontAjaxResponse())
 114            ->setContent($edit_dialog->render());
 115        case 'create':
 116  
 117          $text = $this->getCommentText();
 118  
 119          if (!$request->isFormPost() || !strlen($text)) {
 120            return $this->buildEmptyResponse();
 121          }
 122  
 123          $inline = $this->createComment()
 124            ->setChangesetID($this->getChangesetID())
 125            ->setAuthorPHID($user->getPHID())
 126            ->setLineNumber($this->getLineNumber())
 127            ->setLineLength($this->getLineLength())
 128            ->setIsNewFile($this->getIsNewFile())
 129            ->setContent($text);
 130          $this->saveComment($inline);
 131  
 132          return $this->buildRenderedCommentResponse(
 133            $inline,
 134            $this->getIsOnRight());
 135        case 'reply':
 136        default:
 137          $edit_dialog = $this->buildEditDialog();
 138  
 139          if ($this->getOperation() == 'reply') {
 140            $inline = $this->loadComment($this->getCommentID());
 141  
 142            $edit_dialog->setTitle(pht('Reply to Inline Comment'));
 143            $changeset = $inline->getChangesetID();
 144            $is_new = $inline->getIsNewFile();
 145            $number = $inline->getLineNumber();
 146            $length = $inline->getLineLength();
 147          } else {
 148            $edit_dialog->setTitle(pht('New Inline Comment'));
 149            $changeset = $this->getChangesetID();
 150            $is_new = $this->getIsNewFile();
 151            $number = $this->getLineNumber();
 152            $length = $this->getLineLength();
 153          }
 154  
 155          $edit_dialog->addHiddenInput('op', 'create');
 156          $edit_dialog->addHiddenInput('changeset', $changeset);
 157          $edit_dialog->addHiddenInput('is_new', $is_new);
 158          $edit_dialog->addHiddenInput('number', $number);
 159          $edit_dialog->addHiddenInput('length', $length);
 160  
 161          $text_area = $this->renderTextArea($this->getCommentText());
 162          $edit_dialog->appendChild($text_area);
 163  
 164          return id(new AphrontAjaxResponse())
 165            ->setContent($edit_dialog->render());
 166      }
 167    }
 168  
 169    private function readRequestParameters() {
 170      $request = $this->getRequest();
 171  
 172      // NOTE: This isn't necessarily a DifferentialChangeset ID, just an
 173      // application identifier for the changeset. In Diffusion, it's a Path ID.
 174      $this->changesetID    = $request->getInt('changeset');
 175  
 176      $this->isNewFile      = (int)$request->getBool('is_new');
 177      $this->isOnRight      = $request->getBool('on_right');
 178      $this->lineNumber     = $request->getInt('number');
 179      $this->lineLength     = $request->getInt('length');
 180      $this->commentText    = $request->getStr('text');
 181      $this->commentID      = $request->getInt('id');
 182      $this->operation      = $request->getStr('op');
 183    }
 184  
 185    private function buildEditDialog() {
 186      $request = $this->getRequest();
 187      $user = $request->getUser();
 188  
 189      $edit_dialog = new DifferentialInlineCommentEditView();
 190      $edit_dialog->setUser($user);
 191      $edit_dialog->setSubmitURI($request->getRequestURI());
 192      $edit_dialog->setOnRight($this->getIsOnRight());
 193      $edit_dialog->setNumber($this->getLineNumber());
 194      $edit_dialog->setLength($this->getLineLength());
 195  
 196      return $edit_dialog;
 197    }
 198  
 199    private function buildEmptyResponse() {
 200      return id(new AphrontAjaxResponse())
 201        ->setContent(
 202          array(
 203            'markup'          => '',
 204          ));
 205    }
 206  
 207    private function buildRenderedCommentResponse(
 208      PhabricatorInlineCommentInterface $inline,
 209      $on_right) {
 210  
 211      $request = $this->getRequest();
 212      $user = $request->getUser();
 213  
 214      $engine = new PhabricatorMarkupEngine();
 215      $engine->setViewer($user);
 216      $engine->addObject(
 217        $inline,
 218        PhabricatorInlineCommentInterface::MARKUP_FIELD_BODY);
 219      $engine->process();
 220  
 221      $phids = array($user->getPHID());
 222  
 223      $handles = $this->loadViewerHandles($phids);
 224  
 225      $view = new DifferentialInlineCommentView();
 226      $view->setInlineComment($inline);
 227      $view->setOnRight($on_right);
 228      $view->setBuildScaffolding(true);
 229      $view->setMarkupEngine($engine);
 230      $view->setHandles($handles);
 231      $view->setEditable(true);
 232  
 233      return id(new AphrontAjaxResponse())
 234        ->setContent(
 235          array(
 236            'inlineCommentID' => $inline->getID(),
 237            'markup'          => $view->render(),
 238          ));
 239    }
 240  
 241    private function renderTextArea($text) {
 242      return id(new PhabricatorRemarkupControl())
 243        ->setUser($this->getRequest()->getUser())
 244        ->setSigil('differential-inline-comment-edit-textarea')
 245        ->setName('text')
 246        ->setValue($text)
 247        ->setDisableFullScreen(true);
 248    }
 249  
 250  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1