1 | <?php |
||
32 | abstract class IntegrationField extends SortableFields |
||
33 | { |
||
34 | /** |
||
35 | * @inheritdoc |
||
36 | */ |
||
37 | const TARGET_ATTRIBUTE = IntegrationAssociation::TARGET_ATTRIBUTE; |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | const SOURCE_ATTRIBUTE = IntegrationAssociation::SOURCE_ATTRIBUTE; |
||
43 | |||
44 | /** |
||
45 | * @return IntegrationAssociations |
||
46 | */ |
||
47 | abstract protected function associationService(): IntegrationAssociations; |
||
48 | |||
49 | /** |
||
50 | * @var Integrations[] |
||
51 | */ |
||
52 | private $fields = []; |
||
53 | |||
54 | /** |
||
55 | * @var IntegrationActionInterface[] |
||
56 | */ |
||
57 | protected $defaultAvailableActions = []; |
||
58 | |||
59 | /** |
||
60 | * @var IntegrationItemActionInterface[] |
||
61 | */ |
||
62 | protected $defaultAvailableItemActions = []; |
||
63 | |||
64 | /** |
||
65 | * @param int $id |
||
66 | * @return Integrations|null |
||
67 | */ |
||
68 | public function findById(int $id) |
||
81 | |||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | * @param Integrations $field |
||
86 | * @return IntegrationAssociationQuery |
||
87 | * @throws Exception |
||
88 | */ |
||
89 | public function getQuery( |
||
103 | |||
104 | /** |
||
105 | * @param FieldInterface $field |
||
106 | * @param ElementInterface|null $element |
||
107 | * @return IntegrationAssociationQuery |
||
108 | * @throws Exception |
||
109 | */ |
||
110 | private function baseQuery( |
||
131 | |||
132 | |||
133 | /******************************************* |
||
134 | * NORMALIZE VALUE |
||
135 | *******************************************/ |
||
136 | |||
137 | /** |
||
138 | * @inheritdoc |
||
139 | * @throws \Throwable |
||
140 | */ |
||
141 | protected function normalizeQueryInputValue( |
||
169 | |||
170 | /** |
||
171 | * @param Integrations $field |
||
172 | * @param IntegrationAssociationQuery $query |
||
173 | * @param ElementInterface|null $element |
||
174 | * @param bool $static |
||
175 | * @return null|string |
||
176 | * @throws Exception |
||
177 | * @throws \Twig_Error_Loader |
||
178 | */ |
||
179 | public function getInputHtml( |
||
180 | Integrations $field, |
||
181 | IntegrationAssociationQuery $query, |
||
182 | ElementInterface $element = null, |
||
183 | bool $static |
||
184 | ) { |
||
185 | Craft::$app->getView()->registerAssetBundle(IntegrationsAsset::class); |
||
186 | |||
187 | return Craft::$app->getView()->renderTemplate( |
||
188 | $field::INPUT_TEMPLATE_PATH, |
||
189 | [ |
||
190 | 'field' => $field, |
||
191 | 'element' => $element, |
||
192 | 'value' => $query, |
||
193 | 'objectLabel' => $this->getObjectLabel($field), |
||
194 | 'static' => $static, |
||
195 | 'itemTemplate' => $field::INPUT_ITEM_TEMPLATE_PATH, |
||
196 | 'settings' => [ |
||
197 | 'translationCategory' => $field::TRANSLATION_CATEGORY, |
||
198 | 'limit' => $field->max ? $field->max : null, |
||
199 | 'data' => [ |
||
200 | 'field' => $field->id, |
||
201 | 'element' => $element ? $element->getId() : null |
||
202 | ], |
||
203 | 'actions' => $this->getActionHtml($field, $element), |
||
204 | 'actionAction' => $field::ACTION_PREFORM_ACTION_PATH, |
||
205 | 'createItemAction' => $field::ACTION_CREATE_ITEM_PATH, |
||
206 | 'itemData' => [ |
||
207 | 'field' => $field->id, |
||
208 | 'element' => $element ? $element->getId() : null |
||
209 | ], |
||
210 | 'itemSettings' => [ |
||
211 | 'translationCategory' => $field::TRANSLATION_CATEGORY, |
||
212 | 'actionAction' => $field::ACTION_PREFORM_ITEM_ACTION_PATH, |
||
213 | 'associateAction' => $field::ACTION_ASSOCIATION_ITEM_PATH, |
||
214 | 'dissociateAction' => $field::ACTION_DISSOCIATION_ITEM_PATH, |
||
215 | 'data' => [ |
||
216 | 'field' => $field->id, |
||
217 | 'element' => $element ? $element->getId() : null |
||
218 | ], |
||
219 | 'actions' => $this->getItemActionHtml($field, $element), |
||
220 | ] |
||
221 | ] |
||
222 | ] |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param Integrations $field |
||
228 | * @return null|string |
||
229 | * @throws Exception |
||
230 | * @throws \Twig_Error_Loader |
||
231 | */ |
||
232 | public function getSettingsHtml( |
||
245 | |||
246 | |||
247 | /******************************************* |
||
248 | * OBJECT |
||
249 | *******************************************/ |
||
250 | |||
251 | /** |
||
252 | * @param Integrations $field |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getObjectLabel(Integrations $field): string |
||
259 | |||
260 | /******************************************* |
||
261 | * ACTIONS |
||
262 | *******************************************/ |
||
263 | |||
264 | /** |
||
265 | * @param Integrations $field |
||
266 | * @return IntegrationActionInterface[] |
||
267 | * @throws \craft\errors\MissingComponentException |
||
268 | * @throws \yii\base\InvalidConfigException |
||
269 | */ |
||
270 | public function getAvailableActions(Integrations $field): array |
||
286 | |||
287 | /** |
||
288 | * @param Integrations $field |
||
289 | * @param ElementInterface|null $element |
||
290 | * @return IntegrationActionInterface[] |
||
291 | * @throws \craft\errors\MissingComponentException |
||
292 | * @throws \yii\base\InvalidConfigException |
||
293 | */ |
||
294 | public function getActions(Integrations $field, ElementInterface $element = null): array |
||
311 | |||
312 | /** |
||
313 | * @param Integrations $field |
||
314 | * @return IntegrationActionInterface[] |
||
315 | * @throws \craft\errors\MissingComponentException |
||
316 | * @throws \yii\base\InvalidConfigException |
||
317 | */ |
||
318 | public function getAvailableItemActions(Integrations $field): array |
||
334 | |||
335 | /** |
||
336 | * @param Integrations $field |
||
337 | * @param ElementInterface|null $element |
||
338 | * @return IntegrationItemActionInterface[] |
||
339 | * @throws \craft\errors\MissingComponentException |
||
340 | * @throws \yii\base\InvalidConfigException |
||
341 | */ |
||
342 | public function getItemActions(Integrations $field, ElementInterface $element = null): array |
||
359 | |||
360 | /** |
||
361 | * @param array $actions |
||
362 | * @param string $instance |
||
363 | * @return array |
||
364 | * @throws \craft\errors\MissingComponentException |
||
365 | * @throws \yii\base\InvalidConfigException |
||
366 | */ |
||
367 | protected function resolveActions(array $actions, string $instance) |
||
382 | |||
383 | /** |
||
384 | * @param Integrations $field |
||
385 | * @param ElementInterface|null $element |
||
386 | * @return array |
||
387 | * @throws \craft\errors\MissingComponentException |
||
388 | * @throws \yii\base\InvalidConfigException |
||
389 | */ |
||
390 | protected function getActionHtml(Integrations $field, ElementInterface $element = null): array |
||
406 | |||
407 | /** |
||
408 | * @param Integrations $field |
||
409 | * @param ElementInterface|null $element |
||
410 | * @return array |
||
411 | * @throws \craft\errors\MissingComponentException |
||
412 | * @throws \yii\base\InvalidConfigException |
||
413 | */ |
||
414 | protected function getItemActionHtml(Integrations $field, ElementInterface $element = null): array |
||
430 | } |
||
431 |