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
This commit is contained in:
epriestley 2022-05-26 10:35:38 -07:00
parent 03d3d1889d
commit c1887f0c1d
8 changed files with 102 additions and 15 deletions

View file

@ -5874,6 +5874,7 @@ phutil_register_library_map(array(
'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php', 'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php',
'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php', 'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php',
'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php', 'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php',
'SlowvotePollStatus' => 'applications/slowvote/constants/SlowvotePollStatus.php',
'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php', 'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php',
'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php', 'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php',
'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php', 'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php',
@ -12778,6 +12779,7 @@ phutil_register_library_map(array(
'SlowvoteEmbedView' => 'AphrontView', 'SlowvoteEmbedView' => 'AphrontView',
'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod', 'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod',
'SlowvotePollResponseVisibility' => 'Phobject', 'SlowvotePollResponseVisibility' => 'Phobject',
'SlowvotePollStatus' => 'Phobject',
'SlowvotePollVotingMethod' => 'Phobject', 'SlowvotePollVotingMethod' => 'Phobject',
'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule', 'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule',
'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod', 'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod',

View file

@ -0,0 +1,70 @@
<?php
final class SlowvotePollStatus
extends Phobject {
const STATUS_OPEN = 0;
const STATUS_CLOSED = 1;
private $key;
public static function newStatusObject($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::newStatusObject($key);
}
return $result;
}
public function getName() {
$name = $this->getProperty('name');
if ($name === null) {
$name = pht('Unknown ("%s")', $this->getKey());
}
return $name;
}
public function getHeaderTagIcon() {
return $this->getProperty('header.tag.icon');
}
public function getHeaderTagColor() {
return $this->getProperty('header.tag.color');
}
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::STATUS_OPEN => array(
'name' => pht('Open'),
'header.tag.icon' => 'fa-square-o',
'header.tag.color' => 'bluegrey',
),
self::STATUS_CLOSED => array(
'name' => pht('Closed'),
'header.tag.icon' => 'fa-ban',
'header.tag.color' => 'indigo',
),
);
}
}

View file

@ -23,10 +23,10 @@ final class PhabricatorSlowvoteCloseController
$close_uri = '/V'.$poll->getID(); $close_uri = '/V'.$poll->getID();
if ($request->isFormPost()) { if ($request->isFormPost()) {
if ($poll->getIsClosed()) { if ($poll->isClosed()) {
$new_status = 0; $new_status = SlowvotePollStatus::STATUS_OPEN;
} else { } else {
$new_status = 1; $new_status = SlowvotePollStatus::STATUS_CLOSED;
} }
$xactions = array(); $xactions = array();
@ -46,7 +46,7 @@ final class PhabricatorSlowvoteCloseController
return id(new AphrontRedirectResponse())->setURI($close_uri); return id(new AphrontRedirectResponse())->setURI($close_uri);
} }
if ($poll->getIsClosed()) { if ($poll->isClosed()) {
$title = pht('Reopen Poll'); $title = pht('Reopen Poll');
$content = pht('Are you sure you want to reopen the poll?'); $content = pht('Are you sure you want to reopen the poll?');
$submit = pht('Reopen'); $submit = pht('Reopen');

View file

@ -35,9 +35,11 @@ final class PhabricatorSlowvotePollController
)); ));
} }
$header_icon = $poll->getIsClosed() ? 'fa-ban' : 'fa-square-o'; $status = $poll->getStatusObject();
$header_name = $poll->getIsClosed() ? pht('Closed') : pht('Open');
$header_color = $poll->getIsClosed() ? 'indigo' : 'bluegrey'; $header_icon = $status->getHeaderTagIcon();
$header_color = $status->getHeaderTagColor();
$header_name = $status->getName();
$header = id(new PHUIHeaderView()) $header = id(new PHUIHeaderView())
->setHeader($poll->getQuestion()) ->setHeader($poll->getQuestion())
@ -50,7 +52,7 @@ final class PhabricatorSlowvotePollController
$subheader = $this->buildSubheaderView($poll); $subheader = $this->buildSubheaderView($poll);
$crumbs = $this->buildApplicationCrumbs(); $crumbs = $this->buildApplicationCrumbs();
$crumbs->addTextCrumb('V'.$poll->getID()); $crumbs->addTextCrumb($poll->getMonogram());
$crumbs->setBorder(true); $crumbs->setBorder(true);
$timeline = $this->buildTransactionTimeline( $timeline = $this->buildTransactionTimeline(
@ -71,7 +73,11 @@ final class PhabricatorSlowvotePollController
->setMainColumn($poll_content); ->setMainColumn($poll_content);
return $this->newPage() return $this->newPage()
->setTitle('V'.$poll->getID().' '.$poll->getQuestion()) ->setTitle(
pht(
'%s %s',
$poll->getMonogram(),
$poll->getQuestion()))
->setCrumbs($crumbs) ->setCrumbs($crumbs)
->setPageObjectPHIDs(array($poll->getPHID())) ->setPageObjectPHIDs(array($poll->getPHID()))
->appendChild($view); ->appendChild($view);
@ -87,7 +93,7 @@ final class PhabricatorSlowvotePollController
$curtain = $this->newCurtainView($poll); $curtain = $this->newCurtainView($poll);
$is_closed = $poll->getIsClosed(); $is_closed = $poll->isClosed();
$close_poll_text = $is_closed ? pht('Reopen Poll') : pht('Close Poll'); $close_poll_text = $is_closed ? pht('Reopen Poll') : pht('Close Poll');
$close_poll_icon = $is_closed ? 'fa-check' : 'fa-ban'; $close_poll_icon = $is_closed ? 'fa-check' : 'fa-ban';

View file

@ -21,7 +21,7 @@ final class PhabricatorSlowvoteVoteController
return new Aphront404Response(); return new Aphront404Response();
} }
if ($poll->getIsClosed()) { if ($poll->isClosed()) {
return new Aphront400Response(); return new Aphront400Response();
} }

