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