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