[BUGFIX] Data merging in FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormEngine.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\Form\Utility\FormEngineUtility;
18 use TYPO3\CMS\Backend\Template\DocumentTemplate;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Backend\Utility\IconUtility;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
23 use TYPO3\CMS\Core\Html\HtmlParser;
24 use TYPO3\CMS\Core\Http\AjaxRequestHandler;
25 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\MathUtility;
28 use TYPO3\CMS\Lang\LanguageService;
29 use TYPO3\CMS\Backend\Form\Container\FullRecordContainer;
30 use TYPO3\CMS\Backend\Form\Container\SoloFieldContainer;
31 use TYPO3\CMS\Backend\Form\Container\InlineRecordContainer;
32 use TYPO3\CMS\Backend\Form\Container\ListOfFieldsContainer;
33 use TYPO3\CMS\Core\Utility\ArrayUtility;
34
35 /**
36 * This is form engine - Class for creating the backend editing forms.
37 */
38 class FormEngine {
39
40 /**
41 * @var bool
42 */
43 public $disableWizards = FALSE;
44
45 /**
46 * List of additional preview languages that should be shown to the user. Initialized early.
47 *
48 * array(
49 * $languageUid => array(
50 * 'uid' => $languageUid,
51 * 'ISOcode' => $isoCodeOfLanguage
52 * )
53 * )
54 *
55 * @var array
56 */
57 protected $additionalPreviewLanguages = array();
58
59 /**
60 * @var string
61 */
62 protected $extJSCODE = '';
63
64 /**
65 * @var array HTML of additional hidden fields rendered by sub containers
66 */
67 protected $hiddenFieldAccum = array();
68
69 /**
70 * @var string
71 */
72 public $TBE_EDITOR_fieldChanged_func = '';
73
74 /**
75 * @var bool
76 */
77 public $loadMD5_JS = TRUE;
78
79 /**
80 * Alternative return URL path (default is \TYPO3\CMS\Core\Utility\GeneralUtility::linkThisScript())
81 *
82 * @var string
83 */
84 public $returnUrl = '';
85
86 /**
87 * Can be set to point to a field name in the form which will be set to '1' when the form
88 * is submitted with a *save* button. This way the recipient script can determine that
89 * the form was submitted for save and not "close" for example.
90 *
91 * @var string
92 */
93 public $doSaveFieldName = '';
94
95 /**
96 * Can be set TRUE/FALSE to whether palettes (secondary options) are in the topframe or in form.
97 * TRUE means they are NOT IN-form. So a collapsed palette is one, which is shown in the top frame, not in the page.
98 *
99 * @var bool
100 */
101 public $palettesCollapsed = FALSE;
102
103 /**
104 * If this evaluates to TRUE, the forms are rendering only localization relevant fields of the records.
105 *
106 * @var string
107 */
108 public $localizationMode = '';
109
110 /**
111 * When enabled all elements are rendered non-editable
112 *
113 * @var bool
114 */
115 protected $renderReadonly = FALSE;
116
117 /**
118 * @var InlineStackProcessor
119 */
120 protected $inlineStackProcessor;
121
122 /**
123 * @var array Data array from IRRE pushed to frontend as json array
124 */
125 protected $inlineData = array();
126
127 /**
128 * Set by readPerms() (caching)
129 *
130 * @var string
131 */
132 public $perms_clause = '';
133
134 /**
135 * Set by readPerms() (caching-flag)
136 *
137 * @var bool
138 */
139 public $perms_clause_set = FALSE;
140
141 /**
142 * Total wrapping for the table rows
143 *
144 * @var string
145 * @todo: This is overwritten in __construct
146 */
147 public $totalWrap = '<hr />|<hr />';
148
149 /**
150 * This array of fields will be set as hidden-fields instead of rendered normally!
151 * This is used by EditDocumentController to force some field values if set as "overrideVals" in _GP
152 *
153 * @var array
154 */
155 public $hiddenFieldListArr = array();
156
157 /**
158 * Used to register input-field names, which are required. (Done during rendering of the fields).
159 * This information is then used later when the JavaScript is made.
160 *
161 * @var array
162 */
163 protected $requiredFields = array();
164
165 /**
166 * Used to register input-field names, which are required an have additional requirements.
167 * (e.g. like a date/time must be positive integer)
168 * The information of this array is merged with $this->requiredFields later.
169 *
170 * @var array
171 */
172 protected $requiredAdditional = array();
173
174 /**
175 * Used to register the min and max number of elements
176 * for selector boxes where that apply (in the "group" type for instance)
177 *
178 * @var array
179 */
180 protected $requiredElements = array();
181
182 /**
183 * Used to determine where $requiredFields or $requiredElements are nested (in Tabs or IRRE)
184 *
185 * @var array
186 */
187 public $requiredNested = array();
188
189 // Internal, registers for user defined functions etc.
190 /**
191 * Additional HTML code, printed before the form
192 *
193 * @var array
194 */
195 public $additionalCode_pre = array();
196
197 /**
198 * Additional JavaScript printed after the form
199 *
200 * @var array
201 */
202 protected $additionalJS_post = array();
203
204 /**
205 * Additional JavaScript executed on submit; If you set "OK" variable it will raise an error
206 * about RTEs not being loaded and offer to block further submission.
207 *
208 * @var array
209 */
210 public $additionalJS_submit = array();
211
212 /**
213 * Array containing hook class instances called once for a form
214 *
215 * @var array
216 */
217 public $hookObjectsMainFields = array();
218
219 /**
220 * Rows getting inserted into the headers (when called from the EditDocumentController)
221 *
222 * @var array
223 */
224 public $extraFormHeaders = array();
225
226 /**
227 * Form template, relative to typo3 directory
228 *
229 * @var string
230 */
231 public $templateFile = '';
232
233 /**
234 * @var string The table that is handled
235 */
236 protected $table = '';
237
238 /**
239 * @var array Database row data
240 */
241 protected $databaseRow = array();
242
243 /**
244 * @var NodeFactory Factory taking care of creating appropriate sub container and elements
245 */
246 protected $nodeFactory;
247
248 /**
249 * Constructor function, setting internal variables, loading the styles used.
250 *
251 */
252 public function __construct() {
253 $this->inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
254 $this->initializeAdditionalPreviewLanguages();
255 // Prepare user defined objects (if any) for hooks which extend this function:
256 $this->hookObjectsMainFields = array();
257 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getMainFieldsClass'])) {
258 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getMainFieldsClass'] as $classRef) {
259 $this->hookObjectsMainFields[] = GeneralUtility::getUserObj($classRef);
260 }
261 }
262 $this->templateFile = 'sysext/backend/Resources/Private/Templates/FormEngine.html';
263 $template = GeneralUtility::getUrl(PATH_typo3 . $this->templateFile);
264 // Wrapping all table rows for a particular record being edited:
265 $this->totalWrap = HtmlParser::getSubpart($template, '###TOTALWRAP###');
266 $this->nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
267 }
268
269 /**
270 * Set render read only flag
271 *
272 * @param bool $value
273 */
274 public function setRenderReadonly($value) {
275 $this->renderReadonly = (bool)$value;
276 }
277
278 /*******************************************************
279 *
280 * Rendering the forms, fields etc
281 *
282 *******************************************************/
283
284 /**
285 * Based on the $table and $row of content, this displays the complete TCEform for the record.
286 * The input-$row is required to be preprocessed if necessary by eg.
287 * the \TYPO3\CMS\Backend\Form\DataPreprocessor class. For instance the RTE content
288 * should be transformed through this class first.
289 *
290 * @param string $table The table name
291 * @param array $databaseRow The record from the table for which to render a field.
292 * @return string HTML output
293 */
294 public function getMainFields($table, array $databaseRow) {
295 $this->table = $table;
296 $this->databaseRow = $databaseRow;
297
298 // Hook: getMainFields_preProcess
299 foreach ($this->hookObjectsMainFields as $hookObj) {
300 if (method_exists($hookObj, 'getMainFields_preProcess')) {
301 $hookObj->getMainFields_preProcess($table, $databaseRow, $this);
302 }
303 }
304
305 $options = $this->getConfigurationOptionsForChildElements();
306 $options['renderType'] = 'fullRecordContainer';
307 $resultArray = $this->nodeFactory->create($options)->render();
308
309 $content = $resultArray['html'];
310 $this->mergeResult($resultArray);
311
312 // Hook: getMainFields_postProcess
313 foreach ($this->hookObjectsMainFields as $hookObj) {
314 if (method_exists($hookObj, 'getMainFields_postProcess')) {
315 $hookObj->getMainFields_postProcess($table, $databaseRow, $this);
316 }
317 }
318
319 return $content;
320 }
321
322 /**
323 * Will return the TCEform element for just a single field from a record.
324 * The field must be listed in the currently displayed fields (as found in [types][showitem]) for the record.
325 * This also means that the $table/$row supplied must be complete so the list of fields to show can be found correctly
326 * This method is used by "full screen RTE". Difference to getListedFields() is basically that no wrapper html is rendered around the element.
327 *
328 * @param string $table The table name
329 * @param array $databaseRow The record from the table for which to render a field.
330 * @param string $theFieldToReturn The field name to return the TCEform element for.
331 * @return string HTML output
332 */
333 public function getSoloField($table, $databaseRow, $theFieldToReturn) {
334 $this->table = $table;
335 $this->databaseRow = $databaseRow;
336
337 $options = $this->getConfigurationOptionsForChildElements();
338 $options['singleFieldToRender'] = $theFieldToReturn;
339 $options['renderType'] = 'soloFieldContainer';
340 $resultArray = $this->nodeFactory->create($options)->render();
341 $html = $resultArray['html'];
342
343 $this->requiredElements = $resultArray['requiredElements'];
344 $this->requiredFields = $resultArray['requiredFields'];
345 $this->requiredAdditional = $resultArray['requiredAdditional'];
346 $this->requiredNested = $resultArray['requiredNested'];
347 $this->additionalJS_post = $resultArray['additionalJavaScriptPost'];
348 $this->additionalJS_submit = $resultArray['additionalJavaScriptSubmit'];
349 $this->extJSCODE = $resultArray['extJSCODE'];
350 $this->inlineData = $resultArray['inlineData'];
351 $this->hiddenFieldAccum = $resultArray['additionalHiddenFields'];
352 $this->additionalCode_pre = $resultArray['additionalHeadTags'];
353
354 return $html;
355 }
356
357 /**
358 * Will return the TCEform elements for a pre-defined list of fields.
359 * Notice that this will STILL use the configuration found in the list [types][showitem] for those fields which are found there.
360 * So ideally the list of fields given as argument to this function should also be in the current [types][showitem] list of the record.
361 * Used for displaying forms for the frontend edit icons for instance.
362 *
363 * @todo: The list module calls this method multiple times on the same class instance if single fields
364 * @todo: of multiple records are edited. This is why the properties are accumulated here.
365 *
366 * @param string $table The table name
367 * @param array $databaseRow The record array.
368 * @param string $list Commalist of fields from the table. These will be shown in the specified order in a form.
369 * @return string TCEform elements in a string.
370 */
371 public function getListedFields($table, $databaseRow, $list) {
372 $this->table = $table;
373 $this->databaseRow = $databaseRow;
374
375 $options = $this->getConfigurationOptionsForChildElements();
376 $options['fieldListToRender'] = $list;
377 $options['renderType'] = 'listOfFieldsContainer';
378 $resultArray = $this->nodeFactory->create($options)->render();
379 $html = $resultArray['html'];
380 $this->mergeResult($resultArray);
381
382 return $html;
383 }
384
385
386 /**
387 * Merge existing data with the given result array
388 *
389 * @param array $resultArray Array returned by child
390 * @return void
391 */
392 protected function mergeResult(array $resultArray) {
393 foreach ($resultArray['requiredElements'] as $name => $element) {
394 $this->requiredElements[$name] = $element;
395 }
396 foreach ($resultArray['requiredFields'] as $value => $name) {
397 $this->requiredFields[$value] = $name;
398 }
399 foreach ($resultArray['requiredAdditional'] as $name => $subArray) {
400 $this->requiredAdditional[$name] = $subArray;
401 }
402 foreach ($resultArray['requiredNested'] as $value => $name) {
403 $this->requiredNested[$value] = $name;
404 }
405 foreach ($resultArray['additionalJavaScriptPost'] as $element) {
406 $this->additionalJS_post[] = $element;
407 }
408 foreach ($resultArray['additionalJavaScriptSubmit'] as $element) {
409 $this->additionalJS_submit[] = $element;
410 }
411 $this->extJSCODE = $this->extJSCODE . LF . $resultArray['extJSCODE'];
412 $this->inlineData = $resultArray['inlineData'];
413 foreach ($resultArray['additionalHiddenFields'] as $element) {
414 $this->hiddenFieldAccum[] = $element;
415 }
416 foreach ($resultArray['additionalHeadTags'] as $element) {
417 $this->additionalCode_pre[] = $element;
418 }
419
420 if (!empty($resultArray['inlineData'])) {
421 $resultArrayInlineData = $this->inlineData;
422 $resultInlineData = $resultArray['inlineData'];
423 ArrayUtility::mergeRecursiveWithOverrule($resultArrayInlineData, $resultInlineData);
424 $this->inlineData = $resultArrayInlineData;
425 }
426 }
427
428 /**
429 * Returns an array of global form settings to be given to child elements.
430 *
431 * @return array
432 */
433 protected function getConfigurationOptionsForChildElements() {
434 return array(
435 'renderReadonly' => $this->renderReadonly,
436 'disabledWizards' => $this->disableWizards,
437 'returnUrl' => $this->thisReturnUrl(),
438 'palettesCollapsed' => $this->palettesCollapsed,
439 'table' => $this->table,
440 'databaseRow' => $this->databaseRow,
441 'recordTypeValue' => '',
442 'additionalPreviewLanguages' => $this->additionalPreviewLanguages,
443 'localizationMode' => $this->localizationMode, // @todo: find out the details, Warning, this overlaps with inline behaviour localizationMode
444 'elementBaseName' => '',
445 'tabAndInlineStack' => array(),
446 'inlineFirstPid' => $this->getInlineFirstPid(),
447 'inlineExpandCollapseStateArray' => $this->getInlineExpandCollapseStateArrayForTableUid($this->table, $this->databaseRow['uid']),
448 'inlineData' => $this->inlineData,
449 'inlineStructure' => $this->inlineStackProcessor->getStructure(),
450 'overruleTypesArray' => array(),
451 'hiddenFieldListArray' => $this->hiddenFieldListArr,
452 'isAjaxContext' => FALSE,
453 'flexFormFieldIdentifierPrefix' => 'ID',
454 'nodeFactory' => $this->nodeFactory,
455 );
456 }
457
458 /**
459 * General processor for AJAX requests concerning IRRE.
460 *
461 * @param array $_ Additional parameters (not used here)
462 * @param AjaxRequestHandler $ajaxObj The AjaxRequestHandler object of this request
463 * @throws \RuntimeException
464 * @return void
465 */
466 public function processInlineAjaxRequest($_, AjaxRequestHandler $ajaxObj) {
467 $ajaxArguments = GeneralUtility::_GP('ajax');
468 $ajaxIdParts = explode('::', $GLOBALS['ajaxID'], 2);
469 if (isset($ajaxArguments) && is_array($ajaxArguments) && count($ajaxArguments)) {
470 $ajaxMethod = $ajaxIdParts[1];
471 $ajaxObj->setContentFormat('jsonbody');
472 // Construct runtime environment for Inline Relational Record Editing
473 $this->setUpRuntimeEnvironmentForAjaxRequests();
474 // @todo: ajaxArguments[2] is "returnUrl" in the first 3 calls - still needed?
475 switch ($ajaxMethod) {
476 case 'synchronizeLocalizeRecords':
477 $domObjectId = $ajaxArguments[0];
478 $type = $ajaxArguments[1];
479 // Parse the DOM identifier (string), add the levels to the structure stack (array), load the TCA config:
480 $this->inlineStackProcessor->initializeByParsingDomObjectIdString($domObjectId);
481 $this->inlineStackProcessor->injectAjaxConfiguration($ajaxArguments['context']);
482 $inlineFirstPid = FormEngineUtility::getInlineFirstPidFromDomObjectId($domObjectId);
483 $ajaxObj->setContent($this->renderInlineSynchronizeLocalizeRecords($type, $inlineFirstPid));
484 break;
485 case 'createNewRecord':
486 $domObjectId = $ajaxArguments[0];
487 $createAfterUid = 0;
488 if (isset($ajaxArguments[1])) {
489 $createAfterUid = $ajaxArguments[1];
490 }
491 // Parse the DOM identifier (string), add the levels to the structure stack (array), load the TCA config:
492 $this->inlineStackProcessor->initializeByParsingDomObjectIdString($domObjectId);
493 $this->inlineStackProcessor->injectAjaxConfiguration($ajaxArguments['context']);
494 $ajaxObj->setContent($this->renderInlineNewChildRecord($domObjectId, $createAfterUid));
495 break;
496 case 'getRecordDetails':
497 $domObjectId = $ajaxArguments[0];
498 // Parse the DOM identifier (string), add the levels to the structure stack (array), load the TCA config:
499 $this->inlineStackProcessor->initializeByParsingDomObjectIdString($domObjectId);
500 $this->inlineStackProcessor->injectAjaxConfiguration($ajaxArguments['context']);
501 $ajaxObj->setContent($this->renderInlineChildRecord($domObjectId));
502 break;
503 case 'setExpandedCollapsedState':
504 $domObjectId = $ajaxArguments[0];
505 // Parse the DOM identifier (string), add the levels to the structure stack (array), don't load TCA config
506 $this->inlineStackProcessor->initializeByParsingDomObjectIdString($domObjectId, FALSE);
507 $expand = $ajaxArguments[1];
508 $collapse = $ajaxArguments[2];
509 $this->setInlineExpandedCollapsedState($expand, $collapse);
510 break;
511 default:
512 throw new \RuntimeException('Not a valid ajax identifier', 1428227862);
513 }
514 }
515 }
516
517 /**
518 * Handle AJAX calls to dynamically load the form fields of a given inline record.
519 *
520 * @param string $domObjectId The calling object in hierarchy, that requested a new record.
521 * @return array An array to be used for JSON
522 */
523 protected function renderInlineChildRecord($domObjectId) {
524 // The current table - for this table we should add/import records
525 $current = $this->inlineStackProcessor->getUnstableStructure();
526 // The parent table - this table embeds the current table
527 $parent = $this->inlineStackProcessor->getStructureLevel(-1);
528 $config = $parent['config'];
529
530 if (empty($config['foreign_table']) || !is_array($GLOBALS['TCA'][$config['foreign_table']])) {
531 return $this->getErrorMessageForAJAX('Wrong configuration in table ' . $parent['table']);
532 }
533
534 $config = FormEngineUtility::mergeInlineConfiguration($config);
535
536 // Set flag in config so that only the fields are rendered
537 $config['renderFieldsOnly'] = TRUE;
538 $collapseAll = isset($config['appearance']['collapseAll']) && $config['appearance']['collapseAll'];
539 $expandSingle = isset($config['appearance']['expandSingle']) && $config['appearance']['expandSingle'];
540
541 $inlineRelatedRecordResolver = GeneralUtility::makeInstance(InlineRelatedRecordResolver::class);
542 $record = $inlineRelatedRecordResolver->getRecord($current['table'], $current['uid']);
543
544 $inlineFirstPid = FormEngineUtility::getInlineFirstPidFromDomObjectId($domObjectId);
545 // The HTML-object-id's prefix of the dynamically created record
546 $objectPrefix = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($inlineFirstPid) . '-' . $current['table'];
547 $objectId = $objectPrefix . '-' . $record['uid'];
548
549 $options = $this->getConfigurationOptionsForChildElements();
550 $options['databaseRow'] = array('uid' => $parent['uid']);
551 $options['inlineFirstPid'] = $inlineFirstPid;
552 $options['inlineRelatedRecordToRender'] = $record;
553 $options['inlineRelatedRecordConfig'] = $config;
554 $options['inlineStructure'] = $this->inlineStackProcessor->getStructure();
555 $options['isAjaxContext'] = TRUE;
556
557 $options['renderType'] = 'inlineRecordContainer';
558 $childArray = $this->nodeFactory->create($options)->render();
559
560 if ($childArray === FALSE) {
561 return $this->getErrorMessageForAJAX('Access denied');
562 }
563
564 // @todo: Refactor this mess ... see other methods like getMainFields, too
565 $this->requiredElements = $childArray['requiredElements'];
566 $this->requiredFields = $childArray['requiredFields'];
567 $this->requiredAdditional = $childArray['requiredAdditional'];
568 $this->requiredNested = $childArray['requiredNested'];
569 $this->additionalJS_post = $childArray['additionalJavaScriptPost'];
570 $this->additionalJS_submit = $childArray['additionalJavaScriptSubmit'];
571 $this->extJSCODE = $childArray['extJSCODE'];
572 $this->inlineData = $childArray['inlineData'];
573 $this->hiddenFieldAccum = $childArray['additionalHiddenFields'];
574 $this->additionalCode_pre = $childArray['additionalHeadTags'];
575
576 $jsonArray = array(
577 'data' => $childArray['html'],
578 'scriptCall' => array(),
579 );
580 $jsonArray['scriptCall'][] = 'inline.domAddRecordDetails(' . GeneralUtility::quoteJSvalue($domObjectId) . ',' . GeneralUtility::quoteJSvalue($objectPrefix) . ',' . ($expandSingle ? '1' : '0') . ',json.data);';
581 if ($config['foreign_unique']) {
582 $jsonArray['scriptCall'][] = 'inline.removeUsed(' . GeneralUtility::quoteJSvalue($objectPrefix) . ',' . GeneralUtility::quoteJSvalue($record['uid']) . ');';
583 }
584
585 $jsonArray = $this->getInlineAjaxCommonScriptCalls($jsonArray, $config, $inlineFirstPid);
586
587 // Collapse all other records if requested:
588 if (!$collapseAll && $expandSingle) {
589 $jsonArray['scriptCall'][] = 'inline.collapseAllRecords(' . GeneralUtility::quoteJSvalue($objectId) . ',' . GeneralUtility::quoteJSvalue($objectPrefix) . ',' . GeneralUtility::quoteJSvalue($record['uid']) . ');';
590 }
591
592 return $jsonArray;
593 }
594
595 /**
596 * Handle AJAX calls to show a new inline-record of the given table.
597 *
598 * @param string $domObjectId The calling object in hierarchy, that requested a new record.
599 * @param string|int $foreignUid If set, the new record should be inserted after that one.
600 * @return array An array to be used for JSON
601 */
602 protected function renderInlineNewChildRecord($domObjectId, $foreignUid) {
603 // The current table - for this table we should add/import records
604 $current = $this->inlineStackProcessor->getUnstableStructure();
605 // The parent table - this table embeds the current table
606 $parent = $this->inlineStackProcessor->getStructureLevel(-1);
607 $config = $parent['config'];
608
609 if (empty($config['foreign_table']) || !is_array($GLOBALS['TCA'][$config['foreign_table']])) {
610 return $this->getErrorMessageForAJAX('Wrong configuration in table ' . $parent['table']);
611 }
612
613 $inlineRelatedRecordResolver = GeneralUtility::makeInstance(InlineRelatedRecordResolver::class);
614
615 $config = FormEngineUtility::mergeInlineConfiguration($config);
616
617 $collapseAll = isset($config['appearance']['collapseAll']) && $config['appearance']['collapseAll'];
618 $expandSingle = isset($config['appearance']['expandSingle']) && $config['appearance']['expandSingle'];
619
620 $inlineFirstPid = FormEngineUtility::getInlineFirstPidFromDomObjectId($domObjectId);
621
622 // Dynamically create a new record using \TYPO3\CMS\Backend\Form\DataPreprocessor
623 if (!$foreignUid || !MathUtility::canBeInterpretedAsInteger($foreignUid) || $config['foreign_selector']) {
624 $record = $inlineRelatedRecordResolver->getNewRecord($inlineFirstPid, $current['table']);
625 // Set default values for new created records
626 if (isset($config['foreign_record_defaults']) && is_array($config['foreign_record_defaults'])) {
627 $foreignTableConfig = $GLOBALS['TCA'][$current['table']];
628 // The following system relevant fields can't be set by foreign_record_defaults
629 $notSettableFields = array(
630 'uid', 'pid', 't3ver_oid', 't3ver_id', 't3ver_label', 't3ver_wsid', 't3ver_state', 't3ver_stage',
631 't3ver_count', 't3ver_tstamp', 't3ver_move_id'
632 );
633 $configurationKeysForNotSettableFields = array(
634 'crdate', 'cruser_id', 'delete', 'origUid', 'transOrigDiffSourceField', 'transOrigPointerField',
635 'tstamp'
636 );
637 foreach ($configurationKeysForNotSettableFields as $configurationKey) {
638 if (isset($foreignTableConfig['ctrl'][$configurationKey])) {
639 $notSettableFields[] = $foreignTableConfig['ctrl'][$configurationKey];
640 }
641 }
642 foreach ($config['foreign_record_defaults'] as $fieldName => $defaultValue) {
643 if (isset($foreignTableConfig['columns'][$fieldName]) && !in_array($fieldName, $notSettableFields)) {
644 $record[$fieldName] = $defaultValue;
645 }
646 }
647 }
648 // Set language of new child record to the language of the parent record:
649 if ($parent['localizationMode'] === 'select') {
650 $parentRecord = $inlineRelatedRecordResolver->getRecord($parent['table'], $parent['uid']);
651 $parentLanguageField = $GLOBALS['TCA'][$parent['table']]['ctrl']['languageField'];
652 $childLanguageField = $GLOBALS['TCA'][$current['table']]['ctrl']['languageField'];
653 if ($parentRecord[$parentLanguageField] > 0) {
654 $record[$childLanguageField] = $parentRecord[$parentLanguageField];
655 }
656 }
657 } else {
658 // @todo: Check this: Else also hits if $foreignUid = 0?
659 $record = $inlineRelatedRecordResolver->getRecord($current['table'], $foreignUid);
660 }
661 // Now there is a foreign_selector, so there is a new record on the intermediate table, but
662 // this intermediate table holds a field, which is responsible for the foreign_selector, so
663 // we have to set this field to the uid we get - or if none, to a new uid
664 if ($config['foreign_selector'] && $foreignUid) {
665 $selConfig = FormEngineUtility::getInlinePossibleRecordsSelectorConfig($config, $config['foreign_selector']);
666 // For a selector of type group/db, prepend the tablename (<tablename>_<uid>):
667 $record[$config['foreign_selector']] = $selConfig['type'] != 'groupdb' ? '' : $selConfig['table'] . '_';
668 $record[$config['foreign_selector']] .= $foreignUid;
669 if ($selConfig['table'] === 'sys_file') {
670 $fileRecord = $inlineRelatedRecordResolver->getRecord($selConfig['table'], $foreignUid);
671 if ($fileRecord !== FALSE && !$this->checkInlineFileTypeAccessForField($selConfig, $fileRecord)) {
672 return $this->getErrorMessageForAJAX('File extension ' . $fileRecord['extension'] . ' is not allowed here!');
673 }
674 }
675 }
676 // The HTML-object-id's prefix of the dynamically created record
677 $objectName = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($inlineFirstPid);
678 $objectPrefix = $objectName . '-' . $current['table'];
679 $objectId = $objectPrefix . '-' . $record['uid'];
680
681 $options = $this->getConfigurationOptionsForChildElements();
682 $options['databaseRow'] = array('uid' => $parent['uid']);
683 $options['inlineFirstPid'] = $inlineFirstPid;
684 $options['inlineRelatedRecordToRender'] = $record;
685 $options['inlineRelatedRecordConfig'] = $config;
686 $options['inlineStructure'] = $this->inlineStackProcessor->getStructure();
687 $options['isAjaxContext'] = TRUE;
688
689 $options['renderType'] = 'inlineRecordContainer';
690 $childArray = $this->nodeFactory->create($options)->render();
691
692 if ($childArray === FALSE) {
693 return $this->getErrorMessageForAJAX('Access denied');
694 }
695
696 // @todo: Refactor this mess ... see other methods like getMainFields, too
697 $this->requiredElements = $childArray['requiredElements'];
698 $this->requiredFields = $childArray['requiredFields'];
699 $this->requiredAdditional = $childArray['requiredAdditional'];
700 $this->requiredNested = $childArray['requiredNested'];
701 $this->additionalJS_post = $childArray['additionalJavaScriptPost'];
702 $this->additionalJS_submit = $childArray['additionalJavaScriptSubmit'];
703 $this->extJSCODE = $childArray['extJSCODE'];
704 $this->inlineData = $childArray['inlineData'];
705 $this->hiddenFieldAccum = $childArray['additionalHiddenFields'];
706 $this->additionalCode_pre = $childArray['additionalHeadTags'];
707
708 $jsonArray = array(
709 'data' => $childArray['html'],
710 'scriptCall' => array(),
711 );
712
713 if (!$current['uid']) {
714 $jsonArray['scriptCall'][] = 'inline.domAddNewRecord(\'bottom\',' . GeneralUtility::quoteJSvalue($objectName . '_records') . ',' . GeneralUtility::quoteJSvalue($objectPrefix) . ',json.data);';
715 $jsonArray['scriptCall'][] = 'inline.memorizeAddRecord(' . GeneralUtility::quoteJSvalue($objectPrefix) . ',' . GeneralUtility::quoteJSvalue($record['uid']) . ',null,' . GeneralUtility::quoteJSvalue($foreignUid) . ');';
716 } else {
717 $jsonArray['scriptCall'][] = 'inline.domAddNewRecord(\'after\',' . GeneralUtility::quoteJSvalue($domObjectId . '_div') . ',' . GeneralUtility::quoteJSvalue($objectPrefix) . ',json.data);';
718 $jsonArray['scriptCall'][] = 'inline.memorizeAddRecord(' . GeneralUtility::quoteJSvalue($objectPrefix) . ',' . GeneralUtility::quoteJSvalue($record['uid']) . ',' . GeneralUtility::quoteJSvalue($current['uid']) . ',' . GeneralUtility::quoteJSvalue($foreignUid) . ');';
719 }
720
721 $jsonArray = $this->getInlineAjaxCommonScriptCalls($jsonArray, $config, $inlineFirstPid);
722
723 // Collapse all other records if requested:
724 if (!$collapseAll && $expandSingle) {
725 $jsonArray['scriptCall'][] = 'inline.collapseAllRecords(' . GeneralUtility::quoteJSvalue($objectId) . ', ' . GeneralUtility::quoteJSvalue($objectPrefix) . ', ' . GeneralUtility::quoteJSvalue($record['uid']) . ');';
726 }
727 // Tell the browser to scroll to the newly created record
728
729 $jsonArray['scriptCall'][] = 'Element.scrollTo(' . GeneralUtility::quoteJSvalue($objectId . '_div') . ');';
730 // Fade out and fade in the new record in the browser view to catch the user's eye
731 $jsonArray['scriptCall'][] = 'inline.fadeOutFadeIn(' . GeneralUtility::quoteJSvalue($objectId . '_div') . ');';
732
733 return $jsonArray;
734 }
735
736 /**
737 * Handle AJAX calls to localize all records of a parent, localize a single record or to synchronize with the original language parent.
738 *
739 * @param string $type Defines the type 'localize' or 'synchronize' (string) or a single uid to be localized (int)
740 * @param int $inlineFirstPid Inline first pid
741 * @return array An array to be used for JSON
742 */
743 protected function renderInlineSynchronizeLocalizeRecords($type, $inlineFirstPid) {
744 $jsonArray = FALSE;
745 if (GeneralUtility::inList('localize,synchronize', $type) || MathUtility::canBeInterpretedAsInteger($type)) {
746 $inlineRelatedRecordResolver = GeneralUtility::makeInstance(InlineRelatedRecordResolver::class);
747 // The parent level:
748 $parent = $this->inlineStackProcessor->getStructureLevel(-1);
749 $current = $this->inlineStackProcessor->getUnstableStructure();
750 $parentRecord = $inlineRelatedRecordResolver->getRecord($parent['table'], $parent['uid']);
751
752 $cmd = array();
753 $cmd[$parent['table']][$parent['uid']]['inlineLocalizeSynchronize'] = $parent['field'] . ',' . $type;
754 /** @var $tce \TYPO3\CMS\Core\DataHandling\DataHandler */
755 $tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
756 $tce->stripslashes_values = FALSE;
757 $tce->start(array(), $cmd);
758 $tce->process_cmdmap();
759
760 $oldItemList = $parentRecord[$parent['field']];
761 $newItemList = $tce->registerDBList[$parent['table']][$parent['uid']][$parent['field']];
762
763 $jsonArray = array(
764 'scriptCall' => array(),
765 );
766 $nameObject = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($inlineFirstPid);
767 $nameObjectForeignTable = $nameObject . '-' . $current['table'];
768 // Get the name of the field pointing to the original record:
769 $transOrigPointerField = $GLOBALS['TCA'][$current['table']]['ctrl']['transOrigPointerField'];
770 // Get the name of the field used as foreign selector (if any):
771 $foreignSelector = isset($parent['config']['foreign_selector']) && $parent['config']['foreign_selector'] ? $parent['config']['foreign_selector'] : FALSE;
772 // Convert lists to array with uids of child records:
773 $oldItems = FormEngineUtility::getInlineRelatedRecordsUidArray($oldItemList);
774 $newItems = FormEngineUtility::getInlineRelatedRecordsUidArray($newItemList);
775 // Determine the items that were localized or localized:
776 $removedItems = array_diff($oldItems, $newItems);
777 $localizedItems = array_diff($newItems, $oldItems);
778 // Set the items that should be removed in the forms view:
779 foreach ($removedItems as $item) {
780 $jsonArray['scriptCall'][] = 'inline.deleteRecord(' . GeneralUtility::quoteJSvalue($nameObjectForeignTable . '-' . $item) . ', {forceDirectRemoval: true});';
781 }
782 // Set the items that should be added in the forms view:
783 $html = '';
784 $resultArray = NULL;
785 // @todo: This should be another container ...
786 foreach ($localizedItems as $item) {
787 $row = $inlineRelatedRecordResolver->getRecord($current['table'], $item);
788 $selectedValue = $foreignSelector ? GeneralUtility::quoteJSvalue($row[$foreignSelector]) : 'null';
789
790 $options = $this->getConfigurationOptionsForChildElements();
791 $options['databaseRow'] = array('uid' => $parent['uid']);
792 $options['inlineFirstPid'] = $inlineFirstPid;
793 $options['inlineRelatedRecordToRender'] = $row;
794 $options['inlineRelatedRecordConfig'] = $parent['config'];
795 $options['inlineStructure'] = $this->inlineStackProcessor->getStructure();
796 $options['isAjaxContext'] = TRUE;
797
798 $options['renderType'] = 'inlineRecordContainer';
799 $childArray = $this->nodeFactory->create($options)->render();
800 $html .= $childArray['html'];
801 $childArray['html'] = '';
802
803 // @todo: Obsolete if a container and copied from AbstractContainer for now
804 if ($resultArray === NULL) {
805 $resultArray = $childArray;
806 } else {
807 if (!empty($childArray['extJSCODE'])) {
808 $resultArray['extJSCODE'] .= LF . $childArray['extJSCODE'];
809 }
810 foreach ($childArray['requiredElements'] as $name => $value) {
811 $resultArray['requiredElements'][$name] = $value;
812 }
813 foreach ($childArray['requiredFields'] as $value => $name) { // Params swapped ?!
814 $resultArray['requiredFields'][$value] = $name;
815 }
816 foreach ($childArray['requiredAdditional'] as $name => $subArray) {
817 $resultArray['requiredAdditional'][$name] = $subArray;
818 }
819 foreach ($childArray['requiredNested'] as $value => $name) {
820 $resultArray['requiredNested'][$value] = $name;
821 }
822 foreach ($childArray['additionalJavaScriptPost'] as $value) {
823 $resultArray['additionalJavaScriptPost'][] = $value;
824 }
825 foreach ($childArray['additionalJavaScriptSubmit'] as $value) {
826 $resultArray['additionalJavaScriptSubmit'][] = $value;
827 }
828 if (!empty($childArray['inlineData'])) {
829 $resultArrayInlineData = $resultArray['inlineData'];
830 $childInlineData = $childArray['inlineData'];
831 ArrayUtility::mergeRecursiveWithOverrule($resultArrayInlineData, $childInlineData);
832 $resultArray['inlineData'] = $resultArrayInlineData;
833 }
834 }
835
836 $jsonArray['scriptCall'][] = 'inline.memorizeAddRecord(' . GeneralUtility::quoteJSvalue($nameObjectForeignTable) . ', ' . GeneralUtility::quoteJSvalue($item) . ', null, ' . $selectedValue . ');';
837 // Remove possible virtual records in the form which showed that a child records could be localized:
838 if (isset($row[$transOrigPointerField]) && $row[$transOrigPointerField]) {
839 $jsonArray['scriptCall'][] = 'inline.fadeAndRemove(' . GeneralUtility::quoteJSvalue($nameObjectForeignTable . '-' . $row[$transOrigPointerField] . '_div') . ');';
840 }
841 }
842 if (!empty($html)) {
843 $jsonArray['data'] = $html;
844 array_unshift($jsonArray['scriptCall'], 'inline.domAddNewRecord(\'bottom\', ' . GeneralUtility::quoteJSvalue($nameObject . '_records') . ', ' . GeneralUtility::quoteJSvalue($nameObjectForeignTable) . ', json.data);');
845 }
846
847 // @todo: Refactor this mess ... see other methods like getMainFields, too
848 $this->requiredElements = $resultArray['requiredElements'];
849 $this->requiredFields = $resultArray['requiredFields'];
850 $this->requiredAdditional = $resultArray['requiredAdditional'];
851 $this->requiredNested = $resultArray['requiredNested'];
852 $this->additionalJS_post = $resultArray['additionalJavaScriptPost'];
853 $this->additionalJS_submit = $resultArray['additionalJavaScriptSubmit'];
854 $this->extJSCODE = $resultArray['extJSCODE'];
855 $this->inlineData = $resultArray['inlineData'];
856 $this->hiddenFieldAccum = $resultArray['additionalHiddenFields'];
857 $this->additionalCode_pre = $resultArray['additionalHeadTags'];
858
859 $jsonArray = $this->getInlineAjaxCommonScriptCalls($jsonArray, $parent['config'], $inlineFirstPid);
860 }
861 return $jsonArray;
862 }
863
864 /**
865 * Save the expanded/collapsed state of a child record in the BE_USER->uc.
866 *
867 * @param string $expand Whether this record is expanded.
868 * @param string $collapse Whether this record is collapsed.
869 * @return void
870 */
871 protected function setInlineExpandedCollapsedState($expand, $collapse) {
872 $backendUser = $this->getBackendUserAuthentication();
873 // The current table - for this table we should add/import records
874 $currentTable = $this->inlineStackProcessor->getUnstableStructure();
875 $currentTable = $currentTable['table'];
876 // The top parent table - this table embeds the current table
877 $top = $this->inlineStackProcessor->getStructureLevel(0);
878 $topTable = $top['table'];
879 $topUid = $top['uid'];
880 $inlineView = $this->getInlineExpandCollapseStateArray();
881 // Only do some action if the top record and the current record were saved before
882 if (MathUtility::canBeInterpretedAsInteger($topUid)) {
883 $expandUids = GeneralUtility::trimExplode(',', $expand);
884 $collapseUids = GeneralUtility::trimExplode(',', $collapse);
885 // Set records to be expanded
886 foreach ($expandUids as $uid) {
887 $inlineView[$topTable][$topUid][$currentTable][] = $uid;
888 }
889 // Set records to be collapsed
890 foreach ($collapseUids as $uid) {
891 $inlineView[$topTable][$topUid][$currentTable] = $this->removeFromArray($uid, $inlineView[$topTable][$topUid][$currentTable]);
892 }
893 // Save states back to database
894 if (is_array($inlineView[$topTable][$topUid][$currentTable])) {
895 $inlineView[$topTable][$topUid][$currentTable] = array_unique($inlineView[$topTable][$topUid][$currentTable]);
896 $backendUser->uc['inlineView'] = serialize($inlineView);
897 $backendUser->writeUC();
898 }
899 }
900 }
901
902 /**
903 * Construct runtime environment for Inline Relational Record Editing.
904 * - creates an anonymous \TYPO3\CMS\Backend\Controller\EditDocumentController in $GLOBALS['SOBE']
905 * - sets $this to $GLOBALS['SOBE']->tceforms
906 * -
907 * @return void
908 */
909 protected function setUpRuntimeEnvironmentForAjaxRequests() {
910 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_alt_doc.xlf');
911 // Create a new anonymous object:
912 $GLOBALS['SOBE'] = new \stdClass();
913 $GLOBALS['SOBE']->MOD_MENU = array(
914 'showPalettes' => '',
915 'showDescriptions' => '',
916 );
917 // Setting virtual document name
918 $GLOBALS['SOBE']->MCONF['name'] = 'xMOD_alt_doc.php';
919 // CLEANSE SETTINGS
920 $GLOBALS['SOBE']->MOD_SETTINGS = BackendUtility::getModuleData($GLOBALS['SOBE']->MOD_MENU, GeneralUtility::_GP('SET'), $GLOBALS['SOBE']->MCONF['name']);
921 // Create an instance of the document template object
922 // @todo: resolve clash getDocumentTemplate() / getControllerDocumenttemplate()
923 $GLOBALS['SOBE']->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
924 $GLOBALS['SOBE']->doc->backPath = $GLOBALS['BACK_PATH'];
925 // Initialize FormEngine (rendering the forms)
926 // @todo: check what of this part is still needed, simplify
927 $this->palettesCollapsed = !$GLOBALS['SOBE']->MOD_SETTINGS['showPalettes'];
928 $GLOBALS['SOBE']->tceforms = $this;
929 }
930
931 /**
932 * Return expand / collapse state array for a given table / uid combination
933 *
934 * @param string $table Handled table
935 * @param int $uid Handled uid
936 * @return array
937 */
938 protected function getInlineExpandCollapseStateArrayForTableUid($table, $uid) {
939 $inlineView = $this->getInlineExpandCollapseStateArray();
940 $result = array();
941 if (MathUtility::canBeInterpretedAsInteger($uid)) {
942 if (!empty($inlineView[$table][$uid])) {
943 $result = $inlineView[$table][$uid];
944 }
945 }
946 return $result;
947 }
948
949 /**
950 * Get expand / collapse state of inline items
951 *
952 * @return array
953 */
954 protected function getInlineExpandCollapseStateArray() {
955 $backendUser = $this->getBackendUserAuthentication();
956 $inlineView = unserialize($backendUser->uc['inlineView']);
957 if (!is_array($inlineView)) {
958 $inlineView = array();
959 }
960 return $inlineView;
961 }
962
963 /**
964 * The "entry" pid for inline records. Nested inline records can potentially hang around on different
965 * pid's, but the entry pid is needed for AJAX calls, so that they would know where the action takes place on the page structure.
966 *
967 * @return integer
968 */
969 protected function getInlineFirstPid() {
970 $table = $this->table;
971 $row = $this->databaseRow;
972 // If the parent is a page, use the uid(!) of the (new?) page as pid for the child records:
973 if ($table == 'pages') {
974 $liveVersionId = BackendUtility::getLiveVersionIdOfRecord('pages', $row['uid']);
975 $pid = is_null($liveVersionId) ? $row['uid'] : $liveVersionId;
976 } elseif ($row['pid'] < 0) {
977 $prevRec = BackendUtility::getRecord($table, abs($row['pid']));
978 $pid = $prevRec['pid'];
979 } else {
980 $pid = $row['pid'];
981 }
982 return $pid;
983 }
984
985 /**
986 * Checks if a record selector may select a certain file type
987 *
988 * @param array $selectorConfiguration
989 * @param array $fileRecord
990 * @return bool
991 */
992 protected function checkInlineFileTypeAccessForField(array $selectorConfiguration, array $fileRecord) {
993 if (!empty($selectorConfiguration['PA']['fieldConf']['config']['appearance']['elementBrowserAllowed'])) {
994 $allowedFileExtensions = GeneralUtility::trimExplode(
995 ',',
996 $selectorConfiguration['PA']['fieldConf']['config']['appearance']['elementBrowserAllowed'],
997 TRUE
998 );
999 if (!in_array(strtolower($fileRecord['extension']), $allowedFileExtensions, TRUE)) {
1000 return FALSE;
1001 }
1002 }
1003 return TRUE;
1004 }
1005
1006 /**
1007 * Remove an element from an array.
1008 *
1009 * @param mixed $needle The element to be removed.
1010 * @param array $haystack The array the element should be removed from.
1011 * @param mixed $strict Search elements strictly.
1012 * @return array The array $haystack without the $needle
1013 */
1014 protected function removeFromArray($needle, $haystack, $strict = NULL) {
1015 $pos = array_search($needle, $haystack, $strict);
1016 if ($pos !== FALSE) {
1017 unset($haystack[$pos]);
1018 }
1019 return $haystack;
1020 }
1021
1022 /**
1023 * Generates an error message that transferred as JSON for AJAX calls
1024 *
1025 * @param string $message The error message to be shown
1026 * @return array The error message in a JSON array
1027 */
1028 protected function getErrorMessageForAJAX($message) {
1029 $jsonArray = array(
1030 'data' => $message,
1031 'scriptCall' => array(
1032 'alert("' . $message . '");'
1033 )
1034 );
1035 return $jsonArray;
1036 }
1037
1038 /**
1039 * Determines and sets several script calls to a JSON array, that would have been executed if processed in non-AJAX mode.
1040 *
1041 * @param array &$jsonArray Reference of the array to be used for JSON
1042 * @param array $config The configuration of the IRRE field of the parent record
1043 * @param int $inlineFirstPid Inline first pid
1044 * @return array Modified array
1045 * @todo: Basically, this methods shouldn't be there at all ...
1046 */
1047 protected function getInlineAjaxCommonScriptCalls($jsonArray, $config, $inlineFirstPid) {
1048 // Add data that would have been added at the top of a regular FormEngine call:
1049 if ($headTags = $this->getInlineHeadTags()) {
1050 $jsonArray['headData'] = $headTags;
1051 }
1052 // Add the JavaScript data that would have been added at the bottom of a regular FormEngine call:
1053 $jsonArray['scriptCall'][] = $this->JSbottom('editform', TRUE);
1054 // If script.aculo.us Sortable is used, update the Observer to know the record:
1055 if ($config['appearance']['useSortable']) {
1056 $inlineObjectName = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($inlineFirstPid);
1057 $jsonArray['scriptCall'][] = 'inline.createDragAndDropSorting(' . GeneralUtility::quoteJSvalue($inlineObjectName . '_records') . ');';
1058 }
1059 // If FormEngine has some JavaScript code to be executed, just do it
1060 // @todo: this is done by JSBottom() already?!
1061 if ($this->extJSCODE) {
1062 $jsonArray['scriptCall'][] = $this->extJSCODE;
1063 }
1064 return $jsonArray;
1065 }
1066
1067 /**
1068 * Parses the HTML tags that would have been inserted to the <head> of a HTML document and returns the found tags as multidimensional array.
1069 *
1070 * @return array The parsed tags with their attributes and innerHTML parts
1071 * @todo: WTF?
1072 */
1073 protected function getInlineHeadTags() {
1074 $headTags = array();
1075 $headDataRaw = $this->JStop() . $this->getJavaScriptAndStyleSheetsOfPageRenderer();
1076 if ($headDataRaw) {
1077 // Create instance of the HTML parser:
1078 $parseObj = GeneralUtility::makeInstance(HtmlParser::class);
1079 // Removes script wraps:
1080 $headDataRaw = str_replace(array('/*<![CDATA[*/', '/*]]>*/'), '', $headDataRaw);
1081 // Removes leading spaces of a multi-line string:
1082 $headDataRaw = trim(preg_replace('/(^|\\r|\\n)( |\\t)+/', '$1', $headDataRaw));
1083 // Get script and link tags:
1084 $tags = array_merge($parseObj->getAllParts($parseObj->splitTags('link', $headDataRaw)), $parseObj->getAllParts($parseObj->splitIntoBlock('script', $headDataRaw)));
1085 foreach ($tags as $tagData) {
1086 $tagAttributes = $parseObj->get_tag_attributes($parseObj->getFirstTag($tagData), TRUE);
1087 $headTags[] = array(
1088 'name' => $parseObj->getFirstTagName($tagData),
1089 'attributes' => $tagAttributes[0],
1090 'innerHTML' => $parseObj->removeFirstAndLastTag($tagData)
1091 );
1092 }
1093 }
1094 return $headTags;
1095 }
1096
1097 /**
1098 * Gets the JavaScript of the pageRenderer.
1099 * This can be used to extract newly added files which have been added
1100 * during an AJAX request. Due to the spread possibilities of the pageRenderer
1101 * to add JavaScript rendering and extracting seems to be the easiest way.
1102 *
1103 * @return string
1104 * @todo: aaaargs ...
1105 */
1106 protected function getJavaScriptAndStyleSheetsOfPageRenderer() {
1107 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
1108 $pageRenderer = clone $GLOBALS['SOBE']->doc->getPageRenderer();
1109 $pageRenderer->setCharSet($this->getLanguageService()->charSet);
1110 $pageRenderer->setTemplateFile('EXT:backend/Resources/Private/Templates/helper_javascript_css.html');
1111 $javaScriptAndStyleSheets = $pageRenderer->render();
1112 return $javaScriptAndStyleSheets;
1113 }
1114
1115 /**
1116 * Returns the "returnUrl" of the form. Can be set externally or will be taken from "GeneralUtility::linkThisScript()"
1117 *
1118 * @return string Return URL of current script
1119 */
1120 protected function thisReturnUrl() {
1121 return $this->returnUrl ? $this->returnUrl : GeneralUtility::linkThisScript();
1122 }
1123
1124 /********************************************
1125 *
1126 * Template functions
1127 *
1128 ********************************************/
1129 /**
1130 * Wraps all the table rows into a single table.
1131 * Used externally from scripts like EditDocumentController and PageLayoutController (which uses FormEngine)
1132 *
1133 * @param string $c Code to output between table-parts; table rows
1134 * @param array $rec The record
1135 * @param string $table The table name
1136 * @return string
1137 */
1138 public function wrapTotal($c, $rec, $table) {
1139 $parts = $this->replaceTableWrap(explode('|', $this->totalWrap, 2), $rec, $table);
1140 return $parts[0] . $c . $parts[1] . implode(LF, $this->hiddenFieldAccum);
1141 }
1142
1143 /**
1144 * Generates a token and returns an input field with it
1145 *
1146 * @param string $formName Context of the token
1147 * @param string $tokenName The name of the token GET/POST variable
1148 * @return string A complete input field
1149 */
1150 static public function getHiddenTokenField($formName = 'securityToken', $tokenName = 'formToken') {
1151 $formprotection = FormProtectionFactory::get();
1152 return '<input type="hidden" name="' . $tokenName . '" value="' . $formprotection->generateToken($formName) . '" />';
1153 }
1154
1155 /**
1156 * This replaces markers in the total wrap
1157 *
1158 * @param array $arr An array of template parts containing some markers.
1159 * @param array $rec The record
1160 * @param string $table The table name
1161 * @return string
1162 */
1163 public function replaceTableWrap($arr, $rec, $table) {
1164 $icon = IconUtility::getSpriteIconForRecord($table, $rec, array('title' => $this->getRecordPath($table, $rec)));
1165 // Make "new"-label
1166 $languageService = $this->getLanguageService();
1167 if (strstr($rec['uid'], 'NEW')) {
1168 $newLabel = ' <span class="typo3-TCEforms-newToken">' . $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.new', TRUE) . '</span>';
1169 // BackendUtility::fixVersioningPid Should not be used here because NEW records are not offline workspace versions...
1170 $truePid = BackendUtility::getTSconfig_pidValue($table, $rec['uid'], $rec['pid']);
1171 $prec = BackendUtility::getRecordWSOL('pages', $truePid, 'title');
1172 $pageTitle = BackendUtility::getRecordTitle('pages', $prec, TRUE, FALSE);
1173 $rLabel = '<em>[PID: ' . $truePid . '] ' . $pageTitle . '</em>';
1174 // Fetch translated title of the table
1175 $tableTitle = $languageService->sL($GLOBALS['TCA'][$table]['ctrl']['title']);
1176 if ($table === 'pages') {
1177 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.createNewPage', TRUE);
1178 $pageTitle = sprintf($label, $tableTitle);
1179 } else {
1180 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.createNewRecord', TRUE);
1181 if ($rec['pid'] == 0) {
1182 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.createNewRecordRootLevel', TRUE);
1183 }
1184 $pageTitle = sprintf($label, $tableTitle, $pageTitle);
1185 }
1186 } else {
1187 $newLabel = ' <span class="typo3-TCEforms-recUid">[' . $rec['uid'] . ']</span>';
1188 $rLabel = BackendUtility::getRecordTitle($table, $rec, TRUE, FALSE);
1189 $prec = BackendUtility::getRecordWSOL('pages', $rec['pid'], 'uid,title');
1190 // Fetch translated title of the table
1191 $tableTitle = $languageService->sL($GLOBALS['TCA'][$table]['ctrl']['title']);
1192 if ($table === 'pages') {
1193 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.editPage', TRUE);
1194 // Just take the record title and prepend an edit label.
1195 $pageTitle = sprintf($label, $tableTitle, $rLabel);
1196 } else {
1197 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.editRecord', TRUE);
1198 $pageTitle = BackendUtility::getRecordTitle('pages', $prec, TRUE, FALSE);
1199 if ($rLabel === BackendUtility::getNoRecordTitle(TRUE)) {
1200 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.editRecordNoTitle', TRUE);
1201 }
1202 if ($rec['pid'] == 0) {
1203 $label = $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.editRecordRootLevel', TRUE);
1204 }
1205 if ($rLabel !== BackendUtility::getNoRecordTitle(TRUE)) {
1206 // Just take the record title and prepend an edit label.
1207 $pageTitle = sprintf($label, $tableTitle, $rLabel, $pageTitle);
1208 } else {
1209 // Leave out the record title since it is not set.
1210 $pageTitle = sprintf($label, $tableTitle, $pageTitle);
1211 }
1212 }
1213 $icon = $this->getControllerDocumentTemplate()->wrapClickMenuOnIcon($icon, $table, $rec['uid'], 1, '', '+copy,info,edit,view');
1214 }
1215 foreach ($arr as $k => $v) {
1216 // Make substitutions:
1217 $arr[$k] = str_replace(
1218 array(
1219 '###PAGE_TITLE###',
1220 '###ID_NEW_INDICATOR###',
1221 '###RECORD_LABEL###',
1222 '###TABLE_TITLE###',
1223 '###RECORD_ICON###'
1224 ),
1225 array(
1226 $pageTitle,
1227 $newLabel,
1228 $rLabel,
1229 htmlspecialchars($languageService->sL($GLOBALS['TCA'][$table]['ctrl']['title'])),
1230 $icon
1231 ),
1232 $arr[$k]
1233 );
1234 }
1235 return $arr;
1236 }
1237
1238
1239 /********************************************
1240 *
1241 * JavaScript related functions
1242 *
1243 ********************************************/
1244 /**
1245 * JavaScript code added BEFORE the form is drawn:
1246 *
1247 * @return string A <script></script> section with JavaScript.
1248 */
1249 public function JStop() {
1250 $out = '';
1251 // Additional top HTML:
1252 if (count($this->additionalCode_pre)) {
1253 $out .= implode('
1254
1255 <!-- NEXT: -->
1256 ', $this->additionalCode_pre);
1257 }
1258 return $out;
1259 }
1260
1261 /**
1262 * JavaScript bottom code
1263 *
1264 * @param string $formname The identification of the form on the page.
1265 * @param bool $update Just extend/update existing settings, e.g. for AJAX call
1266 * @return string A section with JavaScript - if $update is FALSE, embedded in <script></script>
1267 */
1268 public function JSbottom($formname = 'forms[0]', $update = FALSE) {
1269 $languageService = $this->getLanguageService();
1270 $jsFile = array();
1271 $elements = array();
1272 $out = '';
1273 // Required:
1274 foreach ($this->requiredFields as $itemImgName => $itemName) {
1275 $match = array();
1276 if (preg_match('/^(.+)\\[((\\w|\\d|_)+)\\]$/', $itemName, $match)) {
1277 $record = $match[1];
1278 $field = $match[2];
1279 $elements[$record][$field]['required'] = 1;
1280 $elements[$record][$field]['requiredImg'] = $itemImgName;
1281 if (isset($this->requiredAdditional[$itemName]) && is_array($this->requiredAdditional[$itemName])) {
1282 $elements[$record][$field]['additional'] = $this->requiredAdditional[$itemName];
1283 }
1284 }
1285 }
1286 // Range:
1287 foreach ($this->requiredElements as $itemName => $range) {
1288 if (preg_match('/^(.+)\\[((\\w|\\d|_)+)\\]$/', $itemName, $match)) {
1289 $record = $match[1];
1290 $field = $match[2];
1291 $elements[$record][$field]['range'] = array($range[0], $range[1]);
1292 $elements[$record][$field]['rangeImg'] = $range['imgName'];
1293 }
1294 }
1295 $this->TBE_EDITOR_fieldChanged_func = 'TBE_EDITOR.fieldChanged_fName(fName,formObj[fName+"_list"]);';
1296 if (!$update) {
1297 if ($this->loadMD5_JS) {
1298 $this->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/md5.js');
1299 }
1300 $pageRenderer = $this->getPageRenderer();
1301 // load the main module for FormEngine with all important JS functions
1302 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FormEngine', 'function(FormEngine) {
1303 FormEngine.setBrowserUrl(' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('browser')) . ');
1304 }');
1305 $pageRenderer->loadPrototype();
1306 $pageRenderer->loadJquery();
1307 $pageRenderer->loadExtJS();
1308 $beUserAuth = $this->getBackendUserAuthentication();
1309 // Make textareas resizable and flexible ("autogrow" in height)
1310 $textareaSettings = array(
1311 'autosize' => (bool)$beUserAuth->uc['resizeTextareas_Flexible']
1312 );
1313 $pageRenderer->addInlineSettingArray('Textarea', $textareaSettings);
1314
1315 $this->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/jsfunc.evalfield.js');
1316 $this->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/jsfunc.tbe_editor.js');
1317 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ValueSlider');
1318 // Needed for FormEngine manipulation (date picker)
1319 $dateFormat = ($GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? array('MM-DD-YYYY', 'HH:mm MM-DD-YYYY') : array('DD-MM-YYYY', 'HH:mm DD-MM-YYYY'));
1320 $pageRenderer->addInlineSetting('DateTimePicker', 'DateFormat', $dateFormat);
1321
1322 // support placeholders for IE9 and lower
1323 $clientInfo = GeneralUtility::clientInfo();
1324 if ($clientInfo['BROWSER'] == 'msie' && $clientInfo['VERSION'] <= 9) {
1325 $this->loadJavascriptLib('sysext/core/Resources/Public/JavaScript/Contrib/placeholders.jquery.min.js');
1326 }
1327
1328 // @todo: remove scriptaclous once suggest & flex form foo is moved to RequireJS, see #55575
1329 $pageRenderer->loadScriptaculous();
1330 $this->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/tceforms.js');
1331 $this->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/jsfunc.tceforms_suggest.js');
1332
1333 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FormEngineFlexForm');
1334 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileListLocalisation');
1335 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DragUploader');
1336
1337 $pageRenderer->addInlineLanguagelabelFile(
1338 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
1339 'file_upload'
1340 );
1341
1342 // We want to load jQuery-ui inside our js. Enable this using requirejs.
1343 $this->loadJavascriptLib('sysext/backend/Resources/Public/JavaScript/jsfunc.inline.js');
1344 $out .= '
1345 inline.setNoTitleString("' . addslashes(BackendUtility::getNoRecordTitle(TRUE)) . '");
1346 ';
1347
1348 $out .= '
1349 TBE_EDITOR.images.req.src = "' . IconUtility::skinImg('', 'gfx/required_h.gif', '', 1) . '";
1350 TBE_EDITOR.images.clear.src = "clear.gif";
1351
1352 TBE_EDITOR.formname = "' . $formname . '";
1353 TBE_EDITOR.formnameUENC = "' . rawurlencode($formname) . '";
1354 TBE_EDITOR.backPath = "";
1355 TBE_EDITOR.isPalettedoc = null;
1356 TBE_EDITOR.doSaveFieldName = "' . ($this->doSaveFieldName ? addslashes($this->doSaveFieldName) : '') . '";
1357 TBE_EDITOR.labels.fieldsChanged = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.fieldsChanged')) . ';
1358 TBE_EDITOR.labels.fieldsMissing = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.fieldsMissing')) . ';
1359 TBE_EDITOR.labels.maxItemsAllowed = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.maxItemsAllowed')) . ';
1360 TBE_EDITOR.labels.refresh_login = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:mess.refresh_login')) . ';
1361 TBE_EDITOR.labels.onChangeAlert = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:mess.onChangeAlert')) . ';
1362 TBE_EDITOR.labels.remainingCharacters = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.remainingCharacters')) . ';
1363 evalFunc.USmode = ' . ($GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? '1' : '0') . ';
1364
1365 TBE_EDITOR.customEvalFunctions = {};
1366
1367 ';
1368 }
1369 // Add JS required for inline fields
1370 if (!empty($this->inlineData)) {
1371 $out .= '
1372 inline.addToDataArray(' . json_encode($this->inlineData) . ');
1373 ';
1374 }
1375 // Registered nested elements for tabs or inline levels:
1376 if (count($this->requiredNested)) {
1377 $out .= '
1378 TBE_EDITOR.addNested(' . json_encode($this->requiredNested) . ');
1379 ';
1380 }
1381 // Elements which are required or have a range definition:
1382 if (count($elements)) {
1383 $out .= '
1384 TBE_EDITOR.addElements(' . json_encode($elements) . ');
1385 TBE_EDITOR.initRequired();
1386 ';
1387 }
1388 // $this->additionalJS_submit:
1389 if ($this->additionalJS_submit) {
1390 $additionalJS_submit = implode('', $this->additionalJS_submit);
1391 $additionalJS_submit = str_replace(array(CR, LF), '', $additionalJS_submit);
1392 $out .= '
1393 TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJS_submit) . '");
1394 ';
1395 }
1396 $out .= LF . implode(LF, $this->additionalJS_post) . LF . $this->extJSCODE;
1397 // Regular direct output:
1398 if (!$update) {
1399 $spacer = LF . TAB;
1400 $out = $spacer . implode($spacer, $jsFile) . GeneralUtility::wrapJS($out);
1401 }
1402 return $out;
1403 }
1404
1405 /**
1406 * Prints necessary JavaScript for TCEforms (after the form HTML).
1407 * currently this is used to transform page-specific options in the TYPO3.Settings array for JS
1408 * so the JS module can access these values
1409 *
1410 * @return string
1411 */
1412 public function printNeededJSFunctions() {
1413 /** @var $pageRenderer \TYPO3\CMS\Core\Page\PageRenderer */
1414 $pageRenderer = $this->getControllerDocumentTemplate()->getPageRenderer();
1415
1416 // set variables to be accessible for JS
1417 $pageRenderer->addInlineSetting('FormEngine', 'formName', 'editform');
1418 $pageRenderer->addInlineSetting('FormEngine', 'backPath', '');
1419
1420 // Integrate JS functions for the element browser if such fields or IRRE fields were processed
1421 $pageRenderer->addInlineSetting('FormEngine', 'legacyFieldChangedCb', 'function() { ' . $this->TBE_EDITOR_fieldChanged_func . ' };');
1422
1423 return $this->JSbottom('editform');
1424 }
1425
1426 /**
1427 * Returns necessary JavaScript for the top
1428 *
1429 * @return string
1430 */
1431 public function printNeededJSFunctions_top() {
1432 return $this->JStop('editform');
1433 }
1434
1435 /**
1436 * Includes a javascript library that exists in the core /typo3/ directory. The
1437 * backpath is automatically applied.
1438 * This method acts as wrapper for $GLOBALS['SOBE']->doc->loadJavascriptLib($lib).
1439 *
1440 * @param string $lib Library name. Call it with the full path like "contrib/prototype/prototype.js" to load it
1441 * @return void
1442 */
1443 public function loadJavascriptLib($lib) {
1444 $this->getControllerDocumentTemplate()->loadJavascriptLib($lib);
1445 }
1446
1447 /********************************************
1448 *
1449 * Various helper functions
1450 *
1451 ********************************************/
1452
1453 /**
1454 * Return record path (visually formatted, using BackendUtility::getRecordPath() )
1455 *
1456 * @param string $table Table name
1457 * @param array $rec Record array
1458 * @return string The record path.
1459 * @see BackendUtility::getRecordPath()
1460 */
1461 public function getRecordPath($table, $rec) {
1462 BackendUtility::fixVersioningPid($table, $rec);
1463 list($tscPID, $thePidValue) = BackendUtility::getTSCpidCached($table, $rec['uid'], $rec['pid']);
1464 if ($thePidValue >= 0) {
1465 return BackendUtility::getRecordPath($tscPID, $this->readPerms(), 15);
1466 }
1467 return '';
1468 }
1469
1470 /**
1471 * Returns the select-page read-access SQL clause.
1472 * Returns cached string, so you can call this function as much as you like without performance loss.
1473 *
1474 * @return string
1475 */
1476 public function readPerms() {
1477 if (!$this->perms_clause_set) {
1478 $this->perms_clause = $this->getBackendUserAuthentication()->getPagePermsClause(1);
1479 $this->perms_clause_set = TRUE;
1480 }
1481 return $this->perms_clause;
1482 }
1483
1484 /**
1485 * Returns TRUE if descriptions should be loaded always
1486 *
1487 * @param string $table Table for which to check
1488 * @return bool
1489 */
1490 public function doLoadTableDescr($table) {
1491 return $GLOBALS['TCA'][$table]['interface']['always_description'];
1492 }
1493
1494 /**
1495 * Initialize list of additional preview languages.
1496 * Sets according list in $this->additionalPreviewLanguages
1497 *
1498 * @return void
1499 */
1500 protected function initializeAdditionalPreviewLanguages() {
1501 $backendUserAuthentication = $this->getBackendUserAuthentication();
1502 $additionalPreviewLanguageListOfUser = $backendUserAuthentication->getTSConfigVal('options.additionalPreviewLanguages');
1503 $additionalPreviewLanguages = array();
1504 if ($additionalPreviewLanguageListOfUser) {
1505 $uids = GeneralUtility::intExplode(',', $additionalPreviewLanguageListOfUser);
1506 foreach ($uids as $uid) {
1507 if ($sys_language_rec = BackendUtility::getRecord('sys_language', $uid)) {
1508 $additionalPreviewLanguages[$uid]['uid'] = $uid;
1509 if (!empty($sys_language_rec['language_isocode'])) {
1510 $additionalPreviewLanguages[$uid]['ISOcode'] = $sys_language_rec['language_isocode'];
1511 } elseif ($sys_language_rec['static_lang_isocode'] && ExtensionManagementUtility::isLoaded('static_info_tables')) {
1512 GeneralUtility::deprecationLog('Usage of the field "static_lang_isocode" is discouraged, and will stop working with CMS 8. Use the built-in language field "language_isocode" in your sys_language records.');
1513 $staticLangRow = BackendUtility::getRecord('static_languages', $sys_language_rec['static_lang_isocode'], 'lg_iso_2');
1514 if ($staticLangRow['lg_iso_2']) {
1515 $additionalPreviewLanguages[$uid]['ISOcode'] = $staticLangRow['lg_iso_2'];
1516 }
1517 }
1518 }
1519 }
1520 }
1521 $this->additionalPreviewLanguages = $additionalPreviewLanguages;
1522 }
1523
1524 /**
1525 * @return BackendUserAuthentication
1526 */
1527 protected function getBackendUserAuthentication() {
1528 return $GLOBALS['BE_USER'];
1529 }
1530
1531 /**
1532 * @return DocumentTemplate
1533 */
1534 protected function getControllerDocumentTemplate() {
1535 // $GLOBALS['SOBE'] might be any kind of PHP class (controller most of the times)
1536 // These class do not inherit from any common class, but they all seem to have a "doc" member
1537 return $GLOBALS['SOBE']->doc;
1538 }
1539
1540 /**
1541 * @return LanguageService
1542 */
1543 protected function getLanguageService() {
1544 return $GLOBALS['LANG'];
1545 }
1546
1547 /**
1548 * Wrapper for access to the current page renderer object
1549 *
1550 * @return \TYPO3\CMS\Core\Page\PageRenderer
1551 */
1552 protected function getPageRenderer() {
1553 return $this->getControllerDocumentTemplate()->getPageRenderer();
1554 }
1555
1556 }