Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / macro / query / PhabricatorMacroQuery.php
blob72618038889fa8516eb60e72715e4f5caa9823c2
1 <?php
3 final class PhabricatorMacroQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
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;
16 private $needFiles;
18 private $status = 'status-any';
19 const STATUS_ANY = 'status-any';
20 const STATUS_ACTIVE = 'status-active';
21 const STATUS_DISABLED = 'status-disabled';
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'),
31 public static function getFlagColorsOptions() {
32 $options = array(
33 '-1' => pht('(No Filtering)'),
34 '-2' => pht('(Marked With Any Flag)'),
37 foreach (PhabricatorFlagColor::getColorNameMap() as $color => $name) {
38 $options[$color] = $name;
41 return $options;
44 public function withIDs(array $ids) {
45 $this->ids = $ids;
46 return $this;
49 public function withPHIDs(array $phids) {
50 $this->phids = $phids;
51 return $this;
54 public function withAuthorPHIDs(array $author_phids) {
55 $this->authorPHIDs = $author_phids;
56 return $this;
59 public function withNameLike($name) {
60 $this->nameLike = $name;
61 return $this;
64 public function withNames(array $names) {
65 $this->names = $names;
66 return $this;
69 public function withNamePrefix($prefix) {
70 $this->namePrefix = $prefix;
71 return $this;
74 public function withStatus($status) {
75 $this->status = $status;
76 return $this;
79 public function withDateCreatedBefore($date_created_before) {
80 $this->dateCreatedBefore = $date_created_before;
81 return $this;
84 public function withDateCreatedAfter($date_created_after) {
85 $this->dateCreatedAfter = $date_created_after;
86 return $this;
89 public function withFlagColor($flag_color) {
90 $this->flagColor = $flag_color;
91 return $this;
94 public function needFiles($need_files) {
95 $this->needFiles = $need_files;
96 return $this;
99 public function newResultObject() {
100 return new PhabricatorFileImageMacro();
103 protected function loadPage() {
104 return $this->loadStandardPage(new PhabricatorFileImageMacro());
107 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
108 $where = parent::buildWhereClauseParts($conn);
110 if ($this->ids !== null) {
111 $where[] = qsprintf(
112 $conn,
113 'm.id IN (%Ld)',
114 $this->ids);
117 if ($this->phids !== null) {
118 $where[] = qsprintf(
119 $conn,
120 'm.phid IN (%Ls)',
121 $this->phids);
124 if ($this->authorPHIDs !== null) {
125 $where[] = qsprintf(
126 $conn,
127 'm.authorPHID IN (%Ls)',
128 $this->authorPHIDs);
131 if (($this->nameLike !== null) && strlen($this->nameLike)) {
132 $where[] = qsprintf(
133 $conn,
134 'm.name LIKE %~',
135 $this->nameLike);
138 if ($this->names !== null) {
139 $where[] = qsprintf(
140 $conn,
141 'm.name IN (%Ls)',
142 $this->names);
145 if (($this->namePrefix !== null) && strlen($this->namePrefix)) {
146 $where[] = qsprintf(
147 $conn,
148 'm.name LIKE %>',
149 $this->namePrefix);
152 switch ($this->status) {
153 case self::STATUS_ACTIVE:
154 $where[] = qsprintf(
155 $conn,
156 'm.isDisabled = 0');
157 break;
158 case self::STATUS_DISABLED:
159 $where[] = qsprintf(
160 $conn,
161 'm.isDisabled = 1');
162 break;
163 case self::STATUS_ANY:
164 break;
165 default:
166 throw new Exception(pht("Unknown status '%s'!", $this->status));
169 if ($this->dateCreatedAfter) {
170 $where[] = qsprintf(
171 $conn,
172 'm.dateCreated >= %d',
173 $this->dateCreatedAfter);
176 if ($this->dateCreatedBefore) {
177 $where[] = qsprintf(
178 $conn,
179 'm.dateCreated <= %d',
180 $this->dateCreatedBefore);
183 if ($this->flagColor != '-1' && $this->flagColor !== null) {
184 if ($this->flagColor == '-2') {
185 $flag_colors = array_keys(PhabricatorFlagColor::getColorNameMap());
186 } else {
187 $flag_colors = array($this->flagColor);
189 $flags = id(new PhabricatorFlagQuery())
190 ->withOwnerPHIDs(array($this->getViewer()->getPHID()))
191 ->withTypes(array(PhabricatorMacroMacroPHIDType::TYPECONST))
192 ->withColors($flag_colors)
193 ->setViewer($this->getViewer())
194 ->execute();
196 if (empty($flags)) {
197 throw new PhabricatorEmptyQueryException(pht('No matching flags.'));
198 } else {
199 $where[] = qsprintf(
200 $conn,
201 'm.phid IN (%Ls)',
202 mpull($flags, 'getObjectPHID'));
206 return $where;
209 protected function didFilterPage(array $macros) {
210 if ($this->needFiles) {
211 $file_phids = mpull($macros, 'getFilePHID');
212 $files = id(new PhabricatorFileQuery())
213 ->setViewer($this->getViewer())
214 ->setParentQuery($this)
215 ->withPHIDs($file_phids)
216 ->execute();
217 $files = mpull($files, null, 'getPHID');
219 foreach ($macros as $key => $macro) {
220 $file = idx($files, $macro->getFilePHID());
221 if (!$file) {
222 unset($macros[$key]);
223 continue;
225 $macro->attachFile($file);
229 return $macros;
232 protected function getPrimaryTableAlias() {
233 return 'm';
236 public function getQueryApplicationClass() {
237 return 'PhabricatorMacroApplication';
240 public function getOrderableColumns() {
241 return parent::getOrderableColumns() + array(
242 'name' => array(
243 'table' => 'm',
244 'column' => 'name',
245 'type' => 'string',
246 'reverse' => true,
247 'unique' => true,
252 protected function newPagingMapFromPartialObject($object) {
253 return array(
254 'id' => (int)$object->getID(),
255 'name' => $object->getName(),
259 public function getBuiltinOrders() {
260 return array(
261 'name' => array(
262 'vector' => array('name'),
263 'name' => pht('Name'),
265 ) + parent::getBuiltinOrders();