-
Notifications
You must be signed in to change notification settings - Fork 8
/
SubSelectPalettesListener.php
377 lines (338 loc) · 11.8 KB
/
SubSelectPalettesListener.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
<?php
/**
* This file is part of contao-community-alliance/meta-palettes.
*
* (c) 2015-2023 Contao Community Alliance.
*
* 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 contao-community-alliance/meta-palettes
* @author Tristan Lins <tristan.lins@bit3.de>
* @author David Molineus <david.molineus@netzmacht.de>
* @author Christopher Bölter <christopher@boelter.eu>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2014 bit3 UG
* @copyright 2015-2023 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/meta-palettes/license LGPL-3.0-or-later
* @filesource
*/
namespace ContaoCommunityAlliance\MetaPalettes\Listener;
use Contao\Config;
use Contao\DataContainer;
use Contao\DC_File;
use Contao\DC_Table;
use Contao\Input;
use Contao\System;
use ContaoCommunityAlliance\DcGeneral\Contao\Compatibility\DcCompat;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\MetaPalettes\MetaPalettes;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use const E_USER_DEPRECATED;
use const E_USER_ERROR;
/**
* Class SubSelectPalettesListener
*
* @package ContaoCommunityAlliance\MetaPalettes\Listener
*/
class SubSelectPalettesListener
{
/**
* Database connection.
*
* @var Connection
*/
private $connection;
/**
* SubSelectPalettesListener constructor.
*
* @param Connection $connection Database connection.
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* Handle the onlaod callback.
*
* @param DataContainer|null $dataContainer Data container driver.
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function onLoad($dataContainer = null)
{
// Break if no data container driver is given.
if (!$dataContainer instanceof DataContainer) {
return;
}
$strTable = $dataContainer->table;
// No subselect palettes registered.
if (empty($GLOBALS['TL_DCA'][$strTable]['metasubselectpalettes'])) {
return;
}
// Trigger the error.
if (!is_array($GLOBALS['TL_DCA'][$strTable]['metasubselectpalettes'])) {
$this->triggerInvalidSubSelectPalettesError($strTable);
return;
}
foreach ($GLOBALS['TL_DCA'][$strTable]['metasubselectpalettes'] as $strSelector => $arrPalettes) {
if (!is_array($arrPalettes)) {
$this->triggerSubselectPaletteError($strTable, $strSelector, $arrPalettes);
continue;
}
$strValue = $this->getValue($dataContainer, $strTable, $strSelector);
if ($strValue === null) {
continue;
}
$strValue = $this->invokeLoadCallback($dataContainer, $strTable, $strSelector, $strValue);
$strPalette = $this->buildPalette($arrPalettes, $strValue, $strTable);
$this->applyPalette($strTable, $strSelector, $strPalette);
}
}
/**
* Trigger the sub select palette error.
*
* @param string $strTable Table name.
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function triggerInvalidSubSelectPalettesError($strTable)
{
trigger_error(
sprintf(
'The field $GLOBALS[\'TL_DCA\'][\'%s\'][\'metasubselectpalettes\'] ' .
'has to be an array, %s given!',
$strTable,
gettype($GLOBALS['TL_DCA'][$strTable]['metasubselectpalettes'])
),
E_USER_ERROR
);
}
/**
* Trigger the subselect palette error.
*
* @param string $strTable Table name.
* @param string $strSelector Selector field name.
* @param array $arrPalettes Given palettes value.
*
* @return void
*/
protected function triggerSubselectPaletteError($strTable, $strSelector, $arrPalettes)
{
trigger_error(
sprintf(
'The field $GLOBALS[\'TL_DCA\'][\'%s\'][\'metasubselectpalettes\'][\'%s\'] ' .
'has to be an array, %s given!',
$strTable,
$strSelector,
gettype($arrPalettes)
),
E_USER_ERROR
);
}
/**
* Get the value.
*
* @param DataContainer $dataContainer Data container driver.
* @param string $strTable Table name.
* @param string $strSelector Selector field name.
*
* @return mixed|null
*
* @SuppressWarnings(PHPMD.Superglobals)
* @throws Exception
*/
private function getValue($dataContainer, $strTable, $strSelector)
{
$strValue = null;
if ($dataContainer instanceof DcCompat) {
$objModel = $dataContainer->getModel();
return $this->getValueFromDcGeneralModel($objModel, $strSelector);
}
// on post, use new value
if (Input::post('FORM_SUBMIT') == $strTable) {
return Input::post($strSelector);
}
// support for TL_CONFIG data container
if ($dataContainer instanceof DC_File) {
return Config::get($strSelector);
}
// try getting activeRecord value
if ($dataContainer->activeRecord) {
return $dataContainer->activeRecord->$strSelector;
}
// or break, when unable to handle data container
if ($dataContainer instanceof DC_Table
&& $this->connection->createSchemaManager()()->tablesExist([$dataContainer->table])
) {
return $this->fetchValueFromDatabase($dataContainer, $strSelector);
}
return $strValue;
}
/**
* Invoke the load callbacks.
*
* @param DataContainer $dataContainer Data container driver.
* @param string $strTable Data container table name.
* @param string $strSelector Selector field name.
* @param string $strValue Selector value.
*
* @return mixed
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function invokeLoadCallback($dataContainer, $strTable, $strSelector, $strValue)
{
// call load callback if the value is not result of a submit.
if ((Input::post('FORM_SUBMIT') != $strTable)
&& isset($GLOBALS['TL_DCA'][$strTable]['fields'][$strSelector]['load_callback'])
&& is_array($GLOBALS['TL_DCA'][$strTable]['fields'][$strSelector]['load_callback'])
) {
$callbacks = $GLOBALS['TL_DCA'][$strTable]['fields'][$strSelector]['load_callback'];
foreach ($callbacks as $callback) {
if (is_array($callback)) {
$callback[0] = System::importStatic($callback[0]);
$strValue = $callback[0]->{$callback[1]}($strValue, $dataContainer);
} elseif (is_callable($callback)) {
$strValue = $callback($strValue, $dataContainer);
}
}
}
return $strValue;
}
/**
* Build the sub select palette.
*
* @param array $arrPalettes Sub select palettes definitions.
* @param string $strValue Selector value.
* @param string $strTable Data container table name.
*
* @return string
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function buildPalette($arrPalettes, $strValue, $strTable)
{
$strPalette = '';
foreach ($arrPalettes as $strSelectValue => $arrSelectPalette) {
// add palette if value is selected or not
if (count($arrSelectPalette)
&& $this->isValueSelected($strValue, $strSelectValue)
) {
foreach ($arrSelectPalette as $strLegend => $mixSub) {
if (is_array($mixSub)) {
// supporting sub sub palettes :)
foreach ($mixSub as $arrValue) {
foreach (array_keys($GLOBALS['TL_DCA'][$strTable]['palettes']) as $k) {
if ($k == '__selector__') {
continue;
}
MetaPalettes::appendFields($strTable, $k, $strLegend, [$arrValue]);
}
}
} else {
// legacy, simple array of fields.
$strPalette .= ',' . $mixSub;
}
}
}
}
return $strPalette;
}
/**
* Apply generated subselect palette to the palettes.
*
* @param string $strTable Name of the data container table.
* @param string $strSelector Selector field name.
* @param string $strPalette Palette which should be applied.
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function applyPalette($strTable, $strSelector, $strPalette)
{
if (!strlen($strPalette)) {
return;
}
foreach ($GLOBALS['TL_DCA'][$strTable]['palettes'] as $k => $v) {
if ($k !== '__selector__') {
$GLOBALS['TL_DCA'][$strTable]['palettes'][$k] = preg_replace(
'#([,;]' . preg_quote($strSelector) . ')([,;].*)?$#',
'$1' . $strPalette . '$2',
$GLOBALS['TL_DCA'][$strTable]['palettes'][$k]
);
}
}
if (empty($GLOBALS['TL_DCA'][$strTable]['subpalettes'])
|| !is_array($GLOBALS['TL_DCA'][$strTable]['subpalettes'])
) {
return;
}
foreach ($GLOBALS['TL_DCA'][$strTable]['subpalettes'] as $k => $v) {
$GLOBALS['TL_DCA'][$strTable]['subpalettes'][$k] = preg_replace(
'#([,;]?' . preg_quote($strSelector) . ')([,;].*)?$#',
'$1' . $strPalette . '$2',
$GLOBALS['TL_DCA'][$strTable]['subpalettes'][$k]
);
}
}
/**
* Fetch value from database.
*
* @param DataContainer $dataContainer Data container driver.
* @param string $strSelector Selector field name.
*
* @return mixed
*/
private function fetchValueFromDatabase($dataContainer, $strSelector)
{
$statement = $this->connection->createQueryBuilder()
->select($this->connection->quoteIdentifier($strSelector))
->from($this->connection->quoteIdentifier($dataContainer->table))
->where('id=:value')
->setParameter('value', $dataContainer->id)
->setMaxResults(1)
->executeQuery();
assert(! is_int($statement));
if ($statement->rowCount() === 0) {
return null;
}
return $statement->fetchOne();
}
/**
* Get the value from the model.
*
* @param ModelInterface|null $objModel Data model.
* @param string $strSelector Selector field name.
*
* @return mixed
*/
private function getValueFromDcGeneralModel($objModel, $strSelector)
{
if ($objModel) {
return $objModel->getProperty($strSelector);
}
return null;
}
/**
* Check if value is selected.
*
* @param string $strValue Actual value.
* @param string $strSelectValue Select value definition.
*
* @return bool
*/
protected function isValueSelected($strValue, $strSelectValue)
{
return ($strSelectValue == $strValue || $strSelectValue[0] == '!' && substr($strSelectValue, 1) != $strValue);
}
}