TYPO3 CMS  TYPO3_8-7
TcaRecordTitle.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
29 {
36  public function addData(array $result)
37  {
38  if (!isset($result['processedTca']['ctrl']['label'])) {
39  throw new \UnexpectedValueException(
40  'TCA of table ' . $result['tableName'] . ' misses required [\'ctrl\'][\'label\'] definition.',
41  1443706103
42  );
43  }
44 
45  if ($result['isInlineChild'] && isset($result['processedTca']['ctrl']['formattedLabel_userFunc'])) {
46  // inline child with formatted user func is first
47  $parameters = [
48  'table' => $result['tableName'],
49  'row' => $result['databaseRow'],
50  'title' => '',
51  'isOnSymmetricSide' => $result['isOnSymmetricSide'],
52  'options' => isset($result['processedTca']['ctrl']['formattedLabel_userFunc_options'])
53  ? $result['processedTca']['ctrl']['formattedLabel_userFunc_options']
54  : [],
55  'parent' => [
56  'uid' => $result['databaseRow']['uid'],
57  'config' => $result['inlineParentConfig']
58  ]
59  ];
60  // callUserFunction requires a third parameter, but we don't want to give $this as reference!
61  $null = null;
62  GeneralUtility::callUserFunction($result['processedTca']['ctrl']['formattedLabel_userFunc'], $parameters, $null);
63  $result['recordTitle'] = $parameters['title'];
64  } elseif ($result['isInlineChild'] && (isset($result['inlineParentConfig']['foreign_label'])
65  || isset($result['inlineParentConfig']['symmetric_label']))
66  ) {
67  // inline child with foreign label or symmetric inline child with symmetric_label
68  $fieldName = $result['isOnSymmetricSide']
69  ? $result['inlineParentConfig']['symmetric_label']
70  : $result['inlineParentConfig']['foreign_label'];
71  $result['recordTitle'] = $this->getRecordTitleForField($fieldName, $result);
72  } elseif (isset($result['processedTca']['ctrl']['label_userFunc'])) {
73  // userFunc takes precedence over everything else
74  $parameters = [
75  'table' => $result['tableName'],
76  'row' => $result['databaseRow'],
77  'title' => '',
78  'options' => isset($result['processedTca']['ctrl']['label_userFunc_options'])
79  ? $result['processedTca']['ctrl']['label_userFunc_options']
80  : [],
81  ];
82  $null = null;
83  GeneralUtility::callUserFunction($result['processedTca']['ctrl']['label_userFunc'], $parameters, $null);
84  $result['recordTitle'] = $parameters['title'];
85  } else {
86  // standard record
87  $result = $this->getRecordTitleByLabelProperties($result);
88  }
89 
90  return $result;
91  }
92 
99  protected function getRecordTitleByLabelProperties(array $result)
100  {
101  $titles = [];
102  $titleByLabel = $this->getRecordTitleForField($result['processedTca']['ctrl']['label'], $result);
103  if (!empty($titleByLabel)) {
104  $titles[] = $titleByLabel;
105  }
106 
107  $labelAltForce = isset($result['processedTca']['ctrl']['label_alt_force'])
108  ? (bool)$result['processedTca']['ctrl']['label_alt_force']
109  : false;
110  if (!empty($result['processedTca']['ctrl']['label_alt']) && ($labelAltForce || empty($titleByLabel))) {
111  // Dive into label_alt evaluation if label_alt_force is set or if label did not came up with a title yet
112  $labelAltFields = GeneralUtility::trimExplode(',', $result['processedTca']['ctrl']['label_alt'], true);
113  foreach ($labelAltFields as $fieldName) {
114  $titleByLabelAlt = $this->getRecordTitleForField($fieldName, $result);
115  if (!empty($titleByLabelAlt)) {
116  $titles[] = $titleByLabelAlt;
117  }
118  if (!$labelAltForce && !empty($titleByLabelAlt)) {
119  // label_alt_force creates a comma separated list of multiple fields.
120  // If not set, one found field with content is enough
121  break;
122  }
123  }
124  }
125 
126  $result['recordTitle'] = implode(', ', $titles);
127  return $result;
128  }
129 
137  protected function getRecordTitleForField($fieldName, $result)
138  {
139  if ($fieldName === 'uid') {
140  // uid return field content directly since it usually has not TCA definition
141  return $result['databaseRow']['uid'];
142  }
143 
144  if (!isset($result['processedTca']['columns'][$fieldName]['config']['type'])
145  || !is_string($result['processedTca']['columns'][$fieldName]['config']['type'])
146  ) {
147  return '';
148  }
149 
150  $recordTitle = '';
151  $rawValue = null;
152  if (array_key_exists($fieldName, $result['databaseRow'])) {
153  $rawValue = $result['databaseRow'][$fieldName];
154  }
155  $fieldConfig = $result['processedTca']['columns'][$fieldName]['config'];
156  switch ($fieldConfig['type']) {
157  case 'radio':
158  $recordTitle = $this->getRecordTitleForRadioType($rawValue, $fieldConfig);
159  break;
160  case 'inline':
161  $recordTitle = $this->getRecordTitleForInlineType(
162  $rawValue,
163  $result['processedTca']['columns'][$fieldName]['children']
164  );
165  break;
166  case 'select':
167  $recordTitle = $this->getRecordTitleForSelectType($rawValue, $fieldConfig);
168  break;
169  case 'group':
170  $recordTitle = $this->getRecordTitleForGroupType($rawValue, $fieldConfig);
171  break;
172  case 'check':
173  $recordTitle = $this->getRecordTitleForCheckboxType($rawValue, $fieldConfig);
174  break;
175  case 'input':
176  $recordTitle = $this->getRecordTitleForInputType($rawValue, $fieldConfig);
177  break;
178  case 'text':
179  $recordTitle = $this->getRecordTitleForTextType($rawValue);
180  break;
181  case 'flex':
182  // @todo: Check if and how a label could be generated from flex field data
183  default:
184 
185  }
186 
187  return $recordTitle;
188  }
189 
197  protected function getRecordTitleForRadioType($value, $fieldConfig)
198  {
199  if (!isset($fieldConfig['items']) || !is_array($fieldConfig['items'])) {
200  return '';
201  }
202  foreach ($fieldConfig['items'] as $item) {
203  list($itemLabel, $itemValue) = $item;
204  if ((string)$value === (string)$itemValue) {
205  return $itemLabel;
206  }
207  }
208  return '';
209  }
210 
216  protected function getRecordTitleForInlineType($value, array $children)
217  {
218  foreach ($children as $child) {
219  if ((int)$value === $child['vanillaUid']) {
220  return $child['recordTitle'];
221  }
222  }
223 
224  return '';
225  }
226 
234  protected function getRecordTitleForSelectType($value, $fieldConfig)
235  {
236  if (!is_array($value)) {
237  return '';
238  }
239  $labelParts = [];
240  if (!empty($fieldConfig['items'])) {
241  $listOfValues = array_column($fieldConfig['items'], 1);
242  foreach ($value as $itemValue) {
243  $itemKey = array_search($itemValue, $listOfValues);
244  if ($itemKey !== false) {
245  $labelParts[] = $fieldConfig['items'][$itemKey][0];
246  }
247  }
248  }
249  $title = implode(', ', $labelParts);
250  if (empty($title) && !empty($value)) {
251  $title = implode(', ', $value);
252  }
253  return $title;
254  }
255 
263  protected function getRecordTitleForGroupType($value, $fieldConfig)
264  {
265  $labelParts = [];
266  foreach ($value as $singleValue) {
267  if (isset($singleValue['uidOrPath'])) {
268  $labelParts[] = $singleValue['uidOrPath'];
269  } elseif (isset($singleValue['folder'])) {
270  $labelParts[] = $singleValue['folder'];
271  } else {
272  $labelParts[] = $singleValue['title'];
273  }
274  }
275  return implode(', ', $labelParts);
276  }
277 
285  protected function getRecordTitleForCheckboxType($value, $fieldConfig)
286  {
287  $languageService = $this->getLanguageService();
288  if (empty($fieldConfig['items']) || !is_array($fieldConfig['items'])) {
289  $title = (bool)$value
290  ? $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:yes')
291  : $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:no');
292  } else {
293  $labelParts = [];
294  foreach ($fieldConfig['items'] as $key => $val) {
295  if ($value & pow(2, $key)) {
296  $labelParts[] = $val[0];
297  }
298  }
299  $title = implode(', ', $labelParts);
300  }
301  return $title;
302  }
303 
311  protected function getRecordTitleForInputType($value, $fieldConfig)
312  {
313  if (!isset($value)) {
314  return '';
315  }
316  $title = $value;
317  if (GeneralUtility::inList($fieldConfig['eval'], 'date')) {
318  if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'date') {
319  $value = $value === '0000-00-00' ? 0 : (int)strtotime($value);
320  } else {
321  $value = (int)$value;
322  }
323  if (!empty($value)) {
324  $ageSuffix = '';
325  // Generate age suffix as long as not explicitly suppressed
326  if (!isset($fieldConfig['disableAgeDisplay']) || (bool)$fieldConfig['disableAgeDisplay'] === false) {
327  $ageDelta = $GLOBALS['EXEC_TIME'] - $value;
328  $calculatedAge = BackendUtility::calcAge(
329  abs($ageDelta),
330  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears')
331  );
332  $ageSuffix = ' (' . ($ageDelta > 0 ? '-' : '') . $calculatedAge . ')';
333  }
334  $title = BackendUtility::date($value) . $ageSuffix;
335  }
336  } elseif (GeneralUtility::inList($fieldConfig['eval'], 'time')) {
337  if (!empty($value)) {
338  $title = gmdate('H:i', (int)$value);
339  }
340  } elseif (GeneralUtility::inList($fieldConfig['eval'], 'timesec')) {
341  if (!empty($value)) {
342  $title = gmdate('H:i:s', (int)$value);
343  }
344  } elseif (GeneralUtility::inList($fieldConfig['eval'], 'datetime')) {
345  // Handle native date/time field
346  if (isset($fieldConfig['dbType']) && $fieldConfig['dbType'] === 'datetime') {
347  $value = $value === '0000-00-00 00:00:00' ? 0 : (int)strtotime($value);
348  } else {
349  $value = (int)$value;
350  }
351  if (!empty($value)) {
352  $title = BackendUtility::datetime($value);
353  }
354  }
355  return $title;
356  }
357 
364  protected function getRecordTitleForTextType($value)
365  {
366  return trim(strip_tags($value));
367  }
368 
372  protected function getLanguageService()
373  {
374  return $GLOBALS['LANG'];
375  }
376 }
static callUserFunction($funcName, &$params, &$ref, $_='', $errorMode=0)
static calcAge($seconds, $labels='min|hrs|days|yrs|min|hour|day|year')
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']