-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathListControllerTrait.php
More file actions
504 lines (455 loc) · 18.1 KB
/
ListControllerTrait.php
File metadata and controls
504 lines (455 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2025 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/core
* @author Richard Henkenjohann <richardhenkenjohann@googlemail.com>
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Ingolf Steinhardt <info@e-spin.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @copyright 2012-2025 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace MetaModels\CoreBundle\Controller;
use Contao\BackendTemplate;
use Contao\CoreBundle\Csrf\ContaoCsrfTokenManager;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Input;
use Contao\Model;
use Contao\StringUtil;
use Contao\System;
use Contao\Template;
use MetaModels\Filter\FilterUrl;
use MetaModels\Filter\FilterUrlBuilder;
use MetaModels\Filter\Setting\IFilterSettingFactory;
use MetaModels\Helper\SortingLinkGenerator;
use MetaModels\IFactory;
use MetaModels\IItem;
use MetaModels\IMetaModel;
use MetaModels\ItemList;
use MetaModels\Render\Setting\IRenderSettingFactory;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Helper trait for lists (CE and MOD).
*/
trait ListControllerTrait
{
/**
* The filter setting factory.
*
* @var IFilterSettingFactory
*/
private IFilterSettingFactory $filterFactory;
/**
* The MetaModels factory.
*
* @var IFactory
*/
private IFactory $factory;
/**
* The event dispatcher.
*
* @var EventDispatcherInterface
*/
private EventDispatcherInterface $eventDispatcher;
/**
* The render setting factory.
*
* @var IRenderSettingFactory
*/
private IRenderSettingFactory $renderSettingFactory;
/**
* The filter url builder.
*
* @var FilterUrlBuilder
*/
private FilterUrlBuilder $filterUrlBuilder;
/**
* The translator.
*
* @var TranslatorInterface
*/
private TranslatorInterface $translator;
/**
* The router.
*
* @var RouterInterface
*/
private RouterInterface $router;
/**
* The scope matcher.
*
* @var ScopeMatcher
*/
private ScopeMatcher $scopeMatcher;
/**
* ItemListController constructor.
*
* @param IFactory $factory The MetaModels factory (required in MetaModels 3.0).
* @param IFilterSettingFactory $filterFactory The filter setting factory (required in MetaModels 3.0).
* @param IRenderSettingFactory $renderSettingFactory The render setting factory (required in MetaModels 3.0).
* @param EventDispatcherInterface $eventDispatcher The event dispatcher (required in MetaModels 3.0).
* @param FilterUrlBuilder $filterUrlBuilder The filter url builder.
* @param TranslatorInterface|null $translator The translator.
* @param RouterInterface|null $router The router.
* @param ScopeMatcher|null $scopeMatcher The scope matcher.
*/
public function __construct(
IFactory $factory,
IFilterSettingFactory $filterFactory,
IRenderSettingFactory $renderSettingFactory,
EventDispatcherInterface $eventDispatcher,
FilterUrlBuilder $filterUrlBuilder,
?TranslatorInterface $translator = null,
?RouterInterface $router = null,
?ScopeMatcher $scopeMatcher = null
) {
$this->factory = $factory;
$this->filterFactory = $filterFactory;
$this->renderSettingFactory = $renderSettingFactory;
$this->eventDispatcher = $eventDispatcher;
$this->filterUrlBuilder = $filterUrlBuilder;
if (null === $translator) {
// @codingStandardsIgnoreStart
@trigger_error(
'Translator is missing. It has to be passed in the constructor. Fallback will be dropped.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
$translator = System::getContainer()->get('translator');
assert($translator instanceof TranslatorInterface);
}
$this->translator = $translator;
if (null === $router) {
// @codingStandardsIgnoreStart
@trigger_error(
'Router is missing. It has to be passed in the constructor. Fallback will be dropped.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
$router = System::getContainer()->get('router');
assert($router instanceof RouterInterface);
}
$this->router = $router;
if (null === $scopeMatcher) {
// @codingStandardsIgnoreStart
@trigger_error(
'ScopeMatcher is missing. It has to be passed in the constructor. Fallback will be dropped.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
$scopeMatcher = System::getContainer()->get('contao.routing.scope_matcher');
assert($scopeMatcher instanceof ScopeMatcher);
}
$this->scopeMatcher = $scopeMatcher;
}
/**
* Generate the response.
*
* @param Template $template The template.
* @param Model $model The content model.
* @param Request $request The request.
*
* @return Response The response.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*
* @psalm-suppress DeprecatedClass
*/
private function getResponseInternal(Template $template, Model $model, Request $request): Response
{
/** @psalm-suppress UndefinedMagicPropertyFetch */
if ('' === ($pageParam = $model->metamodel_page_param)) {
/** @psalm-suppress UndefinedMagicPropertyFetch */
switch ($model->type) {
case 'metamodel_content':
$pageParam = 'page_mmce' . $model->id;
break;
case 'metamodel_list':
$pageParam = 'page_mmfm' . $model->id;
break;
default:
$pageParam = 'page_mm' . $model->id;
}
}
/** @psalm-suppress UndefinedMagicPropertyFetch */
$itemRenderer = new ItemList(
$this->factory,
$this->filterFactory,
$this->renderSettingFactory,
$this->eventDispatcher,
$this->filterUrlBuilder,
$pageParam,
$model->metamodel_page_param_type,
$model->metamodel_maxpaginationlinks,
$model->metamodel_pagination,
$model->metamodel_pagination_urlfragment
);
/**
* @psalm-suppress UndefinedMagicPropertyAssignment
* @psalm-suppress UndefinedMagicPropertyFetch
*/
$template->searchable = !$model->metamodel_donotindex;
/** @psalm-suppress UndefinedMagicPropertyFetch */
$sorting = $model->metamodel_sortby;
/** @psalm-suppress UndefinedMagicPropertyFetch */
$direction = $model->metamodel_sortby_direction;
/** @psalm-suppress UndefinedMagicPropertyFetch */
$sortParamType = $model->metamodel_sort_param_type;
$sortOrderByParam = 'orderBy';
$sortOrderDirParam = 'orderDir';
/** @psalm-suppress UndefinedMagicPropertyFetch */
$sortOverride = $model->metamodel_sort_override;
/** @psalm-suppress UndefinedMagicPropertyFetch */
$sortFragment = $model->metamodel_sort_urlfragment;
// @codingStandardsIgnoreStart
// FIXME: filter URL should be created from local request and not from master request.
// @codingStandardsIgnoreEnd
$filterUrl = $this->filterUrlBuilder->getCurrentFilterUrl();
if ($sortOverride) {
/** @psalm-suppress UndefinedMagicPropertyFetch */
$sortOrderByParam = $model->metamodel_order_by_param ?: $sortOrderByParam;
/** @psalm-suppress UndefinedMagicPropertyFetch */
$sortOrderDirParam = $model->metamodel_order_dir_param ?: $sortOrderDirParam;
if (
null !==
$value = $this->tryReadFromSlugOrGet(
$filterUrl,
$sortOrderByParam,
$sortParamType
)
) {
$sorting = $value;
}
if (
null !==
$value = $this->tryReadFromSlugOrGet(
$filterUrl,
$sortOrderDirParam,
$sortParamType
)
) {
$direction = $value;
}
}
/** @psalm-suppress UndefinedMagicPropertyFetch */
$filterParams = StringUtil::deserialize($model->metamodel_filterparams, true);
/** @psalm-suppress UndefinedMagicPropertyFetch */
$itemRenderer
->setMetaModel($model->metamodel, $model->metamodel_rendersettings)
->setListTemplate($template)
->setLimit($model->metamodel_use_limit, $model->metamodel_offset, $model->metamodel_limit)
->setPageBreak($model->perPage)
->setSorting($sorting, $direction)
->setFilterSettings($model->metamodel_filtering)
->setFilterParameters($filterParams, $this->getFilterParameters($filterUrl, $itemRenderer))
->setMetaTags($model->metamodel_meta_title, $model->metamodel_meta_description);
if ($sortOverride) {
$itemRenderer->setSortingLinkGenerator(
new SortingLinkGenerator(
$this->filterUrlBuilder,
$this->translator,
$sortParamType,
$sortOrderByParam,
$sortOrderDirParam,
$sortFragment,
$model->metamodel_sortby,
$model->metamodel_sortby_direction
)
);
}
/** @psalm-suppress UndefinedMagicPropertyFetch */
if ($model->metamodel_use_parameters) {
foreach (StringUtil::deserialize(($model->metamodel_parameters ?? null), true) as $key => $value) {
$itemRenderer->setTemplateParameter($key, $value);
}
}
/**
* @psalm-suppress UndefinedMagicPropertyAssignment
* @psalm-suppress UndefinedMagicPropertyFetch
*/
$template->items = StringUtil::encodeEmail($itemRenderer->render($model->metamodel_noparsing, $model));
/** @psalm-suppress UndefinedMagicPropertyAssignment */
$template->numberOfItems = $itemRenderer->getItems()->getCount();
/** @psalm-suppress UndefinedMagicPropertyAssignment */
$template->pagination = $itemRenderer->getPagination();
/** @var \Iterator<int, IItem> $items */
$items = $itemRenderer->getItems();
$responseTags = \array_map(
static fn (IItem $item): string
=> \sprintf('contao.db.%s.%d', $item->getMetaModel()->getTableName(), $item->get('id')),
\iterator_to_array($items, false)
);
$response = $template->getResponse();
$this->tagResponse($responseTags);
return $response;
}
/**
* Retrieve all filter parameters from the input class for the specified filter setting.
*
* @param FilterUrl $filterUrl The filter URL to obtain parameters from.
* @param ItemList $itemRenderer The list renderer instance to be used.
*
* @return string[]
*/
private function getFilterParameters(FilterUrl $filterUrl, ItemList $itemRenderer): array
{
$result = [];
foreach ($itemRenderer->getFilterSettings()->getParameters() as $name) {
if (null !== $value = $this->tryReadFromSlugOrGet($filterUrl, $name, 'slugNget')) {
$result[$name] = $value;
}
}
return $result;
}
/**
* Get parameter from get or slug.
*
* @param FilterUrl $filterUrl The filter URL to obtain parameters from.
* @param string $sortParam The sort parameter name to obtain.
* @param string $sortType The sort URL type.
*
* @return string|null
*/
private function tryReadFromSlugOrGet(FilterUrl $filterUrl, string $sortParam, string $sortType): ?string
{
$result = null;
switch ($sortType) {
case 'get':
$result = $filterUrl->getGet($sortParam);
break;
case 'slug':
$result = $filterUrl->getSlug($sortParam);
break;
case 'slugNget':
$result = ($filterUrl->getGet($sortParam) ?? $filterUrl->getSlug($sortParam));
break;
default:
}
// Mark the parameter as used (otherwise, a 404 is thrown)
Input::get($sortParam);
return $result;
}
/**
* Return a back end wildcard response.
*
* @param string $href The edit href.
* @param string $name The name of the element.
* @param Model $model The database list model.
*
* @return Response The response.
*/
private function renderBackendWildcard(string $href, string $name, Model $model): Response
{
$template = new BackendTemplate('be_wildcard');
/** @psalm-suppress UndefinedMagicPropertyFetch */
$headline = StringUtil::deserialize($model->headline);
/** @psalm-suppress UndefinedMagicPropertyAssignment */
$template->wildcard = $this->getWildcardInfoText($model, $href, $name);
/** @psalm-suppress UndefinedMagicPropertyAssignment */
$template->title = (\is_array($headline) ? $headline['value'] : $headline);
/** @psalm-suppress UndefinedMagicPropertyAssignment */
$template->id = $model->id;
return new Response($template->parse());
}
/**
* Obtain the text for the wildcard.
*
* @param Model $model The database list model.
* @param string $href The edit href.
* @param string $name The name of the element.
*
* @return string
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
private function getWildcardInfoText(Model $model, string $href, string $name): string
{
/** @psalm-suppress UndefinedMagicPropertyFetch */
if (null === $model->metamodel) {
return 'MetaModel not configured.';
}
if ('' === ($metaModelName = $this->factory->translateIdToMetaModelName($model->metamodel))) {
return 'Unknown MetaModel: ' . $model->metamodel;
}
// Add CSS file.
$GLOBALS['TL_CSS'][] = '/bundles/metamodelscore/css/style.css';
// Retrieve name of MetaModel.
$infoTemplate =
'<div class="wc_info tl_gray"><span class="wc_label"><abbr title="%s">%s:</abbr></span> %s</div>';
$metaModel = $this->factory->getMetaModel($metaModelName);
assert($metaModel instanceof IMetaModel);
$tokenManager = System::getContainer()->get('contao.csrf.token_manager');
assert($tokenManager instanceof ContaoCsrfTokenManager);
$header = $metaModel->getName();
if ($href) {
$header .= \sprintf(
' (<a href="%1$s&rt=%2$s" class="tl_gray">ID: %3$s</a>)',
$href,
$tokenManager->getDefaultTokenValue(),
(string) $model->id
);
}
$infoText = '### ' . $name . ' ###' . \sprintf(
$infoTemplate,
$this->translator->trans('mm_be_info_name.description', [], 'metamodels_wildcard'),
$this->translator->trans('mm_be_info_name.label', [], 'metamodels_wildcard'),
$header,
);
// Retrieve name of filter.
/** @psalm-suppress UndefinedMagicPropertyFetch */
if ($model->metamodel_filtering) {
$filterparams = [];
/** @psalm-suppress UndefinedMagicPropertyFetch */
foreach (StringUtil::deserialize($model->metamodel_filterparams, true) as $filterparam) {
if ($filterparam['value']) {
$filterparams[] = $filterparam['value'];
}
}
$infoFiPa = \count($filterparams) ? ': ' . \implode(', ', $filterparams) : '';
$infoFi = $this->filterFactory->createCollection($model->metamodel_filtering)->get('name');
if (null !== $infoFi) {
$infoText .= \sprintf(
$infoTemplate,
$this->translator->trans('mm_be_info_filter.description', [], 'metamodels_wildcard'),
$this->translator->trans('mm_be_info_filter.label', [], 'metamodels_wildcard'),
$infoFi . $infoFiPa
);
}
}
// Retrieve name of render setting.
/** @psalm-suppress UndefinedMagicPropertyFetch */
if ($model->metamodel_rendersettings) {
$infoRs = $this->renderSettingFactory
->createCollection($metaModel, $model->metamodel_rendersettings)
->get('name');
if (null !== $infoRs) {
$infoText .= \sprintf(
$infoTemplate,
$this->translator->trans('mm_be_info_render_setting.description', [], 'metamodels_wildcard'),
$this->translator->trans('mm_be_info_render_setting.label', [], 'metamodels_wildcard'),
$infoRs
);
}
}
return $infoText . '<br>';
}
}