Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / macro / query / PhabricatorMacroSearchEngine.php
blob7ef57a6a108bffd6bbcdd5027d69b65e0c7ebdd9
1 <?php
3 final class PhabricatorMacroSearchEngine
4 extends PhabricatorApplicationSearchEngine {
6 public function getResultTypeDescription() {
7 return pht('Macros');
10 public function getApplicationClassName() {
11 return 'PhabricatorMacroApplication';
14 public function newQuery() {
15 return id(new PhabricatorMacroQuery())
16 ->needFiles(true);
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'),
49 protected function getDefaultFieldOrder() {
50 return array(
51 '...',
52 'createdStart',
53 'createdEnd',
57 protected function buildQueryFromParameters(array $map) {
58 $query = $this->newQuery();
60 if ($map['authorPHIDs']) {
61 $query->withAuthorPHIDs($map['authorPHIDs']);
64 if ($map['status']) {
65 $query->withStatus($map['status']);
68 if ($map['names']) {
69 $query->withNames($map['names']);
72 if (strlen($map['nameLike'])) {
73 $query->withNameLike($map['nameLike']);
76 if ($map['createdStart']) {
77 $query->withDateCreatedAfter($map['createdStart']);
80 if ($map['createdEnd']) {
81 $query->withDateCreatedBefore($map['createdEnd']);
84 if ($map['flagColor'] !== null) {
85 $query->withFlagColor($map['flagColor']);
88 return $query;
91 protected function getURI($path) {
92 return '/macro/'.$path;
95 protected function getBuiltinQueryNames() {
96 $names = array(
97 'active' => pht('Active'),
98 'all' => pht('All'),
101 if ($this->requireViewer()->isLoggedIn()) {
102 $names['authored'] = pht('Authored');
105 return $names;
108 public function buildSavedQueryFromBuiltin($query_key) {
109 $query = $this->newSavedQuery();
110 $query->setQueryKey($query_key);
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()));
127 return parent::buildSavedQueryFromBuiltin($query_key);
130 protected function renderResultList(
131 array $macros,
132 PhabricatorSavedQuery $query,
133 array $handles) {
135 assert_instances_of($macros, 'PhabricatorFileImageMacro');
136 $viewer = $this->requireViewer();
137 $handles = $viewer->loadHandles(mpull($macros, 'getAuthorPHID'));
139 $xform = PhabricatorFileTransform::getTransformByKey(
140 PhabricatorFileThumbnailTransform::TRANSFORM_PINBOARD);
142 $pinboard = new PHUIPinboardView();
143 foreach ($macros as $macro) {
144 $file = $macro->getFile();
146 $item = id(new PHUIPinboardItemView())
147 ->setUser($viewer)
148 ->setObject($macro);
150 if ($file) {
151 $item->setImageURI($file->getURIForTransform($xform));
152 list($x, $y) = $xform->getTransformedDimensions($file);
153 $item->setImageSize($x, $y);
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')));
173 if ($macro->getAuthorPHID()) {
174 $author_handle = $handles[$macro->getAuthorPHID()];
175 $item->appendChild(
176 pht('Created by %s', $author_handle->renderLink()));
179 $item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/'));
180 $item->setDisabled($macro->getisDisabled());
181 $item->setHeader($macro->getName());
183 $pinboard->addItem($item);
186 $result = new PhabricatorApplicationSearchResultView();
187 $result->setContent($pinboard);
189 return $result;
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);
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);
208 return $view;