[CLEANUP] Cleanup jumpToUrl() and unused JS in Backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Wizard / RteController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\Wizard;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Backend\Utility\BackendUtility;
31 use TYPO3\CMS\Backend\Utility\IconUtility;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34 /**
35 * Script Class for rendering the full screen RTE display
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 */
39 class RteController {
40
41 // Internal, dynamic:
42 /**
43 * document template object
44 *
45 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
46 * @todo Define visibility
47 */
48 public $doc;
49
50 // Content accumulation for the module.
51 /**
52 * @todo Define visibility
53 */
54 public $content;
55
56 // Internal, static: GPvars
57 // Wizard parameters, coming from TCEforms linking to the wizard.
58 /**
59 * @todo Define visibility
60 */
61 public $P;
62
63 // If set, launch a new window with the current records pid.
64 /**
65 * @todo Define visibility
66 */
67 public $popView;
68
69 // Set to the URL of this script including variables which is needed to re-display the form. See main()
70 /**
71 * @todo Define visibility
72 */
73 public $R_URI;
74
75 /**
76 * Constructor
77 */
78 public function __construct() {
79 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_wizards.xlf');
80 $GLOBALS['SOBE'] = $this;
81
82 $this->init();
83 }
84
85 /**
86 * Initialization of the class
87 *
88 * @return void
89 */
90 protected function init() {
91 // Setting GPvars:
92 $this->P = GeneralUtility::_GP('P');
93 $this->popView = GeneralUtility::_GP('popView');
94 $this->R_URI = GeneralUtility::linkThisScript(array('popView' => ''));
95 // "Module name":
96 $this->MCONF['name'] = 'wizard_rte';
97 // Starting the document template object:
98 $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
99 $this->doc->backPath = $GLOBALS['BACK_PATH'];
100 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/wizard_rte.html');
101 // Need to NOT have the page wrapped in DIV since if we do that we destroy
102 // the feature that the RTE spans the whole height of the page!!!
103 $this->doc->divClass = '';
104 $this->doc->form = '<form action="tce_db.php" method="post" enctype="' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'] . '" name="editform" onsubmit="return TBE_EDITOR.checkSubmit(1);">';
105 }
106
107 /**
108 * Main function, rendering the document with the iframe with the RTE in.
109 *
110 * @return void
111 */
112 public function main() {
113 // Translate id to the workspace version:
114 if ($versionRec = BackendUtility::getWorkspaceVersionOfRecord($GLOBALS['BE_USER']->workspace, $this->P['table'], $this->P['uid'], 'uid')) {
115 $this->P['uid'] = $versionRec['uid'];
116 }
117 // If all parameters are available:
118 if ($this->P['table'] && $this->P['field'] && $this->P['uid'] && $this->checkEditAccess($this->P['table'], $this->P['uid'])) {
119 // Getting the raw record (we need only the pid-value from here...)
120 $rawRec = BackendUtility::getRecord($this->P['table'], $this->P['uid']);
121 BackendUtility::fixVersioningPid($this->P['table'], $rawRec);
122
123 // override the default jumpToUrl
124 $this->doc->JScodeArray['jumpToUrl'] = '
125 function jumpToUrl(URL,formEl) {
126 if (document.editform) {
127 if (!TBE_EDITOR.isFormChanged()) {
128 window.location.href = URL;
129 } else if (formEl) {
130 if (formEl.type=="checkbox") formEl.checked = formEl.checked ? 0 : 1;
131 }
132 } else {
133 window.location.href = URL;
134 }
135 }
136 ';
137
138 // Setting JavaScript of the pid value for viewing:
139 if ($this->popView) {
140 $this->doc->JScode = $this->doc->wrapScriptTags(BackendUtility::viewOnClick($rawRec['pid'], '', BackendUtility::BEgetRootLine($rawRec['pid'])));
141 }
142 // Initialize TCeforms - for rendering the field:
143 $tceforms = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Form\\FormEngine');
144 // Init...
145 $tceforms->initDefaultBEMode();
146 // SPECIAL: Disables all wizards - we are NOT going to need them.
147 $tceforms->disableWizards = 1;
148 // SPECIAL: Setting background color of the RTE to ordinary background
149 $tceforms->colorScheme[0] = $this->doc->bgColor;
150 // Initialize style for RTE object:
151 // Getting reference to the RTE object used to render the field!
152 $RTEobj = BackendUtility::RTEgetObj();
153 if ($RTEobj->ID == 'rte') {
154 $RTEobj->RTEdivStyle = 'position:relative; left:0px; top:0px; height:100%; width:100%; border:solid 0px;';
155 }
156 // Fetching content of record:
157 $trData = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Form\\DataPreprocessor');
158 $trData->lockRecords = 1;
159 $trData->fetchRecord($this->P['table'], $this->P['uid'], '');
160 // Getting the processed record content out:
161 $rec = reset($trData->regTableItems_data);
162 $rec['uid'] = $this->P['uid'];
163 $rec['pid'] = $rawRec['pid'];
164 // TSconfig, setting width:
165 $fieldTSConfig = $tceforms->setTSconfig($this->P['table'], $rec, $this->P['field']);
166 if ((string)$fieldTSConfig['RTEfullScreenWidth'] !== '') {
167 $width = $fieldTSConfig['RTEfullScreenWidth'];
168 } else {
169 $width = '100%';
170 }
171 // Get the form field and wrap it in the table with the buttons:
172 $formContent = $tceforms->getSoloField($this->P['table'], $rec, $this->P['field']);
173 $formContent = '
174
175
176 <!--
177 RTE wizard:
178 -->
179 <table border="0" cellpadding="0" cellspacing="0" width="' . $width . '" id="typo3-rtewizard">
180 <tr>
181 <td width="' . $width . '" colspan="2" id="c-formContent">' . $formContent . '</td>
182 <td></td>
183 </tr>
184 </table>';
185 // Adding hidden fields:
186 $formContent .= '<input type="hidden" name="redirect" value="' . htmlspecialchars($this->R_URI) . '" />
187 <input type="hidden" name="_serialNumber" value="' . md5(microtime()) . '" />' . \TYPO3\CMS\Backend\Form\FormEngine::getHiddenTokenField('tceAction');
188 // Finally, add the whole setup:
189 $this->content .= $tceforms->printNeededJSFunctions_top() . $formContent . $tceforms->printNeededJSFunctions();
190 } else {
191 // ERROR:
192 $this->content .= $this->doc->section($GLOBALS['LANG']->getLL('forms_title'), '<span class="typo3-red">' . $GLOBALS['LANG']->getLL('table_noData', TRUE) . '</span>', 0, 1);
193 }
194 // Setting up the buttons and markers for docheader
195 $docHeaderButtons = $this->getButtons();
196 $markers['CONTENT'] = $this->content;
197 // Build the <body> for the module
198 $this->content = $this->doc->startPage('');
199 $this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
200 $this->content .= $this->doc->endPage();
201 $this->content = $this->doc->insertStylesAndJS($this->content);
202 }
203
204 /**
205 * Outputting the accumulated content to screen
206 *
207 * @return void
208 * @todo Define visibility
209 */
210 public function printContent() {
211 $this->content .= $this->doc->endPage();
212 $this->content = $this->doc->insertStylesAndJS($this->content);
213 echo $this->content;
214 }
215
216 /**
217 * Create the panel of buttons for submitting the form or otherwise perform operations.
218 *
219 * @return array All available buttons as an assoc. array
220 */
221 protected function getButtons() {
222 $buttons = array(
223 'close' => '',
224 'save' => '',
225 'save_view' => '',
226 'save_close' => '',
227 'shortcut' => '',
228 'undo' => ''
229 );
230 if ($this->P['table'] && $this->P['field'] && $this->P['uid'] && $this->checkEditAccess($this->P['table'], $this->P['uid'])) {
231 $closeUrl = GeneralUtility::sanitizeLocalUrl($this->P['returnUrl']);
232 // Getting settings for the undo button:
233 $undoButton = 0;
234 $undoRes = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tstamp', 'sys_history', 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->P['table'], 'sys_history') . ' AND recuid=' . (int)$this->P['uid'], '', 'tstamp DESC', '1');
235 if ($undoButtonR = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($undoRes)) {
236 $undoButton = 1;
237 }
238 // Close
239 $buttons['close'] = '<a href="#" onclick="' . htmlspecialchars(('jumpToUrl(unescape(\'' . rawurlencode($closeUrl) . '\')); return false;')) . '">' . '<img' . IconUtility::skinImg($this->doc->backPath, 'gfx/closedok.gif') . ' class="c-inputButton" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', TRUE) . '" alt="" />' . '</a>';
240 // Save
241 $buttons['save'] = '<a href="#" onclick="TBE_EDITOR.checkAndDoSubmit(1); return false;">' . '<img' . IconUtility::skinImg($this->doc->backPath, 'gfx/savedok.gif') . ' class="c-inputButton" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDoc', TRUE) . '" alt="" />' . '</a>';
242 // Save & View
243 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('cms')) {
244 $buttons['save_view'] = '<a href="#" onclick="' . htmlspecialchars('document.editform.redirect.value+=\'&popView=1\'; TBE_EDITOR.checkAndDoSubmit(1); return false;') . '">' . '<img' . IconUtility::skinImg($this->doc->backPath, 'gfx/savedokshow.gif') . ' class="c-inputButton" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveDocShow', TRUE) . '" alt="" />' . '</a>';
245 }
246 // Save & Close
247 $buttons['save_close'] = '<input type="image" class="c-inputButton" onclick="' . htmlspecialchars(('document.editform.redirect.value=\'' . $closeUrl . '\'; TBE_EDITOR.checkAndDoSubmit(1); return false;')) . '" name="_saveandclosedok"' . IconUtility::skinImg($this->doc->backPath, 'gfx/saveandclosedok.gif', '') . ' title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveCloseDoc', TRUE) . '" />';
248 // Undo/Revert:
249 if ($undoButton) {
250 $buttons['undo'] = '<a href="#" onclick="' . htmlspecialchars(('window.location.href=\'show_rechis.php?element=' . rawurlencode(($this->P['table'] . ':' . $this->P['uid'])) . '&revert=' . rawurlencode(('field:' . $this->P['field'])) . '&sumUp=-1&returnUrl=' . rawurlencode($this->R_URI) . '\'; return false;')) . '">' . '<img' . IconUtility::skinImg($this->doc->backPath, 'gfx/undo.gif') . ' class="c-inputButton" title="' . htmlspecialchars(sprintf($GLOBALS['LANG']->getLL('rte_undoLastChange'), BackendUtility::calcAge(($GLOBALS['EXEC_TIME'] - $undoButtonR['tstamp']), $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.minutesHoursDaysYears')))) . '" alt="" />' . '</a>';
251 }
252 // Shortcut
253 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
254 $buttons['shortcut'] = $this->doc->makeShortcutIcon('P', '', $this->MCONF['name'], 1);
255 }
256 }
257 return $buttons;
258 }
259
260 /**
261 * Checks access for element
262 *
263 * @param string $table Table name
264 * @param integer $uid Record uid
265 * @return boolean
266 * @todo Define visibility
267 * @todo: Refactor to remove duplicate code (see FormsController, TableController)
268 */
269 public function checkEditAccess($table, $uid) {
270 $calcPRec = BackendUtility::getRecord($table, $uid);
271 BackendUtility::fixVersioningPid($table, $calcPRec);
272 if (is_array($calcPRec)) {
273 // If pages:
274 if ($table == 'pages') {
275 $CALC_PERMS = $GLOBALS['BE_USER']->calcPerms($calcPRec);
276 $hasAccess = $CALC_PERMS & 2 ? TRUE : FALSE;
277 } else {
278 // Fetching pid-record first.
279 $CALC_PERMS = $GLOBALS['BE_USER']->calcPerms(BackendUtility::getRecord('pages', $calcPRec['pid']));
280 $hasAccess = $CALC_PERMS & 16 ? TRUE : FALSE;
281 }
282 // Check internals regarding access:
283 if ($hasAccess) {
284 $hasAccess = $GLOBALS['BE_USER']->recordEditAccessInternals($table, $calcPRec);
285 }
286 } else {
287 $hasAccess = FALSE;
288 }
289 return $hasAccess;
290 }
291
292 }