Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/legalpad/query/LegalpadDocumentQuery.php
13450 views
1
<?php
2
3
final class LegalpadDocumentQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $ids;
7
private $phids;
8
private $creatorPHIDs;
9
private $contributorPHIDs;
10
private $signerPHIDs;
11
private $dateCreatedAfter;
12
private $dateCreatedBefore;
13
private $signatureRequired;
14
15
private $needDocumentBodies;
16
private $needContributors;
17
private $needSignatures;
18
private $needViewerSignatures;
19
20
public function withIDs(array $ids) {
21
$this->ids = $ids;
22
return $this;
23
}
24
25
public function withPHIDs(array $phids) {
26
$this->phids = $phids;
27
return $this;
28
}
29
30
public function withCreatorPHIDs(array $phids) {
31
$this->creatorPHIDs = $phids;
32
return $this;
33
}
34
35
public function withContributorPHIDs(array $phids) {
36
$this->contributorPHIDs = $phids;
37
return $this;
38
}
39
40
public function withSignerPHIDs(array $phids) {
41
$this->signerPHIDs = $phids;
42
return $this;
43
}
44
45
public function withSignatureRequired($bool) {
46
$this->signatureRequired = $bool;
47
return $this;
48
}
49
50
public function needDocumentBodies($need_bodies) {
51
$this->needDocumentBodies = $need_bodies;
52
return $this;
53
}
54
55
public function needContributors($need_contributors) {
56
$this->needContributors = $need_contributors;
57
return $this;
58
}
59
60
public function needSignatures($need_signatures) {
61
$this->needSignatures = $need_signatures;
62
return $this;
63
}
64
65
public function withDateCreatedBefore($date_created_before) {
66
$this->dateCreatedBefore = $date_created_before;
67
return $this;
68
}
69
70
public function withDateCreatedAfter($date_created_after) {
71
$this->dateCreatedAfter = $date_created_after;
72
return $this;
73
}
74
75
public function needViewerSignatures($need) {
76
$this->needViewerSignatures = $need;
77
return $this;
78
}
79
80
public function newResultObject() {
81
return new LegalpadDocument();
82
}
83
84
protected function willFilterPage(array $documents) {
85
if ($this->needDocumentBodies) {
86
$documents = $this->loadDocumentBodies($documents);
87
}
88
89
if ($this->needContributors) {
90
$documents = $this->loadContributors($documents);
91
}
92
93
if ($this->needSignatures) {
94
$documents = $this->loadSignatures($documents);
95
}
96
97
if ($this->needViewerSignatures) {
98
if ($documents) {
99
if ($this->getViewer()->getPHID()) {
100
$signatures = id(new LegalpadDocumentSignatureQuery())
101
->setViewer($this->getViewer())
102
->withSignerPHIDs(array($this->getViewer()->getPHID()))
103
->withDocumentPHIDs(mpull($documents, 'getPHID'))
104
->execute();
105
$signatures = mpull($signatures, null, 'getDocumentPHID');
106
} else {
107
$signatures = array();
108
}
109
110
foreach ($documents as $document) {
111
$signature = idx($signatures, $document->getPHID());
112
$document->attachUserSignature(
113
$this->getViewer()->getPHID(),
114
$signature);
115
}
116
}
117
}
118
119
return $documents;
120
}
121
122
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
123
$joins = parent::buildJoinClauseParts($conn);
124
125
if ($this->contributorPHIDs !== null) {
126
$joins[] = qsprintf(
127
$conn,
128
'JOIN edge contributor ON contributor.src = d.phid
129
AND contributor.type = %d',
130
PhabricatorObjectHasContributorEdgeType::EDGECONST);
131
}
132
133
if ($this->signerPHIDs !== null) {
134
$joins[] = qsprintf(
135
$conn,
136
'JOIN %T signer ON signer.documentPHID = d.phid
137
AND signer.signerPHID IN (%Ls)',
138
id(new LegalpadDocumentSignature())->getTableName(),
139
$this->signerPHIDs);
140
}
141
142
return $joins;
143
}
144
145
protected function shouldGroupQueryResultRows() {
146
if ($this->contributorPHIDs) {
147
return true;
148
}
149
150
if ($this->signerPHIDs) {
151
return true;
152
}
153
154
return parent::shouldGroupQueryResultRows();
155
}
156
157
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
158
$where = parent::buildWhereClauseParts($conn);
159
160
if ($this->ids !== null) {
161
$where[] = qsprintf(
162
$conn,
163
'd.id IN (%Ld)',
164
$this->ids);
165
}
166
167
if ($this->phids !== null) {
168
$where[] = qsprintf(
169
$conn,
170
'd.phid IN (%Ls)',
171
$this->phids);
172
}
173
174
if ($this->creatorPHIDs !== null) {
175
$where[] = qsprintf(
176
$conn,
177
'd.creatorPHID IN (%Ls)',
178
$this->creatorPHIDs);
179
}
180
181
if ($this->dateCreatedAfter !== null) {
182
$where[] = qsprintf(
183
$conn,
184
'd.dateCreated >= %d',
185
$this->dateCreatedAfter);
186
}
187
188
if ($this->dateCreatedBefore !== null) {
189
$where[] = qsprintf(
190
$conn,
191
'd.dateCreated <= %d',
192
$this->dateCreatedBefore);
193
}
194
195
if ($this->contributorPHIDs !== null) {
196
$where[] = qsprintf(
197
$conn,
198
'contributor.dst IN (%Ls)',
199
$this->contributorPHIDs);
200
}
201
202
if ($this->signatureRequired !== null) {
203
$where[] = qsprintf(
204
$conn,
205
'd.requireSignature = %d',
206
$this->signatureRequired);
207
}
208
209
return $where;
210
}
211
212
private function loadDocumentBodies(array $documents) {
213
$body_phids = mpull($documents, 'getDocumentBodyPHID');
214
$bodies = id(new LegalpadDocumentBody())->loadAllWhere(
215
'phid IN (%Ls)',
216
$body_phids);
217
$bodies = mpull($bodies, null, 'getPHID');
218
219
foreach ($documents as $document) {
220
$body = idx($bodies, $document->getDocumentBodyPHID());
221
$document->attachDocumentBody($body);
222
}
223
224
return $documents;
225
}
226
227
private function loadContributors(array $documents) {
228
$document_map = mpull($documents, null, 'getPHID');
229
$edge_type = PhabricatorObjectHasContributorEdgeType::EDGECONST;
230
$contributor_data = id(new PhabricatorEdgeQuery())
231
->withSourcePHIDs(array_keys($document_map))
232
->withEdgeTypes(array($edge_type))
233
->execute();
234
235
foreach ($document_map as $document_phid => $document) {
236
$data = $contributor_data[$document_phid];
237
$contributors = array_keys(idx($data, $edge_type, array()));
238
$document->attachContributors($contributors);
239
}
240
241
return $documents;
242
}
243
244
private function loadSignatures(array $documents) {
245
$document_map = mpull($documents, null, 'getPHID');
246
247
$signatures = id(new LegalpadDocumentSignatureQuery())
248
->setViewer($this->getViewer())
249
->withDocumentPHIDs(array_keys($document_map))
250
->execute();
251
$signatures = mgroup($signatures, 'getDocumentPHID');
252
253
foreach ($documents as $document) {
254
$sigs = idx($signatures, $document->getPHID(), array());
255
$document->attachSignatures($sigs);
256
}
257
258
return $documents;
259
}
260
261
public function getQueryApplicationClass() {
262
return 'PhabricatorLegalpadApplication';
263
}
264
265
protected function getPrimaryTableAlias() {
266
return 'd';
267
}
268
269
}
270
271