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