Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / slowvote / query / PhabricatorSlowvoteQuery.php
blobd2ae9f1a3535acf78b60f9c9b9728fa7535f614e
1 <?php
3 final class PhabricatorSlowvoteQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $authorPHIDs;
9 private $withVotesByViewer;
10 private $statuses;
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 withStatuses(array $statuses) {
37 $this->statuses = $statuses;
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 willFilterPage(array $polls) {
61 assert_instances_of($polls, 'PhabricatorSlowvotePoll');
63 $ids = mpull($polls, 'getID');
64 $viewer = $this->getViewer();
66 if ($this->needOptions) {
67 $options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
68 'pollID IN (%Ld)',
69 $ids);
71 $options = mgroup($options, 'getPollID');
72 foreach ($polls as $poll) {
73 $poll->attachOptions(idx($options, $poll->getID(), array()));
77 if ($this->needChoices) {
78 $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
79 'pollID IN (%Ld)',
80 $ids);
82 $choices = mgroup($choices, 'getPollID');
83 foreach ($polls as $poll) {
84 $poll->attachChoices(idx($choices, $poll->getID(), array()));
87 // If we need the viewer's choices, we can just fill them from the data
88 // we already loaded.
89 if ($this->needViewerChoices) {
90 foreach ($polls as $poll) {
91 $poll->attachViewerChoices(
92 $viewer,
93 idx(
94 mgroup($poll->getChoices(), 'getAuthorPHID'),
95 $viewer->getPHID(),
96 array()));
99 } else if ($this->needViewerChoices) {
100 $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
101 'pollID IN (%Ld) AND authorPHID = %s',
102 $ids,
103 $viewer->getPHID());
105 $choices = mgroup($choices, 'getPollID');
106 foreach ($polls as $poll) {
107 $poll->attachViewerChoices(
108 $viewer,
109 idx($choices, $poll->getID(), array()));
113 return $polls;
116 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
117 $where = parent::buildWhereClauseParts($conn);
119 if ($this->ids !== null) {
120 $where[] = qsprintf(
121 $conn,
122 'p.id IN (%Ld)',
123 $this->ids);
126 if ($this->phids !== null) {
127 $where[] = qsprintf(
128 $conn,
129 'p.phid IN (%Ls)',
130 $this->phids);
133 if ($this->authorPHIDs !== null) {
134 $where[] = qsprintf(
135 $conn,
136 'p.authorPHID IN (%Ls)',
137 $this->authorPHIDs);
140 if ($this->statuses !== null) {
141 $where[] = qsprintf(
142 $conn,
143 'p.status IN (%Ls)',
144 $this->statuses);
147 return $where;
150 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
151 $joins = parent::buildJoinClauseParts($conn);
153 if ($this->withVotesByViewer !== null) {
154 $joins[] = qsprintf(
155 $conn,
156 'JOIN %T vv ON vv.pollID = p.id AND vv.authorPHID = %s',
157 id(new PhabricatorSlowvoteChoice())->getTableName(),
158 $this->getViewer()->getPHID());
160 return $joins;
163 protected function getPrimaryTableAlias() {
164 return 'p';
167 public function getQueryApplicationClass() {
168 return 'PhabricatorSlowvoteApplication';