Differential D20125 Diff 48051 src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php
Show All 31 Lines | public function handleRequest(AphrontRequest $request) { | ||||
$old_votes = mpull($old_votes, null, 'getOptionID'); | $old_votes = mpull($old_votes, null, 'getOptionID'); | ||||
$votes = $request->getArr('vote'); | $votes = $request->getArr('vote'); | ||||
$votes = array_fuse($votes); | $votes = array_fuse($votes); | ||||
$method = $poll->getMethod(); | $method = $poll->getMethod(); | ||||
$is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY); | $is_plurality = ($method == PhabricatorSlowvotePoll::METHOD_PLURALITY); | ||||
if (!$votes) { | |||||
if ($is_plurality) { | |||||
$message = pht('You must vote for something.'); | |||||
} else { | |||||
$message = pht('You must vote for at least one option.'); | |||||
} | |||||
return $this->newDialog() | |||||
->setTitle(pht('Stand For Something')) | |||||
->appendParagraph($message) | |||||
->addCancelButton($poll->getURI()); | |||||
} | |||||
if ($is_plurality && count($votes) > 1) { | if ($is_plurality && count($votes) > 1) { | ||||
throw new Exception( | throw new Exception( | ||||
pht('In this poll, you may only vote for one option.')); | pht('In this poll, you may only vote for one option.')); | ||||
} | } | ||||
foreach ($votes as $vote) { | foreach ($votes as $vote) { | ||||
if (!isset($options[$vote])) { | if (!isset($options[$vote])) { | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'Option ("%s") is not a valid poll option. You may only '. | 'Option ("%s") is not a valid poll option. You may only '. | ||||
'vote for valid options.', | 'vote for valid options.', | ||||
$vote)); | $vote)); | ||||
} | } | ||||
} | } | ||||
$poll->openTransaction(); | |||||
$poll->beginReadLocking(); | |||||
$poll->reload(); | |||||
$old_votes = id(new PhabricatorSlowvoteChoice())->loadAllWhere( | |||||
'pollID = %d AND authorPHID = %s', | |||||
$poll->getID(), | |||||
$viewer->getPHID()); | |||||
$old_votes = mpull($old_votes, null, 'getOptionID'); | |||||
foreach ($old_votes as $old_vote) { | foreach ($old_votes as $old_vote) { | ||||
if (!idx($votes, $old_vote->getOptionID(), false)) { | if (idx($votes, $old_vote->getOptionID())) { | ||||
$old_vote->delete(); | continue; | ||||
} | } | ||||
$old_vote->delete(); | |||||
} | } | ||||
foreach ($votes as $vote) { | foreach ($votes as $vote) { | ||||
if (idx($old_votes, $vote, false)) { | if (idx($old_votes, $vote)) { | ||||
continue; | continue; | ||||
} | } | ||||
id(new PhabricatorSlowvoteChoice()) | id(new PhabricatorSlowvoteChoice()) | ||||
->setAuthorPHID($viewer->getPHID()) | ->setAuthorPHID($viewer->getPHID()) | ||||
->setPollID($poll->getID()) | ->setPollID($poll->getID()) | ||||
->setOptionID($vote) | ->setOptionID($vote) | ||||
->save(); | ->save(); | ||||
} | } | ||||
$poll->endReadLocking(); | |||||
$poll->saveTransaction(); | |||||
return id(new AphrontRedirectResponse()) | return id(new AphrontRedirectResponse()) | ||||
->setURI($poll->getURI()); | ->setURI($poll->getURI()); | ||||
} | } | ||||
} | } |