From c1887f0c1d55524e6af49e9fa245b71e2514ae6a Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 26 May 2022 10:35:38 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 2 + .../slowvote/constants/SlowvotePollStatus.php | 70 +++++++++++++++++++ .../PhabricatorSlowvoteCloseController.php | 8 +-- .../PhabricatorSlowvotePollController.php | 18 +++-- .../PhabricatorSlowvoteVoteController.php | 2 +- .../query/PhabricatorSlowvoteSearchEngine.php | 2 +- .../storage/PhabricatorSlowvotePoll.php | 11 ++- .../slowvote/view/SlowvoteEmbedView.php | 4 +- 8 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 src/applications/slowvote/constants/SlowvotePollStatus.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 44cf25ee13..c88c8d6ab5 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -5874,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', + 'SlowvotePollStatus' => 'applications/slowvote/constants/SlowvotePollStatus.php', 'SlowvotePollVotingMethod' => 'applications/slowvote/constants/SlowvotePollVotingMethod.php', 'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php', 'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php', @@ -12778,6 +12779,7 @@ phutil_register_library_map(array( 'SlowvoteEmbedView' => 'AphrontView', 'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod', 'SlowvotePollResponseVisibility' => 'Phobject', + 'SlowvotePollStatus' => 'Phobject', 'SlowvotePollVotingMethod' => 'Phobject', 'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule', 'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod', diff --git a/src/applications/slowvote/constants/SlowvotePollStatus.php b/src/applications/slowvote/constants/SlowvotePollStatus.php new file mode 100644 index 0000000000..5df0511aba --- /dev/null +++ b/src/applications/slowvote/constants/SlowvotePollStatus.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::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', + ), + ); + } + +} diff --git a/src/applications/slowvote/controller/PhabricatorSlowvoteCloseController.php b/src/applications/slowvote/controller/PhabricatorSlowvoteCloseController.php index a4da0f7f7d..115592cae8 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteCloseController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteCloseController.php @@ -23,10 +23,10 @@ final class PhabricatorSlowvoteCloseController $close_uri = '/V'.$poll->getID(); if ($request->isFormPost()) { - if ($poll->getIsClosed()) { - $new_status = 0; + if ($poll->isClosed()) { + $new_status = SlowvotePollStatus::STATUS_OPEN; } else { - $new_status = 1; + $new_status = SlowvotePollStatus::STATUS_CLOSED; } $xactions = array(); @@ -46,7 +46,7 @@ final class PhabricatorSlowvoteCloseController return id(new AphrontRedirectResponse())->setURI($close_uri); } - if ($poll->getIsClosed()) { + if ($poll->isClosed()) { $title = pht('Reopen Poll'); $content = pht('Are you sure you want to reopen the poll?'); $submit = pht('Reopen'); diff --git a/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php b/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php index 1ffab17791..7ed83fb13f 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php @@ -35,9 +35,11 @@ final class PhabricatorSlowvotePollController )); } - $header_icon = $poll->getIsClosed() ? 'fa-ban' : 'fa-square-o'; - $header_name = $poll->getIsClosed() ? pht('Closed') : pht('Open'); - $header_color = $poll->getIsClosed() ? 'indigo' : 'bluegrey'; + $status = $poll->getStatusObject(); + + $header_icon = $status->getHeaderTagIcon(); + $header_color = $status->getHeaderTagColor(); + $header_name = $status->getName(); $header = id(new PHUIHeaderView()) ->setHeader($poll->getQuestion()) @@ -50,7 +52,7 @@ final class PhabricatorSlowvotePollController $subheader = $this->buildSubheaderView($poll); $crumbs = $this->buildApplicationCrumbs(); - $crumbs->addTextCrumb('V'.$poll->getID()); + $crumbs->addTextCrumb($poll->getMonogram()); $crumbs->setBorder(true); $timeline = $this->buildTransactionTimeline( @@ -71,7 +73,11 @@ final class PhabricatorSlowvotePollController ->setMainColumn($poll_content); return $this->newPage() - ->setTitle('V'.$poll->getID().' '.$poll->getQuestion()) + ->setTitle( + pht( + '%s %s', + $poll->getMonogram(), + $poll->getQuestion())) ->setCrumbs($crumbs) ->setPageObjectPHIDs(array($poll->getPHID())) ->appendChild($view); @@ -87,7 +93,7 @@ final class PhabricatorSlowvotePollController $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_icon = $is_closed ? 'fa-check' : 'fa-ban'; diff --git a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php index cd6205ce61..8c70c2833b 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php @@ -21,7 +21,7 @@ final class PhabricatorSlowvoteVoteController return new Aphront404Response(); } - if ($poll->getIsClosed()) { + if ($poll->isClosed()) { return new Aphront400Response(); } diff --git a/src/applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php b/src/applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php index 44bfb917c2..2905161323 100644 --- a/src/applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php +++ b/src/applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php @@ -142,7 +142,7 @@ final class PhabricatorSlowvoteSearchEngine ->setHref('/V'.$poll->getID()) ->addIcon('none', $date_created); - if ($poll->getIsClosed()) { + if ($poll->isClosed()) { $item->setStatusIcon('fa-ban grey'); $item->setDisabled(true); } else { diff --git a/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php b/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php index 7c44f5ea4d..07cd332e34 100644 --- a/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php +++ b/src/applications/slowvote/storage/PhabricatorSlowvotePoll.php @@ -20,7 +20,7 @@ final class PhabricatorSlowvotePoll protected $shuffle = 0; protected $method; protected $viewPolicy; - protected $isClosed = 0; + protected $isClosed; protected $spacePHID; private $options = self::ATTACHABLE; @@ -43,6 +43,7 @@ final class PhabricatorSlowvotePoll ->setAuthorPHID($actor->getPHID()) ->setViewPolicy($view_policy) ->setSpacePHID($actor->getDefaultSpacePHID()) + ->setIsClosed(SlowvotePollStatus::STATUS_OPEN) ->setMethod($default_method) ->setResponseVisibility($default_responses); } @@ -67,6 +68,14 @@ final class PhabricatorSlowvotePoll return PhabricatorSlowvotePollPHIDType::TYPECONST; } + public function getStatusObject() { + return SlowvotePollStatus::newStatusObject($this->getIsClosed()); + } + + public function isClosed() { + return ($this->getIsClosed() == SlowvotePollStatus::STATUS_CLOSED); + } + public function getOptions() { return $this->assertAttached($this->options); } diff --git a/src/applications/slowvote/view/SlowvoteEmbedView.php b/src/applications/slowvote/view/SlowvoteEmbedView.php index fcc0ae9d28..323801de47 100644 --- a/src/applications/slowvote/view/SlowvoteEmbedView.php +++ b/src/applications/slowvote/view/SlowvoteEmbedView.php @@ -95,7 +95,7 @@ final class SlowvoteEmbedView extends AphrontView { ), $quip); - if ($poll->getIsClosed()) { + if ($poll->isClosed()) { $submit = null; } else { $submit = phutil_tag( @@ -228,7 +228,7 @@ final class SlowvoteEmbedView extends AphrontView { SlowvotePollVotingMethod::METHOD_APPROVAL => 'checkbox', ); - $closed = $this->getPoll()->getIsClosed(); + $closed = $this->getPoll()->isClosed(); return phutil_tag( 'input',