3 final class PhabricatorMacroQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
12 private $dateCreatedAfter;
13 private $dateCreatedBefore;
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() {
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() {
33 '-1' => pht('(No Filtering)'),
34 '-2' => pht('(Marked With Any Flag)'),
37 foreach (PhabricatorFlagColor
::getColorNameMap() as $color => $name) {
38 $options[$color] = $name;
44 public function withIDs(array $ids) {
49 public function withPHIDs(array $phids) {
50 $this->phids
= $phids;
54 public function withAuthorPHIDs(array $author_phids) {
55 $this->authorPHIDs
= $author_phids;
59 public function withNameLike($name) {
60 $this->nameLike
= $name;
64 public function withNames(array $names) {
65 $this->names
= $names;
69 public function withNamePrefix($prefix) {
70 $this->namePrefix
= $prefix;
74 public function withStatus($status) {
75 $this->status
= $status;
79 public function withDateCreatedBefore($date_created_before) {
80 $this->dateCreatedBefore
= $date_created_before;
84 public function withDateCreatedAfter($date_created_after) {
85 $this->dateCreatedAfter
= $date_created_after;
89 public function withFlagColor($flag_color) {
90 $this->flagColor
= $flag_color;
94 public function needFiles($need_files) {
95 $this->needFiles
= $need_files;
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) {
117 if ($this->phids
!== null) {
124 if ($this->authorPHIDs
!== null) {
127 'm.authorPHID IN (%Ls)',
131 if (($this->nameLike
!== null) && strlen($this->nameLike
)) {
138 if ($this->names
!== null) {
145 if (($this->namePrefix
!== null) && strlen($this->namePrefix
)) {
152 switch ($this->status
) {
153 case self
::STATUS_ACTIVE
:
158 case self
::STATUS_DISABLED
:
163 case self
::STATUS_ANY
:
166 throw new Exception(pht("Unknown status '%s'!", $this->status
));
169 if ($this->dateCreatedAfter
) {
172 'm.dateCreated >= %d',
173 $this->dateCreatedAfter
);
176 if ($this->dateCreatedBefore
) {
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());
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())
197 throw new PhabricatorEmptyQueryException(pht('No matching flags.'));
202 mpull($flags, 'getObjectPHID'));
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)
217 $files = mpull($files, null, 'getPHID');
219 foreach ($macros as $key => $macro) {
220 $file = idx($files, $macro->getFilePHID());
222 unset($macros[$key]);
225 $macro->attachFile($file);
232 protected function getPrimaryTableAlias() {
236 public function getQueryApplicationClass() {
237 return 'PhabricatorMacroApplication';
240 public function getOrderableColumns() {
241 return parent
::getOrderableColumns() +
array(
252 protected function newPagingMapFromPartialObject($object) {
254 'id' => (int)$object->getID(),
255 'name' => $object->getName(),
259 public function getBuiltinOrders() {
262 'vector' => array('name'),
263 'name' => pht('Name'),
265 ) + parent
::getBuiltinOrders();