[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/slowvote/query/ -> PhabricatorSlowvoteQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorSlowvoteQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $authorPHIDs;
   9    private $withVotesByViewer;
  10    private $isClosed;
  11  
  12    private $needOptions;
  13    private $needChoices;
  14    private $needViewerChoices;
  15  
  16    public function withIDs($ids) {
  17      $this->ids = $ids;
  18      return $this;
  19    }
  20  
  21    public function withPHIDs($phids) {
  22      $this->phids = $phids;
  23      return $this;
  24    }
  25  
  26    public function withAuthorPHIDs($author_phids) {
  27      $this->authorPHIDs = $author_phids;
  28      return $this;
  29    }
  30  
  31    public function withVotesByViewer($with_vote) {
  32      $this->withVotesByViewer = $with_vote;
  33      return $this;
  34    }
  35  
  36    public function withIsClosed($with_closed) {
  37      $this->isClosed = $with_closed;
  38      return $this;
  39    }
  40  
  41    public function needOptions($need_options) {
  42      $this->needOptions = $need_options;
  43      return $this;
  44    }
  45  
  46    public function needChoices($need_choices) {
  47      $this->needChoices = $need_choices;
  48      return $this;
  49    }
  50  
  51    public function needViewerChoices($need_viewer_choices) {
  52      $this->needViewerChoices = $need_viewer_choices;
  53      return $this;
  54    }
  55  
  56    public function loadPage() {
  57      $table = new PhabricatorSlowvotePoll();
  58      $conn_r = $table->establishConnection('r');
  59  
  60      $data = queryfx_all(
  61        $conn_r,
  62        'SELECT p.* FROM %T p %Q %Q %Q %Q',
  63        $table->getTableName(),
  64        $this->buildJoinsClause($conn_r),
  65        $this->buildWhereClause($conn_r),
  66        $this->buildOrderClause($conn_r),
  67        $this->buildLimitClause($conn_r));
  68  
  69      return $table->loadAllFromArray($data);
  70    }
  71  
  72    public function willFilterPage(array $polls) {
  73      assert_instances_of($polls, 'PhabricatorSlowvotePoll');
  74  
  75      $ids = mpull($polls, 'getID');
  76      $viewer = $this->getViewer();
  77  
  78      if ($this->needOptions) {
  79        $options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
  80          'pollID IN (%Ld)',
  81          $ids);
  82  
  83        $options = mgroup($options, 'getPollID');
  84        foreach ($polls as $poll) {
  85          $poll->attachOptions(idx($options, $poll->getID(), array()));
  86        }
  87      }
  88  
  89      if ($this->needChoices) {
  90        $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
  91          'pollID IN (%Ld)',
  92          $ids);
  93  
  94        $choices = mgroup($choices, 'getPollID');
  95        foreach ($polls as $poll) {
  96          $poll->attachChoices(idx($choices, $poll->getID(), array()));
  97        }
  98  
  99        // If we need the viewer's choices, we can just fill them from the data
 100        // we already loaded.
 101        if ($this->needViewerChoices) {
 102          foreach ($polls as $poll) {
 103            $poll->attachViewerChoices(
 104              $viewer,
 105              idx(
 106                mgroup($poll->getChoices(), 'getAuthorPHID'),
 107                $viewer->getPHID(),
 108                array()));
 109          }
 110        }
 111      } else if ($this->needViewerChoices) {
 112        $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
 113          'pollID IN (%Ld) AND authorPHID = %s',
 114          $ids,
 115          $viewer->getPHID());
 116  
 117        $choices = mgroup($choices, 'getPollID');
 118        foreach ($polls as $poll) {
 119          $poll->attachViewerChoices(
 120            $viewer,
 121            idx($choices, $poll->getID(), array()));
 122        }
 123      }
 124  
 125      return $polls;
 126    }
 127  
 128    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 129      $where = array();
 130  
 131      if ($this->ids) {
 132        $where[] = qsprintf(
 133          $conn_r,
 134          'p.id IN (%Ld)',
 135          $this->ids);
 136      }
 137  
 138      if ($this->phids) {
 139        $where[] = qsprintf(
 140          $conn_r,
 141          'p.phid IN (%Ls)',
 142          $this->phids);
 143      }
 144  
 145      if ($this->authorPHIDs) {
 146        $where[] = qsprintf(
 147          $conn_r,
 148          'p.authorPHID IN (%Ls)',
 149          $this->authorPHIDs);
 150      }
 151  
 152      if ($this->isClosed !== null) {
 153        $where[] = qsprintf(
 154          $conn_r,
 155          'p.isClosed = %d',
 156          (int)$this->isClosed);
 157      }
 158  
 159      $where[] = $this->buildPagingClause($conn_r);
 160      return $this->formatWhereClause($where);
 161    }
 162  
 163    private function buildJoinsClause(AphrontDatabaseConnection $conn_r) {
 164      $joins = array();
 165  
 166      if ($this->withVotesByViewer) {
 167        $joins[] = qsprintf(
 168          $conn_r,
 169          'JOIN %T vv ON vv.pollID = p.id AND vv.authorPHID = %s',
 170          id(new PhabricatorSlowvoteChoice())->getTableName(),
 171          $this->getViewer()->getPHID());
 172      }
 173  
 174      return implode(' ', $joins);
 175    }
 176  
 177    protected function getPagingColumn() {
 178      return 'p.id';
 179    }
 180  
 181    public function getQueryApplicationClass() {
 182      return 'PhabricatorSlowvoteApplication';
 183    }
 184  
 185  }


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