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