[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/slowvote/controller/ -> PhabricatorSlowvoteVoteController.php (source)

   1  <?php
   2  
   3  final class PhabricatorSlowvoteVoteController
   4    extends PhabricatorSlowvoteController {
   5  
   6    private $id;
   7  
   8    public function willProcessRequest(array $data) {
   9      $this->id = $data['id'];
  10    }
  11  
  12    public function processRequest() {
  13      $request = $this->getRequest();
  14      $user = $request->getUser();
  15  
  16      $poll = id(new PhabricatorSlowvoteQuery())
  17        ->setViewer($user)
  18        ->withIDs(array($this->id))
  19        ->needOptions(true)
  20        ->needViewerChoices(true)
  21        ->executeOne();
  22      if (!$poll) {
  23        return new Aphront404Response();
  24      }
  25      if ($poll->getIsClosed()) {
  26        return new Aphront400Response();
  27      }
  28  
  29      $options = $poll->getOptions();
  30      $user_choices = $poll->getViewerChoices($user);
  31  
  32      $old_votes = mpull($user_choices, null, 'getOptionID');
  33  
  34      if ($request->isAjax()) {
  35        $vote = $request->getInt('vote');
  36        $votes = array_keys($old_votes);
  37        $votes = array_fuse($votes, $votes);
  38  
  39        if ($poll->getMethod() == PhabricatorSlowvotePoll::METHOD_PLURALITY) {
  40          if (idx($votes, $vote, false)) {
  41            $votes = array();
  42          } else {
  43            $votes = array($vote);
  44          }
  45        } else {
  46          if (idx($votes, $vote, false)) {
  47            unset($votes[$vote]);
  48          } else {
  49            $votes[$vote] = $vote;
  50          }
  51        }
  52  
  53        $this->updateVotes($user, $poll, $old_votes, $votes);
  54  
  55        $updated_choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
  56          'pollID = %d AND authorPHID = %s',
  57          $poll->getID(),
  58          $user->getPHID());
  59  
  60        $embed = id(new SlowvoteEmbedView())
  61          ->setPoll($poll)
  62          ->setOptions($options)
  63          ->setViewerChoices($updated_choices);
  64  
  65        return id(new AphrontAjaxResponse())
  66          ->setContent(array(
  67            'pollID' => $poll->getID(),
  68            'contentHTML' => $embed->render(),
  69          ));
  70      }
  71  
  72      if (!$request->isFormPost()) {
  73        return id(new Aphront404Response());
  74      }
  75  
  76      $votes = $request->getArr('vote');
  77      $votes = array_fuse($votes, $votes);
  78  
  79      $this->updateVotes($user, $poll, $old_votes, $votes);
  80  
  81      return id(new AphrontRedirectResponse())->setURI('/V'.$poll->getID());
  82    }
  83  
  84    private function updateVotes($user, $poll, $old_votes, $votes) {
  85      if (!empty($votes) && count($votes) > 1 &&
  86          $poll->getMethod() == PhabricatorSlowvotePoll::METHOD_PLURALITY) {
  87        return id(new Aphront400Response());
  88      }
  89  
  90      foreach ($old_votes as $old_vote) {
  91        if (!idx($votes, $old_vote->getOptionID(), false)) {
  92          $old_vote->delete();
  93        }
  94      }
  95  
  96      foreach ($votes as $vote) {
  97        if (idx($old_votes, $vote, false)) {
  98          continue;
  99        }
 100  
 101        id(new PhabricatorSlowvoteChoice())
 102          ->setAuthorPHID($user->getPHID())
 103          ->setPollID($poll->getID())
 104          ->setOptionID($vote)
 105          ->save();
 106      }
 107    }
 108  
 109  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1