[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/ponder/editor/ -> PonderQuestionEditor.php (source)

   1  <?php
   2  
   3  final class PonderQuestionEditor
   4    extends PonderEditor {
   5  
   6    private $answer;
   7  
   8    public function getEditorObjectsDescription() {
   9      return pht('Ponder Questions');
  10    }
  11  
  12    /**
  13     * This is used internally on @{method:applyInitialEffects} if a transaction
  14     * of type PonderQuestionTransaction::TYPE_ANSWERS is in the mix. The value
  15     * is set to the //last// answer in the transactions. Practically, one
  16     * answer is given at a time in the application, though theoretically
  17     * this is buggy.
  18     *
  19     * The answer is used in emails to generate proper links.
  20     */
  21    private function setAnswer(PonderAnswer $answer) {
  22      $this->answer = $answer;
  23      return $this;
  24    }
  25    private function getAnswer() {
  26      return $this->answer;
  27    }
  28  
  29    protected function shouldApplyInitialEffects(
  30      PhabricatorLiskDAO $object,
  31      array $xactions) {
  32  
  33      foreach ($xactions as $xaction) {
  34        switch ($xaction->getTransactionType()) {
  35          case PonderQuestionTransaction::TYPE_ANSWERS:
  36            return true;
  37        }
  38      }
  39  
  40      return false;
  41    }
  42  
  43    protected function applyInitialEffects(
  44      PhabricatorLiskDAO $object,
  45      array $xactions) {
  46  
  47      foreach ($xactions as $xaction) {
  48        switch ($xaction->getTransactionType()) {
  49          case PonderQuestionTransaction::TYPE_ANSWERS:
  50            $new_value = $xaction->getNewValue();
  51            $new = idx($new_value, '+', array());
  52            foreach ($new as $new_answer) {
  53              $answer = idx($new_answer, 'answer');
  54              if (!$answer) {
  55                continue;
  56              }
  57              $answer->save();
  58              $this->setAnswer($answer);
  59            }
  60            break;
  61        }
  62      }
  63    }
  64  
  65    public function getTransactionTypes() {
  66      $types = parent::getTransactionTypes();
  67  
  68      $types[] = PhabricatorTransactions::TYPE_COMMENT;
  69      $types[] = PonderQuestionTransaction::TYPE_TITLE;
  70      $types[] = PonderQuestionTransaction::TYPE_CONTENT;
  71      $types[] = PonderQuestionTransaction::TYPE_ANSWERS;
  72      $types[] = PonderQuestionTransaction::TYPE_STATUS;
  73  
  74      return $types;
  75    }
  76  
  77    protected function getCustomTransactionOldValue(
  78      PhabricatorLiskDAO $object,
  79      PhabricatorApplicationTransaction $xaction) {
  80  
  81      switch ($xaction->getTransactionType()) {
  82        case PonderQuestionTransaction::TYPE_TITLE:
  83          return $object->getTitle();
  84        case PonderQuestionTransaction::TYPE_CONTENT:
  85          return $object->getContent();
  86        case PonderQuestionTransaction::TYPE_ANSWERS:
  87          return mpull($object->getAnswers(), 'getPHID');
  88        case PonderQuestionTransaction::TYPE_STATUS:
  89          return $object->getStatus();
  90      }
  91    }
  92  
  93    protected function getCustomTransactionNewValue(
  94      PhabricatorLiskDAO $object,
  95      PhabricatorApplicationTransaction $xaction) {
  96  
  97      switch ($xaction->getTransactionType()) {
  98        case PonderQuestionTransaction::TYPE_TITLE:
  99        case PonderQuestionTransaction::TYPE_CONTENT:
 100        case PonderQuestionTransaction::TYPE_STATUS:
 101          return $xaction->getNewValue();
 102        case PonderQuestionTransaction::TYPE_ANSWERS:
 103          $raw_new_value = $xaction->getNewValue();
 104          $new_value = array();
 105          foreach ($raw_new_value as $key => $answers) {
 106            $phids = array();
 107            foreach ($answers as $answer) {
 108              $obj = idx($answer, 'answer');
 109              if (!$answer) {
 110                continue;
 111              }
 112              $phids[] = $obj->getPHID();
 113            }
 114            $new_value[$key] = $phids;
 115          }
 116          $xaction->setNewValue($new_value);
 117          return $this->getPHIDTransactionNewValue($xaction);
 118      }
 119    }
 120  
 121    protected function applyCustomInternalTransaction(
 122      PhabricatorLiskDAO $object,
 123      PhabricatorApplicationTransaction $xaction) {
 124  
 125      switch ($xaction->getTransactionType()) {
 126        case PonderQuestionTransaction::TYPE_TITLE:
 127          $object->setTitle($xaction->getNewValue());
 128          break;
 129        case PonderQuestionTransaction::TYPE_CONTENT:
 130          $object->setContent($xaction->getNewValue());
 131          break;
 132        case PonderQuestionTransaction::TYPE_STATUS:
 133          $object->setStatus($xaction->getNewValue());
 134          break;
 135        case PonderQuestionTransaction::TYPE_ANSWERS:
 136          $old = $xaction->getOldValue();
 137          $new = $xaction->getNewValue();
 138  
 139          $add = array_diff_key($new, $old);
 140          $rem = array_diff_key($old, $new);
 141  
 142          $count = $object->getAnswerCount();
 143          $count += count($add);
 144          $count -= count($rem);
 145  
 146          $object->setAnswerCount($count);
 147          break;
 148        case PhabricatorTransactions::TYPE_EDGE:
 149          return;
 150      }
 151    }
 152  
 153    protected function applyCustomExternalTransaction(
 154      PhabricatorLiskDAO $object,
 155      PhabricatorApplicationTransaction $xaction) {
 156      return;
 157    }
 158  
 159    protected function mergeTransactions(
 160      PhabricatorApplicationTransaction $u,
 161      PhabricatorApplicationTransaction $v) {
 162  
 163      $type = $u->getTransactionType();
 164      switch ($type) {
 165        case PonderQuestionTransaction::TYPE_TITLE:
 166        case PonderQuestionTransaction::TYPE_CONTENT:
 167        case PonderQuestionTransaction::TYPE_STATUS:
 168          return $v;
 169      }
 170  
 171      return parent::mergeTransactions($u, $v);
 172    }
 173  
 174    protected function supportsSearch() {
 175      return true;
 176    }
 177  
 178    protected function getFeedStoryType() {
 179      return 'PonderTransactionFeedStory';
 180    }
 181  
 182    protected function getFeedStoryData(
 183      PhabricatorLiskDAO $object,
 184      array $xactions) {
 185  
 186      $data = parent::getFeedStoryData($object, $xactions);
 187      $answer = $this->getAnswer();
 188      if ($answer) {
 189        $data['answerPHID'] = $answer->getPHID();
 190      }
 191  
 192      return $data;
 193   }
 194  
 195    protected function shouldImplyCC(
 196      PhabricatorLiskDAO $object,
 197      PhabricatorApplicationTransaction $xaction) {
 198  
 199      switch ($xaction->getTransactionType()) {
 200        case PonderQuestionTransaction::TYPE_ANSWERS:
 201          return true;
 202      }
 203  
 204      return parent::shouldImplyCC($object, $xaction);
 205    }
 206  
 207    protected function shouldSendMail(
 208      PhabricatorLiskDAO $object,
 209      array $xactions) {
 210      return true;
 211    }
 212  
 213    protected function buildReplyHandler(PhabricatorLiskDAO $object) {
 214      return id(new PonderQuestionReplyHandler())
 215        ->setMailReceiver($object);
 216    }
 217  
 218    protected function buildMailBody(
 219      PhabricatorLiskDAO $object,
 220      array $xactions) {
 221  
 222      $body = parent::buildMailBody($object, $xactions);
 223  
 224      $header = pht('QUESTION DETAIL');
 225      $uri = '/Q'.$object->getID();
 226      foreach ($xactions as $xaction) {
 227        $type = $xaction->getTransactionType();
 228        $old = $xaction->getOldValue();
 229        $new = $xaction->getNewValue();
 230        // If the user just asked the question, add the question text.
 231        if ($type == PonderQuestionTransaction::TYPE_CONTENT) {
 232          if ($old === null) {
 233            $body->addRawSection($new);
 234          }
 235        }
 236        // If the user gave an answer, add the answer text. Also update
 237        // the header and uri to be more answer-specific.
 238        if ($type == PonderQuestionTransaction::TYPE_ANSWERS) {
 239          $answer = $this->getAnswer();
 240          $body->addRawSection($answer->getContent());
 241          $header = pht('ANSWER DETAIL');
 242          $uri = $answer->getURI();
 243        }
 244      }
 245  
 246      $body->addLinkSection(
 247        $header,
 248        PhabricatorEnv::getProductionURI($uri));
 249  
 250      return $body;
 251    }
 252  
 253  }


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