1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 00:42:41 +01:00

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
This commit is contained in:
epriestley 2022-05-26 10:04:11 -07:00
parent 9f075839a2
commit 9dad49472c
7 changed files with 151 additions and 16 deletions

View file

@ -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',

View file

@ -0,0 +1,70 @@
<?php
final class SlowvotePollVotingMethod
extends Phobject {
const METHOD_PLURALITY = 0;
const METHOD_APPROVAL = 1;
private $key;
public static function newVotingMethodObject($key) {
$object = new self();
$object->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)'),
),
);
}
}

View file

@ -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) {

View file

@ -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) {

View file

@ -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);
}

View file

@ -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;
}

View file

@ -0,0 +1,55 @@
<?php
final class PhabricatorSlowvoteVotingMethodTransaction
extends PhabricatorSlowvoteTransactionType {
const TRANSACTIONTYPE = 'vote:method';
public function generateOldValue($object) {
return (string)$object->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);
}
}