Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace Civi\Api4\Service\Schema\Joinable; |
21 | ||
22 | use Civi\Api4\Utils\CoreUtil; | |
23 | use CRM_Core_DAO_AllCoreTables as AllCoreTables; | |
24 | ||
25 | class Joinable { | |
26 | ||
27 | const JOIN_SIDE_LEFT = 'LEFT'; | |
28 | const JOIN_SIDE_INNER = 'INNER'; | |
29 | ||
30 | const JOIN_TYPE_ONE_TO_ONE = '1_to_1'; | |
31 | const JOIN_TYPE_MANY_TO_ONE = 'n_to_1'; | |
32 | const JOIN_TYPE_ONE_TO_MANY = '1_to_n'; | |
33 | ||
34 | /** | |
35 | * @var string | |
36 | */ | |
37 | protected $baseTable; | |
38 | ||
39 | /** | |
40 | * @var string | |
41 | */ | |
42 | protected $baseColumn; | |
43 | ||
44 | /** | |
45 | * @var string | |
46 | */ | |
47 | protected $targetTable; | |
48 | ||
49 | /** | |
50 | * @var string | |
51 | * | |
52 | * Name (or alias) of the target column) | |
53 | */ | |
54 | protected $targetColumn; | |
55 | ||
56 | /** | |
57 | * @var string | |
58 | */ | |
59 | protected $alias; | |
60 | ||
61 | /** | |
62 | * @var array | |
63 | */ | |
64 | protected $conditions = []; | |
65 | ||
66 | /** | |
67 | * @var string | |
68 | */ | |
69 | protected $joinSide = self::JOIN_SIDE_LEFT; | |
70 | ||
71 | /** | |
72 | * @var int | |
73 | */ | |
74 | protected $joinType = self::JOIN_TYPE_ONE_TO_ONE; | |
75 | ||
76 | /** | |
77 | * @var string | |
78 | */ | |
79 | protected $entity; | |
80 | ||
19b53e5b C |
81 | /** |
82 | * @param $targetTable | |
83 | * @param $targetColumn | |
84 | * @param string|null $alias | |
85 | */ | |
86 | public function __construct($targetTable, $targetColumn, $alias = NULL) { | |
87 | $this->targetTable = $targetTable; | |
88 | $this->targetColumn = $targetColumn; | |
89 | if (!$this->entity) { | |
90 | $this->entity = CoreUtil::getApiNameFromTableName($targetTable); | |
91 | } | |
92 | $this->alias = $alias ?: str_replace('civicrm_', '', $targetTable); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Gets conditions required when joining to a base table | |
97 | * | |
98 | * @param string|null $baseTableAlias | |
99 | * Name of the base table, if aliased. | |
100 | * | |
101 | * @return array | |
102 | */ | |
103 | public function getConditionsForJoin($baseTableAlias = NULL) { | |
104 | $baseCondition = sprintf( | |
105 | '%s.%s = %s.%s', | |
106 | $baseTableAlias ?: $this->baseTable, | |
107 | $this->baseColumn, | |
108 | $this->getAlias(), | |
109 | $this->targetColumn | |
110 | ); | |
111 | ||
112 | return array_merge([$baseCondition], $this->conditions); | |
113 | } | |
114 | ||
115 | /** | |
116 | * @return string | |
117 | */ | |
118 | public function getBaseTable() { | |
119 | return $this->baseTable; | |
120 | } | |
121 | ||
122 | /** | |
123 | * @param string $baseTable | |
124 | * | |
125 | * @return $this | |
126 | */ | |
127 | public function setBaseTable($baseTable) { | |
128 | $this->baseTable = $baseTable; | |
129 | ||
130 | return $this; | |
131 | } | |
132 | ||
133 | /** | |
134 | * @return string | |
135 | */ | |
136 | public function getBaseColumn() { | |
137 | return $this->baseColumn; | |
138 | } | |
139 | ||
140 | /** | |
141 | * @param string $baseColumn | |
142 | * | |
143 | * @return $this | |
144 | */ | |
145 | public function setBaseColumn($baseColumn) { | |
146 | $this->baseColumn = $baseColumn; | |
147 | ||
148 | return $this; | |
149 | } | |
150 | ||
151 | /** | |
152 | * @return string | |
153 | */ | |
154 | public function getAlias() { | |
155 | return $this->alias; | |
156 | } | |
157 | ||
158 | /** | |
159 | * @param string $alias | |
160 | * | |
161 | * @return $this | |
162 | */ | |
163 | public function setAlias($alias) { | |
164 | $this->alias = $alias; | |
165 | ||
166 | return $this; | |
167 | } | |
168 | ||
169 | /** | |
170 | * @return string | |
171 | */ | |
172 | public function getTargetTable() { | |
173 | return $this->targetTable; | |
174 | } | |
175 | ||
176 | /** | |
177 | * @return string | |
178 | */ | |
179 | public function getTargetColumn() { | |
180 | return $this->targetColumn; | |
181 | } | |
182 | ||
183 | /** | |
184 | * @return string | |
185 | */ | |
186 | public function getEntity() { | |
187 | return $this->entity; | |
188 | } | |
189 | ||
190 | /** | |
191 | * @param $condition | |
192 | * | |
193 | * @return $this | |
194 | */ | |
195 | public function addCondition($condition) { | |
196 | $this->conditions[] = $condition; | |
197 | ||
198 | return $this; | |
199 | } | |
200 | ||
201 | /** | |
202 | * @return array | |
203 | */ | |
204 | public function getExtraJoinConditions() { | |
205 | return $this->conditions; | |
206 | } | |
207 | ||
208 | /** | |
209 | * @param array $conditions | |
210 | * | |
211 | * @return $this | |
212 | */ | |
213 | public function setConditions($conditions) { | |
214 | $this->conditions = $conditions; | |
215 | ||
216 | return $this; | |
217 | } | |
218 | ||
219 | /** | |
220 | * @return string | |
221 | */ | |
222 | public function getJoinSide() { | |
223 | return $this->joinSide; | |
224 | } | |
225 | ||
226 | /** | |
227 | * @param string $joinSide | |
228 | * | |
229 | * @return $this | |
230 | */ | |
231 | public function setJoinSide($joinSide) { | |
232 | $this->joinSide = $joinSide; | |
233 | ||
234 | return $this; | |
235 | } | |
236 | ||
237 | /** | |
238 | * @return int | |
239 | */ | |
240 | public function getJoinType() { | |
241 | return $this->joinType; | |
242 | } | |
243 | ||
244 | /** | |
245 | * @param int $joinType | |
246 | * | |
247 | * @return $this | |
248 | */ | |
249 | public function setJoinType($joinType) { | |
250 | $this->joinType = $joinType; | |
251 | ||
252 | return $this; | |
253 | } | |
254 | ||
255 | /** | |
256 | * @return array | |
257 | */ | |
258 | public function toArray() { | |
259 | return get_object_vars($this); | |
260 | } | |
261 | ||
262 | /** | |
263 | * @return \Civi\Api4\Service\Spec\FieldSpec[] | |
264 | */ | |
265 | public function getEntityFields() { | |
721c9da1 CW |
266 | $entityFields = []; |
267 | $bao = AllCoreTables::getClassForTable($this->getTargetTable()); | |
268 | if ($bao) { | |
269 | foreach ($bao::getSupportedFields() as $field) { | |
270 | $entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, $this->getEntity()); | |
19b53e5b C |
271 | } |
272 | } | |
721c9da1 | 273 | return $entityFields; |
19b53e5b C |
274 | } |
275 | ||
39e0f675 CW |
276 | /** |
277 | * @return array | |
278 | */ | |
279 | public function getEntityFieldNames() { | |
280 | $fieldNames = []; | |
281 | foreach ($this->getEntityFields() as $fieldSpec) { | |
282 | $fieldNames[] = $fieldSpec->getName(); | |
283 | } | |
284 | return $fieldNames; | |
285 | } | |
286 | ||
19b53e5b C |
287 | /** |
288 | * @return \Civi\Api4\Service\Spec\FieldSpec|NULL | |
289 | */ | |
290 | public function getField($fieldName) { | |
291 | foreach ($this->getEntityFields() as $field) { | |
292 | if ($field->getName() === $fieldName) { | |
293 | return $field; | |
294 | } | |
295 | } | |
296 | return NULL; | |
297 | } | |
298 | ||
299 | } |