[TASK] BACK_PATH DocumentTemplate
[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 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
83 $pageRenderer->loadExtJS();
84 $pageRenderer->addJsFile(ExtensionManagementUtility::extRelPath('backend') . 'Resources/Public/JavaScript/grideditor.js');
85 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip');
86 $pageRenderer->addInlineSetting('ContextHelp', 'moduleUrl', BackendUtility::getModuleUrl('help_cshmanual'));
87 $pageRenderer->addJsInlineCode('storeData', '
88 function storeData(data) {
89 if (parent.opener && parent.opener.document && parent.opener.document.' . $this->formName . ' && parent.opener.document.' . $this->formName . '[' . GeneralUtility::quoteJSvalue($this->fieldName) . ']) {
90 parent.opener.document.' . $this->formName . '[' . GeneralUtility::quoteJSvalue($this->fieldName) . '].value = data;
91 parent.opener.TBE_EDITOR.fieldChanged("backend_layout","' . $uid . '","config","data[backend_layout][' . $uid . '][config]");
92 }
93 }
94 ', FALSE);
95 $languageLabels = array(
96 'save' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_labelSave', TRUE),
97 'title' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_windowTitle', TRUE),
98 'editCell' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_editCell', TRUE),
99 'mergeCell' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_mergeCell', TRUE),
100 'splitCell' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_splitCell', TRUE),
101 'name' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_name', TRUE),
102 'column' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_column', TRUE),
103 'notSet' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_notSet', TRUE),
104 'nameHelp' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_nameHelp', TRUE),
105 'columnHelp' => $lang->sL('LLL:EXT:lang/locallang_wizards.xlf:grid_columnHelp', TRUE)
106 );
107 $pageRenderer->addInlineLanguageLabelArray($languageLabels);
108 // Select record
109 $record = $this->getDatabaseConnection()->exec_SELECTgetRows($this->P['field'], $this->P['table'], 'uid=' . (int)$this->P['uid']);
110 if (trim($record[0][$this->P['field']]) == '') {
111 $rows = array(array(array('colspan' => 1, 'rowspan' => 1, 'spanned' => FALSE, 'name' => '')));
112 $colCount = 1;
113 $rowCount = 1;
114 } else {
115 // load TS parser
116 $parser = GeneralUtility::makeInstance(TypoScriptParser::class);
117 $parser->parse($record[0][$this->P['field']]);
118 $data = $parser->setup['backend_layout.'];
119 $rows = array();
120 $colCount = $data['colCount'];
121 $rowCount = $data['rowCount'];
122 $dataRows = $data['rows.'];
123 $spannedMatrix = array();
124 for ($i = 1; $i <= $rowCount; $i++) {
125 $cells = array();
126 $row = array_shift($dataRows);
127 $columns = $row['columns.'];
128 for ($j = 1; $j <= $colCount; $j++) {
129 $cellData = array();
130 if (!$spannedMatrix[$i][$j]) {
131 if (is_array($columns) && !empty($columns)) {
132 $column = array_shift($columns);
133 if (isset($column['colspan'])) {
134 $cellData['colspan'] = (int)$column['colspan'];
135 $columnColSpan = (int)$column['colspan'];
136 if (isset($column['rowspan'])) {
137 $columnRowSpan = (int)$column['rowspan'];
138 for ($spanRow = 0; $spanRow < $columnRowSpan; $spanRow++) {
139 for ($spanColumn = 0; $spanColumn < $columnColSpan; $spanColumn++) {
140 $spannedMatrix[$i + $spanRow][$j + $spanColumn] = 1;
141 }
142 }
143 } else {
144 for ($spanColumn = 0; $spanColumn < $columnColSpan; $spanColumn++) {
145 $spannedMatrix[$i][$j + $spanColumn] = 1;
146 }
147 }
148 } else {
149 $cellData['colspan'] = 1;
150 if (isset($column['rowspan'])) {
151 $columnRowSpan = (int)$column['rowspan'];
152 for ($spanRow = 0; $spanRow < $columnRowSpan; $spanRow++) {
153 $spannedMatrix[$i + $spanRow][$j] = 1;
154 }
155 }
156 }
157 if (isset($column['rowspan'])) {
158 $cellData['rowspan'] = (int)$column['rowspan'];
159 } else {
160 $cellData['rowspan'] = 1;
161 }
162 if (isset($column['name'])) {
163 $cellData['name'] = $column['name'];
164 }
165 if (isset($column['colPos'])) {
166 $cellData['column'] = (int)$column['colPos'];
167 }
168 }
169 } else {
170 $cellData = array('colspan' => 1, 'rowspan' => 1, 'spanned' => 1);
171 }
172 $cells[] = $cellData;
173 }
174 $rows[] = $cells;
175 if (!empty($spannedMatrix[$i]) && is_array($spannedMatrix[$i])) {
176 ksort($spannedMatrix[$i]);
177 }
178 }
179 }
180 $pageRenderer->addExtOnReadyCode('
181 t3Grid = new TYPO3.Backend.t3Grid({
182 data: ' . json_encode($rows, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS) . ',
183 colCount: ' . (int)$colCount . ',
184 rowCount: ' . (int)$rowCount . ',
185 targetElement: \'editor\'
186 });
187 t3Grid.drawTable();
188 ');
189 $this->doc->styleSheetFile_post = ExtensionManagementUtility::extRelPath('backend') . 'Resources/Public/Css/grideditor.css';
190 }
191
192 /**
193 * Main Method, rendering either colorpicker or frameset depending on ->showPicker
194 *
195 * @return void
196 */
197 public function main() {
198 $lang = $this->getLanguageService();
199 $resourcePath = ExtensionManagementUtility::extRelPath('backend') . 'Resources/Public/Images/BackendLayoutWizard/';
200 $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>';
201 $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>';
202 $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>';
203 $content .= $this->doc->spacer(10);
204 $content .= '
205 <table border="0" width="90%" height="90%" id="outer_container">
206 <tr>
207 <td class="editor_cell">
208 <div id="editor">
209 </div>
210 </td>
211 <td width="20" valign="center">
212 <a class="addCol" href="#" title="' . $lang->getLL('grid_addColumn') . '" onclick="t3Grid.addColumn(); t3Grid.drawTable(\'editor\');">
213 <img src="' . $resourcePath . 't3grid-tableright.png" border="0" />
214 </a><br />
215 <a class="removeCol" href="#" title="' . $lang->getLL('grid_removeColumn') . '" onclick="t3Grid.removeColumn(); t3Grid.drawTable(\'editor\');">
216 <img src="' . $resourcePath . 't3grid-tableleft.png" border="0" />
217 </a>
218 </td>
219 </tr>
220 <tr>
221 <td colspan="2" height="20" align="center">
222 <a class="addCol" href="#" title="' . $lang->getLL('grid_addRow') . '" onclick="t3Grid.addRow(); t3Grid.drawTable(\'editor\');">
223 <img src="' . $resourcePath . 't3grid-tabledown.png" border="0" />
224 </a>
225 <a class="removeCol" href="#" title="' . $lang->getLL('grid_removeRow') . '" onclick="t3Grid.removeRow(); t3Grid.drawTable(\'editor\');">
226 <img src="' . $resourcePath . 't3grid-tableup.png" border="0" />
227 </a>
228 </td>
229 </tr>
230 </table>
231 ';
232 $this->content = $content;
233 }
234
235 /**
236 * Returns the sourcecode to the browser
237 *
238 * @return void
239 */
240 public function printContent() {
241 echo $this->doc->render('Grid wizard', $this->content);
242 }
243
244 /**
245 * Returns LanguageService
246 *
247 * @return \TYPO3\CMS\Lang\LanguageService
248 */
249 protected function getLanguageService() {
250 return $GLOBALS['LANG'];
251 }
252
253 /**
254 * Returns the database connection
255 *
256 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
257 */
258 protected function getDatabaseConnection() {
259 return $GLOBALS['TYPO3_DB'];
260 }
261
262 }