1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spoon plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Enhance Matrix |
6
|
|
|
* |
7
|
|
|
* @link https://angell.io |
8
|
|
|
* @copyright Copyright (c) 2018 Angell & Co |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace angellco\spoon\services; |
12
|
|
|
|
13
|
|
|
use angellco\spoon\assetbundles\ConfiguratorAsset; |
14
|
|
|
use angellco\spoon\assetbundles\FieldManipulatorAsset; |
15
|
|
|
use angellco\spoon\Spoon; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\base\Component; |
19
|
|
|
use craft\helpers\Json; |
20
|
|
|
use craft\models\Section; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Loader methods |
24
|
|
|
* |
25
|
|
|
* @author Angell & Co |
26
|
|
|
* @package Spoon |
27
|
|
|
* @since 3.0.0 |
28
|
|
|
*/ |
29
|
|
|
class Loader extends Component |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Loads the configurator and field manipulator code in all the |
34
|
|
|
* core supported contexts as well as providing a hook for |
35
|
|
|
* third-party contexts. |
36
|
|
|
* |
37
|
|
|
* @throws \yii\base\InvalidConfigException |
38
|
|
|
*/ |
39
|
|
|
public function run() |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
// Check the conditions are right to run |
43
|
|
|
if (Craft::$app->request->isCpRequest && !Craft::$app->request->getAcceptsJson()) |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
$segments = Craft::$app->request->getSegments(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check third party plugin support |
50
|
|
|
*/ |
51
|
|
|
$commercePlugin = \Craft::$app->plugins->getPlugin('commerce'); |
52
|
|
|
$calendarPlugin = \Craft::$app->plugins->getPlugin('calendar'); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Work out the context for the block type groups configuration |
56
|
|
|
*/ |
57
|
|
|
// Entry types |
58
|
|
|
if (count($segments) === 5 |
59
|
|
|
&& $segments[0] === 'settings' |
60
|
|
|
&& $segments[1] === 'sections' |
61
|
|
|
&& $segments[3] === 'entrytypes' |
62
|
|
|
&& $segments[4] !== 'new' |
63
|
|
|
) |
64
|
|
|
{ |
65
|
|
|
$this->configurator('#fieldlayoutform', 'entrytype:'.$segments[4]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Category groups |
69
|
|
|
if (count($segments) === 3 |
70
|
|
|
&& $segments[0] === 'settings' |
71
|
|
|
&& $segments[1] === 'categories' |
72
|
|
|
&& $segments[2] !== 'new' |
73
|
|
|
) |
74
|
|
|
{ |
75
|
|
|
$this->configurator('#fieldlayoutform', 'categorygroup:'.$segments[2]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Global sets |
79
|
|
|
if (count($segments) === 3 |
80
|
|
|
&& $segments[0] === 'settings' |
81
|
|
|
&& $segments[1] === 'globals' |
82
|
|
|
&& $segments[2] !== 'new' |
83
|
|
|
) |
84
|
|
|
{ |
85
|
|
|
$this->configurator('#fieldlayoutform', 'globalset:'.$segments[2]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Users |
89
|
|
|
if (count($segments) === 2 |
90
|
|
|
&& $segments[0] === 'settings' |
91
|
|
|
&& $segments[1] === 'users' |
92
|
|
|
) |
93
|
|
|
{ |
94
|
|
|
$this->configurator('#fieldlayoutform', 'users'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Solpace Calendar |
98
|
|
|
if (count($segments) === 3 |
99
|
|
|
&& $segments[0] === 'calendar' |
100
|
|
|
&& $segments[1] === 'calendars' |
101
|
|
|
&& $segments[2] !== 'new' |
102
|
|
|
&& $calendarPlugin |
103
|
|
|
) |
104
|
|
|
{ |
105
|
|
|
$calendarService = new \Solspace\Calendar\Services\CalendarsService(); |
|
|
|
|
106
|
|
|
if ($calendarService) { |
|
|
|
|
107
|
|
|
$calendar = $calendarService->getCalendarByHandle(end($segments)); |
108
|
|
|
if ($calendar) { |
109
|
|
|
$this->configurator('#fieldlayoutform', 'calendar:'.$calendar->id); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Commerce |
115
|
|
|
if (count($segments) === 4 |
116
|
|
|
&& $segments[0] === 'commerce' |
117
|
|
|
&& $segments[1] === 'settings' |
118
|
|
|
&& $segments[2] === 'producttypes' |
119
|
|
|
&& $segments[3] !== 'new' |
120
|
|
|
&& $commercePlugin |
121
|
|
|
) { |
122
|
|
|
$productTypesService = new \craft\commerce\services\ProductTypes(); |
|
|
|
|
123
|
|
|
if ($productTypesService) { |
|
|
|
|
124
|
|
|
$productType = $productTypesService->getProductTypeById($segments[3]); |
125
|
|
|
if ($productType) { |
126
|
|
|
$this->configurator('#fieldlayoutform', 'producttype:'.$productType->id); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Work out the context for the Matrix field manipulation |
133
|
|
|
*/ |
134
|
|
|
// Global |
135
|
|
|
$context = 'global'; |
136
|
|
|
$versioned = false; |
137
|
|
|
|
138
|
|
|
// Entry types |
139
|
|
|
if (count($segments) >= 3 && $segments[0] === 'entries') { |
140
|
|
|
|
141
|
|
|
if ($segments[2] === 'new') { |
142
|
|
|
/** @var Section $section */ |
143
|
|
|
$section = Craft::$app->sections->getSectionByHandle($segments[1]); |
144
|
|
|
$sectionEntryTypes = $section->getEntryTypes(); |
145
|
|
|
$entryType = reset($sectionEntryTypes); |
146
|
|
|
} else { |
147
|
|
|
$entryId = (integer)explode('-', $segments[2])[0]; |
148
|
|
|
$entry = Craft::$app->entries->getEntryById($entryId); |
149
|
|
|
|
150
|
|
|
if ($entry) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$entryType = $entry->type; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (isset($segments[3]) && $segments[3] === 'versions') { |
157
|
|
|
$versioned = true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (isset($entryType) && $entryType) { |
161
|
|
|
$context = 'entrytype:'.$entryType->id; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
// Category groups |
166
|
|
|
else if (count($segments) >= 3 && $segments[0] === 'categories') |
167
|
|
|
{ |
168
|
|
|
$group = Craft::$app->categories->getGroupByHandle($segments[1]); |
169
|
|
|
if ($group) |
170
|
|
|
{ |
171
|
|
|
$context = 'categorygroup:'.$group->id; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
// Global sets |
175
|
|
|
else if (count($segments) >= 2 && $segments[0] === 'globals') |
176
|
|
|
{ |
177
|
|
|
$set = Craft::$app->globals->getSetByHandle(end($segments)); |
178
|
|
|
if ($set) |
179
|
|
|
{ |
180
|
|
|
$context = 'globalset:'.$set->id; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
// Users |
184
|
|
|
else if ((count($segments) === 1 && $segments[0] === 'myaccount') || (count($segments) == 2 && $segments[0] === 'users')) |
185
|
|
|
{ |
186
|
|
|
$context = 'users'; |
187
|
|
|
} |
188
|
|
|
// Solspace Calendar |
189
|
|
|
else if (count($segments) >= 4 |
190
|
|
|
&& $segments[0] === 'calendar' |
191
|
|
|
&& $segments[1] === 'events' |
192
|
|
|
&& $calendarPlugin |
193
|
|
|
) { |
194
|
|
|
|
195
|
|
|
if ($segments[2] === 'new') { |
196
|
|
|
$calendarService = new \Solspace\Calendar\Services\CalendarsService(); |
197
|
|
|
$calendar = $calendarService->getCalendarByHandle($segments[3]); |
198
|
|
|
if ($calendar) { |
199
|
|
|
$context = 'calendar:'.$calendar->id; |
200
|
|
|
} |
201
|
|
|
} else { |
202
|
|
|
$calendarEventsService = new \Solspace\Calendar\Services\EventsService(); |
|
|
|
|
203
|
|
|
if ($calendarEventsService) { |
|
|
|
|
204
|
|
|
$event = $calendarEventsService->getEventById($segments[2]); |
205
|
|
|
if ($event) { |
206
|
|
|
$context = 'calendar:'.$event->getCalendar()->id; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
// Commerce |
212
|
|
|
else if (count($segments) >= 3 |
213
|
|
|
&& $segments[0] === 'commerce' |
214
|
|
|
&& $segments[1] === 'products' |
215
|
|
|
&& $commercePlugin |
216
|
|
|
) { |
217
|
|
|
$productTypesService = new \craft\commerce\services\ProductTypes(); |
218
|
|
|
if ($productTypesService) { |
|
|
|
|
219
|
|
|
$productType = $productTypesService->getProductTypeByHandle($segments[2]); |
220
|
|
|
if ($productType) { |
221
|
|
|
$context = 'producttype:'.$productType->id; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// Run the field manipulation code |
227
|
|
|
$this->fieldManipulator($context, $versioned); |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Loads a Spoon.Configurator() for the correct context |
235
|
|
|
* |
236
|
|
|
* @param $container |
237
|
|
|
* @param $context |
238
|
|
|
* |
239
|
|
|
* @throws \yii\base\InvalidConfigException |
240
|
|
|
*/ |
241
|
|
|
public function configurator($container, $context) |
242
|
|
|
{ |
243
|
|
|
|
244
|
|
|
$view = Craft::$app->getView(); |
245
|
|
|
|
246
|
|
|
$view->registerAssetBundle(ConfiguratorAsset::class); |
247
|
|
|
|
248
|
|
|
$settings = [ |
249
|
|
|
'matrixFieldIds' => Spoon::$plugin->fields->getMatrixFieldIds(), |
250
|
|
|
'context' => $context |
251
|
|
|
]; |
252
|
|
|
|
253
|
|
|
$view->registerJs('new Spoon.Configurator("'.$container.'", '.Json::encode($settings, JSON_UNESCAPED_UNICODE).');'); |
254
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Loads a Spoon.FieldManipulator() for the correct context |
259
|
|
|
* |
260
|
|
|
* @param $context |
261
|
|
|
* @param bool $versioned |
262
|
|
|
* |
263
|
|
|
* @throws \yii\base\InvalidConfigException |
264
|
|
|
*/ |
265
|
|
|
public function fieldManipulator($context, $versioned = false) |
266
|
|
|
{ |
267
|
|
|
|
268
|
|
|
// Get global data |
269
|
|
|
$globalSpoonedBlockTypes = Spoon::$plugin->blockTypes->getByContext('global', 'context'); |
270
|
|
|
|
271
|
|
|
// Get all the data for the entrytype context regardless of entrytype id |
272
|
|
|
$mainContext = explode(':', $context)[0]; |
273
|
|
|
$contextSpoonedBlockTypes = Spoon::$plugin->blockTypes->getByContext($mainContext, 'context', true); |
274
|
|
|
|
275
|
|
|
$spoonedBlockTypes = array_merge($globalSpoonedBlockTypes, $contextSpoonedBlockTypes); |
276
|
|
|
|
277
|
|
|
if ($spoonedBlockTypes) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
$view = Craft::$app->getView(); |
280
|
|
|
|
281
|
|
|
$view->registerAssetBundle(FieldManipulatorAsset::class); |
282
|
|
|
|
283
|
|
|
$translations = []; |
284
|
|
|
|
285
|
|
|
foreach ($spoonedBlockTypes as $spoonedBlockTypesInContext) { |
286
|
|
|
foreach ($spoonedBlockTypesInContext as $spoonedBlockType) { |
287
|
|
|
$translations[] = $spoonedBlockType->groupName; |
288
|
|
|
$translations[] = $spoonedBlockType->matrixBlockType->name; |
289
|
|
|
|
290
|
|
|
foreach ($spoonedBlockType->fieldLayoutModel['tabs'] as $tab) { |
291
|
|
|
$translations[] = $tab->name; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$view->registerTranslations('site', $translations); |
298
|
|
|
|
299
|
|
|
$settings = [ |
300
|
|
|
'blockTypes' => $spoonedBlockTypes, |
301
|
|
|
'context' => $context, |
302
|
|
|
'versioned' => $versioned, |
303
|
|
|
'nestedSettingsHandles' => Spoon::$plugin->getSettings()->nestedSettings |
304
|
|
|
]; |
305
|
|
|
|
306
|
|
|
$view->registerJs('if (typeof Craft.MatrixInput !== "undefined") { Spoon.fieldmanipulator = new Spoon.FieldManipulator('.Json::encode($settings, JSON_UNESCAPED_UNICODE).') };'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths