Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/macro/query/PhabricatorMacroQuery.php
12242 views
1
<?php
2
3
final class PhabricatorMacroQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $ids;
7
private $phids;
8
private $authorPHIDs;
9
private $names;
10
private $nameLike;
11
private $namePrefix;
12
private $dateCreatedAfter;
13
private $dateCreatedBefore;
14
private $flagColor;
15
16
private $needFiles;
17
18
private $status = 'status-any';
19
const STATUS_ANY = 'status-any';
20
const STATUS_ACTIVE = 'status-active';
21
const STATUS_DISABLED = 'status-disabled';
22
23
public static function getStatusOptions() {
24
return array(
25
self::STATUS_ACTIVE => pht('Active Macros'),
26
self::STATUS_DISABLED => pht('Disabled Macros'),
27
self::STATUS_ANY => pht('Active and Disabled Macros'),
28
);
29
}
30
31
public static function getFlagColorsOptions() {
32
$options = array(
33
'-1' => pht('(No Filtering)'),
34
'-2' => pht('(Marked With Any Flag)'),
35
);
36
37
foreach (PhabricatorFlagColor::getColorNameMap() as $color => $name) {
38
$options[$color] = $name;
39
}
40
41
return $options;
42
}
43
44
public function withIDs(array $ids) {
45
$this->ids = $ids;
46
return $this;
47
}
48
49
public function withPHIDs(array $phids) {
50
$this->phids = $phids;
51
return $this;
52
}
53
54
public function withAuthorPHIDs(array $author_phids) {
55
$this->authorPHIDs = $author_phids;
56
return $this;
57
}
58
59
public function withNameLike($name) {
60
$this->nameLike = $name;
61
return $this;
62
}
63
64
public function withNames(array $names) {
65
$this->names = $names;
66
return $this;
67
}
68
69
public function withNamePrefix($prefix) {
70
$this->namePrefix = $prefix;
71
return $this;
72
}
73
74
public function withStatus($status) {
75
$this->status = $status;
76
return $this;
77
}
78
79
public function withDateCreatedBefore($date_created_before) {
80
$this->dateCreatedBefore = $date_created_before;
81
return $this;
82
}
83
84
public function withDateCreatedAfter($date_created_after) {
85
$this->dateCreatedAfter = $date_created_after;
86
return $this;
87
}
88
89
public function withFlagColor($flag_color) {
90
$this->flagColor = $flag_color;
91
return $this;
92
}
93
94
public function needFiles($need_files) {
95
$this->needFiles = $need_files;
96
return $this;
97
}
98
99
public function newResultObject() {
100
return new PhabricatorFileImageMacro();
101
}
102
103
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
104
$where = parent::buildWhereClauseParts($conn);
105
106
if ($this->ids !== null) {
107
$where[] = qsprintf(
108
$conn,
109
'm.id IN (%Ld)',
110
$this->ids);
111
}
112
113
if ($this->phids !== null) {
114
$where[] = qsprintf(
115
$conn,
116
'm.phid IN (%Ls)',
117
$this->phids);
118
}
119
120
if ($this->authorPHIDs !== null) {
121
$where[] = qsprintf(
122
$conn,
123
'm.authorPHID IN (%Ls)',
124
$this->authorPHIDs);
125
}
126
127
if (($this->nameLike !== null) && strlen($this->nameLike)) {
128
$where[] = qsprintf(
129
$conn,
130
'm.name LIKE %~',
131
$this->nameLike);
132
}
133
134
if ($this->names !== null) {
135
$where[] = qsprintf(
136
$conn,
137
'm.name IN (%Ls)',
138
$this->names);
139
}
140
141
if (($this->namePrefix !== null) && strlen($this->namePrefix)) {
142
$where[] = qsprintf(
143
$conn,
144
'm.name LIKE %>',
145
$this->namePrefix);
146
}
147
148
switch ($this->status) {
149
case self::STATUS_ACTIVE:
150
$where[] = qsprintf(
151
$conn,
152
'm.isDisabled = 0');
153
break;
154
case self::STATUS_DISABLED:
155
$where[] = qsprintf(
156
$conn,
157
'm.isDisabled = 1');
158
break;
159
case self::STATUS_ANY:
160
break;
161
default:
162
throw new Exception(pht("Unknown status '%s'!", $this->status));
163
}
164
165
if ($this->dateCreatedAfter) {
166
$where[] = qsprintf(
167
$conn,
168
'm.dateCreated >= %d',
169
$this->dateCreatedAfter);
170
}
171
172
if ($this->dateCreatedBefore) {
173
$where[] = qsprintf(
174
$conn,
175
'm.dateCreated <= %d',
176
$this->dateCreatedBefore);
177
}
178
179
if ($this->flagColor != '-1' && $this->flagColor !== null) {
180
if ($this->flagColor == '-2') {
181
$flag_colors = array_keys(PhabricatorFlagColor::getColorNameMap());
182
} else {
183
$flag_colors = array($this->flagColor);
184
}
185
$flags = id(new PhabricatorFlagQuery())
186
->withOwnerPHIDs(array($this->getViewer()->getPHID()))
187
->withTypes(array(PhabricatorMacroMacroPHIDType::TYPECONST))
188
->withColors($flag_colors)
189
->setViewer($this->getViewer())
190
->execute();
191
192
if (empty($flags)) {
193
throw new PhabricatorEmptyQueryException(pht('No matching flags.'));
194
} else {
195
$where[] = qsprintf(
196
$conn,
197
'm.phid IN (%Ls)',
198
mpull($flags, 'getObjectPHID'));
199
}
200
}
201
202
return $where;
203
}
204
205
protected function didFilterPage(array $macros) {
206
if ($this->needFiles) {
207
$file_phids = mpull($macros, 'getFilePHID');
208
$files = id(new PhabricatorFileQuery())
209
->setViewer($this->getViewer())
210
->setParentQuery($this)
211
->withPHIDs($file_phids)
212
->execute();
213
$files = mpull($files, null, 'getPHID');
214
215
foreach ($macros as $key => $macro) {
216
$file = idx($files, $macro->getFilePHID());
217
if (!$file) {
218
unset($macros[$key]);
219
continue;
220
}
221
$macro->attachFile($file);
222
}
223
}
224
225
return $macros;
226
}
227
228
protected function getPrimaryTableAlias() {
229
return 'm';
230
}
231
232
public function getQueryApplicationClass() {
233
return 'PhabricatorMacroApplication';
234
}
235
236
public function getOrderableColumns() {
237
return parent::getOrderableColumns() + array(
238
'name' => array(
239
'table' => 'm',
240
'column' => 'name',
241
'type' => 'string',
242
'reverse' => true,
243
'unique' => true,
244
),
245
);
246
}
247
248
protected function newPagingMapFromPartialObject($object) {
249
return array(
250
'id' => (int)$object->getID(),
251
'name' => $object->getName(),
252
);
253
}
254
255
public function getBuiltinOrders() {
256
return array(
257
'name' => array(
258
'vector' => array('name'),
259
'name' => pht('Name'),
260
),
261
) + parent::getBuiltinOrders();
262
}
263
264
}
265
266