[TASK] Remove the "@todo define visibility" in ext:backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / DataPreprocessor.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\PathUtility;
20
21 /**
22 * Class for getting and transforming data for display in backend forms (TCEforms)
23 *
24 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
25 */
26 class DataPreprocessor {
27
28 // External, static:
29 // If set, the records requested are locked.
30 /**
31 * @var int
32 */
33 public $lockRecords = 0;
34
35 // Is set externally if RTE is disabled.
36 /**
37 * @var int
38 */
39 public $disableRTE = 0;
40
41 // If the pid in the command is 'prev' then $prevPageID is used as pid for the record. This is used to attach new records to other previous records eg. new pages.
42 /**
43 * @var string
44 */
45 public $prevPageID = '';
46
47 // Can be set with an array of default values for tables. First key is table name, second level keys are field names. Originally this was a GLOBAL array used internally.
48 /**
49 * @var array
50 */
51 public $defVals = array();
52
53 // If set, the processed data is overlaid the raw record.
54 /**
55 * @var bool
56 */
57 public $addRawData = FALSE;
58
59 // Internal, dynamic
60 // Used to register, which items are already loaded!!
61 /**
62 * @var array
63 */
64 public $regTableItems = array();
65
66 // This stores the record data of the loaded records
67 /**
68 * @var array
69 */
70 public $regTableItems_data = array();
71
72 // Contains loadModules object, if used. (for reuse internally)
73 /**
74 * @var string
75 */
76 public $loadModules = '';
77
78 /***********************************************
79 *
80 * Getting record content, ready for display in TCEforms
81 *
82 ***********************************************/
83 /**
84 * A function which can be used for load a batch of records from $table into internal memory of this object.
85 * The function is also used to produce proper default data for new records
86 * Ultimately the function will call renderRecord()
87 *
88 * @param string $table Table name, must be found in $GLOBALS['TCA']
89 * @param string $idList Comma list of id values. If $idList is "prev" then the value from $this->prevPageID is used. NOTICE: If $operation is "new", then negative ids are meant to point to a "previous" record and positive ids are PID values for new records. Otherwise (for existing records that is) it is straight forward table/id pairs.
90 * @param string $operation If "new", then a record with default data is returned. Further, the $id values are meant to be PID values (or if negative, pointing to a previous record). If NOT new, then the table/ids are just pointing to an existing record!
91 * @return void
92 * @see renderRecord()
93 */
94 public function fetchRecord($table, $idList, $operation) {
95 if ((string)$idList === 'prev') {
96 $idList = $this->prevPageID;
97 }
98 if ($GLOBALS['TCA'][$table]) {
99 // For each ID value (integer) we
100 $ids = GeneralUtility::trimExplode(',', $idList, TRUE);
101 foreach ($ids as $id) {
102 // If ID is not blank:
103 if ((string)$id !== '') {
104 // For new records to be created, find default values:
105 if ($operation == 'new') {
106 // Default values:
107 // Used to store default values as found here:
108 $newRow = array();
109 // Default values as set in userTS:
110 $TCAdefaultOverride = $GLOBALS['BE_USER']->getTSConfigProp('TCAdefaults');
111 if (is_array($TCAdefaultOverride[$table . '.'])) {
112 foreach ($TCAdefaultOverride[$table . '.'] as $theF => $theV) {
113 if (isset($GLOBALS['TCA'][$table]['columns'][$theF])) {
114 $newRow[$theF] = $theV;
115 }
116 }
117 }
118 if ($id < 0) {
119 $record = BackendUtility::getRecord($table, abs($id), 'pid');
120 $pid = $record['pid'];
121 unset($record);
122 } else {
123 $pid = (int)$id;
124 }
125 $pageTS = BackendUtility::getPagesTSconfig($pid);
126 if (isset($pageTS['TCAdefaults.'])) {
127 $TCAPageTSOverride = $pageTS['TCAdefaults.'];
128 if (is_array($TCAPageTSOverride[$table . '.'])) {
129 foreach ($TCAPageTSOverride[$table . '.'] as $theF => $theV) {
130 if (isset($GLOBALS['TCA'][$table]['columns'][$theF])) {
131 $newRow[$theF] = $theV;
132 }
133 }
134 }
135 }
136 // Default values as submitted:
137 if (!empty($this->defVals[$table]) && is_array($this->defVals[$table])) {
138 foreach ($this->defVals[$table] as $theF => $theV) {
139 if (isset($GLOBALS['TCA'][$table]['columns'][$theF])) {
140 $newRow[$theF] = $theV;
141 }
142 }
143 }
144 // Fetch default values if a previous record exists
145 if ($id < 0 && $GLOBALS['TCA'][$table]['ctrl']['useColumnsForDefaultValues']) {
146 // Fetches the previous record:
147 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, 'uid=' . abs($id) . BackendUtility::deleteClause($table));
148 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
149 // Gets the list of fields to copy from the previous record.
150 $fArr = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$table]['ctrl']['useColumnsForDefaultValues'], TRUE);
151 foreach ($fArr as $theF) {
152 if (isset($GLOBALS['TCA'][$table]['columns'][$theF]) && !isset($newRow[$theF])) {
153 $newRow[$theF] = $row[$theF];
154 }
155 }
156 }
157 $GLOBALS['TYPO3_DB']->sql_free_result($res);
158 }
159 // Finally, call renderRecord:
160 $this->renderRecord($table, uniqid('NEW'), $id, $newRow);
161 } else {
162 $id = (int)$id;
163 // Fetch database values
164 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, 'uid=' . $id . BackendUtility::deleteClause($table));
165 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
166 BackendUtility::fixVersioningPid($table, $row);
167 $this->renderRecord($table, $id, $row['pid'], $row);
168 $this->lockRecord($table, $id, $table === 'tt_content' ? $row['pid'] : 0);
169 }
170 $GLOBALS['TYPO3_DB']->sql_free_result($res);
171 }
172 }
173 }
174 }
175 }
176
177 /**
178 * This function performs processing on the input $row array and stores internally a corresponding array which contains processed values, ready to pass on to the TCEforms rendering in the frontend!
179 * The objective with this function is to prepare the content for handling in TCEforms.
180 * Default values from outside/TSconfig is added by fetchRecord(). In this function default values from TCA is used if a field is NOT defined in $row.
181 * The resulting, processed row is stored in $this->regTableItems_data[$uniqueItemRef], where $uniqueItemRef is "[tablename]_[id-value]"
182 *
183 * @param string $table The table name
184 * @param string $id The uid value of the record (integer). Can also be a string (NEW-something) if the record is a NEW record.
185 * @param integer $pid The pid integer. For existing records this is of course the row's "pid" field. For new records it can be either a page id (positive) or a pointer to another record from the SAME table (negative) after which the record should be inserted (or on same page)
186 * @param array $row The row of the current record. If NEW record, then it may be loaded with default values (by eg. fetchRecord()).
187 * @return void
188 * @see fetchRecord()
189 */
190 public function renderRecord($table, $id, $pid, $row) {
191 $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
192 foreach ($GLOBALS['TCA'][$table]['columns'] as $column => $config) {
193 if (isset($config['config']['dbType']) && GeneralUtility::inList('date,datetime', $config['config']['dbType'])) {
194 $emptyValue = $dateTimeFormats[$config['config']['dbType']]['empty'];
195 $row[$column] = !empty($row[$column]) && $row[$column] !== $emptyValue ? strtotime($row[$column]) : 0;
196 }
197 }
198 // Init:
199 $uniqueItemRef = $table . '_' . $id;
200 // Fetches the true PAGE TSconfig pid to use later, if needed. (Until now, only for the RTE, but later..., who knows?)
201 list($tscPID) = BackendUtility::getTSCpid($table, $id, $pid);
202 $TSconfig = BackendUtility::getTCEFORM_TSconfig($table, array_merge($row, array('uid' => $id, 'pid' => $pid)));
203 // If the record has not already been loaded (in which case we DON'T do it again)...
204 if (!$this->regTableItems[$uniqueItemRef]) {
205 $this->regTableItems[$uniqueItemRef] = 1;
206 // set "loaded" flag.
207 // If the table is pages, set the previous page id internally.
208 if ($table == 'pages') {
209 $this->prevPageID = $id;
210 }
211 $this->regTableItems_data[$uniqueItemRef] = $this->renderRecordRaw($table, $id, $pid, $row, $TSconfig, $tscPID);
212 // Merges the processed array on-top of the raw one - this is done because some things in TCEforms may need access to other fields than those in the columns configuration!
213 if ($this->addRawData && is_array($row) && is_array($this->regTableItems_data[$uniqueItemRef])) {
214 $this->regTableItems_data[$uniqueItemRef] = array_merge($row, $this->regTableItems_data[$uniqueItemRef]);
215 }
216 }
217 }
218
219 /**
220 * This function performs processing on the input $row array and stores internally a corresponding array which contains processed values, ready to pass on to the TCEforms rendering in the frontend!
221 * The objective with this function is to prepare the content for handling in TCEforms.
222 * In opposite to renderRecord() this function do not prepare things like fetching TSconfig and others.
223 * The resulting, processed row will be returned.
224 *
225 * @param string $table The table name
226 * @param string $id The uid value of the record (integer). Can also be a string (NEW-something) if the record is a NEW record.
227 * @param integer $pid The pid integer. For existing records this is of course the row's "pid" field. For new records it can be either a page id (positive) or a pointer to another record from the SAME table (negative) after which the record should be inserted (or on same page)
228 * @param array $row The row of the current record. If NEW record, then it may be loaded with default values (by eg. fetchRecord()).
229 * @param array $TSconfig Tsconfig array
230 * @param integer $tscPID PAGE TSconfig pid
231 * @return array Processed record data
232 * @see renderRecord()
233 */
234 public function renderRecordRaw($table, $id, $pid, $row, $TSconfig = '', $tscPID = 0) {
235 if (!is_array($TSconfig)) {
236 $TSconfig = array();
237 }
238 // Create blank accumulation array:
239 $totalRecordContent = array();
240 // Traverse the configured columns for the table (TCA):
241 // For each column configured, we will perform processing if needed based on the type (eg. for "group" and "select" types this is needed)
242 $copyOfColumns = $GLOBALS['TCA'][$table]['columns'];
243 foreach ($copyOfColumns as $field => $fieldConfig) {
244 // Set $data variable for the field, either inputted value from $row - or if not found, the default value as defined in the "config" array
245 if (isset($row[$field])) {
246 $data = (string)$row[$field];
247 } elseif (!empty($fieldConfig['config']['eval']) && GeneralUtility::inList($fieldConfig['config']['eval'], 'null')) {
248 // Field exists but is set to NULL
249 if (array_key_exists($field, $row)) {
250 $data = NULL;
251 // Only use NULL if default value was explicitly set to be backward compatible.
252 } elseif (array_key_exists('default', $fieldConfig['config']) && $fieldConfig['config']['default'] === NULL) {
253 $data = NULL;
254 } else {
255 $data = (string)$fieldConfig['config']['default'];
256 }
257 } else {
258 $data = (string)$fieldConfig['config']['default'];
259 }
260 $data = $this->renderRecord_SW($data, $fieldConfig, $TSconfig, $table, $row, $field);
261 $totalRecordContent[$field] = $data;
262 }
263 // Further processing may apply for each field in the record depending on the settings in the "types" configuration (the list of fields to currently display for a record in TCEforms).
264 // For instance this could be processing instructions for the Rich Text Editor.
265 $types_fieldConfig = BackendUtility::getTCAtypes($table, $totalRecordContent);
266 if (is_array($types_fieldConfig)) {
267 $totalRecordContent = $this->renderRecord_typesProc($totalRecordContent, $types_fieldConfig, $tscPID, $table, $pid);
268 }
269 // Register items, mostly for external use (overriding the regItem() function)
270 foreach ($totalRecordContent as $field => $data) {
271 $this->regItem($table, $id, $field, $data);
272 }
273 // Finally, store the result:
274 reset($totalRecordContent);
275 return $totalRecordContent;
276 }
277
278 /**
279 * Function with the switch() construct which triggers functions for processing of the data value depending on the TCA-config field type.
280 *
281 * @param string $data Value to process
282 * @param array $fieldConfig TCA/columns array for field (independant of TCA for flexforms - coming from XML then)
283 * @param array $TSconfig TSconfig (blank for flexforms for now)
284 * @param string $table Table name
285 * @param array $row The row array, always of the real record (also for flexforms)
286 * @param string $field The field
287 * @return string Modified $value
288 */
289 public function renderRecord_SW($data, $fieldConfig, $TSconfig, $table, $row, $field) {
290 switch ((string)$fieldConfig['config']['type']) {
291 case 'group':
292 $data = $this->renderRecord_groupProc($data, $fieldConfig, $TSconfig, $table, $row, $field);
293 break;
294 case 'select':
295 $data = $this->renderRecord_selectProc($data, $fieldConfig, $TSconfig, $table, $row, $field);
296 break;
297 case 'flex':
298 $data = $this->renderRecord_flexProc($data, $fieldConfig, $TSconfig, $table, $row, $field);
299 break;
300 case 'inline':
301 $data = $this->renderRecord_inlineProc($data, $fieldConfig, $TSconfig, $table, $row, $field);
302 break;
303 }
304 return $data;
305 }
306
307 /**
308 * Processing of the data value in case the field type is "group"
309 *
310 * @param string $data The field value
311 * @param array $fieldConfig TCA field config
312 * @param array $TSconfig TCEform TSconfig for the record
313 * @param string $table Table name
314 * @param array $row The row
315 * @param string $field Field name
316 * @return string The processed input field value ($data)
317 * @access private
318 * @see renderRecord()
319 */
320 public function renderRecord_groupProc($data, $fieldConfig, $TSconfig, $table, $row, $field) {
321 switch ($fieldConfig['config']['internal_type']) {
322 case 'file_reference':
323 case 'file':
324 // Init array used to accumulate the files:
325 $dataAcc = array();
326 // Now, load the files into the $dataAcc array, whether stored by MM or as a list of filenames:
327 if ($fieldConfig['config']['MM']) {
328 $loadDB = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
329 $loadDB->start('', 'files', $fieldConfig['config']['MM'], $row['uid']);
330 // Setting dummy startup
331 foreach ($loadDB->itemArray as $value) {
332 if ($value['id']) {
333 $dataAcc[] = rawurlencode($value['id']) . '|' . rawurlencode(PathUtility::basename($value['id']));
334 }
335 }
336 } else {
337 $fileList = GeneralUtility::trimExplode(',', $data, TRUE);
338 foreach ($fileList as $value) {
339 if ($value) {
340 $dataAcc[] = rawurlencode($value) . '|' . rawurlencode(PathUtility::basename($value));
341 }
342 }
343 }
344 // Implode the accumulation array to a comma separated string:
345 $data = implode(',', $dataAcc);
346 break;
347 case 'db':
348 $loadDB = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
349 /** @var $loadDB \TYPO3\CMS\Core\Database\RelationHandler */
350 $loadDB->start($data, $fieldConfig['config']['allowed'], $fieldConfig['config']['MM'], $row['uid'], $table, $fieldConfig['config']);
351 $loadDB->getFromDB();
352 $data = $loadDB->readyForInterface();
353 break;
354 }
355 return $data;
356 }
357
358 /**
359 * Processing of the data value in case the field type is "select"
360 *
361 * @param string $data The field value
362 * @param array $fieldConfig TCA field config
363 * @param array $TSconfig TCEform TSconfig for the record
364 * @param string $table Table name
365 * @param array $row The row
366 * @param string $field Field name
367 * @return string The processed input field value ($data)
368 * @access private
369 * @see renderRecord()
370 */
371 public function renderRecord_selectProc($data, $fieldConfig, $TSconfig, $table, $row, $field) {
372 // Initialize:
373 // Current data set.
374 $elements = GeneralUtility::trimExplode(',', $data, TRUE);
375 // New data set, ready for interface (list of values, rawurlencoded)
376 $dataAcc = array();
377 // For list selectors (multi-value):
378 if ((int)$fieldConfig['config']['maxitems'] > 1 || $fieldConfig['config']['renderMode'] === 'tree') {
379 // Add regular elements:
380 if (!is_array($fieldConfig['config']['items'])) {
381 $fieldConfig['config']['items'] = array();
382 }
383 $fieldConfig['config']['items'] = $this->procesItemArray($fieldConfig['config']['items'], $fieldConfig['config'], $TSconfig[$field], $table, $row, $field);
384 foreach ($fieldConfig['config']['items'] as $pvpv) {
385 foreach ($elements as $eKey => $value) {
386 if ((string)$value === (string)$pvpv[1]) {
387 $dataAcc[$eKey] = rawurlencode($pvpv[1]) . '|' . rawurlencode($this->sL($pvpv[0]));
388 }
389 }
390 }
391 // Add "special"
392 if ($fieldConfig['config']['special']) {
393 $dataAcc = $this->selectAddSpecial($dataAcc, $elements, $fieldConfig['config']['special']);
394 }
395 // Add "foreign table" stuff:
396 if ($GLOBALS['TCA'][$fieldConfig['config']['foreign_table']]) {
397 $dataAcc = $this->selectAddForeign($dataAcc, $elements, $fieldConfig, $field, $TSconfig, $row, $table);
398 }
399 // Always keep the native order for display in interface:
400 ksort($dataAcc);
401 } else {
402 // Normal, <= 1 -> value without title on it
403 if ($GLOBALS['TCA'][$fieldConfig['config']['foreign_table']]) {
404 // Getting the data
405 $dataIds = $this->getDataIdList($elements, $fieldConfig, $row, $table);
406 if (!count($dataIds)) {
407 $dataIds = array(0);
408 }
409 $dataAcc[] = $dataIds[0];
410 } else {
411 $dataAcc[] = $elements[0];
412 }
413 }
414 return implode(',', $dataAcc);
415 }
416
417 /**
418 * Processing of the data value in case the field type is "flex"
419 * MUST NOT be called in case of already INSIDE a flexform!
420 *
421 * @param string $data The field value
422 * @param array $fieldConfig CA field config
423 * @param array $TSconfig TCEform TSconfig for the record
424 * @param string $table Table name
425 * @param array $row The row
426 * @param string $field Field name
427 * @return string The processed input field value ($data)
428 * @access private
429 * @see renderRecord()
430 */
431 public function renderRecord_flexProc($data, $fieldConfig, $TSconfig, $table, $row, $field) {
432 // Convert the XML data to PHP array:
433 if (!is_array($data)) {
434 $currentValueArray = GeneralUtility::xml2array($data);
435 } else {
436 $currentValueArray = $data;
437 }
438 if (is_array($currentValueArray)) {
439 // Get current value array:
440 $dataStructArray = BackendUtility::getFlexFormDS($fieldConfig['config'], $row, $table, $field);
441 // Manipulate Flexform DS via TSConfig and group access lists
442 if (is_array($dataStructArray)) {
443 $flexFormHelper = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Form\\FlexFormsHelper');
444 $dataStructArray = $flexFormHelper->modifyFlexFormDS($dataStructArray, $table, $field, $row, $fieldConfig);
445 unset($flexFormHelper);
446 }
447 if (is_array($dataStructArray)) {
448 $currentValueArray['data'] = $this->renderRecord_flexProc_procInData($currentValueArray['data'], $dataStructArray, array($data, $fieldConfig, $TSconfig, $table, $row, $field));
449 $flexObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Configuration\\FlexForm\\FlexFormTools');
450 $data = $flexObj->flexArray2Xml($currentValueArray, TRUE);
451 }
452 }
453 return $data;
454 }
455
456 /**
457 * Processing of the content in $totalRecordcontent based on settings in the types-configuration
458 *
459 * @param array $totalRecordContent The array of values which has been processed according to their type (eg. "group" or "select")
460 * @param array $types_fieldConfig The "types" configuration for the current display of fields.
461 * @param integer $tscPID PAGE TSconfig PID
462 * @param string $table Table name
463 * @param integer $pid PID value
464 * @return array The processed version of $totalRecordContent
465 * @access private
466 */
467 public function renderRecord_typesProc($totalRecordContent, $types_fieldConfig, $tscPID, $table, $pid) {
468 foreach ($types_fieldConfig as $vconf) {
469 // Find file to write to, if configured:
470 $eFile = \TYPO3\CMS\Core\Html\RteHtmlParser::evalWriteFile($vconf['spec']['static_write'], $totalRecordContent);
471 // Write file configuration:
472 if (is_array($eFile)) {
473 if ($eFile['loadFromFileField'] && $totalRecordContent[$eFile['loadFromFileField']]) {
474 // Read the external file, and insert the content between the ###TYPO3_STATICFILE_EDIT### markers:
475 $SW_fileContent = GeneralUtility::getUrl($eFile['editFile']);
476 $parseHTML = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\RteHtmlParser');
477 $parseHTML->init('', '');
478 $totalRecordContent[$vconf['field']] = $parseHTML->getSubpart($SW_fileContent, $eFile['markerField'] && trim($totalRecordContent[$eFile['markerField']]) ? trim($totalRecordContent[$eFile['markerField']]) : '###TYPO3_STATICFILE_EDIT###');
479 }
480 }
481 }
482 return $totalRecordContent;
483 }
484
485 /**
486 * Processing of the data value in case the field type is "inline"
487 * In some parts nearly the same as type "select"
488 *
489 * @param string $data The field value
490 * @param array $fieldConfig TCA field config
491 * @param array $TSconfig TCEform TSconfig for the record
492 * @param string $table Table name
493 * @param array $row The row
494 * @param string $field Field name
495 * @return string The processed input field value ($data)
496 * @access private
497 * @see renderRecord()
498 */
499 public function renderRecord_inlineProc($data, $fieldConfig, $TSconfig, $table, $row, $field) {
500 // Initialize:
501 // Current data set.
502 $elements = GeneralUtility::trimExplode(',', $data);
503 // New data set, ready for interface (list of values, rawurlencoded)
504 $dataAcc = array();
505 // At this point all records that CAN be selected is found in $recordList
506 // Now, get the data from loadDBgroup based on the input list of values.
507 $dataIds = $this->getDataIdList($elements, $fieldConfig, $row, $table);
508 // After this we can traverse the loadDBgroup values and match values with the list of possible values in $recordList:
509 foreach ($dataIds as $theId) {
510 if ($fieldConfig['config']['MM'] || $fieldConfig['config']['foreign_field']) {
511 $dataAcc[] = $theId;
512 } else {
513 foreach ($elements as $eKey => $value) {
514 if ((int)$theId === (int)$value) {
515 $dataAcc[$eKey] = $theId;
516 }
517 }
518 }
519 }
520 return implode(',', $dataAcc);
521 }
522
523 /***********************************************
524 *
525 * FlexForm processing functions
526 *
527 ***********************************************/
528 /**
529 * Function traversing sheets/languages for flex form data structures
530 *
531 * @param array $dataPart Data array
532 * @param array $dataStructArray Data Structure array
533 * @param array $pParams Various parameters to pass-through
534 * @return array Modified $dataPart array.
535 * @access private
536 * @see \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_flex_procInData(), renderRecord_flexProc_procInData_travDS()
537 */
538 public function renderRecord_flexProc_procInData($dataPart, $dataStructArray, $pParams) {
539 if (is_array($dataPart)) {
540 foreach ($dataPart as $sKey => $sheetDef) {
541 list($dataStruct, $actualSheet) = GeneralUtility::resolveSheetDefInDS($dataStructArray, $sKey);
542 if (is_array($dataStruct) && $actualSheet == $sKey && is_array($sheetDef)) {
543 foreach ($sheetDef as $lKey => $lData) {
544 $this->renderRecord_flexProc_procInData_travDS($dataPart[$sKey][$lKey], $dataStruct['ROOT']['el'], $pParams);
545 }
546 }
547 }
548 }
549 return $dataPart;
550 }
551
552 /**
553 * Traverse data array / structure
554 *
555 * @param array $dataValues Data array passed by reference.
556 * @param array $DSelements Data structure
557 * @param array $pParams Various parameters pass-through.
558 * @return void
559 * @see \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_flex_procInData(), renderRecord_flexProc_procInData_travDS()
560 */
561 public function renderRecord_flexProc_procInData_travDS(&$dataValues, $DSelements, $pParams) {
562 if (is_array($DSelements)) {
563 // For each DS element:
564 foreach ($DSelements as $key => $dsConf) {
565 // Array/Section:
566 if ($DSelements[$key]['type'] == 'array') {
567 if (is_array($dataValues[$key]['el'])) {
568 if ($DSelements[$key]['section']) {
569 foreach ($dataValues[$key]['el'] as $ik => $el) {
570 if (is_array($el)) {
571 $theKey = key($el);
572 if (is_array($dataValues[$key]['el'][$ik][$theKey]['el'])) {
573 $this->renderRecord_flexProc_procInData_travDS($dataValues[$key]['el'][$ik][$theKey]['el'], $DSelements[$key]['el'][$theKey]['el'], $pParams);
574 }
575 }
576 }
577 } else {
578 if (!isset($dataValues[$key]['el'])) {
579 $dataValues[$key]['el'] = array();
580 }
581 $this->renderRecord_flexProc_procInData_travDS($dataValues[$key]['el'], $DSelements[$key]['el'], $pParams);
582 }
583 }
584 } else {
585 if (is_array($dsConf['TCEforms']['config']) && is_array($dataValues[$key])) {
586 foreach ($dataValues[$key] as $vKey => $data) {
587 // $data,$fieldConfig,$TSconfig,$table,$row,$field
588 list(, , $CVTSconfig, $CVtable, $CVrow, $CVfield) = $pParams;
589 // Set default value:
590 if (!isset($dataValues[$key][$vKey])) {
591 $dataValues[$key][$vKey] = $dsConf['TCEforms']['config']['default'];
592 }
593 // Process value:
594 $dataValues[$key][$vKey] = $this->renderRecord_SW($dataValues[$key][$vKey], $dsConf['TCEforms'], $CVTSconfig, $CVtable, $CVrow, $CVfield);
595 }
596 }
597 }
598 }
599 }
600 }
601
602 /***********************************************
603 *
604 * Selector box processing functions
605 *
606 ***********************************************/
607 /**
608 * Adding "special" types to the $dataAcc array of selector items
609 *
610 * @param array $dataAcc Array with numeric keys, containing values for the selector box, prepared for interface. We are going to add elements to this array as needed.
611 * @param array $elements The array of original elements - basically the field value exploded by ",
612 * @param string $specialKey The "special" key from the TCA config of the field. Determines the type of processing in here.
613 * @return array Modified $dataAcc array
614 * @access private
615 * @see renderRecord_selectProc()
616 */
617 public function selectAddSpecial($dataAcc, $elements, $specialKey) {
618 // Special select types:
619 switch ((string)$specialKey) {
620 case 'tables':
621 $tNames = array_keys($GLOBALS['TCA']);
622 foreach ($tNames as $tableName) {
623 foreach ($elements as $eKey => $value) {
624 if ((string)$tableName === (string)$value) {
625 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode($this->sL($GLOBALS['TCA'][$value]['ctrl']['title']));
626 }
627 }
628 }
629 break;
630 case 'pagetypes':
631 $theTypes = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
632 if (is_array($theTypes)) {
633 foreach ($theTypes as $theTypesArrays) {
634 foreach ($elements as $eKey => $value) {
635 if ((string)$theTypesArrays[1] === (string)$value) {
636 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode($this->sL($theTypesArrays[0]));
637 }
638 }
639 }
640 }
641 break;
642 case 'exclude':
643 $theExcludeFields = BackendUtility::getExcludeFields();
644 if (is_array($theExcludeFields)) {
645 foreach ($theExcludeFields as $theExcludeFieldsArrays) {
646 foreach ($elements as $eKey => $value) {
647 if ((string)$theExcludeFieldsArrays[1] === (string)$value) {
648 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode(rtrim($theExcludeFieldsArrays[0], ':'));
649 }
650 }
651 }
652 }
653 break;
654 case 'explicitValues':
655 $theTypes = BackendUtility::getExplicitAuthFieldValues();
656 foreach ($theTypes as $tableFieldKey => $theTypeArrays) {
657 if (is_array($theTypeArrays['items'])) {
658 foreach ($theTypeArrays['items'] as $itemValue => $itemContent) {
659 foreach ($elements as $eKey => $value) {
660 if (($tableFieldKey . ':' . $itemValue . ':' . $itemContent[0]) === (string)$value) {
661 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode(('[' . $itemContent[2] . '] ' . $itemContent[1]));
662 }
663 }
664 }
665 }
666 }
667 break;
668 case 'languages':
669 $theLangs = BackendUtility::getSystemLanguages();
670 foreach ($theLangs as $lCfg) {
671 foreach ($elements as $eKey => $value) {
672 if ((string)$lCfg[1] === (string)$value) {
673 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode($lCfg[0]);
674 }
675 }
676 }
677 break;
678 case 'custom':
679 $customOptions = $GLOBALS['TYPO3_CONF_VARS']['BE']['customPermOptions'];
680 if (is_array($customOptions)) {
681 foreach ($customOptions as $coKey => $coValue) {
682 if (is_array($coValue['items'])) {
683 // Traverse items:
684 foreach ($coValue['items'] as $itemKey => $itemCfg) {
685 foreach ($elements as $eKey => $value) {
686 if (($coKey . ':' . $itemKey) === (string)$value) {
687 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode($this->sL($itemCfg[0]));
688 }
689 }
690 }
691 }
692 }
693 }
694 break;
695 case 'modListGroup':
696
697 case 'modListUser':
698 if (!$this->loadModules) {
699 $this->loadModules = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Module\\ModuleLoader');
700 $this->loadModules->load($GLOBALS['TBE_MODULES']);
701 }
702 $modList = $specialKey == 'modListUser' ? $this->loadModules->modListUser : $this->loadModules->modListGroup;
703 foreach ($modList as $theModName) {
704 foreach ($elements as $eKey => $value) {
705 $label = '';
706 // Add label for main module:
707 $pp = explode('_', $value);
708 if (count($pp) > 1) {
709 $label .= $GLOBALS['LANG']->moduleLabels['tabs'][($pp[0] . '_tab')] . '>';
710 }
711 // Add modules own label now:
712 $label .= $GLOBALS['LANG']->moduleLabels['tabs'][$value . '_tab'];
713 if ((string)$theModName === (string)$value) {
714 $dataAcc[$eKey] = rawurlencode($value) . '|' . rawurlencode($label);
715 }
716 }
717 }
718 break;
719 }
720 return $dataAcc;
721 }
722
723 /**
724 * Adds the foreign record elements to $dataAcc, if any
725 *
726 * @param array $dataAcc Array with numeric keys, containing values for the selector box, prepared for interface. We are going to add elements to this array as needed.
727 * @param array $elements The array of original elements - basically the field value exploded by ",
728 * @param array $fieldConfig Field configuration from TCA
729 * @param string $field The field name
730 * @param array $TSconfig TSconfig for the record
731 * @param array $row The record
732 * @param array $table The current table
733 * @return array Modified $dataAcc array
734 * @access private
735 * @see renderRecord_selectProc()
736 */
737 public function selectAddForeign($dataAcc, $elements, $fieldConfig, $field, $TSconfig, $row, $table) {
738 // Init:
739 $recordList = array();
740 // Foreign_table
741 $subres = BackendUtility::exec_foreign_table_where_query($fieldConfig, $field, $TSconfig);
742 while ($subrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($subres)) {
743 // Resolve move-placeholder, to check the right uid against $dataIds
744 BackendUtility::workspaceOL($fieldConfig['config']['foreign_table'], $subrow);
745 $recordList[$subrow['uid']] = BackendUtility::getRecordTitle($fieldConfig['config']['foreign_table'], $subrow);
746 }
747 $GLOBALS['TYPO3_DB']->sql_free_result($subres);
748 // neg_foreign_table
749 if (is_array($GLOBALS['TCA'][$fieldConfig['config']['neg_foreign_table']])) {
750 $subres = BackendUtility::exec_foreign_table_where_query($fieldConfig, $field, $TSconfig, 'neg_');
751 while ($subrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($subres)) {
752 // Resolve move-placeholder, to check the right uid against $dataIds
753 BackendUtility::workspaceOL($fieldConfig['config']['nes_foreign_table'], $subrow);
754 $recordList[-$subrow['uid']] = BackendUtility::getRecordTitle($fieldConfig['config']['neg_foreign_table'], $subrow);
755 }
756 $GLOBALS['TYPO3_DB']->sql_free_result($subres);
757 }
758 // At this point all records that CAN be selected is found in $recordList
759 // Now, get the data from loadDBgroup based on the input list of values.
760 $dataIds = $this->getDataIdList($elements, $fieldConfig, $row, $table);
761 if ($fieldConfig['config']['MM']) {
762 $dataAcc = array();
763 }
764 // Reset, if MM (which cannot bear anything but real relations!)
765 // After this we can traverse the loadDBgroup values and match values with the list of possible values in $recordList:
766 foreach ($dataIds as $theId) {
767 if (isset($recordList[$theId])) {
768 $lPrefix = $this->sL($fieldConfig['config'][($theId > 0 ? '' : 'neg_') . 'foreign_table_prefix']);
769 if ($fieldConfig['config']['MM'] || $fieldConfig['config']['foreign_field']) {
770 $dataAcc[] = rawurlencode($theId) . '|' . rawurlencode(GeneralUtility::fixed_lgd_cs(($lPrefix . strip_tags($recordList[$theId])), $GLOBALS['BE_USER']->uc['titleLen']));
771 } else {
772 foreach ($elements as $eKey => $value) {
773 if ((int)$theId === (int)$value) {
774 $dataAcc[$eKey] = rawurlencode($theId) . '|' . rawurlencode(GeneralUtility::fixed_lgd_cs(($lPrefix . strip_tags($recordList[$theId])), $GLOBALS['BE_USER']->uc['titleLen']));
775 }
776 }
777 }
778 }
779 }
780 return $dataAcc;
781 }
782
783 /**
784 * Returning the id-list processed by loadDBgroup for the foreign tables.
785 *
786 * @param array $elements The array of original elements - basically the field value exploded by ",
787 * @param array $fieldConfig Field configuration from TCA
788 * @param array $row The data array, currently. Used to set the "local_uid" for selecting MM relation records.
789 * @param string $table Current table name. passed on to \TYPO3\CMS\Core\Database\RelationHandler
790 * @return array An array with ids of the records from the input elements array.
791 * @access private
792 */
793 public function getDataIdList($elements, $fieldConfig, $row, $table) {
794 // Use given uid (might be the uid of a workspace record)
795 $recordId = $row['uid'];
796 // If not dealing with MM relations, then always(!) use the default live uid
797 if (empty($fieldConfig['config']['MM'])) {
798 $recordId = $this->getLiveDefaultId($table, $row['uid']);
799 }
800 $loadDB = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
801 $loadDB->registerNonTableValues = $fieldConfig['config']['allowNonIdValues'] ? 1 : 0;
802 $loadDB->start(implode(',', $elements), $fieldConfig['config']['foreign_table'] . ',' . $fieldConfig['config']['neg_foreign_table'], $fieldConfig['config']['MM'], $recordId, $table, $fieldConfig['config']);
803 $idList = $loadDB->convertPosNeg($loadDB->getValueArray(), $fieldConfig['config']['foreign_table'], $fieldConfig['config']['neg_foreign_table']);
804 return $idList;
805 }
806
807 /**
808 * Processing of selector box items. This includes the automated adding of elements plus user-function processing.
809 *
810 * @param array The elements to process
811 * @param array TCA/columns configuration
812 * @param array TSconfig for the field
813 * @param string The table name
814 * @param array The current row
815 * @param string The field name
816 * @return array The modified input $selItems array
817 * @access private
818 * @see renderRecord_selectProc()
819 */
820 public function procesItemArray($selItems, $config, $fieldTSConfig, $table, $row, $field) {
821 $selItems = $this->addItems($selItems, $fieldTSConfig['addItems.']);
822 if ($config['itemsProcFunc']) {
823 $selItems = $this->procItems($selItems, $fieldTSConfig['itemsProcFunc.'], $config, $table, $row, $field);
824 }
825 return $selItems;
826 }
827
828 /**
829 * Adding items from $iArray to $items array
830 *
831 * @param array $items The array of selector box items to which key(value) / value(label) pairs from $iArray will be added.
832 * @param array $iArray The array of elements to add. The keys will become values. The value will become the label.
833 * @return array The modified input $items array
834 * @access private
835 * @see procesItemArray()
836 */
837 public function addItems($items, $iArray) {
838 if (is_array($iArray)) {
839 foreach ($iArray as $value => $label) {
840 $items[] = array($label, $value);
841 }
842 }
843 return $items;
844 }
845
846 /**
847 * User processing of a selector box array of values.
848 *
849 * @param array $items The array of selector box items
850 * @param array $itemsProcFuncTSconfig TSconfig for the fields itemProcFunc
851 * @param array $config TCA/columns configuration
852 * @param string $table The table name
853 * @param array $row The current row
854 * @param string $field The field name
855 * @return array The modified input $items array
856 * @access private
857 * @see procesItemArray()
858 */
859 public function procItems($items, $itemsProcFuncTSconfig, $config, $table, $row, $field) {
860 $params = array();
861 $params['items'] = &$items;
862 $params['config'] = $config;
863 $params['TSconfig'] = $itemsProcFuncTSconfig;
864 $params['table'] = $table;
865 $params['row'] = $row;
866 $params['field'] = $field;
867 GeneralUtility::callUserFunction($config['itemsProcFunc'], $params, $this);
868 return $items;
869 }
870
871 /***********************************************
872 *
873 * Helper functions
874 *
875 ***********************************************/
876 /**
877 * Sets the lock for a record from table/id, IF $this->lockRecords is set!
878 *
879 * @param string $table The table name
880 * @param integer $id The id of the record
881 * @param integer $pid The pid of the record
882 * @return void
883 */
884 public function lockRecord($table, $id, $pid = 0) {
885 if ($this->lockRecords) {
886 BackendUtility::lockRecords($table, $id, $pid);
887 }
888 }
889
890 /**
891 * Dummy function, can be used to "register" records. Used by eg. the "show_item" script.
892 *
893 * @param string $table Table name
894 * @param integer $id Record id
895 * @param string $field Field name
896 * @param string $content Field content.
897 * @return void
898 * @access private
899 * @see renderRecord()
900 */
901 public function regItem($table, $id, $field, $content) {
902
903 }
904
905 /**
906 * Local wrapper function for LANG->sL (returning language labels)
907 *
908 * @param string Language label key
909 * @return string Localized label value.
910 * @access private
911 */
912 public function sL($in) {
913 return $GLOBALS['LANG']->sL($in);
914 }
915
916 /**
917 * Gets the record uid of the live default record. If already
918 * pointing to the live record, the submitted record uid is returned.
919 *
920 * @param string $tableName
921 * @param int $id
922 * @return int
923 */
924 protected function getLiveDefaultId($tableName, $id) {
925 $liveDefaultId = BackendUtility::getLiveVersionIdOfRecord($tableName, $id);
926 if ($liveDefaultId === NULL) {
927 $liveDefaultId = $id;
928 }
929 return $liveDefaultId;
930 }
931
932 }