|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-integration/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\integration\fields; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\ElementInterface; |
|
13
|
|
|
use craft\base\Field; |
|
14
|
|
|
use craft\elements\db\ElementQueryInterface; |
|
15
|
|
|
use flipbox\craft\integration\db\IntegrationAssociationQuery; |
|
16
|
|
|
use flipbox\craft\integration\fields\actions\IntegrationActionInterface; |
|
17
|
|
|
use flipbox\craft\integration\fields\actions\IntegrationItemActionInterface; |
|
18
|
|
|
use flipbox\craft\integration\records\IntegrationAssociation; |
|
19
|
|
|
use flipbox\craft\integration\services\IntegrationAssociations; |
|
20
|
|
|
use flipbox\craft\integration\services\IntegrationField; |
|
21
|
|
|
use flipbox\ember\helpers\ModelHelper; |
|
22
|
|
|
use flipbox\ember\validators\MinMaxValidator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Flipbox Factory <[email protected]> |
|
26
|
|
|
* @since 1.0.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class Integrations extends Field |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The Plugin's translation category |
|
32
|
|
|
*/ |
|
33
|
|
|
const TRANSLATION_CATEGORY = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The action path to preform field actions |
|
37
|
|
|
*/ |
|
38
|
|
|
const ACTION_PREFORM_ACTION_PATH = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The action path to preform field actions |
|
42
|
|
|
*/ |
|
43
|
|
|
const ACTION_PREFORM_ITEM_ACTION_PATH = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The action path to associate an item |
|
47
|
|
|
*/ |
|
48
|
|
|
const ACTION_ASSOCIATION_ITEM_PATH = ''; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The action path to dissociate an item |
|
52
|
|
|
*/ |
|
53
|
|
|
const ACTION_DISSOCIATION_ITEM_PATH = ''; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The action path to create an integration object |
|
57
|
|
|
*/ |
|
58
|
|
|
const ACTION_CREATE_ITEM_PATH = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The action event name |
|
62
|
|
|
*/ |
|
63
|
|
|
const EVENT_REGISTER_ACTIONS = 'registerActions'; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The action event name |
|
67
|
|
|
*/ |
|
68
|
|
|
const EVENT_REGISTER_AVAILABLE_ACTIONS = 'registerAvailableActions'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The item action event name |
|
72
|
|
|
*/ |
|
73
|
|
|
const EVENT_REGISTER_ITEM_ACTIONS = 'registerItemActions'; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The item action event name |
|
77
|
|
|
*/ |
|
78
|
|
|
const EVENT_REGISTER_AVAILABLE_ITEM_ACTIONS = 'registerAvailableItemActions'; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The input template path |
|
82
|
|
|
*/ |
|
83
|
|
|
const INPUT_TEMPLATE_PATH = ''; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The input template path |
|
87
|
|
|
*/ |
|
88
|
|
|
const INPUT_ITEM_TEMPLATE_PATH = '_inputItem'; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* The input template path |
|
92
|
|
|
*/ |
|
93
|
|
|
const SETTINGS_TEMPLATE_PATH = ''; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var string |
|
97
|
|
|
*/ |
|
98
|
|
|
public $object; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @var int|null |
|
102
|
|
|
*/ |
|
103
|
|
|
public $min; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @var int|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public $max; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @var string |
|
112
|
|
|
*/ |
|
113
|
|
|
public $viewUrl = ''; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @var string |
|
117
|
|
|
*/ |
|
118
|
|
|
public $listUrl = ''; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @var IntegrationActionInterface[] |
|
122
|
|
|
*/ |
|
123
|
|
|
public $selectedActions = []; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @var IntegrationItemActionInterface[] |
|
127
|
|
|
*/ |
|
128
|
|
|
public $selectedItemActions = []; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var string|null |
|
132
|
|
|
*/ |
|
133
|
|
|
public $selectionLabel; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return IntegrationField |
|
137
|
|
|
*/ |
|
138
|
|
|
abstract protected function fieldService(): IntegrationField; |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return IntegrationAssociations |
|
142
|
|
|
*/ |
|
143
|
|
|
abstract protected function associationService(): IntegrationAssociations; |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @inheritdoc |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function hasContentColumn(): bool |
|
149
|
|
|
{ |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function defaultSelectionLabel(): string |
|
157
|
|
|
{ |
|
158
|
|
|
return Craft::t(static::TRANSLATION_CATEGORY, 'Add an Object'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/******************************************* |
|
162
|
|
|
* OBJECT |
|
163
|
|
|
*******************************************/ |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getObjectLabel(): string |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->fieldService()->getObjectLabel($this); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/******************************************* |
|
174
|
|
|
* VALIDATION |
|
175
|
|
|
*******************************************/ |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @inheritdoc |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getElementValidationRules(): array |
|
181
|
|
|
{ |
|
182
|
|
|
return [ |
|
183
|
|
|
[ |
|
184
|
|
|
MinMaxValidator::class, |
|
185
|
|
|
'min' => $this->min ? (int)$this->min : null, |
|
186
|
|
|
'max' => $this->max ? (int)$this->max : null, |
|
187
|
|
|
'tooFew' => Craft::t( |
|
188
|
|
|
static::TRANSLATION_CATEGORY, |
|
189
|
|
|
'{attribute} should contain at least {min, number} {min, plural, one{domain} other{domains}}.' |
|
190
|
|
|
), |
|
191
|
|
|
'tooMany' => Craft::t( |
|
192
|
|
|
static::TRANSLATION_CATEGORY, |
|
193
|
|
|
'{attribute} should contain at most {max, number} {max, plural, one{domain} other{domains}}.' |
|
194
|
|
|
), |
|
195
|
|
|
'skipOnEmpty' => false |
|
196
|
|
|
] |
|
197
|
|
|
]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
/******************************************* |
|
203
|
|
|
* VALUE |
|
204
|
|
|
*******************************************/ |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @inheritdoc |
|
208
|
|
|
*/ |
|
209
|
|
|
public function normalizeValue($value, ElementInterface $element = null) |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->fieldService()->normalizeValue( |
|
212
|
|
|
$this, |
|
213
|
|
|
$value, |
|
214
|
|
|
$element |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
/******************************************* |
|
220
|
|
|
* QUERY |
|
221
|
|
|
*******************************************/ |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @inheritdoc |
|
225
|
|
|
*/ |
|
226
|
|
|
public function modifyElementsQuery(ElementQueryInterface $query, $value) |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->fieldService()->modifyElementsQuery( |
|
229
|
|
|
$this, |
|
230
|
|
|
$query, |
|
231
|
|
|
$value |
|
232
|
|
|
); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/******************************************* |
|
237
|
|
|
* RULES |
|
238
|
|
|
*******************************************/ |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @inheritdoc |
|
242
|
|
|
*/ |
|
243
|
|
|
public function rules() |
|
244
|
|
|
{ |
|
245
|
|
|
return array_merge( |
|
246
|
|
|
parent::rules(), |
|
247
|
|
|
[ |
|
248
|
|
|
[ |
|
249
|
|
|
'object', |
|
250
|
|
|
'required', |
|
251
|
|
|
'message' => Craft::t(static::TRANSLATION_CATEGORY, 'Object cannot be empty.') |
|
252
|
|
|
], |
|
253
|
|
|
[ |
|
254
|
|
|
[ |
|
255
|
|
|
'object', |
|
256
|
|
|
'min', |
|
257
|
|
|
'max', |
|
258
|
|
|
'viewUrl', |
|
259
|
|
|
'listUrl', |
|
260
|
|
|
'selectionLabel' |
|
261
|
|
|
], |
|
262
|
|
|
'safe', |
|
263
|
|
|
'on' => [ |
|
264
|
|
|
ModelHelper::SCENARIO_DEFAULT |
|
265
|
|
|
] |
|
266
|
|
|
] |
|
267
|
|
|
] |
|
268
|
|
|
); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
/******************************************* |
|
273
|
|
|
* SEARCH |
|
274
|
|
|
*******************************************/ |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @param IntegrationAssociationQuery $value |
|
278
|
|
|
* @inheritdoc |
|
279
|
|
|
*/ |
|
280
|
|
|
public function getSearchKeywords($value, ElementInterface $element): string |
|
281
|
|
|
{ |
|
282
|
|
|
$objects = []; |
|
283
|
|
|
|
|
284
|
|
|
/** @var IntegrationAssociation $association */ |
|
285
|
|
|
foreach ($value->all() as $association) { |
|
286
|
|
|
array_push($objects, $association->objectId); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return parent::getSearchKeywords($objects, $element); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/******************************************* |
|
294
|
|
|
* VIEWS |
|
295
|
|
|
*******************************************/ |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @inheritdoc |
|
299
|
|
|
* @param IntegrationAssociationQuery $value |
|
300
|
|
|
* @throws \Twig_Error_Loader |
|
301
|
|
|
* @throws \yii\base\Exception |
|
302
|
|
|
*/ |
|
303
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
|
304
|
|
|
{ |
|
305
|
|
|
$value->limit(null); |
|
306
|
|
|
return $this->fieldService()->getInputHtml($this, $value, $element, false); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* @inheritdoc |
|
311
|
|
|
* @throws \Twig_Error_Loader |
|
312
|
|
|
* @throws \yii\base\Exception |
|
313
|
|
|
*/ |
|
314
|
|
|
public function getSettingsHtml() |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->fieldService()->getSettingsHtml($this); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
|
|
320
|
|
|
/******************************************* |
|
321
|
|
|
* EVENTS |
|
322
|
|
|
*******************************************/ |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @inheritdoc |
|
326
|
|
|
* @throws \Exception |
|
327
|
|
|
*/ |
|
328
|
|
|
public function afterElementSave(ElementInterface $element, bool $isNew) |
|
329
|
|
|
{ |
|
330
|
|
|
$this->associationService()->save( |
|
331
|
|
|
$element->getFieldValue($this->handle) |
|
332
|
|
|
); |
|
333
|
|
|
|
|
334
|
|
|
parent::afterElementSave($element, $isNew); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
/******************************************* |
|
339
|
|
|
* SETTINGS |
|
340
|
|
|
*******************************************/ |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @inheritdoc |
|
344
|
|
|
*/ |
|
345
|
|
|
public function settingsAttributes(): array |
|
346
|
|
|
{ |
|
347
|
|
|
return array_merge( |
|
348
|
|
|
[ |
|
349
|
|
|
'object', |
|
350
|
|
|
'min', |
|
351
|
|
|
'max', |
|
352
|
|
|
'viewUrl', |
|
353
|
|
|
'listUrl', |
|
354
|
|
|
'selectedActions', |
|
355
|
|
|
'selectedItemActions', |
|
356
|
|
|
'selectionLabel' |
|
357
|
|
|
], |
|
358
|
|
|
parent::settingsAttributes() |
|
359
|
|
|
); |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|