Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/query/AlmanacBindingQuery.php
12256 views
1
<?php
2
3
final class AlmanacBindingQuery
4
extends AlmanacQuery {
5
6
private $ids;
7
private $phids;
8
private $servicePHIDs;
9
private $devicePHIDs;
10
private $interfacePHIDs;
11
private $isActive;
12
13
public function withIDs(array $ids) {
14
$this->ids = $ids;
15
return $this;
16
}
17
18
public function withPHIDs(array $phids) {
19
$this->phids = $phids;
20
return $this;
21
}
22
23
public function withServicePHIDs(array $phids) {
24
$this->servicePHIDs = $phids;
25
return $this;
26
}
27
28
public function withDevicePHIDs(array $phids) {
29
$this->devicePHIDs = $phids;
30
return $this;
31
}
32
33
public function withInterfacePHIDs(array $phids) {
34
$this->interfacePHIDs = $phids;
35
return $this;
36
}
37
38
public function withIsActive($active) {
39
$this->isActive = $active;
40
return $this;
41
}
42
43
public function newResultObject() {
44
return new AlmanacBinding();
45
}
46
47
protected function willFilterPage(array $bindings) {
48
$service_phids = mpull($bindings, 'getServicePHID');
49
$device_phids = mpull($bindings, 'getDevicePHID');
50
$interface_phids = mpull($bindings, 'getInterfacePHID');
51
52
$services = id(new AlmanacServiceQuery())
53
->setParentQuery($this)
54
->setViewer($this->getViewer())
55
->withPHIDs($service_phids)
56
->needProperties($this->getNeedProperties())
57
->execute();
58
$services = mpull($services, null, 'getPHID');
59
60
$devices = id(new AlmanacDeviceQuery())
61
->setParentQuery($this)
62
->setViewer($this->getViewer())
63
->withPHIDs($device_phids)
64
->needProperties($this->getNeedProperties())
65
->execute();
66
$devices = mpull($devices, null, 'getPHID');
67
68
$interfaces = id(new AlmanacInterfaceQuery())
69
->setParentQuery($this)
70
->setViewer($this->getViewer())
71
->withPHIDs($interface_phids)
72
->needProperties($this->getNeedProperties())
73
->execute();
74
$interfaces = mpull($interfaces, null, 'getPHID');
75
76
foreach ($bindings as $key => $binding) {
77
$service = idx($services, $binding->getServicePHID());
78
$device = idx($devices, $binding->getDevicePHID());
79
$interface = idx($interfaces, $binding->getInterfacePHID());
80
if (!$service || !$device || !$interface) {
81
$this->didRejectResult($binding);
82
unset($bindings[$key]);
83
continue;
84
}
85
86
$binding->attachService($service);
87
$binding->attachDevice($device);
88
$binding->attachInterface($interface);
89
}
90
91
return $bindings;
92
}
93
94
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
95
$where = parent::buildWhereClauseParts($conn);
96
97
if ($this->ids !== null) {
98
$where[] = qsprintf(
99
$conn,
100
'binding.id IN (%Ld)',
101
$this->ids);
102
}
103
104
if ($this->phids !== null) {
105
$where[] = qsprintf(
106
$conn,
107
'binding.phid IN (%Ls)',
108
$this->phids);
109
}
110
111
if ($this->servicePHIDs !== null) {
112
$where[] = qsprintf(
113
$conn,
114
'binding.servicePHID IN (%Ls)',
115
$this->servicePHIDs);
116
}
117
118
if ($this->devicePHIDs !== null) {
119
$where[] = qsprintf(
120
$conn,
121
'binding.devicePHID IN (%Ls)',
122
$this->devicePHIDs);
123
}
124
125
if ($this->interfacePHIDs !== null) {
126
$where[] = qsprintf(
127
$conn,
128
'binding.interfacePHID IN (%Ls)',
129
$this->interfacePHIDs);
130
}
131
132
if ($this->isActive !== null) {
133
if ($this->isActive) {
134
$where[] = qsprintf(
135
$conn,
136
'(binding.isDisabled = 0) AND (device.status IN (%Ls))',
137
AlmanacDeviceStatus::getActiveStatusList());
138
} else {
139
$where[] = qsprintf(
140
$conn,
141
'(binding.isDisabled = 1) OR (device.status IN (%Ls))',
142
AlmanacDeviceStatus::getDisabledStatusList());
143
}
144
}
145
146
return $where;
147
}
148
149
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
150
$joins = parent::buildJoinClauseParts($conn);
151
152
if ($this->shouldJoinDeviceTable()) {
153
$device_table = new AlmanacDevice();
154
$joins[] = qsprintf(
155
$conn,
156
'JOIN %R device ON binding.devicePHID = device.phid',
157
$device_table);
158
}
159
160
return $joins;
161
}
162
163
private function shouldJoinDeviceTable() {
164
if ($this->isActive !== null) {
165
return true;
166
}
167
168
return false;
169
}
170
171
protected function getPrimaryTableAlias() {
172
return 'binding';
173
}
174
175
}
176
177