-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractSelect.php
More file actions
323 lines (291 loc) · 8.49 KB
/
AbstractSelect.php
File metadata and controls
323 lines (291 loc) · 8.49 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
<?php
/**
* This file is part of MetaModels/attribute_select.
*
* (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/attribute_select
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author David Molineus <david.molineus@netzmacht.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2025 The MetaModels team.
* @license https://github.com/MetaModels/attribute_select/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace MetaModels\AttributeSelectBundle\Attribute;
use Doctrine\DBAL\ArrayParameterType;
use MetaModels\Attribute\AbstractHybrid;
use MetaModels\AttributeSelectBundle\FilterRule\FilterRuleSelect;
/**
* This is the MetaModelAttribute class for handling select attributes.
*/
abstract class AbstractSelect extends AbstractHybrid
{
/**
* The widget mode to use.
*
* @var int
*/
protected $widgetMode = 0;
/**
* Local cached flag if the attribute has been properly configured.
*
* @var bool|null
*/
private ?bool $isProperlyConfigured = null;
/**
* {@inheritdoc}
*/
public function getSQLDataType()
{
return 'int(11) NULL';
}
/**
* Determine if we want to use tree selection.
*
* @return bool
*/
protected function isTreePicker()
{
return $this->widgetMode === 2;
}
/**
* Determine the correct sorting column to use.
*
* @return string
*/
protected function getSelectSource()
{
return $this->get('select_table');
}
/**
* Determine the correct sorting column to use.
*
* @return string
*/
protected function getIdColumn()
{
return $this->get('select_id') ?: 'id';
}
/**
* Determine the correct sort direction to use.
*
* @return string
*/
protected function getSortDirection()
{
return $this->get('select_sort');
}
/**
* Determine the correct sorting column to use.
*
* @return string
*/
protected function getSortingColumn()
{
return $this->get('select_sorting') ?: $this->getIdColumn();
}
/**
* Determine the correct sorting column to use.
*
* @return string
*/
protected function getValueColumn()
{
return $this->get('select_column');
}
/**
* Determine the correct alias column to use.
*
* @return string
*/
protected function getAliasColumn()
{
$colNameAlias = $this->get('select_alias');
if ($this->isTreePicker() || !$colNameAlias) {
$colNameAlias = $this->getIdColumn();
}
return $colNameAlias;
}
/**
* Ensure the attribute has been configured correctly.
*
* @return bool
*/
protected function isProperlyConfigured()
{
if (null !== $this->isProperlyConfigured) {
return $this->isProperlyConfigured;
}
return $this->isProperlyConfigured = $this->checkConfiguration();
}
/**
* Check the configuration of the attribute.
*
* @return bool
*/
protected function checkConfiguration()
{
return $this->getSelectSource()
&& $this->getValueColumn()
&& $this->getAliasColumn()
&& $this->getIdColumn()
&& $this->getSortingColumn();
}
/**
* Test that we can create the filter options.
*
* @param string[]|null $idList The ids of items that the values shall be fetched from
* (If empty or null, all items).
*
* @return bool
*/
protected function isFilterOptionRetrievingPossible($idList)
{
return $this->isProperlyConfigured() && (($idList === null) || !empty($idList));
}
/**
* Get the picker input type.
*
* @return string
*/
private function getPickerType()
{
$sourceName = $this->getSelectSource();
if (!\in_array($sourceName, ['tl_page', 'tl_files'])) {
return 'DcGeneralTreePicker';
}
return $sourceName === 'tl_page' ? 'pageTree' : 'fileTree';
}
/**
* Obtain the filter options with always the id being contained instead of the alias.
*
* This is being called from BackendSubscriber to circumvent problems when dealing with translated aliases.
*
* @return array<string, string>
*/
abstract public function getFilterOptionsForDcGeneral();
/**
* {@inheritdoc}
*/
public function getFieldDefinition($arrOverrides = [])
{
$arrFieldDef = parent::getFieldDefinition($arrOverrides);
$this->widgetMode = (int) $arrOverrides['select_as_radio'];
if ($this->isTreePicker()) {
$arrFieldDef['inputType'] = $this->getPickerType();
$arrFieldDef['eval']['sourceName'] = $this->getSelectSource();
$arrFieldDef['eval']['fieldType'] = 'radio';
$arrFieldDef['eval']['idProperty'] = $this->getAliasColumn();
$arrFieldDef['eval']['orderField'] = $this->getSortingColumn();
$arrFieldDef['eval']['minLevel'] = $arrOverrides['select_minLevel'];
$arrFieldDef['eval']['maxLevel'] = $arrOverrides['select_maxLevel'];
} elseif ($this->widgetMode === 1) {
// If select as radio is true, change the input type.
$arrFieldDef['inputType'] = 'radio';
} else {
$arrFieldDef['inputType'] = 'select';
}
return $arrFieldDef;
}
/**
* {@inheritdoc}
*/
public function getAttributeSettingNames()
{
return \array_merge(
parent::getAttributeSettingNames(),
[
'select_table',
'select_column',
'select_alias',
'select_sorting',
'select_sort',
'select_as_radio',
'includeBlankOption',
'submitOnChange',
'mandatory',
'chosen',
'filterable',
'searchable'
]
);
}
/**
* {@inheritdoc}
*
* Search value in table.
*/
public function searchFor($strPattern)
{
return (new FilterRuleSelect($this, $strPattern, $this->connection))->getMatchingIds() ?? [];
}
/**
* {@inheritdoc}
*/
public function unsetDataFor($arrIds)
{
$this->connection->createQueryBuilder()
->update($this->getMetaModel()->getTableName(), 't')
->set('t.' . $this->getColName(), '0')
->where('t.id IN (:ids)')
->setParameter('ids', $arrIds, ArrayParameterType::STRING)
->executeQuery();
}
/**
* Convert the passed values to a list of value ids.
*
* @param list<string> $values The values to convert.
*
* @return list<string>
*/
public function convertValuesToValueIds($values)
{
$tableName = $this->getSelectSource();
$idColumn = $this->getIdColumn();
$aliasColumn = $this->getAliasColumn();
if ($idColumn === $aliasColumn) {
return $values;
}
$values = \array_unique(\array_filter($values));
if (empty($values)) {
return [];
}
return $this->connection->createQueryBuilder()
->select('t.' . $idColumn)
->from($tableName, 't')
->where('t.' . $aliasColumn . ' IN (:values)')
->setParameter('values', $values, ArrayParameterType::STRING)
->executeQuery()
->fetchFirstColumn();
}
/**
* Convert a native attribute value into a value to be used in a filter Url.
*
* This returns the value of the alias if any defined or the value of the id otherwise.
*
* @param mixed $varValue The source value.
*
* @return string
*/
public function getFilterUrlValue($varValue)
{
return \urlencode($varValue[$this->getAliasColumn()]);
}
/**
* {@inheritDoc}
*
* This is needed for compatibility with MySQL strict mode.
*/
public function serializeData($value)
{
return $value === '' ? null : $value;
}
}