-
Notifications
You must be signed in to change notification settings - Fork 648
/
TcaRecordTitle.php
386 lines (363 loc) · 14.5 KB
/
TcaRecordTitle.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
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\Form\FormDataProvider;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Determine the title of a record and write it to $result['recordTitle'].
*
* TCA ctrl fields like label and label_alt are evaluated and their
* current values from databaseRow used to create the title.
*/
class TcaRecordTitle implements FormDataProviderInterface
{
/**
* Enrich the processed record information with the resolved title
*
* @param array $result Incoming result array
* @return array Modified array
*/
public function addData(array $result)
{
if (!isset($result['processedTca']['ctrl']['label'])) {
throw new \UnexpectedValueException(
'TCA of table ' . $result['tableName'] . ' misses required [\'ctrl\'][\'label\'] definition.',
1443706103
);
}
if ($result['isInlineChild'] && isset($result['processedTca']['ctrl']['formattedLabel_userFunc'])) {
// inline child with formatted user func is first
$parameters = [
'table' => $result['tableName'],
'row' => $result['databaseRow'],
'title' => '',
'isOnSymmetricSide' => $result['isOnSymmetricSide'],
'options' => $result['processedTca']['ctrl']['formattedLabel_userFunc_options'] ?? [],
'parent' => [
'uid' => $result['databaseRow']['uid'],
'config' => $result['inlineParentConfig'],
],
];
// callUserFunction requires a third parameter, but we don't want to give $this as reference!
$null = null;
GeneralUtility::callUserFunction($result['processedTca']['ctrl']['formattedLabel_userFunc'], $parameters, $null);
$result['recordTitle'] = $parameters['title'];
} elseif ($result['isInlineChild'] && (isset($result['inlineParentConfig']['foreign_label'])
|| isset($result['inlineParentConfig']['symmetric_label']))
) {
// inline child with foreign label or symmetric inline child with symmetric_label
$fieldName = $result['isOnSymmetricSide']
? $result['inlineParentConfig']['symmetric_label']
: $result['inlineParentConfig']['foreign_label'];
$result['recordTitle'] = $this->getRecordTitleForField($fieldName, $result);
} elseif (isset($result['processedTca']['ctrl']['label_userFunc'])) {
// userFunc takes precedence over everything else
$parameters = [
'table' => $result['tableName'],
'row' => $result['databaseRow'],
'title' => '',
'options' => $result['processedTca']['ctrl']['label_userFunc_options'] ?? [],
];
$null = null;
GeneralUtility::callUserFunction($result['processedTca']['ctrl']['label_userFunc'], $parameters, $null);
$result['recordTitle'] = $parameters['title'];
} else {
// standard record
$result = $this->getRecordTitleByLabelProperties($result);
}
return $result;
}
/**
* Build the record title from label, label_alt and label_alt_force properties
*
* @param array $result Incoming result array
* @return array Modified result array
*/
protected function getRecordTitleByLabelProperties(array $result)
{
$titles = [];
$titleByLabel = $this->getRecordTitleForField($result['processedTca']['ctrl']['label'], $result);
if (!empty($titleByLabel)) {
$titles[] = $titleByLabel;
}
$labelAltForce = isset($result['processedTca']['ctrl']['label_alt_force'])
? (bool)$result['processedTca']['ctrl']['label_alt_force']
: false;
if (!empty($result['processedTca']['ctrl']['label_alt']) && ($labelAltForce || empty($titleByLabel))) {
// Dive into label_alt evaluation if label_alt_force is set or if label did not came up with a title yet
$labelAltFields = GeneralUtility::trimExplode(',', $result['processedTca']['ctrl']['label_alt'], true);
foreach ($labelAltFields as $fieldName) {
$titleByLabelAlt = $this->getRecordTitleForField($fieldName, $result);
if (!empty($titleByLabelAlt)) {
$titles[] = $titleByLabelAlt;
}
if (!$labelAltForce && !empty($titleByLabelAlt)) {
// label_alt_force creates a comma separated list of multiple fields.
// If not set, one found field with content is enough
break;
}
}
}
$result['recordTitle'] = implode(', ', $titles);
return $result;
}
/**
* Record title of a single field
*
* @param string $fieldName Field to handle
* @param array $result Incoming result array
* @return string
*/
protected function getRecordTitleForField($fieldName, $result)
{
if ($fieldName === 'uid') {
// uid return field content directly since it usually has not TCA definition
return $result['databaseRow']['uid'];
}
if (!isset($result['processedTca']['columns'][$fieldName]['config']['type'])
|| !is_string($result['processedTca']['columns'][$fieldName]['config']['type'])
) {
return '';
}
$recordTitle = '';
$rawValue = null;
if (array_key_exists($fieldName, $result['databaseRow'])) {
$rawValue = $result['databaseRow'][$fieldName];
}
$fieldConfig = $result['processedTca']['columns'][$fieldName]['config'];
switch ($fieldConfig['type']) {
case 'radio':
$recordTitle = $this->getRecordTitleForRadioType($rawValue, $fieldConfig);
break;
case 'inline':
$recordTitle = $this->getRecordTitleForInlineType(
$rawValue,
$result['processedTca']['columns'][$fieldName]['children']
);
break;
case 'select':
case 'category':
$recordTitle = $this->getRecordTitleForSelectType($rawValue, $fieldConfig);
break;
case 'group':
$recordTitle = $this->getRecordTitleForGroupType($rawValue);
break;
case 'check':
$recordTitle = $this->getRecordTitleForCheckboxType($rawValue, $fieldConfig);
break;
case 'input':
$recordTitle = $this->getRecordTitleForInputType($rawValue, $fieldConfig);
break;
case 'text':
$recordTitle = $this->getRecordTitleForTextType($rawValue);
break;
case 'flex':
// @todo: Check if and how a label could be generated from flex field data
default:
}
return $recordTitle;
}
/**
* Return the record title for radio fields
*
* @param mixed $value Current database value of this field
* @param array $fieldConfig TCA field configuration
* @return string
*/
protected function getRecordTitleForRadioType($value, $fieldConfig)
{
if (!isset($fieldConfig['items']) || !is_array($fieldConfig['items'])) {
return '';
}
foreach ($fieldConfig['items'] as $item) {
[$itemLabel, $itemValue] = $item;
if ((string)$value === (string)$itemValue) {
return $itemLabel;
}
}
return '';
}
/**
* @param int $value
* @param array $children
* @return string
*/
protected function getRecordTitleForInlineType($value, array $children)
{
foreach ($children as $child) {
if ((int)$value === $child['vanillaUid']) {
return $child['recordTitle'];
}
}
return '';
}
/**
* Return the record title for database records
*
* @param mixed $value Current database value of this field
* @param array $fieldConfig TCA field configuration
* @return string
*/
protected function getRecordTitleForSelectType($value, $fieldConfig)
{
if (!is_array($value)) {
return '';
}
$labelParts = [];
if (!empty($fieldConfig['items'])) {
$listOfValues = array_column($fieldConfig['items'], 1);
foreach ($value as $itemValue) {
$itemKey = array_search($itemValue, $listOfValues);
if ($itemKey !== false) {
$labelParts[] = $fieldConfig['items'][$itemKey][0];
}
}
}
$title = implode(', ', $labelParts);
if (empty($title) && !empty($value)) {
$title = implode(', ', $value);
}
return $title;
}
/**
* Return the record title for database records
*
* @param mixed $value Current database value of this field
* @return string
*/
protected function getRecordTitleForGroupType($value)
{
$labelParts = [];
foreach ($value as $singleValue) {
if (isset($singleValue['folder'])) {
$labelParts[] = $singleValue['folder'];
} else {
$labelParts[] = $singleValue['title'];
}
}
return implode(', ', $labelParts);
}
/**
* Returns the record title for checkbox fields
*
* @param mixed $value Current database value of this field
* @param array $fieldConfig TCA field configuration
* @return string
*/
protected function getRecordTitleForCheckboxType($value, $fieldConfig)
{
$languageService = $this->getLanguageService();
if (empty($fieldConfig['items']) || !is_array($fieldConfig['items'])) {
$title = (bool)$value
? $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:yes')
: $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:no');
} else {
$labelParts = [];
foreach ($fieldConfig['items'] as $key => $val) {
if ($value & 2 ** $key) {
$labelParts[] = $val[0];
}
}
$title = implode(', ', $labelParts);
}
return $title;
}
/**
* Returns the record title for input fields
*
* @param mixed $value Current database value of this field
* @param array $fieldConfig TCA field configuration
* @return string
*/
protected function getRecordTitleForInputType($value, $fieldConfig)
{
if (!isset($value)) {
return '';
}
if (!isset($fieldConfig['eval'])) {
return $value;
}
$title = $value;
$dateTimeFormats = QueryHelper::getDateTimeFormats();
if (GeneralUtility::inList($fieldConfig['eval'], 'date')) {
// Handle native date field
if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'date') {
$value = $value === $dateTimeFormats['date']['empty'] ? 0 : (int)strtotime($value);
} else {
$value = (int)$value;
}
if (!empty($value)) {
$ageSuffix = '';
// Generate age suffix as long as not explicitly suppressed
if (!isset($fieldConfig['disableAgeDisplay']) || (bool)$fieldConfig['disableAgeDisplay'] === false) {
$ageDelta = $GLOBALS['EXEC_TIME'] - $value;
$calculatedAge = BackendUtility::calcAge(
(int)abs($ageDelta),
$this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
);
$ageSuffix = ' (' . ($ageDelta > 0 ? '-' : '') . $calculatedAge . ')';
}
$title = BackendUtility::date($value) . $ageSuffix;
}
} elseif (GeneralUtility::inList($fieldConfig['eval'], 'time')) {
// Handle native time field
if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'time') {
$value = $value === $dateTimeFormats['time']['empty'] ? 0 : (int)strtotime('1970-01-01 ' . $value . ' UTC');
} else {
$value = (int)$value;
}
if (!empty($value)) {
$title = gmdate('H:i', (int)$value);
}
} elseif (GeneralUtility::inList($fieldConfig['eval'], 'timesec')) {
// Handle native time field
if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'time') {
$value = $value === $dateTimeFormats['time']['empty'] ? 0 : (int)strtotime('1970-01-01 ' . $value . ' UTC');
} else {
$value = (int)$value;
}
if (!empty($value)) {
$title = gmdate('H:i:s', (int)$value);
}
} elseif (GeneralUtility::inList($fieldConfig['eval'], 'datetime')) {
// Handle native datetime field
if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'datetime') {
$value = $value === $dateTimeFormats['datetime']['empty'] ? 0 : (int)strtotime($value);
} else {
$value = (int)$value;
}
if (!empty($value)) {
$title = BackendUtility::datetime($value);
}
}
return $title;
}
/**
* Returns the record title for text fields
*
* @param mixed $value Current database value of this field
* @return string
*/
protected function getRecordTitleForTextType($value)
{
return trim(strip_tags($value));
}
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
}