Update Slowvote poll status to use sensible string constants
[phabricator.git] / src / applications / slowvote / storage / PhabricatorSlowvotePoll.php
blob9ee33c81bb9adb15b083945f4cfebf9b2f33352b
1 <?php
3 final class PhabricatorSlowvotePoll
4 extends PhabricatorSlowvoteDAO
5 implements
6 PhabricatorApplicationTransactionInterface,
7 PhabricatorPolicyInterface,
8 PhabricatorSubscribableInterface,
9 PhabricatorFlaggableInterface,
10 PhabricatorTokenReceiverInterface,
11 PhabricatorProjectInterface,
12 PhabricatorDestructibleInterface,
13 PhabricatorSpacesInterface,
14 PhabricatorConduitResultInterface {
16 protected $question;
17 protected $description;
18 protected $authorPHID;
19 protected $responseVisibility;
20 protected $shuffle = 0;
21 protected $method;
22 protected $viewPolicy;
23 protected $status;
24 protected $spacePHID;
26 private $options = self::ATTACHABLE;
27 private $choices = self::ATTACHABLE;
28 private $viewerChoices = self::ATTACHABLE;
30 public static function initializeNewPoll(PhabricatorUser $actor) {
31 $app = id(new PhabricatorApplicationQuery())
32 ->setViewer($actor)
33 ->withClasses(array('PhabricatorSlowvoteApplication'))
34 ->executeOne();
36 $view_policy = $app->getPolicy(
37 PhabricatorSlowvoteDefaultViewCapability::CAPABILITY);
39 $default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE;
40 $default_method = SlowvotePollVotingMethod::METHOD_PLURALITY;
42 return id(new PhabricatorSlowvotePoll())
43 ->setAuthorPHID($actor->getPHID())
44 ->setViewPolicy($view_policy)
45 ->setSpacePHID($actor->getDefaultSpacePHID())
46 ->setStatus(SlowvotePollStatus::STATUS_OPEN)
47 ->setMethod($default_method)
48 ->setResponseVisibility($default_responses);
51 protected function getConfiguration() {
52 return array(
53 self::CONFIG_AUX_PHID => true,
54 self::CONFIG_COLUMN_SCHEMA => array(
55 'question' => 'text255',
56 'responseVisibility' => 'text32',
57 'shuffle' => 'bool',
58 'method' => 'text32',
59 'description' => 'text',
60 'status' => 'text32',
62 self::CONFIG_KEY_SCHEMA => array(
64 ) + parent::getConfiguration();
67 public function getPHIDType() {
68 return PhabricatorSlowvotePollPHIDType::TYPECONST;
71 public function getStatusObject() {
72 return SlowvotePollStatus::newStatusObject($this->getStatus());
75 public function isClosed() {
76 return ($this->getStatus() == SlowvotePollStatus::STATUS_CLOSED);
79 public function getOptions() {
80 return $this->assertAttached($this->options);
83 public function attachOptions(array $options) {
84 assert_instances_of($options, 'PhabricatorSlowvoteOption');
85 $this->options = $options;
86 return $this;
89 public function getChoices() {
90 return $this->assertAttached($this->choices);
93 public function attachChoices(array $choices) {
94 assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
95 $this->choices = $choices;
96 return $this;
99 public function getViewerChoices(PhabricatorUser $viewer) {
100 return $this->assertAttachedKey($this->viewerChoices, $viewer->getPHID());
103 public function attachViewerChoices(PhabricatorUser $viewer, array $choices) {
104 if ($this->viewerChoices === self::ATTACHABLE) {
105 $this->viewerChoices = array();
107 assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
108 $this->viewerChoices[$viewer->getPHID()] = $choices;
109 return $this;
112 public function getMonogram() {
113 return 'V'.$this->getID();
116 public function getURI() {
117 return '/'.$this->getMonogram();
121 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
124 public function getApplicationTransactionEditor() {
125 return new PhabricatorSlowvoteEditor();
128 public function getApplicationTransactionTemplate() {
129 return new PhabricatorSlowvoteTransaction();
133 /* -( PhabricatorPolicyInterface )----------------------------------------- */
136 public function getCapabilities() {
137 return array(
138 PhabricatorPolicyCapability::CAN_VIEW,
139 PhabricatorPolicyCapability::CAN_EDIT,
143 public function getPolicy($capability) {
144 switch ($capability) {
145 case PhabricatorPolicyCapability::CAN_VIEW:
146 return $this->viewPolicy;
147 case PhabricatorPolicyCapability::CAN_EDIT:
148 return PhabricatorPolicies::POLICY_NOONE;
152 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
153 return ($viewer->getPHID() == $this->getAuthorPHID());
156 public function describeAutomaticCapability($capability) {
157 return pht('The author of a poll can always view and edit it.');
162 /* -( PhabricatorSubscribableInterface )----------------------------------- */
165 public function isAutomaticallySubscribed($phid) {
166 return ($phid == $this->getAuthorPHID());
170 /* -( PhabricatorTokenReceiverInterface )---------------------------------- */
173 public function getUsersToNotifyOfTokenGiven() {
174 return array($this->getAuthorPHID());
177 /* -( PhabricatorDestructibleInterface )----------------------------------- */
179 public function destroyObjectPermanently(
180 PhabricatorDestructionEngine $engine) {
182 $this->openTransaction();
183 $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
184 'pollID = %d',
185 $this->getID());
186 foreach ($choices as $choice) {
187 $choice->delete();
189 $options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
190 'pollID = %d',
191 $this->getID());
192 foreach ($options as $option) {
193 $option->delete();
195 $this->delete();
196 $this->saveTransaction();
199 /* -( PhabricatorSpacesInterface )----------------------------------------- */
201 public function getSpacePHID() {
202 return $this->spacePHID;
205 /* -( PhabricatorConduitResultInterface )---------------------------------- */
207 public function getFieldSpecificationsForConduit() {
208 return array(
209 id(new PhabricatorConduitSearchFieldSpecification())
210 ->setKey('name')
211 ->setType('string')
212 ->setDescription(pht('The name of the poll.')),
213 id(new PhabricatorConduitSearchFieldSpecification())
214 ->setKey('authorPHID')
215 ->setType('string')
216 ->setDescription(pht('The author of the poll.')),
220 public function getFieldValuesForConduit() {
221 return array(
222 'name' => $this->getQuestion(),
223 'authorPHID' => $this->getAuthorPHID(),
227 public function getConduitSearchAttachments() {
228 return array();