Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/macro/query/PhabricatorMacroSearchEngine.php
12242 views
1
<?php
2
3
final class PhabricatorMacroSearchEngine
4
extends PhabricatorApplicationSearchEngine {
5
6
public function getResultTypeDescription() {
7
return pht('Macros');
8
}
9
10
public function getApplicationClassName() {
11
return 'PhabricatorMacroApplication';
12
}
13
14
public function newQuery() {
15
return id(new PhabricatorMacroQuery())
16
->needFiles(true);
17
}
18
19
protected function buildCustomSearchFields() {
20
return array(
21
id(new PhabricatorSearchSelectField())
22
->setLabel(pht('Status'))
23
->setKey('status')
24
->setOptions(PhabricatorMacroQuery::getStatusOptions()),
25
id(new PhabricatorUsersSearchField())
26
->setLabel(pht('Authors'))
27
->setKey('authorPHIDs')
28
->setAliases(array('author', 'authors')),
29
id(new PhabricatorSearchTextField())
30
->setLabel(pht('Name Contains'))
31
->setKey('nameLike'),
32
id(new PhabricatorSearchStringListField())
33
->setLabel(pht('Exact Names'))
34
->setKey('names'),
35
id(new PhabricatorSearchSelectField())
36
->setLabel(pht('Marked with Flag'))
37
->setKey('flagColor')
38
->setDefault('-1')
39
->setOptions(PhabricatorMacroQuery::getFlagColorsOptions()),
40
id(new PhabricatorSearchDateField())
41
->setLabel(pht('Created After'))
42
->setKey('createdStart'),
43
id(new PhabricatorSearchDateField())
44
->setLabel(pht('Created Before'))
45
->setKey('createdEnd'),
46
);
47
}
48
49
protected function getDefaultFieldOrder() {
50
return array(
51
'...',
52
'createdStart',
53
'createdEnd',
54
);
55
}
56
57
protected function buildQueryFromParameters(array $map) {
58
$query = $this->newQuery();
59
60
if ($map['authorPHIDs']) {
61
$query->withAuthorPHIDs($map['authorPHIDs']);
62
}
63
64
if ($map['status']) {
65
$query->withStatus($map['status']);
66
}
67
68
if ($map['names']) {
69
$query->withNames($map['names']);
70
}
71
72
if (strlen($map['nameLike'])) {
73
$query->withNameLike($map['nameLike']);
74
}
75
76
if ($map['createdStart']) {
77
$query->withDateCreatedAfter($map['createdStart']);
78
}
79
80
if ($map['createdEnd']) {
81
$query->withDateCreatedBefore($map['createdEnd']);
82
}
83
84
if ($map['flagColor'] !== null) {
85
$query->withFlagColor($map['flagColor']);
86
}
87
88
return $query;
89
}
90
91
protected function getURI($path) {
92
return '/macro/'.$path;
93
}
94
95
protected function getBuiltinQueryNames() {
96
$names = array(
97
'active' => pht('Active'),
98
'all' => pht('All'),
99
);
100
101
if ($this->requireViewer()->isLoggedIn()) {
102
$names['authored'] = pht('Authored');
103
}
104
105
return $names;
106
}
107
108
public function buildSavedQueryFromBuiltin($query_key) {
109
$query = $this->newSavedQuery();
110
$query->setQueryKey($query_key);
111
112
switch ($query_key) {
113
case 'active':
114
return $query->setParameter(
115
'status',
116
PhabricatorMacroQuery::STATUS_ACTIVE);
117
case 'all':
118
return $query->setParameter(
119
'status',
120
PhabricatorMacroQuery::STATUS_ANY);
121
case 'authored':
122
return $query->setParameter(
123
'authorPHIDs',
124
array($this->requireViewer()->getPHID()));
125
}
126
127
return parent::buildSavedQueryFromBuiltin($query_key);
128
}
129
130
protected function renderResultList(
131
array $macros,
132
PhabricatorSavedQuery $query,
133
array $handles) {
134
135
assert_instances_of($macros, 'PhabricatorFileImageMacro');
136
$viewer = $this->requireViewer();
137
$handles = $viewer->loadHandles(mpull($macros, 'getAuthorPHID'));
138
139
$xform = PhabricatorFileTransform::getTransformByKey(
140
PhabricatorFileThumbnailTransform::TRANSFORM_PINBOARD);
141
142
$pinboard = new PHUIPinboardView();
143
foreach ($macros as $macro) {
144
$file = $macro->getFile();
145
146
$item = id(new PHUIPinboardItemView())
147
->setUser($viewer)
148
->setObject($macro);
149
150
if ($file) {
151
$item->setImageURI($file->getURIForTransform($xform));
152
list($x, $y) = $xform->getTransformedDimensions($file);
153
$item->setImageSize($x, $y);
154
}
155
156
if ($macro->getDateCreated()) {
157
$datetime = phabricator_date($macro->getDateCreated(), $viewer);
158
$item->appendChild(
159
phutil_tag(
160
'div',
161
array(),
162
pht('Created on %s', $datetime)));
163
} else {
164
// Very old macros don't have a creation date. Rendering something
165
// keeps all the pins at the same height and avoids flow issues.
166
$item->appendChild(
167
phutil_tag(
168
'div',
169
array(),
170
pht('Created in ages long past')));
171
}
172
173
if ($macro->getAuthorPHID()) {
174
$author_handle = $handles[$macro->getAuthorPHID()];
175
$item->appendChild(
176
pht('Created by %s', $author_handle->renderLink()));
177
}
178
179
$item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/'));
180
$item->setDisabled($macro->getisDisabled());
181
$item->setHeader($macro->getName());
182
183
$pinboard->addItem($item);
184
}
185
186
$result = new PhabricatorApplicationSearchResultView();
187
$result->setContent($pinboard);
188
189
return $result;
190
}
191
192
protected function getNewUserBody() {
193
$create_button = id(new PHUIButtonView())
194
->setTag('a')
195
->setText(pht('Create a Macro'))
196
->setHref('/macro/create/')
197
->setColor(PHUIButtonView::GREEN);
198
199
$icon = $this->getApplication()->getIcon();
200
$app_name = $this->getApplication()->getName();
201
$view = id(new PHUIBigInfoView())
202
->setIcon($icon)
203
->setTitle(pht('Welcome to %s', $app_name))
204
->setDescription(
205
pht('Create easy to remember shortcuts to images and memes.'))
206
->addAction($create_button);
207
208
return $view;
209
}
210
211
}
212
213