ece63ceaf49a7e055f1f92fec2ff9f95223b1536
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / BackendLayoutWizardController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller;
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\Template\DocumentTemplate;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\Utility\IconUtility;
20 use TYPO3\CMS\Core\Page\PageRenderer;
21 use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
24
25
26 /**
27 * Script Class for grid wizard
28 */
29 class BackendLayoutWizardController {
30
31 // GET vars:
32 // Wizard parameters, coming from TCEforms linking to the wizard.
33 /**
34 * @var array
35 */
36 public $P;
37
38 /**
39 * Document template object
40 *
41 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
42 */
43 public $doc;
44
45 // Accumulated content.
46 /**
47 * @var string
48 */
49 public $content;
50
51 /**
52 * @var string
53 */
54 public $formName;
55
56 /**
57 * @var string
58 */
59 public $fieldName;
60
61 /**
62 * Initialises the Class
63 *
64 * @return void
65 * @throws \InvalidArgumentException
66 */
67 public function init() {
68 $lang = $this->getLanguageService();
69 $lang->includeLLFile('EXT:lang/locallang_wizards.xlf');
70
71 // Setting GET vars (used in frameset script):
72 $this->P = GeneralUtility::_GP('P');
73 $this->formName = $this->P['formName'];
74 $this->fieldName = $this->P['itemName'];
75 $hmac_validate = GeneralUtility::hmac($this->formName . $this->fieldName, 'wizard_js');
76 if (!$this->P['hmac'] || ($this->P['hmac'] !== $hmac_validate)) {
77 throw new \InvalidArgumentException('Hmac Validation failed for backend_layout wizard', 1385811397);
78 }
79 $uid = (int)$this->P['uid'];
80 // Initialize document object:
81 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
82 $this->doc->backPath = $GLOBALS['BACK_PATH'];
83 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
84 $pageRenderer->loadExtJS();
85 $pageRenderer->addJsFile(ExtensionManagementUtility::extRelPath('backend') . 'Resources/Public/JavaScript/grideditor.js');
86 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip');
87 $pageRenderer->addInlineSetting('ContextHelp', 'moduleUrl', BackendUtility::getModuleUrl('help_cshmanual'));
88 $pageRenderer->addJsInlineCode('storeData', '
89 function storeData(data) {
90 if (parent.opener && parent.opener.document && parent.opener.document.' . $this->formName . ' && parent.opener.document.' . $this->formName . '[' . GeneralUtility::quoteJSvalue($this->fieldName) . ']) {
91 parent.opener.document.' . $this->formName . '[' . GeneralUtility::quoteJSvalue($this->fieldName) . '].value = data;
92 parent.opener.TBE_EDITOR.fieldChanged("backend_layout","' . $uid . '","config","data[backend_layout][' . $uid . '][config]");
93 }
94 }
95 ', FALSE);
96 $languageLabels = array(
97 'save' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_labelSave', TRUE),
98 'title' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_windowTitle', TRUE),
99 'editCell' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_editCell', TRUE),
100 'mergeCell' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_mergeCell', TRUE),
101 'splitCell' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_splitCell', TRUE),
102 'name' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_name', TRUE),
103 'column' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_column', TRUE),
104 'notSet' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_notSet', TRUE),
105 'nameHelp' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_nameHelp', TRUE),
106 'columnHelp' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_columnHelp', TRUE)
107 );
108 $pageRenderer->addInlineLanguageLabelArray($languageLabels);
109 // Select record
110 $record = $this->getDatabaseConnection()->exec_SELECTgetRows($this->P['field'], $this->P['table'], 'uid=' . (int)$this->P['uid']);
111 if (trim($record[0][$this->P['field']]) == '') {
112 $rows = array(array(array('colspan' => 1, 'rowspan' => 1, 'spanned' => FALSE, 'name' => '')));
113 $colCount = 1;
114 $rowCount = 1;
115 } else {
116 // load TS parser
117 $parser = GeneralUtility::makeInstance(TypoScriptParser::class);
118 $parser->parse($record[0][$this->P['field']]);
119 $data = $parser->setup['backend_layout.'];
120 $rows = array();
121 $colCount = $data['colCount'];
122 $rowCount = $data['rowCount'];
123 $dataRows = $data['rows.'];
124 $spannedMatrix = array();
125 for ($i = 1; $i <= $rowCount; $i++) {
126 $cells = array();
127 $row = array_shift($dataRows);
128 $columns = $row['columns.'];
129 for ($j = 1; $j <= $colCount; $j++) {
130 $cellData = array();
131 if (!$spannedMatrix[$i][$j]) {
132 if (is_array($columns) && !empty($columns)) {
133 $column = array_shift($columns);
134 if (isset($column['colspan'])) {
135 $cellData['colspan'] = (int)$column['colspan'];
136 $columnColSpan = (int)$column['colspan'];
137 if (isset($column['rowspan'])) {
138 $columnRowSpan = (int)$column['rowspan'];
139 for ($spanRow = 0; $spanRow < $columnRowSpan; $spanRow++) {
140 for ($spanColumn = 0; $spanColumn < $columnColSpan; $spanColumn++) {
141 $spannedMatrix[$i + $spanRow][$j + $spanColumn] = 1;
142 }
143 }
144 } else {
145 for ($spanColumn = 0; $spanColumn < $columnColSpan; $spanColumn++) {
146 $spannedMatrix[$i][$j + $spanColumn] = 1;
147 }
148 }
149 } else {
150 $cellData['colspan'] = 1;
151 if (isset($column['rowspan'])) {
152 $columnRowSpan = (int)$column['rowspan'];
153 for ($spanRow = 0; $spanRow < $columnRowSpan; $spanRow++) {
154 $spannedMatrix[$i + $spanRow][$j] = 1;
155 }
156 }
157 }
158 if (isset($column['rowspan'])) {
159 $cellData['rowspan'] = (int)$column['rowspan'];
160 } else {
161 $cellData['rowspan'] = 1;
162 }
163 if (isset($column['name'])) {
164 $cellData['name'] = $column['name'];
165 }
166 if (isset($column['colPos'])) {
167 $cellData['column'] = (int)$column['colPos'];
168 }
169 }
170 } else {
171 $cellData = array('colspan' => 1, 'rowspan' => 1, 'spanned' => 1);
172 }
173 $cells[] = $cellData;
174 }
175 $rows[] = $cells;
176 if (!empty($spannedMatrix[$i]) && is_array($spannedMatrix[$i])) {
177 ksort($spannedMatrix[$i]);
178 }
179 }
180 }
181 $pageRenderer->addExtOnReadyCode('
182 t3Grid = new TYPO3.Backend.t3Grid({
183 data: ' . json_encode($rows, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS) . ',
184 colCount: ' . (int)$colCount . ',
185 rowCount: ' . (int)$rowCount . ',
186 targetElement: \'editor\'
187 });
188 t3Grid.drawTable();
189 ');
190 $this->doc->styleSheetFile_post = ExtensionManagementUtility::extRelPath('backend') . 'Resources/Public/Css/grideditor.css';
191 }
192
193 /**
194 * Main Method, rendering either colorpicker or frameset depending on ->showPicker
195 *
196 * @return void
197 */
198 public function main() {
199 $lang = $this->getLanguageService();
200 $resourcePath = ExtensionManagementUtility::extRelPath('backend') . 'Resources/Public/Images/BackendLayoutWizard/';
201 $content = '<a href="#" title="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc', TRUE) . '" onclick="storeData(t3Grid.export2LayoutRecord());return true;">' . IconUtility::getSpriteIcon('actions-document-save') . '</a>';
202 $content .= '<a href="#" title="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveCloseDoc', TRUE) . '" onclick="storeData(t3Grid.export2LayoutRecord());window.close();return true;">' . IconUtility::getSpriteIcon('actions-document-save-close') . '</a>';
203 $content .= '<a href="#" title="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', TRUE) . '" onclick="window.close();return true;">' . IconUtility::getSpriteIcon('actions-document-close') . '</a>';
204 $content .= $this->doc->spacer(10);
205 $content .= '
206 <table border="0" width="90%" height="90%" id="outer_container">
207 <tr>
208 <td class="editor_cell">
209 <div id="editor">
210 </div>
211 </td>
212 <td width="20" valign="center">
213 <a class="addCol" href="#" title="' . $lang->getLL('grid_addColumn') . '" onclick="t3Grid.addColumn(); t3Grid.drawTable(\'editor\');">
214 <img src="' . $resourcePath . 't3grid-tableright.png" border="0" />
215 </a><br />
216 <a class="removeCol" href="#" title="' . $lang->getLL('grid_removeColumn') . '" onclick="t3Grid.removeColumn(); t3Grid.drawTable(\'editor\');">
217 <img src="' . $resourcePath . 't3grid-tableleft.png" border="0" />
218 </a>
219 </td>
220 </tr>
221 <tr>
222 <td colspan="2" height="20" align="center">
223 <a class="addCol" href="#" title="' . $lang->getLL('grid_addRow') . '" onclick="t3Grid.addRow(); t3Grid.drawTable(\'editor\');">
224 <img src="' . $resourcePath . 't3grid-tabledown.png" border="0" />
225 </a>
226 <a class="removeCol" href="#" title="' . $lang->getLL('grid_removeRow') . '" onclick="t3Grid.removeRow(); t3Grid.drawTable(\'editor\');">
227 <img src="' . $resourcePath . 't3grid-tableup.png" border="0" />
228 </a>
229 </td>
230 </tr>
231 </table>
232 ';
233 $this->content = $content;
234 }
235
236 /**
237 * Returns the sourcecode to the browser
238 *
239 * @return void
240 */
241 public function printContent() {
242 echo $this->doc->render('Grid wizard', $this->content);
243 }
244
245 /**
246 * Returns LanguageService
247 *
248 * @return \TYPO3\CMS\Lang\LanguageService
249 */
250 protected function getLanguageService() {
251 return $GLOBALS['LANG'];
252 }
253
254 /**
255 * Returns the database connection
256 *
257 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
258 */
259 protected function getDatabaseConnection() {
260 return $GLOBALS['TYPO3_DB'];
261 }
262
263 }