Skip to content

Commit 9dad494

Browse files
author
epriestley
committedMay 26, 2022
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
1 parent 9f07583 commit 9dad494

7 files changed

+151
-16
lines changed
 

‎src/__phutil_library_map__.php

+4
Original file line numberDiff line numberDiff line change
@@ -4859,6 +4859,7 @@
48594859
'PhabricatorSlowvoteTransactionQuery' => 'applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php',
48604860
'PhabricatorSlowvoteTransactionType' => 'applications/slowvote/xaction/PhabricatorSlowvoteTransactionType.php',
48614861
'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php',
4862+
'PhabricatorSlowvoteVotingMethodTransaction' => 'applications/slowvote/xaction/PhabricatorSlowvoteVotingMethodTransaction.php',
48624863
'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php',
48634864
'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php',
48644865
'PhabricatorSourceCodeView' => 'view/layout/PhabricatorSourceCodeView.php',
@@ -5873,6 +5874,7 @@
58735874
'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php',
58745875
'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php',
58755876
'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php',
5877+
'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php',
58765878
'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php',
58775879
'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php',
58785880
'SubscriptionListDialogBuilder' => 'applications/subscriptions/view/SubscriptionListDialogBuilder.php',
@@ -11573,6 +11575,7 @@
1157311575
'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
1157411576
'PhabricatorSlowvoteTransactionType' => 'PhabricatorModularTransactionType',
1157511577
'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController',
11578+
'PhabricatorSlowvoteVotingMethodTransaction' => 'PhabricatorSlowvoteTransactionType',
1157611579
'PhabricatorSlug' => 'Phobject',
1157711580
'PhabricatorSlugTestCase' => 'PhabricatorTestCase',
1157811581
'PhabricatorSourceCodeView' => 'AphrontView',
@@ -12775,6 +12778,7 @@
1277512778
'SlowvoteEmbedView' => 'AphrontView',
1277612779
'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod',
1277712780
'SlowvotePollResponseVisibility' => 'Phobject',
12781+
'SlowvotePollVotingMethod' => 'Phobject',
1277812782
'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule',
1277912783
'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod',
1278012784
'SubscriptionListDialogBuilder' => 'Phobject',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

‎src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,19 @@ public function handleRequest(AphrontRequest $request) {
189189
}
190190
}
191191

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+
}
198205

199206
$response_type_map = SlowvotePollResponseVisibility::getAll();
200207
$response_type_options = mpull($response_type_map, 'getNameForEdit');
@@ -216,12 +223,12 @@ public function handleRequest(AphrontRequest $request) {
216223
->setLabel(pht('Vote Type'))
217224
->setName('method')
218225
->setValue($poll->getMethod())
219-
->setOptions($poll_type_options));
226+
->setOptions($vote_type_options));
220227
} else {
221228
$form->appendChild(
222229
id(new AphrontFormStaticControl())
223230
->setLabel(pht('Vote Type'))
224-
->setValue(idx($poll_type_options, $poll->getMethod())));
231+
->setValue(idx($vote_type_options, $poll->getMethod())));
225232
}
226233

227234
if ($is_new) {

‎src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function handleRequest(AphrontRequest $request) {
3535
$votes = array_fuse($votes);
3636

3737
$method = $poll->getMethod();
38-
$is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY);
38+
$is_plurality = ($method == SlowvotePollVotingMethod::METHOD_PLURALITY);
3939

4040
if (!$votes) {
4141
if ($is_plurality) {

‎src/applications/slowvote/storage/PhabricatorSlowvotePoll.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ final class PhabricatorSlowvotePoll
1313
PhabricatorSpacesInterface,
1414
PhabricatorConduitResultInterface {
1515

16-
const METHOD_PLURALITY = 0;
17-
const METHOD_APPROVAL = 1;
18-
1916
protected $question;
2017
protected $description;
2118
protected $authorPHID;
@@ -40,11 +37,13 @@ public static function initializeNewPoll(PhabricatorUser $actor) {
4037
PhabricatorSlowvoteDefaultViewCapability::CAPABILITY);
4138

4239
$default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE;
40+
$default_method = SlowvotePollVotingMethod::METHOD_PLURALITY;
4341

4442
return id(new PhabricatorSlowvotePoll())
4543
->setAuthorPHID($actor->getPHID())
4644
->setViewPolicy($view_policy)
4745
->setSpacePHID($actor->getDefaultSpacePHID())
46+
->setMethod($default_method)
4847
->setResponseVisibility($default_responses);
4948
}
5049

‎src/applications/slowvote/view/SlowvoteEmbedView.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ private function renderBar(PhabricatorSlowvoteOption $option) {
224224

225225
private function renderControl(PhabricatorSlowvoteOption $option, $selected) {
226226
$types = array(
227-
PhabricatorSlowvotePoll::METHOD_PLURALITY => 'radio',
228-
PhabricatorSlowvotePoll::METHOD_APPROVAL => 'checkbox',
227+
SlowvotePollVotingMethod::METHOD_PLURALITY => 'radio',
228+
SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox',
229229
);
230230

231231
$closed = $this->getPoll()->getIsClosed();
@@ -302,10 +302,10 @@ private function renderStatus(PhabricatorSlowvoteOption $option) {
302302
$percent = sprintf('%d%%', $count ? 100 * $choices / $count : 0);
303303

304304
switch ($poll->getMethod()) {
305-
case PhabricatorSlowvotePoll::METHOD_PLURALITY:
305+
case SlowvotePollVotingMethod::METHOD_PLURALITY:
306306
$status = pht('%s (%d / %d)', $percent, $choices, $count);
307307
break;
308-
case PhabricatorSlowvotePoll::METHOD_APPROVAL:
308+
case SlowvotePollVotingMethod::METHOD_APPROVAL:
309309
$status = pht('%s Approval (%d / %d)', $percent, $choices, $count);
310310
break;
311311
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

0 commit comments

Comments
 (0)
Failed to load comments.