TYPO3 CMS  TYPO3_6-2
FormDataTraverser.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
3 
19 
27 
35 
42  protected $forceAlternativeFieldValueUse = FALSE;
43 
49  protected $currentRow;
50 
56  protected $currentTable;
57 
63  protected $formEngine;
64 
71  protected $originalLanguageUid = NULL;
72 
79  public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
80  $this->formEngine = $formEngine;
81  }
82 
91  public function getTraversedFieldValue(array $fieldNameArray, $tableName, array $row) {
92  $this->currentTable = $tableName;
93  $this->currentRow = $row;
94  $fieldValue = '';
95  if (count($fieldNameArray) > 0) {
97  $fieldValue = $this->getFieldValueRecursive($fieldNameArray);
98  }
99  return $fieldValue;
100  }
101 
109  protected function initializeOriginalLanguageUid() {
110  $fieldCtrlConfig = $GLOBALS['TCA'][$this->currentTable]['ctrl'];
111  if (!empty($fieldCtrlConfig['languageField']) && isset($this->currentRow[$fieldCtrlConfig['languageField']])) {
112  $this->originalLanguageUid = (int)$this->currentRow[$fieldCtrlConfig['languageField']];
113  } else {
114  $this->originalLanguageUid = FALSE;
115  }
116  }
117 
125  protected function getFieldValueRecursive(array $fieldNameArray) {
126  $value = '';
127 
128  foreach ($fieldNameArray as $fieldName) {
129  // Skip if a defined field was actually not present in the database row
130  // Using array_key_exists here, since TYPO3 supports NULL values as well
131  if (!array_key_exists($fieldName, $this->currentRow)) {
132  $value = '';
133  break;
134  }
135 
136  $value = $this->currentRow[$fieldName];
137  if (empty($value)) {
138  break;
139  }
140 
141  $this->currentRow = $this->getRelatedRecordRow($fieldName, $value);
142  if ($this->currentRow === FALSE) {
143  break;
144  }
145  }
146 
147  if ((empty($value) || $this->forceAlternativeFieldValueUse) && !empty($this->alternativeFieldValue)) {
149  }
150 
151  return $value;
152  }
153 
162  protected function getRelatedRecordRow($fieldName, $value) {
163  $fieldConfig = $GLOBALS['TCA'][$this->currentTable]['columns'][$fieldName]['config'];
164  $possibleUids = array();
165 
166  switch ($fieldConfig['type']) {
167  case 'group':
168  $possibleUids = $this->getRelatedGroupFieldUids($fieldConfig, $value);
169  break;
170  case 'select':
171  $possibleUids = $this->getRelatedSelectFieldUids($fieldConfig, $fieldName, $value);
172  break;
173  case 'inline':
174  $possibleUids = $this->getRelatedInlineFieldUids($fieldConfig, $fieldName, $value);
175  break;
176  }
177 
178  $relatedRow = FALSE;
179  if (count($possibleUids) === 1) {
180  $relatedRow = $this->getRecordRow($possibleUids[0]);
181  } elseif (count($possibleUids) > 1) {
182  $relatedRow = $this->getMatchingRecordRowByTranslation($possibleUids, $fieldConfig);
183  }
184 
185  return $relatedRow;
186  }
187 
195  protected function getRelatedGroupFieldUids(array $fieldConfig, $value) {
196  $relatedUids = array();
197  $allowedTable = $this->getAllowedTableForGroupField($fieldConfig);
198 
199  if (($fieldConfig['internal_type'] !== 'db') || ($allowedTable === FALSE)) {
200  return $relatedUids;
201  }
202 
203  $values = GeneralUtility::trimExplode(',', $value, TRUE);
204  foreach ($values as $groupValue) {
205  list($foreignIdentifier, $foreignTitle) = GeneralUtility::trimExplode('|', $groupValue);
206  list($recordForeignTable, $foreignUid) = BackendUtility::splitTable_Uid($foreignIdentifier);
207  // skip records that do not match the allowed table
208  if (!empty($recordForeignTable) && ($recordForeignTable !== $allowedTable)) {
209  continue;
210  }
211  if (!empty($foreignTitle)) {
212  $this->alternativeFieldValue = rawurldecode($foreignTitle);
213  }
214  $relatedUids[] = $foreignUid;
215  }
216 
217  if (count($relatedUids) > 0) {
218  $this->currentTable = $allowedTable;
219  }
220 
221  return $relatedUids;
222  }
223 
236  protected function getRelatedSelectFieldUids(array $fieldConfig, $fieldName, $value) {
237  $relatedUids = array();
238 
239  $isTraversable = FALSE;
240  if (isset($fieldConfig['foreign_table'])) {
241  $isTraversable = TRUE;
242  // if a foreign_table is used we pre-filter the records for performance
243  $fieldConfig['foreign_table_where'] .= ' AND ' . $fieldConfig['foreign_table'] . '.uid IN (' . $value . ')';
244  }
245 
246  $PA = array();
247  $PA['fieldConf']['config'] = $fieldConfig;
248  $PA['fieldConf']['config']['form_type'] = $PA['fieldConf']['config']['form_type'] ? $PA['fieldConf']['config']['form_type'] : $PA['fieldConf']['config']['type'];
249  $PA['fieldTSConfig'] = $this->formEngine->setTSconfig($this->currentTable, $this->currentRow, $fieldName);
250  $PA['fieldConf']['config'] = $this->formEngine->overrideFieldConf($PA['fieldConf']['config'], $PA['fieldTSConfig']);
251  $selectItemArray = $this->formEngine->getSelectItems($this->currentTable, $fieldName, $this->currentRow, $PA);
252 
253  if ($isTraversable && count($selectItemArray)) {
254  $this->currentTable = $fieldConfig['foreign_table'];
255  $relatedUids = $this->getSelectedValuesFromSelectItemArray($selectItemArray, $value);
256  } else {
257  $selectedLabels = $this->getSelectedValuesFromSelectItemArray($selectItemArray, $value, 1, TRUE);
258  if (count($selectedLabels) === 1) {
259  $this->alternativeFieldValue = $selectedLabels[0];
260  $this->forceAlternativeFieldValueUse = TRUE;
261  }
262  }
263 
264  return $relatedUids;
265  }
266 
276  protected function getSelectedValuesFromSelectItemArray(array $selectItemArray, $value, $maxItems = NULL, $returnLabels = FALSE) {
277  $values = GeneralUtility::trimExplode(',', $value);
278  $selectedValues = array();
279 
280  if ($maxItems !== NULL && (count($values) > (int)$maxItems)) {
281  return $selectedValues;
282  }
283 
284  foreach ($selectItemArray as $selectItem) {
285  $selectItemValue = $selectItem[1];
286  if (in_array($selectItemValue, $values)) {
287  if ($returnLabels) {
288  $selectedValues[] = $selectItem[0];
289  } else {
290  $selectedValues[] = $selectItemValue;
291  }
292  }
293  }
294 
295  return $selectedValues;
296  }
297 
298 
307  protected function getRelatedInlineFieldUids(array $fieldConfig, $fieldName, $value) {
308  $relatedUids = array();
309 
310  $PA = array('itemFormElValue' => $value);
311  $items = $this->formEngine->inline->getRelatedRecords($this->currentTable, $fieldName, $this->currentRow, $PA, $fieldConfig);
312  if ($items['count'] > 0) {
313  $this->currentTable = $fieldConfig['foreign_table'];
314  foreach ($items['records'] as $inlineRecord) {
315  $relatedUids[] = $inlineRecord['uid'];
316  }
317  }
318 
319  return $relatedUids;
320  }
321 
331  protected function getAllowedTableForGroupField(array $fieldConfig) {
332  $allowedTable = FALSE;
333 
334  $allowedTables = GeneralUtility::trimExplode(',', $fieldConfig['allowed'], TRUE);
335  if (count($allowedTables) === 1) {
336  $allowedTable = $allowedTables[0];
337  }
338 
339  return $allowedTable;
340  }
341 
350  protected function getRecordRow($uid) {
352  $dataPreprocessor = GeneralUtility::makeInstance('TYPO3\CMS\Backend\Form\DataPreprocessor');
353  $dataPreprocessor->fetchRecord($this->currentTable, $uid, '');
354  return reset($dataPreprocessor->regTableItems_data);
355  }
356 
368  protected function getMatchingRecordRowByTranslation(array $relatedUids) {
369  if ($this->originalLanguageUid === FALSE) {
370  return FALSE;
371  }
372 
373  $fieldCtrlConfig = $GLOBALS['TCA'][$this->currentTable]['ctrl'];
374  if (empty($fieldCtrlConfig['languageField'])) {
375  return FALSE;
376  }
377 
378  $languageField = $fieldCtrlConfig['languageField'];
379  $foundRows = array();
380  foreach ($relatedUids as $relatedUid) {
381  $currentRow = $this->getRecordRow($relatedUid);
382  if (!isset($currentRow[$languageField])) {
383  continue;
384  }
385  if ((int)$currentRow[$languageField] === $this->originalLanguageUid) {
386  $foundRows[] = $currentRow;
387  }
388  }
389 
390  $relatedRow = FALSE;
391  if (count($foundRows) === 1) {
392  $relatedRow = $foundRows[0];
393  }
394 
395  return $relatedRow;
396  }
397 }
getSelectedValuesFromSelectItemArray(array $selectItemArray, $value, $maxItems=NULL, $returnLabels=FALSE)
$uid
Definition: server.php:36
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
__construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine)
getRelatedInlineFieldUids(array $fieldConfig, $fieldName, $value)
getRelatedGroupFieldUids(array $fieldConfig, $value)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
getTraversedFieldValue(array $fieldNameArray, $tableName, array $row)
getRelatedSelectFieldUids(array $fieldConfig, $fieldName, $value)