Path: blob/master/src/applications/almanac/query/AlmanacBindingQuery.php
12256 views
<?php12final class AlmanacBindingQuery3extends AlmanacQuery {45private $ids;6private $phids;7private $servicePHIDs;8private $devicePHIDs;9private $interfacePHIDs;10private $isActive;1112public function withIDs(array $ids) {13$this->ids = $ids;14return $this;15}1617public function withPHIDs(array $phids) {18$this->phids = $phids;19return $this;20}2122public function withServicePHIDs(array $phids) {23$this->servicePHIDs = $phids;24return $this;25}2627public function withDevicePHIDs(array $phids) {28$this->devicePHIDs = $phids;29return $this;30}3132public function withInterfacePHIDs(array $phids) {33$this->interfacePHIDs = $phids;34return $this;35}3637public function withIsActive($active) {38$this->isActive = $active;39return $this;40}4142public function newResultObject() {43return new AlmanacBinding();44}4546protected function willFilterPage(array $bindings) {47$service_phids = mpull($bindings, 'getServicePHID');48$device_phids = mpull($bindings, 'getDevicePHID');49$interface_phids = mpull($bindings, 'getInterfacePHID');5051$services = id(new AlmanacServiceQuery())52->setParentQuery($this)53->setViewer($this->getViewer())54->withPHIDs($service_phids)55->needProperties($this->getNeedProperties())56->execute();57$services = mpull($services, null, 'getPHID');5859$devices = id(new AlmanacDeviceQuery())60->setParentQuery($this)61->setViewer($this->getViewer())62->withPHIDs($device_phids)63->needProperties($this->getNeedProperties())64->execute();65$devices = mpull($devices, null, 'getPHID');6667$interfaces = id(new AlmanacInterfaceQuery())68->setParentQuery($this)69->setViewer($this->getViewer())70->withPHIDs($interface_phids)71->needProperties($this->getNeedProperties())72->execute();73$interfaces = mpull($interfaces, null, 'getPHID');7475foreach ($bindings as $key => $binding) {76$service = idx($services, $binding->getServicePHID());77$device = idx($devices, $binding->getDevicePHID());78$interface = idx($interfaces, $binding->getInterfacePHID());79if (!$service || !$device || !$interface) {80$this->didRejectResult($binding);81unset($bindings[$key]);82continue;83}8485$binding->attachService($service);86$binding->attachDevice($device);87$binding->attachInterface($interface);88}8990return $bindings;91}9293protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {94$where = parent::buildWhereClauseParts($conn);9596if ($this->ids !== null) {97$where[] = qsprintf(98$conn,99'binding.id IN (%Ld)',100$this->ids);101}102103if ($this->phids !== null) {104$where[] = qsprintf(105$conn,106'binding.phid IN (%Ls)',107$this->phids);108}109110if ($this->servicePHIDs !== null) {111$where[] = qsprintf(112$conn,113'binding.servicePHID IN (%Ls)',114$this->servicePHIDs);115}116117if ($this->devicePHIDs !== null) {118$where[] = qsprintf(119$conn,120'binding.devicePHID IN (%Ls)',121$this->devicePHIDs);122}123124if ($this->interfacePHIDs !== null) {125$where[] = qsprintf(126$conn,127'binding.interfacePHID IN (%Ls)',128$this->interfacePHIDs);129}130131if ($this->isActive !== null) {132if ($this->isActive) {133$where[] = qsprintf(134$conn,135'(binding.isDisabled = 0) AND (device.status IN (%Ls))',136AlmanacDeviceStatus::getActiveStatusList());137} else {138$where[] = qsprintf(139$conn,140'(binding.isDisabled = 1) OR (device.status IN (%Ls))',141AlmanacDeviceStatus::getDisabledStatusList());142}143}144145return $where;146}147148protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {149$joins = parent::buildJoinClauseParts($conn);150151if ($this->shouldJoinDeviceTable()) {152$device_table = new AlmanacDevice();153$joins[] = qsprintf(154$conn,155'JOIN %R device ON binding.devicePHID = device.phid',156$device_table);157}158159return $joins;160}161162private function shouldJoinDeviceTable() {163if ($this->isActive !== null) {164return true;165}166167return false;168}169170protected function getPrimaryTableAlias() {171return 'binding';172}173174}175176177