From 9dad49472c049c94104f8727990363fb2541fdc6 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 26 May 2022 10:04:11 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 4 ++ .../constants/SlowvotePollVotingMethod.php | 70 +++++++++++++++++++ .../PhabricatorSlowvoteEditController.php | 23 +++--- .../PhabricatorSlowvoteVoteController.php | 2 +- .../storage/PhabricatorSlowvotePoll.php | 5 +- .../slowvote/view/SlowvoteEmbedView.php | 8 +-- ...ricatorSlowvoteVotingMethodTransaction.php | 55 +++++++++++++++ 7 files changed, 151 insertions(+), 16 deletions(-) create mode 100644 src/applications/slowvote/constants/SlowvotePollVotingMethod.php create mode 100644 src/applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index c1a30a9535..44cf25ee13 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -4859,6 +4859,7 @@ phutil_register_library_map(array( 'PhabricatorSlowvoteTransactionQuery' => 'applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php', 'PhabricatorSlowvoteTransactionType' => 'applications/slowvote/xaction/PhabricatorSlowvoteTransactionType.php', 'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php', + 'PhabricatorSlowvoteVotingMethodTransaction' => 'applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php', 'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php', 'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php', 'PhabricatorSourceCodeView' => 'view/layout/PhabricatorSourceCodeView.php', @@ -5873,6 +5874,7 @@ phutil_register_library_map(array( 'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php', 'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php', 'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php', + 'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php', 'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php', 'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php', 'SubscriptionListDialogBuilder' => 'applications/subscriptions/view/SubscriptionListDialogBuilder.php', @@ -11573,6 +11575,7 @@ phutil_register_library_map(array( 'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhabricatorSlowvoteTransactionType' => 'PhabricatorModularTransactionType', 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', + 'PhabricatorSlowvoteVotingMethodTransaction' => 'PhabricatorSlowvoteTransactionType', 'PhabricatorSlug' => 'Phobject', 'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 'PhabricatorSourceCodeView' => 'AphrontView', @@ -12775,6 +12778,7 @@ phutil_register_library_map(array( 'SlowvoteEmbedView' => 'AphrontView', 'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod', 'SlowvotePollResponseVisibility' => 'Phobject', + 'SlowvotePollVotingMethod' => 'Phobject', 'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule', 'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod', 'SubscriptionListDialogBuilder' => 'Phobject', diff --git a/src/applications/slowvote/constants/SlowvotePollVotingMethod.php b/src/applications/slowvote/constants/SlowvotePollVotingMethod.php new file mode 100644 index 0000000000..5707cd257f --- /dev/null +++ b/src/applications/slowvote/constants/SlowvotePollVotingMethod.php @@ -0,0 +1,70 @@ +key = $key; + return $object; + } + + public function getKey() { + return $this->key; + } + + public static function getAll() { + $map = self::getMap(); + + $result = array(); + foreach ($map as $key => $spec) { + $result[$key] = self::newVotingMethodObject($key); + } + + return $result; + } + + public function getName() { + $name = $this->getProperty('name'); + + if ($name === null) { + $name = pht('Unknown ("%s")', $this->getKey()); + } + + return $name; + } + + public function getNameForEdit() { + $name = $this->getProperty('name.edit'); + + if ($name === null) { + $name = pht('Unknown ("%s")', $this->getKey()); + } + + return $name; + } + + private function getProperty($key, $default = null) { + $spec = idx(self::getMap(), $this->getKey(), array()); + return idx($spec, $key, $default); + } + + private static function getMap() { + return array( + self::METHOD_PLURALITY => array( + 'name' => pht('Plurality'), + 'name.edit' => pht('Plurality (Single Choice)'), + ), + self::METHOD_APPROVAL => array( + 'name' => pht('Approval'), + 'name.edit' => pht('Approval (Multiple Choice)'), + ), + ); + } + +} diff --git a/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php b/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php index 599f3c83bd..3cafb03a41 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php @@ -189,12 +189,19 @@ final class PhabricatorSlowvoteEditController } } - $poll_type_options = array( - PhabricatorSlowvotePoll::METHOD_PLURALITY => - pht('Plurality (Single Choice)'), - PhabricatorSlowvotePoll::METHOD_APPROVAL => - pht('Approval (Multiple Choice)'), - ); + $vote_type_map = SlowvotePollVotingMethod::getAll(); + $vote_type_options = mpull($vote_type_map, 'getNameForEdit'); + + $method = $poll->getMethod(); + if (!isset($vote_type_options[$method])) { + $method_object = + SlowvotePollVotingMethod::newVotingMethodObject( + $method); + + $vote_type_options = array( + $method => $method_object->getNameForEdit(), + ) + $vote_type_options; + } $response_type_map = SlowvotePollResponseVisibility::getAll(); $response_type_options = mpull($response_type_map, 'getNameForEdit'); @@ -216,12 +223,12 @@ final class PhabricatorSlowvoteEditController ->setLabel(pht('Vote Type')) ->setName('method') ->setValue($poll->getMethod()) - ->setOptions($poll_type_options)); + ->setOptions($vote_type_options)); } else { $form->appendChild( id(new AphrontFormStaticControl()) ->setLabel(pht('Vote Type')) - ->setValue(idx($poll_type_options, $poll->getMethod()))); + ->setValue(idx($vote_type_options, $poll->getMethod()))); } if ($is_new) { diff --git a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php index e1a1b9df34..cd6205ce61 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php @@ -35,7 +35,7 @@ final class PhabricatorSlowvoteVoteController $votes = array_fuse($votes); $method = $poll->getMethod(); - $is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY); + $is_plurality = ($method == SlowvotePollVotingMethod::METHOD_PLURALITY); if (!$votes) { if ($is_plurality) { diff --git a/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php b/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php index 1e57181cf5..419605e2d8 100644 --- a/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php +++ b/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php @@ -13,9 +13,6 @@ final class PhabricatorSlowvotePoll PhabricatorSpacesInterface, PhabricatorConduitResultInterface { - const METHOD_PLURALITY = 0; - const METHOD_APPROVAL = 1; - protected $question; protected $description; protected $authorPHID; @@ -40,11 +37,13 @@ final class PhabricatorSlowvotePoll PhabricatorSlowvoteDefaultViewCapability::CAPABILITY); $default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE; + $default_method = SlowvotePollVotingMethod::METHOD_PLURALITY; return id(new PhabricatorSlowvotePoll()) ->setAuthorPHID($actor->getPHID()) ->setViewPolicy($view_policy) ->setSpacePHID($actor->getDefaultSpacePHID()) + ->setMethod($default_method) ->setResponseVisibility($default_responses); } diff --git a/src/applications/slowvote/view/SlowvoteEmbedView.php b/src/applications/slowvote/view/SlowvoteEmbedView.php index 6164024148..eaf1d2cc93 100644 --- a/src/applications/slowvote/view/SlowvoteEmbedView.php +++ b/src/applications/slowvote/view/SlowvoteEmbedView.php @@ -224,8 +224,8 @@ final class SlowvoteEmbedView extends AphrontView { private function renderControl(PhabricatorSlowvoteOption $option, $selected) { $types = array( - PhabricatorSlowvotePoll::METHOD_PLURALITY => 'radio', - PhabricatorSlowvotePoll::METHOD_APPROVAL => 'checkbox', + SlowvotePollVotingMethod::METHOD_PLURALITY => 'radio', + SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox', ); $closed = $this->getPoll()->getIsClosed(); @@ -302,10 +302,10 @@ final class SlowvoteEmbedView extends AphrontView { $percent = sprintf('%d%%', $count ? 100 * $choices / $count : 0); switch ($poll->getMethod()) { - case PhabricatorSlowvotePoll::METHOD_PLURALITY: + case SlowvotePollVotingMethod::METHOD_PLURALITY: $status = pht('%s (%d / %d)', $percent, $choices, $count); break; - case PhabricatorSlowvotePoll::METHOD_APPROVAL: + case SlowvotePollVotingMethod::METHOD_APPROVAL: $status = pht('%s Approval (%d / %d)', $percent, $choices, $count); break; } diff --git a/src/applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php b/src/applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php new file mode 100644 index 0000000000..ac8eaa15dc --- /dev/null +++ b/src/applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php @@ -0,0 +1,55 @@ +getMethod(); + } + + public function generateNewValue($object, $value) { + return (string)$value; + } + + public function applyInternalEffects($object, $value) { + $object->setMethod($value); + } + + public function getTitle() { + $old_name = $this->getOldVotingMethodObject()->getName(); + $new_name = $this->getNewVotingMethodObject()->getName(); + + return pht( + '%s changed the voting method from %s to %s.', + $this->renderAuthor(), + $this->renderValue($old_name), + $this->renderValue($new_name)); + } + + public function getTitleForFeed() { + $old_name = $this->getOldVotingMethodObject()->getName(); + $new_name = $this->getNewVotingMethodObject()->getName(); + + return pht( + '%s changed the voting method of %s from %s to %s.', + $this->renderAuthor(), + $this->renderObject(), + $this->renderValue($old_name), + $this->renderValue($new_name)); + } + + private function getOldVotingMethodObject() { + return $this->newVotingMethodObject($this->getOldValue()); + } + + private function getNewVotingMethodObject() { + return $this->newVotingMethodObject($this->getNewValue()); + } + + private function newVotingMethodObject($value) { + return SlowvotePollVotingMethod::newVotingMethodObject($value); + } + +}