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