[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/slowvote/storage/ -> PhabricatorSlowvotePoll.php (source)

   1  <?php
   2  
   3  final class PhabricatorSlowvotePoll extends PhabricatorSlowvoteDAO
   4    implements
   5      PhabricatorPolicyInterface,
   6      PhabricatorSubscribableInterface,
   7      PhabricatorFlaggableInterface,
   8      PhabricatorTokenReceiverInterface,
   9      PhabricatorProjectInterface,
  10      PhabricatorDestructibleInterface {
  11  
  12    const RESPONSES_VISIBLE = 0;
  13    const RESPONSES_VOTERS  = 1;
  14    const RESPONSES_OWNER   = 2;
  15  
  16    const METHOD_PLURALITY  = 0;
  17    const METHOD_APPROVAL   = 1;
  18  
  19    protected $question;
  20    protected $description;
  21    protected $authorPHID;
  22    protected $responseVisibility;
  23    protected $shuffle;
  24    protected $method;
  25    protected $viewPolicy;
  26    protected $isClosed = 0;
  27  
  28    private $options = self::ATTACHABLE;
  29    private $choices = self::ATTACHABLE;
  30    private $viewerChoices = self::ATTACHABLE;
  31  
  32    public static function initializeNewPoll(PhabricatorUser $actor) {
  33      $app = id(new PhabricatorApplicationQuery())
  34        ->setViewer($actor)
  35        ->withClasses(array('PhabricatorSlowvoteApplication'))
  36        ->executeOne();
  37  
  38      $view_policy = $app->getPolicy(
  39        PhabricatorSlowvoteDefaultViewCapability::CAPABILITY);
  40  
  41      return id(new PhabricatorSlowvotePoll())
  42        ->setAuthorPHID($actor->getPHID())
  43        ->setViewPolicy($view_policy);
  44    }
  45  
  46    public function getConfiguration() {
  47      return array(
  48        self::CONFIG_AUX_PHID => true,
  49        self::CONFIG_COLUMN_SCHEMA => array(
  50          'question' => 'text255',
  51          'responseVisibility' => 'uint32',
  52          'shuffle' => 'uint32',
  53          'method' => 'uint32',
  54          'description' => 'text',
  55          'isClosed' => 'bool',
  56        ),
  57        self::CONFIG_KEY_SCHEMA => array(
  58          'key_phid' => null,
  59          'phid' => array(
  60            'columns' => array('phid'),
  61            'unique' => true,
  62          ),
  63        ),
  64      ) + parent::getConfiguration();
  65    }
  66  
  67    public function generatePHID() {
  68      return PhabricatorPHID::generateNewPHID(
  69        PhabricatorSlowvotePollPHIDType::TYPECONST);
  70    }
  71  
  72    public function getOptions() {
  73      return $this->assertAttached($this->options);
  74    }
  75  
  76    public function attachOptions(array $options) {
  77      assert_instances_of($options, 'PhabricatorSlowvoteOption');
  78      $this->options = $options;
  79      return $this;
  80    }
  81  
  82    public function getChoices() {
  83      return $this->assertAttached($this->choices);
  84    }
  85  
  86    public function attachChoices(array $choices) {
  87      assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
  88      $this->choices = $choices;
  89      return $this;
  90    }
  91  
  92    public function getViewerChoices(PhabricatorUser $viewer) {
  93      return $this->assertAttachedKey($this->viewerChoices, $viewer->getPHID());
  94    }
  95  
  96    public function attachViewerChoices(PhabricatorUser $viewer, array $choices) {
  97      if ($this->viewerChoices === self::ATTACHABLE) {
  98        $this->viewerChoices = array();
  99      }
 100      assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
 101      $this->viewerChoices[$viewer->getPHID()] = $choices;
 102      return $this;
 103    }
 104  
 105  
 106  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 107  
 108  
 109    public function getCapabilities() {
 110      return array(
 111        PhabricatorPolicyCapability::CAN_VIEW,
 112        PhabricatorPolicyCapability::CAN_EDIT,
 113      );
 114    }
 115  
 116    public function getPolicy($capability) {
 117      switch ($capability) {
 118        case PhabricatorPolicyCapability::CAN_VIEW:
 119          return $this->viewPolicy;
 120        case PhabricatorPolicyCapability::CAN_EDIT:
 121          return PhabricatorPolicies::POLICY_NOONE;
 122      }
 123    }
 124  
 125    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 126      return ($viewer->getPHID() == $this->getAuthorPHID());
 127    }
 128  
 129    public function describeAutomaticCapability($capability) {
 130      return pht('The author of a poll can always view and edit it.');
 131    }
 132  
 133  
 134  
 135  /* -(  PhabricatorSubscribableInterface  )----------------------------------- */
 136  
 137  
 138    public function isAutomaticallySubscribed($phid) {
 139      return ($phid == $this->getAuthorPHID());
 140    }
 141  
 142    public function shouldShowSubscribersProperty() {
 143      return true;
 144    }
 145  
 146    public function shouldAllowSubscription($phid) {
 147      return true;
 148    }
 149  
 150  
 151  /* -(  PhabricatorTokenReceiverInterface  )---------------------------------- */
 152  
 153  
 154    public function getUsersToNotifyOfTokenGiven() {
 155      return array($this->getAuthorPHID());
 156    }
 157  
 158  /* -(  PhabricatorDestructableInterface  )----------------------------------- */
 159  
 160    public function destroyObjectPermanently(
 161      PhabricatorDestructionEngine $engine) {
 162  
 163      $this->openTransaction();
 164        $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
 165          'pollID = %d',
 166          $this->getID());
 167        foreach ($choices as $choice) {
 168          $choice->delete();
 169        }
 170        $options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
 171          'pollID = %d',
 172          $this->getID());
 173        foreach ($options as $option) {
 174          $option->delete();
 175        }
 176        $this->delete();
 177      $this->saveTransaction();
 178    }
 179  
 180  }


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