Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / slowvote / query / PhabricatorSlowvoteQuery.php
blob0b7335326c9f148d65e836e931c84b4d0282a73d
1 <?php
3 final class PhabricatorSlowvoteQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $authorPHIDs;
9 private $withVotesByViewer;
10 private $isClosed;
12 private $needOptions;
13 private $needChoices;
14 private $needViewerChoices;
16 public function withIDs($ids) {
17 $this->ids = $ids;
18 return $this;
21 public function withPHIDs($phids) {
22 $this->phids = $phids;
23 return $this;
26 public function withAuthorPHIDs($author_phids) {
27 $this->authorPHIDs = $author_phids;
28 return $this;
31 public function withVotesByViewer($with_vote) {
32 $this->withVotesByViewer = $with_vote;
33 return $this;
36 public function withIsClosed($with_closed) {
37 $this->isClosed = $with_closed;
38 return $this;
41 public function needOptions($need_options) {
42 $this->needOptions = $need_options;
43 return $this;
46 public function needChoices($need_choices) {
47 $this->needChoices = $need_choices;
48 return $this;
51 public function needViewerChoices($need_viewer_choices) {
52 $this->needViewerChoices = $need_viewer_choices;
53 return $this;
56 public function newResultObject() {
57 return new PhabricatorSlowvotePoll();
60 protected function loadPage() {
61 return $this->loadStandardPage($this->newResultObject());
64 protected function willFilterPage(array $polls) {
65 assert_instances_of($polls, 'PhabricatorSlowvotePoll');
67 $ids = mpull($polls, 'getID');
68 $viewer = $this->getViewer();
70 if ($this->needOptions) {
71 $options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
72 'pollID IN (%Ld)',
73 $ids);
75 $options = mgroup($options, 'getPollID');
76 foreach ($polls as $poll) {
77 $poll->attachOptions(idx($options, $poll->getID(), array()));
81 if ($this->needChoices) {
82 $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
83 'pollID IN (%Ld)',
84 $ids);
86 $choices = mgroup($choices, 'getPollID');
87 foreach ($polls as $poll) {
88 $poll->attachChoices(idx($choices, $poll->getID(), array()));
91 // If we need the viewer's choices, we can just fill them from the data
92 // we already loaded.
93 if ($this->needViewerChoices) {
94 foreach ($polls as $poll) {
95 $poll->attachViewerChoices(
96 $viewer,
97 idx(
98 mgroup($poll->getChoices(), 'getAuthorPHID'),
99 $viewer->getPHID(),
100 array()));
103 } else if ($this->needViewerChoices) {
104 $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
105 'pollID IN (%Ld) AND authorPHID = %s',
106 $ids,
107 $viewer->getPHID());
109 $choices = mgroup($choices, 'getPollID');
110 foreach ($polls as $poll) {
111 $poll->attachViewerChoices(
112 $viewer,
113 idx($choices, $poll->getID(), array()));
117 return $polls;
120 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
121 $where = parent::buildWhereClauseParts($conn);
123 if ($this->ids !== null) {
124 $where[] = qsprintf(
125 $conn,
126 'p.id IN (%Ld)',
127 $this->ids);
130 if ($this->phids !== null) {
131 $where[] = qsprintf(
132 $conn,
133 'p.phid IN (%Ls)',
134 $this->phids);
137 if ($this->authorPHIDs !== null) {
138 $where[] = qsprintf(
139 $conn,
140 'p.authorPHID IN (%Ls)',
141 $this->authorPHIDs);
144 if ($this->isClosed !== null) {
145 $where[] = qsprintf(
146 $conn,
147 'p.isClosed = %d',
148 (int)$this->isClosed);
150 return $where;
153 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
154 $joins = parent::buildJoinClauseParts($conn);
156 if ($this->withVotesByViewer !== null) {
157 $joins[] = qsprintf(
158 $conn,
159 'JOIN %T vv ON vv.pollID = p.id AND vv.authorPHID = %s',
160 id(new PhabricatorSlowvoteChoice())->getTableName(),
161 $this->getViewer()->getPHID());
163 return $joins;
166 protected function getPrimaryTableAlias() {
167 return 'p';
170 public function getQueryApplicationClass() {
171 return 'PhabricatorSlowvoteApplication';