From aa2e6d1e5a4e5fe8ea726bbc38e643c83dd8467e Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 15 Jul 2013 10:50:38 -0700 Subject: [PATCH] Make most slowvote fields edit transactionally Summary: No support for responses yet, since they're more complicated, but put everything else on the transactions plan. This also prepares polls for editability, shortly. Test Plan: {F50205} Reviewers: btrahan Reviewed By: btrahan CC: aran Differential Revision: https://secure.phabricator.com/D6460 --- src/__phutil_library_map__.php | 6 +- .../PhabricatorApplicationSlowvote.php | 2 +- ... => PhabricatorSlowvoteEditController.php} | 72 +++++++++++--- .../PhabricatorSlowvotePollController.php | 2 +- .../editor/PhabricatorSlowvoteEditor.php | 31 ++++++ .../PhabricatorSlowvoteTransaction.php | 95 ++++++++++++++++++- 6 files changed, 188 insertions(+), 20 deletions(-) rename src/applications/slowvote/controller/{PhabricatorSlowvoteCreateController.php => PhabricatorSlowvoteEditController.php} (66%) diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 4ceb86c2c8..08a8db19cb 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1529,8 +1529,8 @@ phutil_register_library_map(array( 'PhabricatorSlowvoteComment' => 'applications/slowvote/storage/PhabricatorSlowvoteComment.php', 'PhabricatorSlowvoteCommentController' => 'applications/slowvote/controller/PhabricatorSlowvoteCommentController.php', 'PhabricatorSlowvoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteController.php', - 'PhabricatorSlowvoteCreateController' => 'applications/slowvote/controller/PhabricatorSlowvoteCreateController.php', 'PhabricatorSlowvoteDAO' => 'applications/slowvote/storage/PhabricatorSlowvoteDAO.php', + 'PhabricatorSlowvoteEditController' => 'applications/slowvote/controller/PhabricatorSlowvoteEditController.php', 'PhabricatorSlowvoteEditor' => 'applications/slowvote/editor/PhabricatorSlowvoteEditor.php', 'PhabricatorSlowvoteListController' => 'applications/slowvote/controller/PhabricatorSlowvoteListController.php', 'PhabricatorSlowvoteOption' => 'applications/slowvote/storage/PhabricatorSlowvoteOption.php', @@ -3490,8 +3490,8 @@ phutil_register_library_map(array( 'PhabricatorSlowvoteComment' => 'PhabricatorSlowvoteDAO', 'PhabricatorSlowvoteCommentController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteController' => 'PhabricatorController', - 'PhabricatorSlowvoteCreateController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteDAO' => 'PhabricatorLiskDAO', + 'PhabricatorSlowvoteEditController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteEditor' => 'PhabricatorApplicationTransactionEditor', 'PhabricatorSlowvoteListController' => array( @@ -3503,6 +3503,8 @@ phutil_register_library_map(array( array( 0 => 'PhabricatorSlowvoteDAO', 1 => 'PhabricatorPolicyInterface', + 2 => 'PhabricatorSubscribableInterface', + 3 => 'PhabricatorTokenReceiverInterface', ), 'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', diff --git a/src/applications/slowvote/application/PhabricatorApplicationSlowvote.php b/src/applications/slowvote/application/PhabricatorApplicationSlowvote.php index 35c7c78fa2..c1ee68c855 100644 --- a/src/applications/slowvote/application/PhabricatorApplicationSlowvote.php +++ b/src/applications/slowvote/application/PhabricatorApplicationSlowvote.php @@ -42,7 +42,7 @@ final class PhabricatorApplicationSlowvote extends PhabricatorApplication { '/vote/' => array( '(?:query/(?P[^/]+)/)?' => 'PhabricatorSlowvoteListController', - 'create/' => 'PhabricatorSlowvoteCreateController', + 'create/' => 'PhabricatorSlowvoteEditController', '(?P[1-9]\d*)/' => 'PhabricatorSlowvoteVoteController', 'comment/(?P[1-9]\d*)/' => 'PhabricatorSlowvoteCommentController', ), diff --git a/src/applications/slowvote/controller/PhabricatorSlowvoteCreateController.php b/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php similarity index 66% rename from src/applications/slowvote/controller/PhabricatorSlowvoteCreateController.php rename to src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php index 668042c2be..417a755324 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteCreateController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php @@ -3,9 +3,15 @@ /** * @group slowvote */ -final class PhabricatorSlowvoteCreateController +final class PhabricatorSlowvoteEditController extends PhabricatorSlowvoteController { + private $id; + + public function willProcessRequest(array $data) { + $this->id = idx($data, 'id'); + } + public function processRequest() { $request = $this->getRequest(); @@ -13,23 +19,31 @@ final class PhabricatorSlowvoteCreateController $poll = new PhabricatorSlowvotePoll(); $poll->setAuthorPHID($user->getPHID()); + $poll->setViewPolicy(PhabricatorPolicies::POLICY_USER); + + $is_new = true; $e_question = true; $e_response = true; $errors = array(); + $v_question = $poll->getQuestion(); + $v_description = $poll->getDescription(); + $v_responses = $poll->getResponseVisibility(); + $v_shuffle = $poll->getShuffle(); + $responses = $request->getArr('response'); - if ($request->isFormPost()) { - $poll->setQuestion($request->getStr('question')); - $poll->setResponseVisibility($request->getInt('response_visibility')); - $poll->setShuffle((int)$request->getBool('shuffle', false)); - $poll->setMethod($request->getInt('method')); + $v_question = $request->getStr('question'); + $v_description = $request->getStr('description'); + $v_responses = $request->getInt('responses'); + $v_shuffle = (int)$request->getBool('shuffle'); - $poll->setDescription(''); - $poll->setViewPolicy(PhabricatorPolicies::POLICY_USER); + if ($is_new) { + $poll->setMethod($request->getInt('method')); + } - if (!strlen($poll->getQuestion())) { + if (!strlen($v_question)) { $e_question = pht('Required'); $errors[] = pht('You must ask a poll question.'); } else { @@ -44,7 +58,32 @@ final class PhabricatorSlowvoteCreateController $e_response = null; } + $xactions = array(); + $template = id(new PhabricatorSlowvoteTransaction()); + + $xactions[] = id(clone $template) + ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_QUESTION) + ->setNewValue($v_question); + + $xactions[] = id(clone $template) + ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION) + ->setNewValue($v_description); + + $xactions[] = id(clone $template) + ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_RESPONSES) + ->setNewValue($v_responses); + + $xactions[] = id(clone $template) + ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_SHUFFLE) + ->setNewValue($v_shuffle); + if (empty($errors)) { + $editor = id(new PhabricatorSlowvoteEditor()) + ->setActor($user) + ->setContinueOnNoEffect(true) + ->setContentSourceFromRequest($request) + ->applyTransactions($poll, $xactions); + $poll->save(); foreach ($responses as $response) { @@ -84,8 +123,13 @@ final class PhabricatorSlowvoteCreateController ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT) ->setLabel(pht('Question')) ->setName('question') - ->setValue($poll->getQuestion()) - ->setError($e_question)); + ->setValue($v_question) + ->setError($e_question)) + ->appendChild( + id(new PhabricatorRemarkupControl()) + ->setLabel(pht('Description')) + ->setName('description') + ->setValue($v_description)); for ($ii = 0; $ii < 10; $ii++) { $n = ($ii + 1); @@ -127,8 +171,8 @@ final class PhabricatorSlowvoteCreateController ->appendChild( id(new AphrontFormSelectControl()) ->setLabel(pht('Responses')) - ->setName('response_visibility') - ->setValue($poll->getResponseVisibility()) + ->setName('responses') + ->setValue($v_responses) ->setOptions($response_type_options)) ->appendChild( id(new AphrontFormCheckboxControl()) @@ -137,7 +181,7 @@ final class PhabricatorSlowvoteCreateController 'shuffle', 1, pht('Show choices in random order.'), - $poll->getShuffle())) + $v_shuffle)) ->appendChild( id(new AphrontFormSubmitControl()) ->setValue(pht('Create Slowvote')) diff --git a/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php b/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php index 923d9f79d4..134c6e28d0 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php @@ -391,7 +391,7 @@ final class PhabricatorSlowvotePollController $view->invokeWillRenderEvent(); if (strlen($poll->getDescription())) { - $view->addTextSection( + $view->addTextContent( $output = PhabricatorMarkupEngine::renderOneObject( id(new PhabricatorMarkupOneOff())->setContent( $poll->getDescription()), diff --git a/src/applications/slowvote/editor/PhabricatorSlowvoteEditor.php b/src/applications/slowvote/editor/PhabricatorSlowvoteEditor.php index c1c4441af1..94c0a27ff0 100644 --- a/src/applications/slowvote/editor/PhabricatorSlowvoteEditor.php +++ b/src/applications/slowvote/editor/PhabricatorSlowvoteEditor.php @@ -7,6 +7,12 @@ final class PhabricatorSlowvoteEditor $types = parent::getTransactionTypes(); $types[] = PhabricatorTransactions::TYPE_COMMENT; + $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY; + + $types[] = PhabricatorSlowvoteTransaction::TYPE_QUESTION; + $types[] = PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION; + $types[] = PhabricatorSlowvoteTransaction::TYPE_RESPONSES; + $types[] = PhabricatorSlowvoteTransaction::TYPE_SHUFFLE; return $types; } @@ -16,6 +22,14 @@ final class PhabricatorSlowvoteEditor PhabricatorApplicationTransaction $xaction) { switch ($xaction->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_QUESTION: + return $object->getQuestion(); + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + return $object->getDescription(); + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + return $object->getResponseVisibility(); + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + return $object->getShuffle(); } } @@ -24,6 +38,11 @@ final class PhabricatorSlowvoteEditor PhabricatorApplicationTransaction $xaction) { switch ($xaction->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_QUESTION: + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + return $xaction->getNewValue(); } } @@ -32,6 +51,18 @@ final class PhabricatorSlowvoteEditor PhabricatorApplicationTransaction $xaction) { switch ($xaction->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_QUESTION: + $object->setQuestion($xaction->getNewValue()); + break; + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + $object->setDescription($xaction->getNewValue()); + break; + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + $object->setResponseVisibility($xaction->getNewValue()); + break; + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + $object->setShuffle($xaction->getNewValue()); + break; } } diff --git a/src/applications/slowvote/storage/PhabricatorSlowvoteTransaction.php b/src/applications/slowvote/storage/PhabricatorSlowvoteTransaction.php index ec58ebfe9f..35b871b6b2 100644 --- a/src/applications/slowvote/storage/PhabricatorSlowvoteTransaction.php +++ b/src/applications/slowvote/storage/PhabricatorSlowvoteTransaction.php @@ -3,9 +3,10 @@ final class PhabricatorSlowvoteTransaction extends PhabricatorApplicationTransaction { - const TYPE_NAME = 'vote:name'; + const TYPE_QUESTION = 'vote:question'; const TYPE_DESCRIPTION = 'vote:description'; - const TYPE_OPTION = 'vote:option'; + const TYPE_RESPONSES = 'vote:responses'; + const TYPE_SHUFFLE = 'vote:shuffle'; public function getApplicationName() { return 'slowvote'; @@ -23,5 +24,95 @@ final class PhabricatorSlowvoteTransaction return pht('vote'); } + public function shouldHide() { + $old = $this->getOldValue(); + $new = $this->getNewValue(); + + switch ($this->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + return ($old === null); + } + + return parent::shouldHide(); + } + + public function getTitle() { + $author_phid = $this->getAuthorPHID(); + + $old = $this->getOldValue(); + $new = $this->getNewValue(); + + switch ($this->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_QUESTION: + if ($old === null) { + return pht( + '%s created this poll.', + $this->renderHandleLink($author_phid)); + } else { + return pht( + '%s changed the poll question from "%s" to "%s".', + $this->renderHandleLink($author_phid), + $old, + $new); + } + break; + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + return pht( + '%s updated the description for this poll.', + $this->renderHandleLink($author_phid)); + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + // TODO: This could be more detailed + return pht( + '%s changed who can see the responses.', + $this->renderHandleLink($author_phid)); + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + if ($new) { + return pht( + '%s made poll responses appear in a random order.', + $this->renderHandleLink($author_phid)); + } else { + return pht( + '%s made poll responses appear in a fixed order.', + $this->renderHandleLink($author_phid)); + } + break; + } + + return parent::getTitle(); + } + + public function getIcon() { + $old = $this->getOldValue(); + $new = $this->getNewValue(); + + switch ($this->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_QUESTION: + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + return 'edit'; + } + + return parent::getIcon(); + } + + + public function getColor() { + $old = $this->getOldValue(); + $new = $this->getNewValue(); + + switch ($this->getTransactionType()) { + case PhabricatorSlowvoteTransaction::TYPE_QUESTION: + case PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION: + case PhabricatorSlowvoteTransaction::TYPE_RESPONSES: + case PhabricatorSlowvoteTransaction::TYPE_SHUFFLE: + return PhabricatorTransactions::COLOR_BLUE; + } + + return parent::getColor(); + } + }