diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -4859,6 +4859,7 @@ '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 @@ '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 @@ 'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhabricatorSlowvoteTransactionType' => 'PhabricatorModularTransactionType', 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', + 'PhabricatorSlowvoteVotingMethodTransaction' => 'PhabricatorSlowvoteTransactionType', 'PhabricatorSlug' => 'Phobject', 'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 'PhabricatorSourceCodeView' => 'AphrontView', @@ -12775,6 +12778,7 @@ '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 --- /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 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php @@ -189,12 +189,19 @@ } } - $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 @@ ->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 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php @@ -35,7 +35,7 @@ $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 --- a/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php +++ b/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php @@ -13,9 +13,6 @@ PhabricatorSpacesInterface, PhabricatorConduitResultInterface { - const METHOD_PLURALITY = 0; - const METHOD_APPROVAL = 1; - protected $question; protected $description; protected $authorPHID; @@ -40,11 +37,13 @@ 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 --- a/src/applications/slowvote/view/SlowvoteEmbedView.php +++ b/src/applications/slowvote/view/SlowvoteEmbedView.php @@ -224,8 +224,8 @@ 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 @@ $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 --- /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); + } + +}