[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class PonderQuestion extends PonderDAO
   4    implements
   5      PhabricatorMarkupInterface,
   6      PonderVotableInterface,
   7      PhabricatorSubscribableInterface,
   8      PhabricatorFlaggableInterface,
   9      PhabricatorPolicyInterface,
  10      PhabricatorTokenReceiverInterface,
  11      PhabricatorProjectInterface,
  12      PhabricatorDestructibleInterface {
  13  
  14    const MARKUP_FIELD_CONTENT = 'markup:content';
  15  
  16    protected $title;
  17    protected $phid;
  18  
  19    protected $authorPHID;
  20    protected $status;
  21    protected $content;
  22    protected $contentSource;
  23  
  24    protected $voteCount;
  25    protected $answerCount;
  26    protected $heat;
  27    protected $mailKey;
  28  
  29    private $answers;
  30    private $vote;
  31    private $comments;
  32  
  33    public function getConfiguration() {
  34      return array(
  35        self::CONFIG_AUX_PHID => true,
  36        self::CONFIG_COLUMN_SCHEMA => array(
  37          'title' => 'text255',
  38          'voteCount' => 'sint32',
  39          'status' => 'uint32',
  40          'content' => 'text',
  41          'heat' => 'double',
  42          'answerCount' => 'uint32',
  43          'mailKey' => 'bytes20',
  44  
  45          // T6203/NULLABILITY
  46          // This should always exist.
  47          'contentSource' => 'text?',
  48        ),
  49        self::CONFIG_KEY_SCHEMA => array(
  50          'key_phid' => null,
  51          'phid' => array(
  52            'columns' => array('phid'),
  53            'unique' => true,
  54          ),
  55          'authorPHID' => array(
  56            'columns' => array('authorPHID'),
  57          ),
  58          'heat' => array(
  59            'columns' => array('heat'),
  60          ),
  61          'status' => array(
  62            'columns' => array('status'),
  63          ),
  64        ),
  65      ) + parent::getConfiguration();
  66    }
  67  
  68    public function generatePHID() {
  69      return PhabricatorPHID::generateNewPHID(PonderQuestionPHIDType::TYPECONST);
  70    }
  71  
  72    public function setContentSource(PhabricatorContentSource $content_source) {
  73      $this->contentSource = $content_source->serialize();
  74      return $this;
  75    }
  76  
  77    public function getContentSource() {
  78      return PhabricatorContentSource::newFromSerialized($this->contentSource);
  79    }
  80  
  81    public function attachVotes($user_phid) {
  82      $qa_phids = mpull($this->answers, 'getPHID') + array($this->getPHID());
  83  
  84      $edges = id(new PhabricatorEdgeQuery())
  85        ->withSourcePHIDs(array($user_phid))
  86        ->withDestinationPHIDs($qa_phids)
  87        ->withEdgeTypes(
  88          array(
  89            PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_QUESTION,
  90            PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_ANSWER,
  91          ))
  92        ->needEdgeData(true)
  93        ->execute();
  94  
  95      $question_edge =
  96        $edges[$user_phid][PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_QUESTION];
  97      $answer_edges =
  98        $edges[$user_phid][PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_ANSWER];
  99      $edges = null;
 100  
 101      $this->setUserVote(idx($question_edge, $this->getPHID()));
 102      foreach ($this->answers as $answer) {
 103        $answer->setUserVote(idx($answer_edges, $answer->getPHID()));
 104      }
 105    }
 106  
 107    public function setUserVote($vote) {
 108      $this->vote = $vote['data'];
 109      if (!$this->vote) {
 110        $this->vote = PonderVote::VOTE_NONE;
 111      }
 112      return $this;
 113    }
 114  
 115    public function attachUserVote($user_phid, $vote) {
 116      $this->vote = $vote;
 117      return $this;
 118    }
 119  
 120    public function getUserVote() {
 121      return $this->vote;
 122    }
 123  
 124    public function setComments($comments) {
 125      $this->comments = $comments;
 126      return $this;
 127    }
 128  
 129    public function getComments() {
 130      return $this->comments;
 131    }
 132  
 133    public function attachAnswers(array $answers) {
 134      assert_instances_of($answers, 'PonderAnswer');
 135      $this->answers = $answers;
 136      return $this;
 137    }
 138  
 139    public function getAnswers() {
 140      return $this->answers;
 141    }
 142  
 143    public function getMarkupField() {
 144      return self::MARKUP_FIELD_CONTENT;
 145    }
 146  
 147    // Markup interface
 148  
 149    public function getMarkupFieldKey($field) {
 150      $hash = PhabricatorHash::digest($this->getMarkupText($field));
 151      $id = $this->getID();
 152      return "ponder:Q{$id}:{$field}:{$hash}";
 153    }
 154  
 155    public function getMarkupText($field) {
 156      return $this->getContent();
 157    }
 158  
 159    public function newMarkupEngine($field) {
 160      return PhabricatorMarkupEngine::getEngine();
 161    }
 162  
 163    public function didMarkupText(
 164      $field,
 165      $output,
 166      PhutilMarkupEngine $engine) {
 167      return $output;
 168    }
 169  
 170    public function shouldUseMarkupCache($field) {
 171      return (bool)$this->getID();
 172    }
 173  
 174    // votable interface
 175    public function getUserVoteEdgeType() {
 176      return PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_QUESTION;
 177    }
 178  
 179    public function getVotablePHID() {
 180      return $this->getPHID();
 181    }
 182  
 183    public function save() {
 184      if (!$this->getMailKey()) {
 185        $this->setMailKey(Filesystem::readRandomCharacters(20));
 186      }
 187      return parent::save();
 188    }
 189  
 190    public function getOriginalTitle() {
 191      // TODO: Make this actually save/return the original title.
 192      return $this->getTitle();
 193    }
 194  
 195    public function getFullTitle() {
 196      $id = $this->getID();
 197      $title = $this->getTitle();
 198      return "Q{$id}: {$title}";
 199    }
 200  
 201  
 202  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 203  
 204    public function getCapabilities() {
 205      return array(
 206        PhabricatorPolicyCapability::CAN_VIEW,
 207        PhabricatorPolicyCapability::CAN_EDIT,
 208      );
 209    }
 210  
 211    public function getPolicy($capability) {
 212      $policy = PhabricatorPolicies::POLICY_NOONE;
 213  
 214      switch ($capability) {
 215        case PhabricatorPolicyCapability::CAN_VIEW:
 216          $policy = PhabricatorPolicies::POLICY_USER;
 217          break;
 218      }
 219  
 220      return $policy;
 221    }
 222  
 223    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 224      return ($viewer->getPHID() == $this->getAuthorPHID());
 225    }
 226  
 227  
 228    public function describeAutomaticCapability($capability) {
 229      return pht(
 230        'The user who asked a question can always view and edit it.');
 231    }
 232  
 233  
 234  /* -(  PhabricatorSubscribableInterface  )----------------------------------- */
 235  
 236  
 237    public function isAutomaticallySubscribed($phid) {
 238      return ($phid == $this->getAuthorPHID());
 239    }
 240  
 241    public function shouldShowSubscribersProperty() {
 242      return true;
 243    }
 244  
 245    public function shouldAllowSubscription($phid) {
 246      return true;
 247    }
 248  
 249  
 250  /* -(  PhabricatorTokenReceiverInterface  )---------------------------------- */
 251  
 252  
 253    public function getUsersToNotifyOfTokenGiven() {
 254      return array(
 255        $this->getAuthorPHID(),
 256      );
 257    }
 258  
 259  
 260  /* -(  PhabricatorDestructibleInterface  )----------------------------------- */
 261  
 262    public function destroyObjectPermanently(
 263      PhabricatorDestructionEngine $engine) {
 264  
 265      $this->openTransaction();
 266        $answers = id(new PonderAnswer())->loadAllWhere(
 267          'questionID = %d',
 268          $this->getID());
 269        foreach ($answers as $answer) {
 270          $engine->destroyObject($answer);
 271        }
 272  
 273        $this->delete();
 274      $this->saveTransaction();
 275    }
 276  
 277  }


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