@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator

Move Slowvote vote types to a separate object

Summary: Ref T13682. Extract Slowvote vote types to a separate object, to prepare for turning them into API-friendly string constants.

Test Plan: Created, edited, and voted in Slowvote polls. Grepped for affected constants.

Maniphest Tasks: T13682

Differential Revision: https://secure.phabricator.com/D21845

+151 -16
+4
src/__phutil_library_map__.php
··· 4859 4859 'PhabricatorSlowvoteTransactionQuery' => 'applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php', 4860 4860 'PhabricatorSlowvoteTransactionType' => 'applications/slowvote/xaction/PhabricatorSlowvoteTransactionType.php', 4861 4861 'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php', 4862 + 'PhabricatorSlowvoteVotingMethodTransaction' => 'applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php', 4862 4863 'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php', 4863 4864 'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php', 4864 4865 'PhabricatorSourceCodeView' => 'view/layout/PhabricatorSourceCodeView.php', ··· 5873 5874 'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php', 5874 5875 'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php', 5875 5876 'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php', 5877 + 'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php', 5876 5878 'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php', 5877 5879 'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php', 5878 5880 'SubscriptionListDialogBuilder' => 'applications/subscriptions/view/SubscriptionListDialogBuilder.php', ··· 11573 11575 'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 11574 11576 'PhabricatorSlowvoteTransactionType' => 'PhabricatorModularTransactionType', 11575 11577 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', 11578 + 'PhabricatorSlowvoteVotingMethodTransaction' => 'PhabricatorSlowvoteTransactionType', 11576 11579 'PhabricatorSlug' => 'Phobject', 11577 11580 'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 11578 11581 'PhabricatorSourceCodeView' => 'AphrontView', ··· 12775 12778 'SlowvoteEmbedView' => 'AphrontView', 12776 12779 'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod', 12777 12780 'SlowvotePollResponseVisibility' => 'Phobject', 12781 + 'SlowvotePollVotingMethod' => 'Phobject', 12778 12782 'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule', 12779 12783 'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod', 12780 12784 'SubscriptionListDialogBuilder' => 'Phobject',
+70
src/applications/slowvote/constants/SlowvotePollVotingMethod.php
··· 1 + <?php 2 + 3 + final class SlowvotePollVotingMethod 4 + extends Phobject { 5 + 6 + const METHOD_PLURALITY = 0; 7 + const METHOD_APPROVAL = 1; 8 + 9 + private $key; 10 + 11 + public static function newVotingMethodObject($key) { 12 + $object = new self(); 13 + $object->key = $key; 14 + return $object; 15 + } 16 + 17 + public function getKey() { 18 + return $this->key; 19 + } 20 + 21 + public static function getAll() { 22 + $map = self::getMap(); 23 + 24 + $result = array(); 25 + foreach ($map as $key => $spec) { 26 + $result[$key] = self::newVotingMethodObject($key); 27 + } 28 + 29 + return $result; 30 + } 31 + 32 + public function getName() { 33 + $name = $this->getProperty('name'); 34 + 35 + if ($name === null) { 36 + $name = pht('Unknown ("%s")', $this->getKey()); 37 + } 38 + 39 + return $name; 40 + } 41 + 42 + public function getNameForEdit() { 43 + $name = $this->getProperty('name.edit'); 44 + 45 + if ($name === null) { 46 + $name = pht('Unknown ("%s")', $this->getKey()); 47 + } 48 + 49 + return $name; 50 + } 51 + 52 + private function getProperty($key, $default = null) { 53 + $spec = idx(self::getMap(), $this->getKey(), array()); 54 + return idx($spec, $key, $default); 55 + } 56 + 57 + private static function getMap() { 58 + return array( 59 + self::METHOD_PLURALITY => array( 60 + 'name' => pht('Plurality'), 61 + 'name.edit' => pht('Plurality (Single Choice)'), 62 + ), 63 + self::METHOD_APPROVAL => array( 64 + 'name' => pht('Approval'), 65 + 'name.edit' => pht('Approval (Multiple Choice)'), 66 + ), 67 + ); 68 + } 69 + 70 + }
+15 -8
src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php
··· 189 189 } 190 190 } 191 191 192 - $poll_type_options = array( 193 - PhabricatorSlowvotePoll::METHOD_PLURALITY => 194 - pht('Plurality (Single Choice)'), 195 - PhabricatorSlowvotePoll::METHOD_APPROVAL => 196 - pht('Approval (Multiple Choice)'), 197 - ); 192 + $vote_type_map = SlowvotePollVotingMethod::getAll(); 193 + $vote_type_options = mpull($vote_type_map, 'getNameForEdit'); 194 + 195 + $method = $poll->getMethod(); 196 + if (!isset($vote_type_options[$method])) { 197 + $method_object = 198 + SlowvotePollVotingMethod::newVotingMethodObject( 199 + $method); 200 + 201 + $vote_type_options = array( 202 + $method => $method_object->getNameForEdit(), 203 + ) + $vote_type_options; 204 + } 198 205 199 206 $response_type_map = SlowvotePollResponseVisibility::getAll(); 200 207 $response_type_options = mpull($response_type_map, 'getNameForEdit'); ··· 216 223 ->setLabel(pht('Vote Type')) 217 224 ->setName('method') 218 225 ->setValue($poll->getMethod()) 219 - ->setOptions($poll_type_options)); 226 + ->setOptions($vote_type_options)); 220 227 } else { 221 228 $form->appendChild( 222 229 id(new AphrontFormStaticControl()) 223 230 ->setLabel(pht('Vote Type')) 224 - ->setValue(idx($poll_type_options, $poll->getMethod()))); 231 + ->setValue(idx($vote_type_options, $poll->getMethod()))); 225 232 } 226 233 227 234 if ($is_new) {
+1 -1
src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php
··· 35 35 $votes = array_fuse($votes); 36 36 37 37 $method = $poll->getMethod(); 38 - $is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY); 38 + $is_plurality = ($method == SlowvotePollVotingMethod::METHOD_PLURALITY); 39 39 40 40 if (!$votes) { 41 41 if ($is_plurality) {
+2 -3
src/applications/slowvote/storage/PhabricatorSlowvotePoll.php
··· 13 13 PhabricatorSpacesInterface, 14 14 PhabricatorConduitResultInterface { 15 15 16 - const METHOD_PLURALITY = 0; 17 - const METHOD_APPROVAL = 1; 18 - 19 16 protected $question; 20 17 protected $description; 21 18 protected $authorPHID; ··· 40 37 PhabricatorSlowvoteDefaultViewCapability::CAPABILITY); 41 38 42 39 $default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE; 40 + $default_method = SlowvotePollVotingMethod::METHOD_PLURALITY; 43 41 44 42 return id(new PhabricatorSlowvotePoll()) 45 43 ->setAuthorPHID($actor->getPHID()) 46 44 ->setViewPolicy($view_policy) 47 45 ->setSpacePHID($actor->getDefaultSpacePHID()) 46 + ->setMethod($default_method) 48 47 ->setResponseVisibility($default_responses); 49 48 } 50 49
+4 -4
src/applications/slowvote/view/SlowvoteEmbedView.php
··· 224 224 225 225 private function renderControl(PhabricatorSlowvoteOption $option, $selected) { 226 226 $types = array( 227 - PhabricatorSlowvotePoll::METHOD_PLURALITY => 'radio', 228 - PhabricatorSlowvotePoll::METHOD_APPROVAL => 'checkbox', 227 + SlowvotePollVotingMethod::METHOD_PLURALITY => 'radio', 228 + SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox', 229 229 ); 230 230 231 231 $closed = $this->getPoll()->getIsClosed(); ··· 302 302 $percent = sprintf('%d%%', $count ? 100 * $choices / $count : 0); 303 303 304 304 switch ($poll->getMethod()) { 305 - case PhabricatorSlowvotePoll::METHOD_PLURALITY: 305 + case SlowvotePollVotingMethod::METHOD_PLURALITY: 306 306 $status = pht('%s (%d / %d)', $percent, $choices, $count); 307 307 break; 308 - case PhabricatorSlowvotePoll::METHOD_APPROVAL: 308 + case SlowvotePollVotingMethod::METHOD_APPROVAL: 309 309 $status = pht('%s Approval (%d / %d)', $percent, $choices, $count); 310 310 break; 311 311 }
+55
src/applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php
··· 1 + <?php 2 + 3 + final class PhabricatorSlowvoteVotingMethodTransaction 4 + extends PhabricatorSlowvoteTransactionType { 5 + 6 + const TRANSACTIONTYPE = 'vote:method'; 7 + 8 + public function generateOldValue($object) { 9 + return (string)$object->getMethod(); 10 + } 11 + 12 + public function generateNewValue($object, $value) { 13 + return (string)$value; 14 + } 15 + 16 + public function applyInternalEffects($object, $value) { 17 + $object->setMethod($value); 18 + } 19 + 20 + public function getTitle() { 21 + $old_name = $this->getOldVotingMethodObject()->getName(); 22 + $new_name = $this->getNewVotingMethodObject()->getName(); 23 + 24 + return pht( 25 + '%s changed the voting method from %s to %s.', 26 + $this->renderAuthor(), 27 + $this->renderValue($old_name), 28 + $this->renderValue($new_name)); 29 + } 30 + 31 + public function getTitleForFeed() { 32 + $old_name = $this->getOldVotingMethodObject()->getName(); 33 + $new_name = $this->getNewVotingMethodObject()->getName(); 34 + 35 + return pht( 36 + '%s changed the voting method of %s from %s to %s.', 37 + $this->renderAuthor(), 38 + $this->renderObject(), 39 + $this->renderValue($old_name), 40 + $this->renderValue($new_name)); 41 + } 42 + 43 + private function getOldVotingMethodObject() { 44 + return $this->newVotingMethodObject($this->getOldValue()); 45 + } 46 + 47 + private function getNewVotingMethodObject() { 48 + return $this->newVotingMethodObject($this->getNewValue()); 49 + } 50 + 51 + private function newVotingMethodObject($value) { 52 + return SlowvotePollVotingMethod::newVotingMethodObject($value); 53 + } 54 + 55 + }