Path: blob/master/src/applications/macro/query/PhabricatorMacroQuery.php
12242 views
<?php12final class PhabricatorMacroQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45private $ids;6private $phids;7private $authorPHIDs;8private $names;9private $nameLike;10private $namePrefix;11private $dateCreatedAfter;12private $dateCreatedBefore;13private $flagColor;1415private $needFiles;1617private $status = 'status-any';18const STATUS_ANY = 'status-any';19const STATUS_ACTIVE = 'status-active';20const STATUS_DISABLED = 'status-disabled';2122public static function getStatusOptions() {23return array(24self::STATUS_ACTIVE => pht('Active Macros'),25self::STATUS_DISABLED => pht('Disabled Macros'),26self::STATUS_ANY => pht('Active and Disabled Macros'),27);28}2930public static function getFlagColorsOptions() {31$options = array(32'-1' => pht('(No Filtering)'),33'-2' => pht('(Marked With Any Flag)'),34);3536foreach (PhabricatorFlagColor::getColorNameMap() as $color => $name) {37$options[$color] = $name;38}3940return $options;41}4243public function withIDs(array $ids) {44$this->ids = $ids;45return $this;46}4748public function withPHIDs(array $phids) {49$this->phids = $phids;50return $this;51}5253public function withAuthorPHIDs(array $author_phids) {54$this->authorPHIDs = $author_phids;55return $this;56}5758public function withNameLike($name) {59$this->nameLike = $name;60return $this;61}6263public function withNames(array $names) {64$this->names = $names;65return $this;66}6768public function withNamePrefix($prefix) {69$this->namePrefix = $prefix;70return $this;71}7273public function withStatus($status) {74$this->status = $status;75return $this;76}7778public function withDateCreatedBefore($date_created_before) {79$this->dateCreatedBefore = $date_created_before;80return $this;81}8283public function withDateCreatedAfter($date_created_after) {84$this->dateCreatedAfter = $date_created_after;85return $this;86}8788public function withFlagColor($flag_color) {89$this->flagColor = $flag_color;90return $this;91}9293public function needFiles($need_files) {94$this->needFiles = $need_files;95return $this;96}9798public function newResultObject() {99return new PhabricatorFileImageMacro();100}101102protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {103$where = parent::buildWhereClauseParts($conn);104105if ($this->ids !== null) {106$where[] = qsprintf(107$conn,108'm.id IN (%Ld)',109$this->ids);110}111112if ($this->phids !== null) {113$where[] = qsprintf(114$conn,115'm.phid IN (%Ls)',116$this->phids);117}118119if ($this->authorPHIDs !== null) {120$where[] = qsprintf(121$conn,122'm.authorPHID IN (%Ls)',123$this->authorPHIDs);124}125126if (($this->nameLike !== null) && strlen($this->nameLike)) {127$where[] = qsprintf(128$conn,129'm.name LIKE %~',130$this->nameLike);131}132133if ($this->names !== null) {134$where[] = qsprintf(135$conn,136'm.name IN (%Ls)',137$this->names);138}139140if (($this->namePrefix !== null) && strlen($this->namePrefix)) {141$where[] = qsprintf(142$conn,143'm.name LIKE %>',144$this->namePrefix);145}146147switch ($this->status) {148case self::STATUS_ACTIVE:149$where[] = qsprintf(150$conn,151'm.isDisabled = 0');152break;153case self::STATUS_DISABLED:154$where[] = qsprintf(155$conn,156'm.isDisabled = 1');157break;158case self::STATUS_ANY:159break;160default:161throw new Exception(pht("Unknown status '%s'!", $this->status));162}163164if ($this->dateCreatedAfter) {165$where[] = qsprintf(166$conn,167'm.dateCreated >= %d',168$this->dateCreatedAfter);169}170171if ($this->dateCreatedBefore) {172$where[] = qsprintf(173$conn,174'm.dateCreated <= %d',175$this->dateCreatedBefore);176}177178if ($this->flagColor != '-1' && $this->flagColor !== null) {179if ($this->flagColor == '-2') {180$flag_colors = array_keys(PhabricatorFlagColor::getColorNameMap());181} else {182$flag_colors = array($this->flagColor);183}184$flags = id(new PhabricatorFlagQuery())185->withOwnerPHIDs(array($this->getViewer()->getPHID()))186->withTypes(array(PhabricatorMacroMacroPHIDType::TYPECONST))187->withColors($flag_colors)188->setViewer($this->getViewer())189->execute();190191if (empty($flags)) {192throw new PhabricatorEmptyQueryException(pht('No matching flags.'));193} else {194$where[] = qsprintf(195$conn,196'm.phid IN (%Ls)',197mpull($flags, 'getObjectPHID'));198}199}200201return $where;202}203204protected function didFilterPage(array $macros) {205if ($this->needFiles) {206$file_phids = mpull($macros, 'getFilePHID');207$files = id(new PhabricatorFileQuery())208->setViewer($this->getViewer())209->setParentQuery($this)210->withPHIDs($file_phids)211->execute();212$files = mpull($files, null, 'getPHID');213214foreach ($macros as $key => $macro) {215$file = idx($files, $macro->getFilePHID());216if (!$file) {217unset($macros[$key]);218continue;219}220$macro->attachFile($file);221}222}223224return $macros;225}226227protected function getPrimaryTableAlias() {228return 'm';229}230231public function getQueryApplicationClass() {232return 'PhabricatorMacroApplication';233}234235public function getOrderableColumns() {236return parent::getOrderableColumns() + array(237'name' => array(238'table' => 'm',239'column' => 'name',240'type' => 'string',241'reverse' => true,242'unique' => true,243),244);245}246247protected function newPagingMapFromPartialObject($object) {248return array(249'id' => (int)$object->getID(),250'name' => $object->getName(),251);252}253254public function getBuiltinOrders() {255return array(256'name' => array(257'vector' => array('name'),258'name' => pht('Name'),259),260) + parent::getBuiltinOrders();261}262263}264265266