3 final class AlmanacServiceQuery
14 private $needBindings;
15 private $needActiveBindings;
17 public function withIDs(array $ids) {
22 public function withPHIDs(array $phids) {
23 $this->phids
= $phids;
27 public function withNames(array $names) {
28 $this->names
= $names;
32 public function withServiceTypes(array $types) {
33 $this->serviceTypes
= $types;
37 public function withDevicePHIDs(array $phids) {
38 $this->devicePHIDs
= $phids;
42 public function withNamePrefix($prefix) {
43 $this->namePrefix
= $prefix;
47 public function withNameSuffix($suffix) {
48 $this->nameSuffix
= $suffix;
52 public function withNameNgrams($ngrams) {
53 return $this->withNgramsConstraint(
54 new AlmanacServiceNameNgrams(),
58 public function needBindings($need_bindings) {
59 $this->needBindings
= $need_bindings;
63 public function needActiveBindings($need_active) {
64 $this->needActiveBindings
= $need_active;
68 public function newResultObject() {
69 return new AlmanacService();
72 protected function loadPage() {
73 return $this->loadStandardPage($this->newResultObject());
76 protected function buildJoinClauseParts(AphrontDatabaseConnection
$conn) {
77 $joins = parent
::buildJoinClauseParts($conn);
79 if ($this->shouldJoinBindingTable()) {
82 'JOIN %T binding ON service.phid = binding.servicePHID',
83 id(new AlmanacBinding())->getTableName());
89 protected function buildWhereClauseParts(AphrontDatabaseConnection
$conn) {
90 $where = parent
::buildWhereClauseParts($conn);
92 if ($this->ids
!== null) {
95 'service.id IN (%Ld)',
99 if ($this->phids
!== null) {
102 'service.phid IN (%Ls)',
106 if ($this->names
!== null) {
108 foreach ($this->names
as $name) {
109 $hashes[] = PhabricatorHash
::digestForIndex($name);
114 'service.nameIndex IN (%Ls)',
118 if ($this->serviceTypes
!== null) {
121 'service.serviceType IN (%Ls)',
122 $this->serviceTypes
);
125 if ($this->devicePHIDs
!== null) {
128 'binding.devicePHID IN (%Ls)',
132 if ($this->namePrefix
!== null) {
135 'service.name LIKE %>',
139 if ($this->nameSuffix
!== null) {
142 'service.name LIKE %<',
149 protected function willFilterPage(array $services) {
150 $service_map = AlmanacServiceType
::getAllServiceTypes();
152 foreach ($services as $key => $service) {
153 $implementation = idx($service_map, $service->getServiceType());
155 if (!$implementation) {
156 $this->didRejectResult($service);
157 unset($services[$key]);
161 $implementation = clone $implementation;
162 $service->attachServiceImplementation($implementation);
168 protected function didFilterPage(array $services) {
169 $need_all = $this->needBindings
;
170 $need_active = $this->needActiveBindings
;
172 $need_any = ($need_all ||
$need_active);
173 $only_active = ($need_active && !$need_all);
176 $service_phids = mpull($services, 'getPHID');
178 $bindings_query = id(new AlmanacBindingQuery())
179 ->setViewer($this->getViewer())
180 ->withServicePHIDs($service_phids)
181 ->needProperties($this->getNeedProperties());
184 $bindings_query->withIsActive(true);
187 $bindings = $bindings_query->execute();
188 $bindings = mgroup($bindings, 'getServicePHID');
190 foreach ($services as $service) {
191 $service_bindings = idx($bindings, $service->getPHID(), array());
194 $service->attachActiveBindings($service_bindings);
196 $service->attachBindings($service_bindings);
201 return parent
::didFilterPage($services);
204 private function shouldJoinBindingTable() {
205 return ($this->devicePHIDs
!== null);
208 protected function shouldGroupQueryResultRows() {
209 if ($this->shouldJoinBindingTable()) {
213 return parent
::shouldGroupQueryResultRows();
216 protected function getPrimaryTableAlias() {
220 public function getOrderableColumns() {
221 return parent
::getOrderableColumns() +
array(
223 'table' => $this->getPrimaryTableAlias(),
232 protected function newPagingMapFromPartialObject($object) {
234 'id' => (int)$object->getID(),
235 'name' => $object->getName(),
239 public function getBuiltinOrders() {
242 'vector' => array('name'),
243 'name' => pht('Service Name'),
245 ) + parent
::getBuiltinOrders();