-
Notifications
You must be signed in to change notification settings - Fork 42
/
InputScreenInformationBuilder.php
508 lines (468 loc) · 17.6 KB
/
InputScreenInformationBuilder.php
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
505
506
507
508
<?php
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2024 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 Christian Schiffler <[email protected]>
* @author David Molineus <[email protected]>
* @author Sven Baumann <[email protected]>
* @author Ingolf Steinhardt <[email protected]>
* @copyright 2012-2024 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace MetaModels\ViewCombination;
use Contao\StringUtil;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use MetaModels\IFactory;
use MetaModels\IMetaModel;
use MetaModels\ITranslatedMetaModel;
/**
* This class obtains information from the database about input screens.
*/
class InputScreenInformationBuilder
{
/**
* The database connection.
*
* @var Connection
*/
private Connection $connection;
/**
* The MetaModels factory.
*
* @var IFactory
*/
private IFactory $factory;
/**
* Create a new instance.
*
* @param Connection $connection The database connection.
* @param IFactory $factory The MetaModels factory.
*/
public function __construct(Connection $connection, IFactory $factory)
{
$this->connection = $connection;
$this->factory = $factory;
}
/**
* Fetch information about all input screens.
*
* @return array
*
* @throws Exception
*/
public function fetchAllInputScreensForTable(string $tableName): array
{
$builder = $this->connection->createQueryBuilder();
$screens = $builder
->select('d.*')
->from('tl_metamodel_dca', 'd')
->leftJoin('d', 'tl_metamodel', 'm', 'm.id=d.pid')
->where($builder->expr()->in('m.tableName', ':tableName'))
->setParameter('tableName', $tableName)
->orderBy('m.sorting')
->executeQuery()
->fetchAllAssociative();
$result = [];
foreach ($screens as $screen) {
$result[$screen['id']] = $this->prepareInputScreen($tableName, $screen);
// FIXME: simplify prepareInputScreen to not translate the values inline but define translation keys.
}
return $result;
}
/**
* Fetch information about an input screen.
*
* @param array $idList The ids of the input screens to obtain (table name => id).
*
* @return array
* @throws Exception
*/
public function fetchInputScreens($idList): array
{
$idList = \array_filter($idList);
$builder = $this->connection->createQueryBuilder();
$screens = $builder
->select('d.*')
->from('tl_metamodel_dca', 'd')
->leftJoin('d', 'tl_metamodel', 'm', 'm.id=d.pid')
->where($builder->expr()->in('d.id', ':idList'))
->setParameter('idList', $idList, ArrayParameterType::STRING)
->orderBy('m.sorting')
->executeQuery()
->fetchAllAssociative();
$result = [];
$keys = \array_flip($idList);
foreach ($screens as $screen) {
$metaModelName = $keys[$screen['id']];
$result[$metaModelName] = $this->prepareInputScreen($metaModelName, $screen);
}
return $result;
}
/**
* Prepare the input screen data.
*
* @param string $modelName The MetaModel name.
* @param array $screen The screen meta data.
*
* @return array{
* meta: array,
* properties: array,
* conditions: list<array{setting_attr_id: string, ...<string, string>}>,
* groupSort: array,
* label: array<string, string>,
* description: array<string, string>,
* legends: array,
* }
*
* @throws \InvalidArgumentException When the MetaModel can not be retrieved.
*/
private function prepareInputScreen(string $modelName, array $screen): array
{
if (null === $metaModel = $this->factory->getMetaModel($modelName)) {
throw new \InvalidArgumentException('Could not retrieve MetaModel ' . $modelName);
}
$caption = ['' => $metaModel->getName()];
$description = ['' => ''];
$fallback = null;
/**
* @psalm-suppress DeprecatedMethod
* @psalm-suppress TooManyArguments
*/
if ($metaModel instanceof ITranslatedMetaModel) {
$fallback = $metaModel->getMainLanguage();
} elseif ($metaModel->isTranslated(false)) {
/** @psalm-suppress DeprecatedMethod */
$fallback = $metaModel->getFallbackLanguage();
}
foreach (StringUtil::deserialize($screen['backendcaption'], true) as $languageEntry) {
$langCode = $languageEntry['langcode'];
$caption[$langCode] = !empty($label = $languageEntry['label']) ? $label : $caption[''];
$description[$langCode] = !empty($title = $languageEntry['description']) ? $title : $description[''];
if ($fallback === $langCode) {
$caption[''] = $label;
$description[''] = $title;
}
}
$result = [
'meta' => $screen,
'properties' => $this->fetchPropertiesFor($screen['id'], $metaModel),
'conditions' => $this->fetchConditions($screen['id']),
'groupSort' => $this->fetchGroupSort($screen['id'], $metaModel),
'label' => $caption,
'description' => $description
];
$bySetting = $this->buildConditionTree($result['conditions']);
$result['legends'] = $this->convertLegends($result['properties'], $metaModel, $bySetting);
return $result;
}
/**
* Build condition tree.
*
* @param array $conditions The condition information.
*
* @return array
*/
private function buildConditionTree(array $conditions): array
{
// Build condition tree.
$conditionMap = [];
$bySetting = [];
foreach ($conditions as $condition) {
unset($converted);
$conditionId = $condition['id'];
$conditionPid = $condition['pid'];
$conditionSettingId = $condition['settingId'];
// Check if already mapped, if so, we need to set the values.
if (\array_key_exists($conditionId, $conditionMap)) {
$converted = &$conditionMap[$conditionId];
foreach ($condition as $key => $value) {
$converted[$key] = $value;
}
} else {
$converted = \array_slice($condition, 0);
$conditionMap[$conditionId] = &$converted;
}
// Is on root level - add to setting now.
if (empty($conditionPid)) {
/** @psalm-suppress UnsupportedReferenceUsage */
$bySetting[$conditionSettingId][] = &$converted;
continue;
}
// Is a child, check if parent already added.
if (!isset($conditionMap[$conditionPid])) {
$temp = ['children' => []];
$conditionMap[$conditionPid] = &$temp;
}
// Add child to parent now.
/** @psalm-suppress UnsupportedReferenceUsage */
$conditionMap[$conditionPid]['children'][] = &$converted;
}
return $bySetting;
}
/**
* Fetch all properties for the passed input screen.
*
* @param string $inputScreenId The input screen to obtain properties for.
* @param IMetaModel $metaModel The MetaModel to fetch properties for.
*
* @return array
* @throws Exception
*/
private function fetchPropertiesFor(string $inputScreenId, IMetaModel $metaModel): array
{
$builder = $this->connection->createQueryBuilder();
return \array_map(
static function ($column) use ($inputScreenId, $metaModel) {
if ('attribute' !== $column['dcatype']) {
return $column;
}
if (!($attribute = $metaModel->getAttributeById((int)$column['attr_id']))) {
// @codingStandardsIgnoreStart
@trigger_error(
'Unknown attribute "' . $column['attr_id'] . '" in input screen "' . $inputScreenId . '"',
E_USER_WARNING
);
// @codingStandardsIgnoreEnd
return $column;
}
$column = \array_merge(
$column,
$attribute->getFieldDefinition($column),
['col_name' => $attribute->getColName()]
);
return $column;
},
$builder
->select('t.*')
->from('tl_metamodel_dcasetting', 't')
->where('t.pid=:pid')
->andWhere('t.published=:published')
->setParameter('pid', $inputScreenId)
->setParameter('published', 1)
->orderBy('t.sorting')
->executeQuery()
->fetchAllAssociative()
);
}
/**
* Fetch conditions for an input screen.
*
* @param string $inputScreenId The input screen to obtain conditions for.
*
* @return list<array{setting_attr_id: string, ...<string, string>}>
*/
private function fetchConditions(string $inputScreenId): array
{
$builder = $this->connection->createQueryBuilder();
return $builder
->select('cond.*', 'setting.attr_id AS setting_attr_id')
->from('tl_metamodel_dcasetting_condition', 'cond')
->leftJoin('cond', 'tl_metamodel_dcasetting', 'setting', 'cond.settingId=setting.id')
->leftJoin('setting', 'tl_metamodel_dca', 'dca', 'setting.pid=dca.id')
->where('cond.enabled=1')
->andWhere('setting.published=1')
->andWhere('dca.id=:screenId')
->setParameter('screenId', $inputScreenId)
->orderBy('cond.pid')
->addOrderBy('cond.sorting')
->executeQuery()
->fetchAllAssociative();
}
/**
* Fetch groupings for an input screen.
*
* @param string $inputScreenId The input screen to obtain properties for.
* @param IMetaModel $metaModel The MetaModel to fetch properties for.
*
* @return array
*
* @throws Exception
*/
private function fetchGroupSort(string $inputScreenId, IMetaModel $metaModel): array
{
$builder = $this->connection->createQueryBuilder();
return \array_map(
static function ($information) use ($inputScreenId, $metaModel) {
$information['isdefault'] = (bool)$information['isdefault'];
$information['ismanualsort'] = (bool)$information['ismanualsort'];
$information['rendergrouplen'] = (int)$information['rendergrouplen'];
if ($information['ismanualsort']) {
$information['rendergrouptype'] = 'none';
}
if (!empty($information['rendersortattr'])) {
if (!($attribute = $metaModel->getAttributeById((int)$information['rendersortattr']))) {
// @codingStandardsIgnoreStart
@trigger_error(
\sprintf(
'Unknown attribute "%1$s" in group sorting "%2$s.%3$s"',
$information['rendersortattr'],
$inputScreenId,
$information['id']
),
E_USER_WARNING
);
// @codingStandardsIgnoreEnd
return $information;
}
$information['col_name'] = $attribute->getColName();
}
return $information;
},
$builder
->select('t.*')
->from('tl_metamodel_dca_sortgroup', 't')
->where('t.pid=:screenId')
->setParameter('screenId', $inputScreenId)
->orderBy('t.sorting')
->executeQuery()
->fetchAllAssociative()
);
}
/**
* Convert property list to legend list.
*
* @param array $properties The property and legend information.
* @param IMetaModel $metaModel The MetaModel to fetch properties for.
* @param array $conditions The conditions for the entries.
*
* @return array
*/
private function convertLegends(array $properties, IMetaModel $metaModel, array $conditions): array
{
$result = [];
/**
* @psalm-suppress DeprecatedMethod
* @psalm-suppress TooManyArguments
*/
$trans = (($metaModel instanceof ITranslatedMetaModel) || $metaModel->isTranslated(false));
$label = $this->buildLabel($trans, $metaModel);
$legend = [
'label' => $label,
'hide' => false,
'properties' => []
];
$condition = function (array $property) use ($conditions): ?array {
if (!isset($conditions[($property['id'])])) {
return null;
}
return [
'type' => 'conditionand',
'children' => $conditions[$property['id']]
];
};
$fallbackLanguage = null;
/**
* @psalm-suppress DeprecatedMethod
* @psalm-suppress TooManyArguments
*/
if ($metaModel instanceof ITranslatedMetaModel) {
$fallbackLanguage = $metaModel->getMainLanguage();
} elseif ($metaModel->isTranslated(false)) {
/** @psalm-suppress DeprecatedMethod */
$fallbackLanguage = $metaModel->getFallbackLanguage();
}
foreach ($properties as $property) {
switch ($property['dcatype']) {
case 'legend':
$this->convertLegend($property, $fallbackLanguage, $condition, $legend, $result);
break;
case 'attribute':
$this->convertAttribute($property, $condition, $legend);
break;
default:
break;
}
}
if (!empty($legend['properties'])) {
$result['legend' . (\count($result) + 1)] = $legend;
}
return $result;
}
/**
* Build the label.
*
* @param bool $isTranslated Flag if the MetaModel is translated.
* @param IMetaModel $metaModel The MetaModel instance.
*
* @return array
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
private function buildLabel(bool $isTranslated, IMetaModel $metaModel): array
{
if ($isTranslated) {
$label = [];
/** @psalm-suppress DeprecatedMethod */
foreach (
($metaModel instanceof ITranslatedMetaModel)
? $metaModel->getLanguages()
: ($metaModel->getAvailableLanguages() ?? []) as $availableLanguage
) {
$label[$availableLanguage] = $metaModel->getName();
}
$label['default'] = $metaModel->getName();
return $label;
}
return ['default' => $metaModel->getName()];
}
/**
* Convert a legend property.
*
* @param array $property The property information to convert.
* @param string|null $fallback The fallback language if the MetaModel is translated or null otherwise.
* @param callable $condition The condition transformer.
* @param array $legend The current legend information.
* @param array $result The resulting information.
*
* @return void
*/
private function convertLegend(array $property, ?string $fallback, $condition, array &$legend, array &$result)
{
if (!empty($legend['properties'])) {
$result['legend' . (\count($result) + 1)] = $legend;
}
if (null === $fallback) {
$label = ['default' => $property['legendtitle']];
} else {
$label = unserialize($property['legendtitle'], ['allowed_classes' => false]);
$label['default'] = $label[$fallback];
}
$legend = [
'label' => $label,
'hide' => (bool) $property['legendhide'],
'properties' => [],
'condition' => $condition($property)
];
}
/**
* Convert an attribute property.
*
* @param array $property The property information to convert.
* @param callable $condition The condition transformer.
* @param array $legend The current legend information.
*
* @return void
*/
private function convertAttribute(array $property, $condition, array &$legend)
{
if (!isset($property['col_name'])) {
return;
}
$legend['properties'][] = [
'name' => $property['col_name'],
'condition' => $condition($property)
];
}
}