-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBaseSimple.php
More file actions
476 lines (419 loc) · 15.4 KB
/
BaseSimple.php
File metadata and controls
476 lines (419 loc) · 15.4 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
<?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 <c.schiffler@cyberspectrum.de>
* @author David Maack <david.maack@arcor.de>
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author David Molineus <david.molineus@netzmacht.de>
* @author Marc Reimann <reimann@mediendepot-ruhr.de>
* @copyright 2012-2024 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace MetaModels\Attribute;
use Contao\System;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use MetaModels\Helper\TableManipulator;
use MetaModels\IMetaModel;
use function array_map;
use function is_array;
use function serialize;
use function str_replace;
/**
* Reference implementation for Simple attributes.
* Simple fields are fields that only consist of one column in the metamodel table and therefore do not need
* to be handled as complex fields must be.
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class BaseSimple extends Base implements ISimple
{
use ManagedAttributeTrait;
/**
* Database connection.
*
* @var Connection
*/
protected $connection;
/**
* Table manipulator.
*
* @var TableManipulator
*/
protected $tableManipulator;
/**
* Instantiate an MetaModel attribute.
*
* Note that you should not use this directly but use the factory classes to instantiate attributes.
*
* @param IMetaModel $objMetaModel The MetaModel instance this attribute belongs to.
* @param array $arrData The information array, for attribute information, refer to
* documentation of table tl_metamodel_attribute and documentation
* of the certain attribute classes for information what values are
* understood.
* @param Connection|null $connection The database connection.
* @param TableManipulator|null $tableManipulator Table manipulator instance.
*/
public function __construct(
IMetaModel $objMetaModel,
$arrData = [],
?Connection $connection = null,
?TableManipulator $tableManipulator = null
) {
parent::__construct($objMetaModel, $arrData);
if (null === $connection) {
// @codingStandardsIgnoreStart
@trigger_error(
'Connection is missing. It has to be passed in the constructor. Fallback will be dropped.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
$connection = System::getContainer()->get('database_connection');
assert($connection instanceof Connection);
}
$this->connection = $connection;
if (null === $tableManipulator) {
// @codingStandardsIgnoreStart
@trigger_error(
'Table manipulator is missing. It has to be passed in the constructor. Fallback will be dropped.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
$tableManipulator = System::getContainer()->get('metamodels.table_manipulator');
assert($tableManipulator instanceof TableManipulator);
}
$this->tableManipulator = $tableManipulator;
}
/**
* Updates the meta information of the attribute.
*
* This tells the attribute to perform any actions that must be done to correctly initialize the new value
* and to perform any action to undo the changes that had been done for the previous value.
* i.e.: when an attribute type needs columns in an auxiliary table, these will have to be updated herein.
*
* This method may throw an exception, when the new value is invalid or any problems appear, the MetaModelAttribute
* will then keep the old meta value.
*
* @param string $strMetaName Name of the meta information that shall be updated.
* @param mixed $varNewValue The new value for this meta information.
*
* @return IAttribute The instance of this attribute, to support chaining.
*
* @throws Exception
*
* @deprecated Implement schema generators instead.
*/
#[\Override]
public function handleMetaChange($strMetaName, $varNewValue)
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
return $this;
}
// By default, we accept any change of meta information.
if ($strMetaName === 'colname') {
if ($this->get($strMetaName) !== $varNewValue) {
/** @psalm-suppress DeprecatedMethod */
$this->renameColumn($varNewValue);
}
return $this;
}
/** @psalm-suppress DeprecatedMethod */
return parent::handleMetaChange($strMetaName, $varNewValue);
}
/**
* This method is called to store the data for certain items to the database.
*
* @param mixed $arrValues The values to be stored into database. Mapping is item id=>value.
*
* @return void
*
* @throws Exception
*/
#[\Override]
public function setDataFor($arrValues)
{
$strTable = $this->getMetaModel()->getTableName();
$strColName = $this->getColName();
foreach ($arrValues as $intId => $varData) {
$this->connection->createQueryBuilder()
->update($strTable, 't')
->where('t.id=:id')
->set('t.' . $strColName, ':' . $strColName)
->setParameter($strColName, is_array($varData) ? serialize($varData) : $varData)
->setParameter('id', $intId)
->executeQuery();
}
}
/**
* {@inheritDoc}
*
* @throws Exception
*/
#[\Override]
public function getFilterOptions($idList, $usedOnly, &$arrCount = null)
{
// If empty list, return empty result. See also #379 for discussion.
if ($idList === []) {
return [];
}
$strCol = $this->getColName();
if ($idList) {
$statement = $this->connection->createQueryBuilder()
->select('t.' . $strCol . ', COUNT(t.' . $strCol . ') as mm_count')
->from($this->getMetaModel()->getTableName(), 't')
->where('t.id IN (:ids)')
->groupBy('t.' . $strCol)
->orderBy('MIN(FIELD(t.id, :ids))')
->setParameter('ids', $idList, ArrayParameterType::STRING)
->executeQuery();
} else {
$statement = $this->connection->createQueryBuilder()
->select('t.' . $strCol . ', COUNT(t.' . $strCol . ') as mm_count')
->from($this->getMetaModel()->getTableName(), 't')
->groupBy('t.' . $strCol)
->orderBy('t.' . $strCol)
->executeQuery();
}
$arrResult = [];
while ($objRow = $statement->fetchAssociative()) {
if (is_array($arrCount)) {
$arrCount[$objRow[$strCol]] = $objRow['mm_count'];
}
$arrResult[$objRow[$strCol]] = $objRow[$strCol];
}
return $arrResult;
}
/**
* {@inheritdoc}
*
* This base implementation does a plain SQL sort by native value as defined by MySQL.
*
* @throws Exception
*/
#[\Override]
public function sortIds($idList, $strDirection)
{
// Base implementation, do a simple sorting on given column.
$statement = $this->connection
->createQueryBuilder()
->select('t.id')
->from($this->getMetaModel()->getTableName(), 't')
->where('t.id IN (:ids)')
->setParameter('ids', $idList, ArrayParameterType::STRING)
->orderBy('t.' . $this->getColName(), $strDirection)
->executeQuery();
// Return value list as list<mixed>, parent function wants a list<string> so we make a cast.
return array_map(static fn (mixed $value) => (string) $value, $statement->fetchFirstColumn());
}
/**
* {@inheritdoc}
*
* Base implementation, perform string matching search.
*/
#[\Override]
public function searchFor($strPattern)
{
// Base implementation, do a simple search on given column.
$strPattern = str_replace(['*', '?'], ['%', '_'], $strPattern);
$statement = $this->connection
->createQueryBuilder()
->select('t.id')
->from($this->getMetaModel()->getTableName(), 't')
->where('t.' . $this->getColName() . ' LIKE :pattern')
->setParameter('pattern', $strPattern)
->executeQuery();
// Return value list as list<mixed>, parent function wants a list<string> so we make a cast.
return array_map(static fn (mixed $value) => (string) $value, $statement->fetchFirstColumn());
}
/**
* Returns the SQL primitive type declaration in MySQL notation.
*
* In this base class a sane value of "blob" allowing NULL is used.
* Deriving classes SHOULD override this function.
*
* @return string 'blob NULL'
*
* @deprecated Implement schema generators instead - see #1267.
*/
#[\Override]
public function getSQLDataType()
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
}
return 'blob NULL';
}
/**
* Create auxiliary data like a column in the MetaModel table or references in another table etc.
*
* @return void
*
* @deprecated Implement schema generators instead - see #1267.
*/
#[\Override]
public function destroyAUX()
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
return;
}
$this->triggerDeprecationIsUnmanagedAttribute(static::class, __METHOD__);
/** @psalm-suppress DeprecatedMethod */
parent::destroyAUX();
/** @psalm-suppress DeprecatedMethod */
$this->deleteColumn();
}
/**
* Delete all auxiliary data like a column in the MetaModel table or references in another table etc.
*
* @return void
*
* @throws Exception
*
* @deprecated Implement schema generators instead - see #1267.
*/
#[\Override]
public function initializeAUX()
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
return;
}
$this->triggerDeprecationIsUnmanagedAttribute(static::class, __METHOD__);
/** @psalm-suppress DeprecatedMethod */
parent::initializeAUX();
/** @psalm-suppress DeprecatedMethod */
$this->createColumn();
}
/**
* Creates the underlying database structure for this field.
*
* You have to override this function in field types, when you want to have multi column structure etc.
*
* @return void
*
* @throws Exception
*
* @deprecated Implement schema generators instead - see #1267.
*/
#[\Override]
public function createColumn()
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
return;
}
$this->triggerDeprecationIsUnmanagedAttribute(static::class, __METHOD__);
if ($this->getColName()) {
/** @psalm-suppress DeprecatedMethod */
$this->tableManipulator->createColumn(
$this->getMetaModel()->getTableName(),
$this->getColName(),
$this->getSQLDataType()
);
}
}
/**
* Removes the underlying database structure for this field.
*
* @return void
*
* @throws Exception
*
* @deprecated Implement schema generators instead - see #1267.
*/
#[\Override]
public function deleteColumn()
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
return;
}
$this->triggerDeprecationIsUnmanagedAttribute(static::class, __METHOD__);
$schemaManager = $this->connection->createSchemaManager();
$columns = $schemaManager->listTableColumns($this->getMetaModel()->getTableName());
// Try to delete the column. If it does not exist as we can assume it has been deleted already then.
if ($this->getColName() && isset($columns[$this->getColName()])) {
$this->tableManipulator->dropColumn($this->getMetaModel()->getTableName(), $this->getColName());
}
}
/**
* Renames the underlying database structure for this field.
*
* @param string $strNewColumnName The new column name.
*
* @return void
*
* @throws Exception
*
* @deprecated Implement schema generators instead - see #1267.
*/
#[\Override]
public function renameColumn($strNewColumnName)
{
if ($this->isManagedAttribute($this->get('type'))) {
$this->triggerDeprecationShouldNotCallManaged(static::class, __METHOD__);
return;
}
$this->triggerDeprecationIsUnmanagedAttribute(static::class, __METHOD__);
$this->tableManipulator->checkColumnName($strNewColumnName);
$schemaManager = $this->connection->createSchemaManager();
$columns = $schemaManager->listTableIndexes($this->getMetaModel()->getTableName());
if ($this->getColName() && isset($columns[$this->getColName()])) {
/** @psalm-suppress DeprecatedMethod */
$this->tableManipulator->renameColumn(
$this->getMetaModel()->getTableName(),
$this->getColName(),
$strNewColumnName,
$this->getSQLDataType()
);
} else {
$strBackupColName = $this->getColName();
$this->set('colname', $strNewColumnName);
/** @psalm-suppress DeprecatedMethod */
$this->createColumn();
$this->set('colname', $strBackupColName);
}
}
/**
* Take the raw data from the DB column and unserialize it.
*
* @param string $value The input value.
*
* @return mixed
*/
#[\Override]
public function unserializeData($value)
{
return $value;
}
/**
* Take the unserialized data and serialize it for the native DB column.
*
* @param mixed $value The input value.
*
* @return string|null
*/
#[\Override]
public function serializeData($value)
{
if (empty($value)) {
return null;
}
return (string) $value;
}
}