Path: blob/master/src/applications/almanac/query/AlmanacServiceQuery.php
12256 views
<?php12final class AlmanacServiceQuery3extends AlmanacQuery {45private $ids;6private $phids;7private $names;8private $serviceTypes;9private $devicePHIDs;10private $namePrefix;11private $nameSuffix;1213private $needBindings;14private $needActiveBindings;1516public function withIDs(array $ids) {17$this->ids = $ids;18return $this;19}2021public function withPHIDs(array $phids) {22$this->phids = $phids;23return $this;24}2526public function withNames(array $names) {27$this->names = $names;28return $this;29}3031public function withServiceTypes(array $types) {32$this->serviceTypes = $types;33return $this;34}3536public function withDevicePHIDs(array $phids) {37$this->devicePHIDs = $phids;38return $this;39}4041public function withNamePrefix($prefix) {42$this->namePrefix = $prefix;43return $this;44}4546public function withNameSuffix($suffix) {47$this->nameSuffix = $suffix;48return $this;49}5051public function withNameNgrams($ngrams) {52return $this->withNgramsConstraint(53new AlmanacServiceNameNgrams(),54$ngrams);55}5657public function needBindings($need_bindings) {58$this->needBindings = $need_bindings;59return $this;60}6162public function needActiveBindings($need_active) {63$this->needActiveBindings = $need_active;64return $this;65}6667public function newResultObject() {68return new AlmanacService();69}7071protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {72$joins = parent::buildJoinClauseParts($conn);7374if ($this->shouldJoinBindingTable()) {75$joins[] = qsprintf(76$conn,77'JOIN %T binding ON service.phid = binding.servicePHID',78id(new AlmanacBinding())->getTableName());79}8081return $joins;82}8384protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {85$where = parent::buildWhereClauseParts($conn);8687if ($this->ids !== null) {88$where[] = qsprintf(89$conn,90'service.id IN (%Ld)',91$this->ids);92}9394if ($this->phids !== null) {95$where[] = qsprintf(96$conn,97'service.phid IN (%Ls)',98$this->phids);99}100101if ($this->names !== null) {102$hashes = array();103foreach ($this->names as $name) {104$hashes[] = PhabricatorHash::digestForIndex($name);105}106107$where[] = qsprintf(108$conn,109'service.nameIndex IN (%Ls)',110$hashes);111}112113if ($this->serviceTypes !== null) {114$where[] = qsprintf(115$conn,116'service.serviceType IN (%Ls)',117$this->serviceTypes);118}119120if ($this->devicePHIDs !== null) {121$where[] = qsprintf(122$conn,123'binding.devicePHID IN (%Ls)',124$this->devicePHIDs);125}126127if ($this->namePrefix !== null) {128$where[] = qsprintf(129$conn,130'service.name LIKE %>',131$this->namePrefix);132}133134if ($this->nameSuffix !== null) {135$where[] = qsprintf(136$conn,137'service.name LIKE %<',138$this->nameSuffix);139}140141return $where;142}143144protected function willFilterPage(array $services) {145$service_map = AlmanacServiceType::getAllServiceTypes();146147foreach ($services as $key => $service) {148$implementation = idx($service_map, $service->getServiceType());149150if (!$implementation) {151$this->didRejectResult($service);152unset($services[$key]);153continue;154}155156$implementation = clone $implementation;157$service->attachServiceImplementation($implementation);158}159160return $services;161}162163protected function didFilterPage(array $services) {164$need_all = $this->needBindings;165$need_active = $this->needActiveBindings;166167$need_any = ($need_all || $need_active);168$only_active = ($need_active && !$need_all);169170if ($need_any) {171$service_phids = mpull($services, 'getPHID');172173$bindings_query = id(new AlmanacBindingQuery())174->setViewer($this->getViewer())175->withServicePHIDs($service_phids)176->needProperties($this->getNeedProperties());177178if ($only_active) {179$bindings_query->withIsActive(true);180}181182$bindings = $bindings_query->execute();183$bindings = mgroup($bindings, 'getServicePHID');184185foreach ($services as $service) {186$service_bindings = idx($bindings, $service->getPHID(), array());187188if ($only_active) {189$service->attachActiveBindings($service_bindings);190} else {191$service->attachBindings($service_bindings);192}193}194}195196return parent::didFilterPage($services);197}198199private function shouldJoinBindingTable() {200return ($this->devicePHIDs !== null);201}202203protected function shouldGroupQueryResultRows() {204if ($this->shouldJoinBindingTable()) {205return true;206}207208return parent::shouldGroupQueryResultRows();209}210211protected function getPrimaryTableAlias() {212return 'service';213}214215public function getOrderableColumns() {216return parent::getOrderableColumns() + array(217'name' => array(218'table' => $this->getPrimaryTableAlias(),219'column' => 'name',220'type' => 'string',221'unique' => true,222'reverse' => true,223),224);225}226227protected function newPagingMapFromPartialObject($object) {228return array(229'id' => (int)$object->getID(),230'name' => $object->getName(),231);232}233234public function getBuiltinOrders() {235return array(236'name' => array(237'vector' => array('name'),238'name' => pht('Service Name'),239),240) + parent::getBuiltinOrders();241}242243}244245246