Skip to content

Commit c1887f0

Browse files
author
epriestley
committedMay 27, 2022
Separate Slowvote poll status onto a dedicated object
Summary: Ref T13682. Prepares for use of API-friendly string constants rather than opaque integers. Test Plan: Created and edited polls, opening and closing them. Grepped for affected methods. Maniphest Tasks: T13682 Differential Revision: https://secure.phabricator.com/D21847
1 parent 03d3d18 commit c1887f0

8 files changed

+102
-15
lines changed
 

‎src/__phutil_library_map__.php

+2
Original file line numberDiff line numberDiff line change
@@ -5874,6 +5874,7 @@
58745874
'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php',
58755875
'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php',
58765876
'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php',
5877+
'SlowvotePollStatus' => 'applications/slowvote/constants/SlowvotePollStatus.php',
58775878
'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php',
58785879
'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php',
58795880
'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php',
@@ -12778,6 +12779,7 @@
1277812779
'SlowvoteEmbedView' => 'AphrontView',
1277912780
'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod',
1278012781
'SlowvotePollResponseVisibility' => 'Phobject',
12782+
'SlowvotePollStatus' => 'Phobject',
1278112783
'SlowvotePollVotingMethod' => 'Phobject',
1278212784
'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule',
1278312785
'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
final class SlowvotePollStatus
4+
extends Phobject {
5+
6+
const STATUS_OPEN = 0;
7+
const STATUS_CLOSED = 1;
8+
9+
private $key;
10+
11+
public static function newStatusObject($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::newStatusObject($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 getHeaderTagIcon() {
43+
return $this->getProperty('header.tag.icon');
44+
}
45+
46+
public function getHeaderTagColor() {
47+
return $this->getProperty('header.tag.color');
48+
}
49+
50+
private function getProperty($key, $default = null) {
51+
$spec = idx(self::getMap(), $this->getKey(), array());
52+
return idx($spec, $key, $default);
53+
}
54+
55+
private static function getMap() {
56+
return array(
57+
self::STATUS_OPEN => array(
58+
'name' => pht('Open'),
59+
'header.tag.icon' => 'fa-square-o',
60+
'header.tag.color' => 'bluegrey',
61+
),
62+
self::STATUS_CLOSED => array(
63+
'name' => pht('Closed'),
64+
'header.tag.icon' => 'fa-ban',
65+
'header.tag.color' => 'indigo',
66+
),
67+
);
68+
}
69+
70+
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public function handleRequest(AphrontRequest $request) {
2323
$close_uri = '/V'.$poll->getID();
2424

2525
if ($request->isFormPost()) {
26-
if ($poll->getIsClosed()) {
27-
$new_status = 0;
26+
if ($poll->isClosed()) {
27+
$new_status = SlowvotePollStatus::STATUS_OPEN;
2828
} else {
29-
$new_status = 1;
29+
$new_status = SlowvotePollStatus::STATUS_CLOSED;
3030
}
3131

3232
$xactions = array();
@@ -46,7 +46,7 @@ public function handleRequest(AphrontRequest $request) {
4646
return id(new AphrontRedirectResponse())->setURI($close_uri);
4747
}
4848

49-
if ($poll->getIsClosed()) {
49+
if ($poll->isClosed()) {
5050
$title = pht('Reopen Poll');
5151
$content = pht('Are you sure you want to reopen the poll?');
5252
$submit = pht('Reopen');

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ public function handleRequest(AphrontRequest $request) {
3535
));
3636
}
3737

38-
$header_icon = $poll->getIsClosed() ? 'fa-ban' : 'fa-square-o';
39-
$header_name = $poll->getIsClosed() ? pht('Closed') : pht('Open');
40-
$header_color = $poll->getIsClosed() ? 'indigo' : 'bluegrey';
38+
$status = $poll->getStatusObject();
39+
40+
$header_icon = $status->getHeaderTagIcon();
41+
$header_color = $status->getHeaderTagColor();
42+
$header_name = $status->getName();
4143

4244
$header = id(new PHUIHeaderView())
4345
->setHeader($poll->getQuestion())
@@ -50,7 +52,7 @@ public function handleRequest(AphrontRequest $request) {
5052
$subheader = $this->buildSubheaderView($poll);
5153

5254
$crumbs = $this->buildApplicationCrumbs();
53-
$crumbs->addTextCrumb('V'.$poll->getID());
55+
$crumbs->addTextCrumb($poll->getMonogram());
5456
$crumbs->setBorder(true);
5557

5658
$timeline = $this->buildTransactionTimeline(
@@ -71,7 +73,11 @@ public function handleRequest(AphrontRequest $request) {
7173
->setMainColumn($poll_content);
7274

7375
return $this->newPage()
74-
->setTitle('V'.$poll->getID().' '.$poll->getQuestion())
76+
->setTitle(
77+
pht(
78+
'%s %s',
79+
$poll->getMonogram(),
80+
$poll->getQuestion()))
7581
->setCrumbs($crumbs)
7682
->setPageObjectPHIDs(array($poll->getPHID()))
7783
->appendChild($view);
@@ -87,7 +93,7 @@ private function buildCurtain(PhabricatorSlowvotePoll $poll) {
8793

8894
$curtain = $this->newCurtainView($poll);
8995

90-
$is_closed = $poll->getIsClosed();
96+
$is_closed = $poll->isClosed();
9197
$close_poll_text = $is_closed ? pht('Reopen Poll') : pht('Close Poll');
9298
$close_poll_icon = $is_closed ? 'fa-check' : 'fa-ban';
9399

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function handleRequest(AphrontRequest $request) {
2121
return new Aphront404Response();
2222
}
2323

24-
if ($poll->getIsClosed()) {
24+
if ($poll->isClosed()) {
2525
return new Aphront400Response();
2626
}
2727

‎src/applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function renderResultList(
142142
->setHref('/V'.$poll->getID())
143143
->addIcon('none', $date_created);
144144

145-
if ($poll->getIsClosed()) {
145+
if ($poll->isClosed()) {
146146
$item->setStatusIcon('fa-ban grey');
147147
$item->setDisabled(true);
148148
} else {

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class PhabricatorSlowvotePoll
2020
protected $shuffle = 0;
2121
protected $method;
2222
protected $viewPolicy;
23-
protected $isClosed = 0;
23+
protected $isClosed;
2424
protected $spacePHID;
2525

2626
private $options = self::ATTACHABLE;
@@ -43,6 +43,7 @@ public static function initializeNewPoll(PhabricatorUser $actor) {
4343
->setAuthorPHID($actor->getPHID())
4444
->setViewPolicy($view_policy)
4545
->setSpacePHID($actor->getDefaultSpacePHID())
46+
->setIsClosed(SlowvotePollStatus::STATUS_OPEN)
4647
->setMethod($default_method)
4748
->setResponseVisibility($default_responses);
4849
}
@@ -67,6 +68,14 @@ public function getPHIDType() {
6768
return PhabricatorSlowvotePollPHIDType::TYPECONST;
6869
}
6970

71+
public function getStatusObject() {
72+
return SlowvotePollStatus::newStatusObject($this->getIsClosed());
73+
}
74+
75+
public function isClosed() {
76+
return ($this->getIsClosed() == SlowvotePollStatus::STATUS_CLOSED);
77+
}
78+
7079
public function getOptions() {
7180
return $this->assertAttached($this->options);
7281
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function render() {
9595
),
9696
$quip);
9797

98-
if ($poll->getIsClosed()) {
98+
if ($poll->isClosed()) {
9999
$submit = null;
100100
} else {
101101
$submit = phutil_tag(
@@ -228,7 +228,7 @@ private function renderControl(PhabricatorSlowvoteOption $option, $selected) {
228228
SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox',
229229
);
230230

231-
$closed = $this->getPoll()->getIsClosed();
231+
$closed = $this->getPoll()->isClosed();
232232

233233
return phutil_tag(
234234
'input',

0 commit comments

Comments
 (0)
Failed to load comments.