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