9a78c50b369636a2e0bff1bad51e75de4d165fed
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Container / TabsContainer.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Container;
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\Lang\LanguageService;
18 use TYPO3\CMS\Backend\Template\DocumentTemplate;
19
20 /**
21 * Render all tabs of a record that has tabs.
22 *
23 * This container is called from FullRecordContainer and resolves the --div-- structure,
24 * operates on given fieldArrays and calls a PaletteAndSingleContainer for each single tab.
25 */
26 class TabsContainer extends AbstractContainer
27 {
28 /**
29 * Entry method
30 *
31 * @return array As defined in initializeResultArray() of AbstractNode
32 * @throws \RuntimeException
33 */
34 public function render()
35 {
36 $languageService = $this->getLanguageService();
37 $docTemplate = $this->getDocumentTemplate();
38
39 // All the fields to handle in a flat list
40 $fieldsArray = $this->data['fieldsArray'];
41
42 // Create a nested array from flat fieldArray list
43 $tabsArray = array();
44 // First element will be a --div--, so it is safe to start -1 here to trigger 0 as first array index
45 $currentTabIndex = -1;
46 foreach ($fieldsArray as $fieldString) {
47 $fieldArray = $this->explodeSingleFieldShowItemConfiguration($fieldString);
48 if ($fieldArray['fieldName'] === '--div--') {
49 $currentTabIndex++;
50 if (empty($fieldArray['fieldLabel'])) {
51 throw new \RuntimeException(
52 'A --div-- has no label (--div--;fieldLabel) in showitem of ' . implode(',', $fieldsArray),
53 1426454001
54 );
55 }
56 $tabsArray[$currentTabIndex] = array(
57 'label' => $languageService->sL($fieldArray['fieldLabel']),
58 'elements' => array(),
59 );
60 } else {
61 $tabsArray[$currentTabIndex]['elements'][] = $fieldArray;
62 }
63 }
64
65 // Iterate over the tabs and compile content in $tabsContent array together with label
66 $tabsContent = array();
67 $resultArray = $this->initializeResultArray();
68
69 $tabId = 'TCEforms:' . $this->data['tableName'] . ':' . $this->data['databaseRow']['uid'];
70 // @todo: This duplicates parts of the docTemplate code
71 $tabIdString = $docTemplate->getDynTabMenuId($tabId);
72
73 $tabCounter = 0;
74 foreach ($tabsArray as $tabWithLabelAndElements) {
75 $tabCounter ++;
76 $elements = $tabWithLabelAndElements['elements'];
77
78 // Merge elements of this tab into a single list again and hand over to
79 // palette and single field container to render this group
80 $options = $this->data;
81 $options['tabAndInlineStack'][] = array(
82 'tab',
83 $tabIdString . '-' . $tabCounter,
84 );
85 $options['fieldsArray'] = array();
86 foreach ($elements as $element) {
87 $options['fieldsArray'][] = implode(';', $element);
88 }
89 $options['renderType'] = 'paletteAndSingleContainer';
90 $childArray = $this->nodeFactory->create($options)->render();
91
92 $tabsContent[] = array(
93 'label' => $tabWithLabelAndElements['label'],
94 'content' => $childArray['html'],
95 );
96 $childArray['html'] = '';
97 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $childArray);
98 }
99
100 // Feed everything to document template for tab rendering
101 $resultArray['html'] = $docTemplate->getDynamicTabMenu($tabsContent, $tabId, 1, false, false);
102 return $resultArray;
103 }
104
105 /**
106 * @return LanguageService
107 */
108 protected function getLanguageService()
109 {
110 return $GLOBALS['LANG'];
111 }
112
113 /**
114 * @throws \RuntimeException
115 * @return DocumentTemplate
116 */
117 protected function getDocumentTemplate()
118 {
119 $docTemplate = $GLOBALS['TBE_TEMPLATE'];
120 if (!is_object($docTemplate)) {
121 throw new \RuntimeException('No instance of DocumentTemplate found', 1426459735);
122 }
123 return $docTemplate;
124 }
125 }