1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-element-lists/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-element-lists/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\element\lists\fields; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\Element; |
13
|
|
|
use craft\base\ElementInterface; |
14
|
|
|
use craft\elements\db\ElementQueryInterface; |
15
|
|
|
use craft\helpers\ArrayHelper; |
16
|
|
|
use flipbox\craft\elements\nestedIndex\web\assets\index\NestedElementIndex; |
17
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 2.0.0 |
22
|
|
|
* |
23
|
|
|
* @property string $inputTemplate |
24
|
|
|
* @property string $inputJsClass |
25
|
|
|
* @property string $handle |
26
|
|
|
* @property int $id |
27
|
|
|
* @property string|null $viewMode |
28
|
|
|
* @property int|null $limit |
29
|
|
|
* @property string $selectionLabel |
30
|
|
|
*/ |
31
|
|
|
trait InputTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Returns the element class associated with this field type. |
35
|
|
|
* |
36
|
|
|
* @return string The Element class name |
37
|
|
|
*/ |
38
|
|
|
abstract protected static function elementType(): string; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns an array of the source keys the field should be able to select elements from. |
42
|
|
|
* |
43
|
|
|
* @param ElementInterface|null $element |
44
|
|
|
* @return array|string |
45
|
|
|
*/ |
46
|
|
|
abstract protected function inputSources(ElementInterface $element = null); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param null $value |
50
|
|
|
* @param ElementInterface|null $element |
51
|
|
|
* @return array |
52
|
|
|
* @throws \yii\base\InvalidConfigException |
53
|
|
|
*/ |
54
|
|
|
protected function inputTemplateVariables($value = null, ElementInterface $element = null): array |
55
|
|
|
{ |
56
|
|
|
if ($value instanceof ElementQueryInterface) { |
57
|
|
|
$value = $value |
58
|
|
|
->anyStatus() |
59
|
|
|
->ids(); |
60
|
|
|
} elseif (!is_array($value)) { |
61
|
|
|
$value = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
Craft::$app->getView()->registerAssetBundle(NestedElementIndex::class); |
65
|
|
|
|
66
|
|
|
$sources = Craft::$app->getElementIndexes()->getSources(static::elementType()); |
67
|
|
|
foreach ($sources as &$source) { |
68
|
|
|
ArrayHelper::remove($source, 'structureEditable'); |
69
|
|
|
ArrayHelper::remove($source, 'structureId'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return [ |
73
|
|
|
'sources' => $sources, |
74
|
|
|
'element' => $element, |
75
|
|
|
'container' => 'nested-index-' . $this->handle, |
76
|
|
|
'elementType' => static::elementType(), |
77
|
|
|
'inputJsClass' => $this->inputJsClass, |
78
|
|
|
'inputJs' => $this->getInputJs($value, $element), |
79
|
|
|
'indexJsClass' => 'Craft.NestedElementIndex', |
80
|
|
|
'indexJs' => $this->getIndexJs($element) |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/******************************************* |
85
|
|
|
* JS CONFIGS |
86
|
|
|
*******************************************/ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $elementIds |
90
|
|
|
* @param ElementInterface $element |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function getInputJs(array $elementIds, ElementInterface $element = null): array |
94
|
|
|
{ |
95
|
|
|
/** @var Element $element */ |
96
|
|
|
$siteId = SiteHelper::ensureSiteId($element ? $element->siteId : null); |
97
|
|
|
|
98
|
|
|
$selectionCriteria = [ |
99
|
|
|
'enabledForSite' => null, |
100
|
|
|
'siteId' => $siteId |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
return [ |
104
|
|
|
'elementType' => static::elementType(), |
105
|
|
|
'sources' => $this->inputSources($element), |
106
|
|
|
'criteria' => $selectionCriteria, |
107
|
|
|
'sourceElementId' => $element->getId() ?: null, |
108
|
|
|
'viewMode' => $this->viewMode, |
109
|
|
|
'limit' => $this->limit, |
110
|
|
|
'selectionLabel' => $this->selectionLabel, |
111
|
|
|
'storageKey' => 'nested.index.input.' . $this->handle, |
112
|
|
|
'elements' => $elementIds, |
113
|
|
|
'addAction' => 'element-lists/source/associate', |
114
|
|
|
'selectTargetAttribute' => 'target', |
115
|
|
|
'selectParams' => [ |
116
|
|
|
'source' => $element->getId() ?: null, |
117
|
|
|
'field' => $this->id |
118
|
|
|
] |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param ElementInterface $element |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
private function getIndexJs(ElementInterface $element = null): array |
127
|
|
|
{ |
128
|
|
|
|
129
|
|
|
/** @var Element $element */ |
130
|
|
|
$elementId = ($element !== null && $element->getId() !== null) ? $element->getId() : false; |
131
|
|
|
|
132
|
|
|
return [ |
133
|
|
|
'source' => 'nested', |
134
|
|
|
'context' => 'index', |
135
|
|
|
'viewMode' => $this->viewMode, |
136
|
|
|
'showStatusMenu' => true, |
137
|
|
|
'showSiteMenu' => true, |
138
|
|
|
'hideSidebar' => false, |
139
|
|
|
'toolbarFixed' => false, |
140
|
|
|
'storageKey' => 'nested.index.' . $this->handle, |
141
|
|
|
'updateElementsAction' => 'element-lists/element-indexes/get-elements', |
142
|
|
|
'submitActionsAction' => 'element-lists/element-indexes/perform-action', |
143
|
|
|
'loadMoreElementsAction' => 'element-lists/element-indexes/get-more-elements', |
144
|
|
|
'criteria' => [ |
145
|
|
|
'enabledForSite' => null, |
146
|
|
|
'siteId' => SiteHelper::ensureSiteId($element ? $element->siteId : null), |
147
|
|
|
$this->handle => $elementId |
148
|
|
|
], |
149
|
|
|
'viewParams' => [ |
150
|
|
|
'sourceId' => $elementId, |
151
|
|
|
'fieldId' => $this->id |
152
|
|
|
] |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|