[BUGFIX] Fix wrong JS function name in RTE
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / User.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 1999-2013 Kasper Skårhøj (kasper@typo3.com)
7 * (c) 2005-2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
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\Core\Utility\GeneralUtility;
32
33 /**
34 * User defined content for htmlArea RTE
35 *
36 * @author Kasper Skårhøj <kasper@typo3.com>
37 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
38 */
39 class User {
40
41 /**
42 * @todo Define visibility
43 */
44 public $content;
45
46 /**
47 * @todo Define visibility
48 */
49 public $modData;
50
51 /**
52 * @todo Define visibility
53 */
54 public $siteUrl;
55
56 /**
57 * document template object
58 *
59 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
60 * @todo Define visibility
61 */
62 public $doc;
63
64 /**
65 * @todo Define visibility
66 */
67 public $editorNo;
68
69 /**
70 * Initialize language files
71 */
72 public function __construct() {
73 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/mod5/locallang.xlf');
74 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/htmlarea/locallang_dialogs.xlf');
75 }
76
77 /**
78 * @return [type] ...
79 * @todo Define visibility
80 */
81 public function init() {
82 $this->editorNo = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('editorNo');
83 $this->siteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
84 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
85 $this->doc->backPath = $GLOBALS['BACK_PATH'];
86 $this->doc->bodyTagAdditions = 'onload="Init();"';
87 $this->doc->form = '
88 <form action="" id="process" name="process" method="post">
89 <input type="hidden" name="processContent" value="" />
90 <input type="hidden" name="returnUrl" value="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')) . '" />
91 ';
92 $JScode = '
93 var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("UserElements");
94 var HTMLArea = window.parent.HTMLArea;
95 var editor = plugin.editor;
96
97 function Init() {
98 };
99 function insertHTML(content,noHide) {
100 plugin.restoreSelection();
101 editor.getSelection().insertHtml(content);
102 if(!noHide) plugin.close();
103 };
104 function wrapHTML(wrap1,wrap2,noHide) {
105 plugin.restoreSelection();
106 if(!editor.getSelection().isEmpty()) {
107 editor.getSelection().surroundHtml(wrap1,wrap2);
108 } else {
109 alert(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('noTextSelection')) . ');
110 }
111 if(!noHide) plugin.close();
112 };
113 function processSelection(script) {
114 plugin.restoreSelection();
115 document.process.action = script;
116 document.process.processContent.value = editor.getSelection().getHtml();
117 document.process.submit();
118 };
119 function jumpToUrl(URL) {
120 var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams')) . '";
121 var editorNo = "&editorNo=' . rawurlencode($this->editorNo) . '";
122 theLocation = URL+RTEtsConfigParams+editorNo;
123 window.location.href = theLocation;
124 }
125 ';
126
127 // unset the default jumpToUrl() function
128 unset($this->doc->JScodeArray['jumpToUrl']);
129
130 $this->doc->JScode = $this->doc->wrapScriptTags($JScode);
131 $this->modData = $GLOBALS['BE_USER']->getModuleData('user.php', 'ses');
132 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key')) {
133 $parts = explode('|', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key'));
134 $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
135 $GLOBALS['BE_USER']->pushModuleData('user.php', $this->modData);
136 }
137 }
138
139 /**
140 * [Describe function...]
141 *
142 * @return [type] ...
143 * @todo Define visibility
144 */
145 public function main() {
146 $this->content = '';
147 $this->content .= $this->main_user($this->modData['openKeys']);
148 }
149
150 /**
151 * [Describe function...]
152 *
153 * @return [type] ...
154 * @todo Define visibility
155 */
156 public function printContent() {
157 echo $this->content;
158 }
159
160 /********************************
161 *
162 * Other functions
163 *
164 *********************************/
165 /**
166 * @param [type] $imgInfo: ...
167 * @param [type] $maxW: ...
168 * @param [type] $maxH: ...
169 * @return [type] ...
170 * @todo Define visibility
171 */
172 public function calcWH($imgInfo, $maxW = 380, $maxH = 500) {
173 $IW = $imgInfo[0];
174 $IH = $imgInfo[1];
175 if ($IW > $maxW) {
176 $IH = ceil($IH / $IW * $maxW);
177 $IW = $maxW;
178 }
179 if ($IH > $maxH) {
180 $IW = ceil($IW / $IH * $maxH);
181 $IH = $maxH;
182 }
183 $imgInfo[3] = 'width="' . $IW . '" height="' . $IH . '"';
184 return $imgInfo;
185 }
186
187 /**
188 * Rich Text Editor (RTE) user element selector
189 *
190 * @param [type] $openKeys: ...
191 * @return [type] ...
192 * @todo Define visibility
193 */
194 public function main_user($openKeys) {
195 // Starting content:
196 $content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Custom Element', TRUE));
197 $RTEtsConfigParts = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams'));
198 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
199 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
200 if (is_array($thisConfig['userElements.'])) {
201 $categories = array();
202 foreach ($thisConfig['userElements.'] as $k => $value) {
203 $ki = (int)$k;
204 $v = $thisConfig['userElements.'][$ki . '.'];
205 if (substr($k, -1) == '.' && is_array($v)) {
206 $subcats = array();
207 $openK = $ki;
208 if ($openKeys[$openK]) {
209 $mArray = '';
210 if ($v['load'] === 'images_from_folder') {
211 $mArray = array();
212 if ($v['path'] && @is_dir((PATH_site . $v['path']))) {
213 $files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . $v['path'], 'gif,jpg,jpeg,png', 0, '');
214 if (is_array($files)) {
215 $c = 0;
216 foreach ($files as $filename) {
217 $iInfo = @getimagesize((PATH_site . $v['path'] . $filename));
218 $iInfo = $this->calcWH($iInfo, 50, 100);
219 $ks = (string) (100 + $c);
220 $mArray[$ks] = $filename;
221 $mArray[$ks . '.'] = array(
222 'content' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" />',
223 '_icon' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" ' . $iInfo[3] . ' />',
224 'description' => $GLOBALS['LANG']->getLL('filesize') . ': ' . str_replace('&nbsp;', ' ', \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize(@filesize((PATH_site . $v['path'] . $filename)))) . ', ' . $GLOBALS['LANG']->getLL('pixels', 1) . ': ' . $iInfo[0] . 'x' . $iInfo[1]
225 );
226 $c++;
227 }
228 }
229 }
230 }
231 if (is_array($mArray)) {
232 if ($v['merge']) {
233 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($mArray, $v);
234 $v = $mArray;
235 } else {
236 $v = $mArray;
237 }
238 }
239 foreach ($v as $k2 => $dummyValue) {
240 $k2i = (int)$k2;
241 if (substr($k2, -1) == '.' && is_array($v[$k2i . '.'])) {
242 $title = trim($v[$k2i]);
243 if (!$title) {
244 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', TRUE) . ']';
245 } else {
246 $title = $GLOBALS['LANG']->sL($title, TRUE);
247 }
248 $description = $GLOBALS['LANG']->sL($v[($k2i . '.')]['description'], TRUE) . '<br />';
249 if (!$v[($k2i . '.')]['dontInsertSiteUrl']) {
250 $v[$k2i . '.']['content'] = str_replace('###_URL###', $this->siteUrl, $v[$k2i . '.']['content']);
251 }
252 $logo = $v[$k2i . '.']['_icon'] ?: '';
253 $onClickEvent = '';
254 switch ((string) $v[($k2i . '.')]['mode']) {
255 case 'wrap':
256 $wrap = explode('|', $v[$k2i . '.']['content']);
257 $onClickEvent = 'wrapHTML(' . GeneralUtility::quoteJSvalue($wrap[0]) . ',' . GeneralUtility::quoteJSvalue($wrap[1]) . ',false);';
258 break;
259 case 'processor':
260 $script = trim($v[$k2i . '.']['submitToScript']);
261 if (substr($script, 0, 4) != 'http') {
262 $script = $this->siteUrl . $script;
263 }
264 if ($script) {
265 $onClickEvent = 'processSelection(' . GeneralUtility::quoteJSvalue($script) . ');';
266 }
267 break;
268 case 'insert':
269
270 default:
271 $onClickEvent = 'insertHTML(' . GeneralUtility::quoteJSvalue($v[($k2i . '.')]['content']) . ');';
272 }
273 $A = array('<a href="#" onClick="' . $onClickEvent . 'return false;">', '</a>');
274 $subcats[$k2i] = '<tr>
275 <td><img src="clear.gif" width="18" height="1" /></td>
276 <td class="bgColor4" valign="top">' . $A[0] . $logo . $A[1] . '</td>
277 <td class="bgColor4" valign="top">' . $A[0] . '<strong>' . $title . '</strong><br />' . $description . $A[1] . '</td>
278 </tr>';
279 }
280 }
281 ksort($subcats);
282 }
283 $categories[$ki] = implode('', $subcats);
284 }
285 }
286 ksort($categories);
287 // Render menu of the items:
288 $lines = array();
289 foreach ($categories as $k => $v) {
290 $title = trim($thisConfig['userElements.'][$k]);
291 $openK = $k;
292 if (!$title) {
293 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', TRUE) . ']';
294 } else {
295 $title = $GLOBALS['LANG']->sL($title, TRUE);
296 }
297 $lines[] = '<tr><td colspan="3" class="bgColor5"><a href="#" title="' . $GLOBALS['LANG']->getLL('expand', TRUE) . '" onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('rtehtmlarea_wizard_user', array('OC_key' => ($openKeys[$openK] ? 'C|' : 'O|') . $openK))) . ');return false;"><img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . ($openKeys[$openK] ? 'minus' : 'plus') . 'bullet.gif'), 'width="18" height="16"') . ' title="' . $GLOBALS['LANG']->getLL('expand', TRUE) . '" /><strong>' . $title . '</strong></a></td></tr>';
298 $lines[] = $v;
299 }
300 $content .= '<table border="0" cellpadding="1" cellspacing="1">' . implode('', $lines) . '</table>';
301 }
302 $content .= $this->doc->endPage();
303 return $content;
304 }
305
306 }