[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/ponder/storage/ -> PonderQuestionTransaction.php (source)

   1  <?php
   2  
   3  final class PonderQuestionTransaction
   4    extends PhabricatorApplicationTransaction {
   5  
   6    const TYPE_TITLE = 'ponder.question:question';
   7    const TYPE_CONTENT = 'ponder.question:content';
   8    const TYPE_ANSWERS = 'ponder.question:answer';
   9    const TYPE_STATUS = 'ponder.question:status';
  10  
  11    public function getApplicationName() {
  12      return 'ponder';
  13    }
  14  
  15    public function getTableName() {
  16      return 'ponder_questiontransaction';
  17    }
  18  
  19    public function getApplicationTransactionType() {
  20      return PonderQuestionPHIDType::TYPECONST;
  21    }
  22  
  23    public function getApplicationTransactionCommentObject() {
  24      return new PonderQuestionTransactionComment();
  25    }
  26  
  27    public function getRequiredHandlePHIDs() {
  28      $phids = parent::getRequiredHandlePHIDs();
  29  
  30      switch ($this->getTransactionType()) {
  31        case self::TYPE_ANSWERS:
  32          $phids[] = $this->getNewAnswerPHID();
  33          $phids[] = $this->getObjectPHID();
  34          break;
  35      }
  36  
  37      return $phids;
  38    }
  39  
  40    public function getRemarkupBlocks() {
  41      $blocks = parent::getRemarkupBlocks();
  42  
  43      switch ($this->getTransactionType()) {
  44        case self::TYPE_CONTENT:
  45          $blocks[] = $this->getNewValue();
  46          break;
  47      }
  48  
  49      return $blocks;
  50    }
  51  
  52    public function getTitle() {
  53      $author_phid = $this->getAuthorPHID();
  54      $object_phid = $this->getObjectPHID();
  55  
  56      $old = $this->getOldValue();
  57      $new = $this->getNewValue();
  58  
  59      switch ($this->getTransactionType()) {
  60        case self::TYPE_TITLE:
  61          if ($old === null) {
  62            return pht(
  63              '%s asked this question.',
  64              $this->renderHandleLink($author_phid));
  65          } else {
  66            return pht(
  67              '%s edited the question title from "%s" to "%s".',
  68              $this->renderHandleLink($author_phid),
  69              $old,
  70              $new);
  71          }
  72        case self::TYPE_CONTENT:
  73          return pht(
  74            '%s edited the question description.',
  75            $this->renderHandleLink($author_phid));
  76        case self::TYPE_ANSWERS:
  77          $answer_handle = $this->getHandle($this->getNewAnswerPHID());
  78          $question_handle = $this->getHandle($object_phid);
  79  
  80          return pht(
  81            '%s answered %s',
  82            $this->renderHandleLink($author_phid),
  83            $this->renderHandleLink($object_phid));
  84        case self::TYPE_STATUS:
  85          switch ($new) {
  86            case PonderQuestionStatus::STATUS_OPEN:
  87              return pht(
  88                '%s reopened this question.',
  89                $this->renderHandleLink($author_phid));
  90            case PonderQuestionStatus::STATUS_CLOSED:
  91              return pht(
  92                '%s closed this question.',
  93                $this->renderHandleLink($author_phid));
  94          }
  95      }
  96  
  97      return parent::getTitle();
  98    }
  99  
 100    public function getIcon() {
 101      $old = $this->getOldValue();
 102      $new = $this->getNewValue();
 103  
 104      switch ($this->getTransactionType()) {
 105        case self::TYPE_TITLE:
 106        case self::TYPE_CONTENT:
 107          return 'fa-pencil';
 108        case self::TYPE_STATUS:
 109          switch ($new) {
 110            case PonderQuestionStatus::STATUS_OPEN:
 111              return 'fa-check-circle';
 112            case PonderQuestionStatus::STATUS_CLOSED:
 113              return 'fa-minus-circle';
 114          }
 115        case self::TYPE_ANSWERS:
 116          return 'fa-plus';
 117      }
 118  
 119      return parent::getIcon();
 120    }
 121  
 122    public function getColor() {
 123      $old = $this->getOldValue();
 124      $new = $this->getNewValue();
 125  
 126      switch ($this->getTransactionType()) {
 127        case self::TYPE_TITLE:
 128        case self::TYPE_CONTENT:
 129          return PhabricatorTransactions::COLOR_BLUE;
 130        case self::TYPE_ANSWERS:
 131          return PhabricatorTransactions::COLOR_GREEN;
 132        case self::TYPE_STATUS:
 133          switch ($new) {
 134            case PonderQuestionStatus::STATUS_OPEN:
 135              return PhabricatorTransactions::COLOR_GREEN;
 136            case PonderQuestionStatus::STATUS_CLOSED:
 137              return PhabricatorTransactions::COLOR_BLACK;
 138          }
 139      }
 140    }
 141  
 142    public function hasChangeDetails() {
 143      switch ($this->getTransactionType()) {
 144        case self::TYPE_CONTENT:
 145          return true;
 146      }
 147      return parent::hasChangeDetails();
 148    }
 149  
 150    public function renderChangeDetails(PhabricatorUser $viewer) {
 151      return $this->renderTextCorpusChangeDetails(
 152        $viewer,
 153        $this->getOldValue(),
 154        $this->getNewValue());
 155    }
 156  
 157    public function getActionStrength() {
 158      $old = $this->getOldValue();
 159      $new = $this->getNewValue();
 160  
 161      switch ($this->getTransactionType()) {
 162        case self::TYPE_TITLE:
 163          if ($old === null) {
 164            return 3;
 165          }
 166          break;
 167        case self::TYPE_ANSWERS:
 168          return 2;
 169      }
 170  
 171      return parent::getActionStrength();
 172    }
 173  
 174    public function getActionName() {
 175      $old = $this->getOldValue();
 176      $new = $this->getNewValue();
 177  
 178      switch ($this->getTransactionType()) {
 179        case self::TYPE_TITLE:
 180          if ($old === null) {
 181            return pht('Asked');
 182          }
 183          break;
 184        case self::TYPE_ANSWERS:
 185          return pht('Answered');
 186      }
 187  
 188      return parent::getActionName();
 189    }
 190  
 191    public function shouldHide() {
 192      switch ($this->getTransactionType()) {
 193        case self::TYPE_CONTENT:
 194          if ($this->getOldValue() === null) {
 195            return true;
 196          } else {
 197            return false;
 198          }
 199          break;
 200      }
 201  
 202      return parent::shouldHide();
 203    }
 204  
 205    public function getTitleForFeed(PhabricatorFeedStory $story) {
 206      $author_phid = $this->getAuthorPHID();
 207      $object_phid = $this->getObjectPHID();
 208  
 209      $old = $this->getOldValue();
 210      $new = $this->getNewValue();
 211  
 212      switch ($this->getTransactionType()) {
 213        case self::TYPE_TITLE:
 214          if ($old === null) {
 215            return pht(
 216              '%s asked a question: %s',
 217              $this->renderHandleLink($author_phid),
 218              $this->renderHandleLink($object_phid));
 219          } else {
 220            return pht(
 221              '%s edited the title of %s (was "%s")',
 222              $this->renderHandleLink($author_phid),
 223              $this->renderHandleLink($object_phid),
 224              $old);
 225          }
 226        case self::TYPE_CONTENT:
 227          return pht(
 228            '%s edited the description of %s',
 229            $this->renderHandleLink($author_phid),
 230            $this->renderHandleLink($object_phid));
 231        case self::TYPE_ANSWERS:
 232          $answer_handle = $this->getHandle($this->getNewAnswerPHID());
 233          $question_handle = $this->getHandle($object_phid);
 234          return pht(
 235            '%s answered %s',
 236            $this->renderHandleLink($author_phid),
 237            $answer_handle->renderLink($question_handle->getFullName()));
 238        case self::TYPE_STATUS:
 239          switch ($new) {
 240            case PonderQuestionStatus::STATUS_OPEN:
 241              return pht(
 242                '%s reopened %s',
 243                $this->renderHandleLink($author_phid),
 244                $this->renderHandleLink($object_phid));
 245            case PonderQuestionStatus::STATUS_CLOSED:
 246              return pht(
 247                '%s closed %s',
 248                $this->renderHandleLink($author_phid),
 249                $this->renderHandleLink($object_phid));
 250          }
 251      }
 252  
 253      return parent::getTitleForFeed($story);
 254    }
 255  
 256    public function getBodyForFeed(PhabricatorFeedStory $story) {
 257      $new = $this->getNewValue();
 258      $old = $this->getOldValue();
 259  
 260      $body = null;
 261  
 262      switch ($this->getTransactionType()) {
 263        case self::TYPE_TITLE:
 264          if ($old === null) {
 265            $question = $story->getObject($this->getObjectPHID());
 266            return phutil_escape_html_newlines(
 267              id(new PhutilUTF8StringTruncator())
 268              ->setMaximumGlyphs(128)
 269              ->truncateString($question->getContent()));
 270          }
 271          break;
 272        case self::TYPE_ANSWERS:
 273          $answer = $this->getNewAnswerObject($story);
 274          if ($answer) {
 275            return phutil_escape_html_newlines(
 276              id(new PhutilUTF8StringTruncator())
 277              ->setMaximumGlyphs(128)
 278              ->truncateString($answer->getContent()));
 279          }
 280          break;
 281      }
 282  
 283      return parent::getBodyForFeed($story);
 284    }
 285  
 286    /**
 287     * Currently the application only supports adding answers one at a time.
 288     * This data is stored as a list of phids. Use this function to get the
 289     * new phid.
 290     */
 291    private function getNewAnswerPHID() {
 292      $new = $this->getNewValue();
 293      $old = $this->getOldValue();
 294      $add = array_diff($new, $old);
 295  
 296      if (count($add) != 1) {
 297        throw new Exception(
 298          'There should be only one answer added at a time.');
 299      }
 300  
 301      return reset($add);
 302    }
 303  
 304    /**
 305     * Generally, the answer object is only available if the transaction
 306     * type is self::TYPE_ANSWERS.
 307     *
 308     * Some stories - notably ones made before D7027 - will be of the more
 309     * generic @{class:PhabricatorApplicationTransactionFeedStory}. These
 310     * poor stories won't have the PonderAnswer loaded, and thus will have
 311     * less cool information.
 312     */
 313    private function getNewAnswerObject(PhabricatorFeedStory $story) {
 314      if ($story instanceof PonderTransactionFeedStory) {
 315        $answer_phid = $this->getNewAnswerPHID();
 316        if ($answer_phid) {
 317          return $story->getObject($answer_phid);
 318        }
 319      }
 320      return null;
 321    }
 322  
 323  }


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