Remove product literal strings in "pht()", part 18
[phabricator.git] / src / applications / slowvote / controller / PhabricatorSlowvoteVoteController.php
blobe1a1b9df34193b510f0357a6b219161a227f8a52
1 <?php
3 final class PhabricatorSlowvoteVoteController
4 extends PhabricatorSlowvoteController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
8 $id = $request->getURIData('id');
10 if (!$request->isFormPost()) {
11 return id(new Aphront404Response());
14 $poll = id(new PhabricatorSlowvoteQuery())
15 ->setViewer($viewer)
16 ->withIDs(array($id))
17 ->needOptions(true)
18 ->needViewerChoices(true)
19 ->executeOne();
20 if (!$poll) {
21 return new Aphront404Response();
24 if ($poll->getIsClosed()) {
25 return new Aphront400Response();
28 $options = $poll->getOptions();
29 $options = mpull($options, null, 'getID');
31 $old_votes = $poll->getViewerChoices($viewer);
32 $old_votes = mpull($old_votes, null, 'getOptionID');
34 $votes = $request->getArr('vote');
35 $votes = array_fuse($votes);
37 $method = $poll->getMethod();
38 $is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY);
40 if (!$votes) {
41 if ($is_plurality) {
42 $message = pht('You must vote for something.');
43 } else {
44 $message = pht('You must vote for at least one option.');
47 return $this->newDialog()
48 ->setTitle(pht('Stand For Something'))
49 ->appendParagraph($message)
50 ->addCancelButton($poll->getURI());
53 if ($is_plurality && count($votes) > 1) {
54 throw new Exception(
55 pht('In this poll, you may only vote for one option.'));
58 foreach ($votes as $vote) {
59 if (!isset($options[$vote])) {
60 throw new Exception(
61 pht(
62 'Option ("%s") is not a valid poll option. You may only '.
63 'vote for valid options.',
64 $vote));
68 $poll->openTransaction();
69 $poll->beginReadLocking();
71 $poll->reload();
73 $old_votes = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
74 'pollID = %d AND authorPHID = %s',
75 $poll->getID(),
76 $viewer->getPHID());
77 $old_votes = mpull($old_votes, null, 'getOptionID');
79 foreach ($old_votes as $old_vote) {
80 if (idx($votes, $old_vote->getOptionID())) {
81 continue;
84 $old_vote->delete();
87 foreach ($votes as $vote) {
88 if (idx($old_votes, $vote)) {
89 continue;
92 id(new PhabricatorSlowvoteChoice())
93 ->setAuthorPHID($viewer->getPHID())
94 ->setPollID($poll->getID())
95 ->setOptionID($vote)
96 ->save();
99 $poll->endReadLocking();
100 $poll->saveTransaction();
102 return id(new AphrontRedirectResponse())
103 ->setURI($poll->getURI());