View file

@ -142,7 +142,7 @@ final class PhabricatorSlowvoteSearchEngine
->setHref('/V'.$poll->getID()) ->setHref('/V'.$poll->getID())
->addIcon('none', $date_created); ->addIcon('none', $date_created);
if ($poll->getIsClosed()) { if ($poll->isClosed()) {
$item->setStatusIcon('fa-ban grey'); $item->setStatusIcon('fa-ban grey');
$item->setDisabled(true); $item->setDisabled(true);
} else { } else {

View file

@ -20,7 +20,7 @@ final class PhabricatorSlowvotePoll
protected $shuffle = 0; protected $shuffle = 0;
protected $method; protected $method;
protected $viewPolicy; protected $viewPolicy;
protected $isClosed = 0; protected $isClosed;
protected $spacePHID; protected $spacePHID;
private $options = self::ATTACHABLE; private $options = self::ATTACHABLE;
@ -43,6 +43,7 @@ final class PhabricatorSlowvotePoll
->setAuthorPHID($actor->getPHID()) ->setAuthorPHID($actor->getPHID())
->setViewPolicy($view_policy) ->setViewPolicy($view_policy)
->setSpacePHID($actor->getDefaultSpacePHID()) ->setSpacePHID($actor->getDefaultSpacePHID())
->setIsClosed(SlowvotePollStatus::STATUS_OPEN)
->setMethod($default_method) ->setMethod($default_method)
->setResponseVisibility($default_responses); ->setResponseVisibility($default_responses);
} }
@ -67,6 +68,14 @@ final class PhabricatorSlowvotePoll
return PhabricatorSlowvotePollPHIDType::TYPECONST; return PhabricatorSlowvotePollPHIDType::TYPECONST;
} }
public function getStatusObject() {
return SlowvotePollStatus::newStatusObject($this->getIsClosed());
}
public function isClosed() {
return ($this->getIsClosed() == SlowvotePollStatus::STATUS_CLOSED);
}
public function getOptions() { public function getOptions() {
return $this->assertAttached($this->options); return $this->assertAttached($this->options);
} }

View file

@ -95,7 +95,7 @@ final class SlowvoteEmbedView extends AphrontView {
), ),
$quip); $quip);
if ($poll->getIsClosed()) { if ($poll->isClosed()) {
$submit = null; $submit = null;
} else { } else {
$submit = phutil_tag( $submit = phutil_tag(
@ -228,7 +228,7 @@ final class SlowvoteEmbedView extends AphrontView {
SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox', SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox',
); );
$closed = $this->getPoll()->getIsClosed(); $closed = $this->getPoll()->isClosed();
return phutil_tag( return phutil_tag(
'input', 'input',