@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
3final class PhabricatorMacroSearchEngine
4 extends PhabricatorApplicationSearchEngine {
5
6 public function getResultTypeDescription() {
7 return pht('Macros');
8 }
9
10 public function getApplicationClassName() {
11 return PhabricatorMacroApplication::class;
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 /**
131 * @param array<PhabricatorFileImageMacro> $macros
132 * @param PhabricatorSavedQuery $query
133 * @param array<PhabricatorObjectHandle> $handles
134 */
135 protected function renderResultList(
136 array $macros,
137 PhabricatorSavedQuery $query,
138 array $handles) {
139
140 assert_instances_of($macros, PhabricatorFileImageMacro::class);
141 $viewer = $this->requireViewer();
142 $handles = $viewer->loadHandles(mpull($macros, 'getAuthorPHID'));
143
144 $xform = PhabricatorFileTransform::getTransformByKey(
145 PhabricatorFileThumbnailTransform::TRANSFORM_PINBOARD);
146
147 $pinboard = new PHUIPinboardView();
148 foreach ($macros as $macro) {
149 $file = $macro->getFile();
150
151 $item = id(new PHUIPinboardItemView())
152 ->setUser($viewer)
153 ->setObject($macro);
154
155 if ($file) {
156 $item->setImageURI($file->getURIForTransform($xform));
157 list($x, $y) = $xform->getTransformedDimensions($file);
158 $item->setImageSize($x, $y);
159 }
160
161 if ($macro->getDateCreated()) {
162 $datetime = phabricator_date($macro->getDateCreated(), $viewer);
163 $item->appendChild(
164 phutil_tag(
165 'div',
166 array(),
167 pht('Created on %s', $datetime)));
168 } else {
169 // Very old macros don't have a creation date. Rendering something
170 // keeps all the pins at the same height and avoids flow issues.
171 $item->appendChild(
172 phutil_tag(
173 'div',
174 array(),
175 pht('Created in ages long past')));
176 }
177
178 if ($macro->getAuthorPHID()) {
179 $author_handle = $handles[$macro->getAuthorPHID()];
180 $item->appendChild(
181 pht('Created by %s', $author_handle->renderLink()));
182 }
183
184 $item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/'));
185 $item->setDisabled($macro->getisDisabled());
186 $item->setHeader($macro->getName());
187
188 $pinboard->addItem($item);
189 }
190
191 $result = new PhabricatorApplicationSearchResultView();
192 $result->setContent($pinboard);
193
194 return $result;
195 }
196
197 protected function getNewUserBody() {
198 $create_button = id(new PHUIButtonView())
199 ->setTag('a')
200 ->setText(pht('Create a Macro'))
201 ->setHref('/macro/create/')
202 ->setColor(PHUIButtonView::GREEN);
203
204 $icon = $this->getApplication()->getIcon();
205 $app_name = $this->getApplication()->getName();
206 $view = id(new PHUIBigInfoView())
207 ->setIcon($icon)
208 ->setTitle(pht('Welcome to %s', $app_name))
209 ->setDescription(
210 pht('Create easy to remember shortcuts to images and memes.'))
211 ->addAction($create_button);
212
213 return $view;
214 }
215
216}