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())
18 ->needViewerChoices(true)
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
);
42 $message = pht('You must vote for something.');
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) {
55 pht('In this poll, you may only vote for one option.'));
58 foreach ($votes as $vote) {
59 if (!isset($options[$vote])) {
62 'Option ("%s") is not a valid poll option. You may only '.
63 'vote for valid options.',
68 $poll->openTransaction();
69 $poll->beginReadLocking();
73 $old_votes = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
74 'pollID = %d AND authorPHID = %s',
77 $old_votes = mpull($old_votes, null, 'getOptionID');
79 foreach ($old_votes as $old_vote) {
80 if (idx($votes, $old_vote->getOptionID())) {
87 foreach ($votes as $vote) {
88 if (idx($old_votes, $vote)) {
92 id(new PhabricatorSlowvoteChoice())
93 ->setAuthorPHID($viewer->getPHID())
94 ->setPollID($poll->getID())
99 $poll->endReadLocking();
100 $poll->saveTransaction();
102 return id(new AphrontRedirectResponse())
103 ->setURI($poll->getURI());