[TASK] Deprecate DocumentTemplate methods
[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\ResponseInterface;
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * User defined content for htmlArea RTE
22 */
23 class UserElementsController
24 {
25 /**
26 * @var string
27 */
28 public $content;
29
30 /**
31 * @var array
32 */
33 public $modData;
34
35 /**
36 * @var string
37 */
38 public $siteUrl;
39
40 /**
41 * document template object
42 *
43 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
44 */
45 public $doc;
46
47 /**
48 * @var string
49 */
50 public $editorNo;
51
52 /**
53 * Initialize language files
54 */
55 public function __construct()
56 {
57 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_userelementscontroller.xlf');
58 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
59
60 $this->init();
61 }
62
63 /**
64 * @return void
65 */
66 public function init()
67 {
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 = GeneralUtility::wrapJS($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 the current request
129 * @param ResponseInterface $response the prepared response object
130 * @return ResponseInterface the response with the content
131 */
132 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
133 {
134 $content = $this->main_user($this->modData['openKeys']);
135 $response->getBody()->write($content);
136 return $response;
137 }
138
139 /********************************
140 *
141 * Other functions
142 *
143 *********************************/
144 /**
145 * @param array $imgInfo
146 * @param int $maxW
147 * @param int $maxH
148 * @return array
149 */
150 public function calcWH($imgInfo, $maxW = 380, $maxH = 500)
151 {
152 $IW = $imgInfo[0];
153 $IH = $imgInfo[1];
154 if ($IW > $maxW) {
155 $IH = ceil($IH / $IW * $maxW);
156 $IW = $maxW;
157 }
158 if ($IH > $maxH) {
159 $IW = ceil($IW / $IH * $maxH);
160 $IH = $maxH;
161 }
162 $imgInfo[3] = 'width="' . $IW . '" height="' . $IH . '"';
163 return $imgInfo;
164 }
165
166 /**
167 * Rich Text Editor (RTE) user element selector
168 *
169 * @param array $openKeys
170 * @return string
171 */
172 public function main_user($openKeys)
173 {
174 // Starting content:
175 $content = $this->doc->startPage(htmlspecialchars($GLOBALS['LANG']->getLL('Insert Custom Element')));
176 $RTEtsConfigParts = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams'));
177 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
178 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
179 if (is_array($thisConfig['userElements.'])) {
180 $categories = array();
181 foreach ($thisConfig['userElements.'] as $k => $value) {
182 $ki = (int)$k;
183 $v = $thisConfig['userElements.'][$ki . '.'];
184 if (substr($k, -1) == '.' && is_array($v)) {
185 $subcats = array();
186 $openK = $ki;
187 if ($openKeys[$openK]) {
188 $mArray = '';
189 if ($v['load'] === 'images_from_folder') {
190 $mArray = array();
191 if ($v['path'] && @is_dir((PATH_site . $v['path']))) {
192 $files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . $v['path'], 'gif,jpg,jpeg,png', 0, '');
193 if (is_array($files)) {
194 $c = 0;
195 foreach ($files as $filename) {
196 $iInfo = @getimagesize((PATH_site . $v['path'] . $filename));
197 $iInfo = $this->calcWH($iInfo, 50, 100);
198 $ks = (string)(100 + $c);
199 $mArray[$ks] = $filename;
200 $mArray[$ks . '.'] = array(
201 'content' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" />',
202 '_icon' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" ' . $iInfo[3] . ' />',
203 'description' => $GLOBALS['LANG']->getLL('filesize') . ': ' . str_replace('&nbsp;', ' ', \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize(@filesize((PATH_site . $v['path'] . $filename)))) . ', ' . htmlspecialchars($GLOBALS['LANG']->getLL('pixels')) . ': ' . $iInfo[0] . 'x' . $iInfo[1]
204 );
205 $c++;
206 }
207 }
208 }
209 }
210 if (is_array($mArray)) {
211 if ($v['merge']) {
212 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($mArray, $v);
213 $v = $mArray;
214 } else {
215 $v = $mArray;
216 }
217 }
218 foreach ($v as $k2 => $dummyValue) {
219 $k2i = (int)$k2;
220 if (substr($k2, -1) == '.' && is_array($v[$k2i . '.'])) {
221 $title = trim($v[$k2i]);
222 if (!$title) {
223 $title = '[' . htmlspecialchars($GLOBALS['LANG']->getLL('noTitle')) . ']';
224 } else {
225 $title = htmlspecialchars($GLOBALS['LANG']->sL($title));
226 }
227 $description = htmlspecialchars($GLOBALS['LANG']->sL($v[$k2i . '.']['description'])) . '<br />';
228 if (!$v[$k2i . '.']['dontInsertSiteUrl']) {
229 $v[$k2i . '.']['content'] = str_replace('###_URL###', $this->siteUrl, $v[$k2i . '.']['content']);
230 }
231 $logo = $v[$k2i . '.']['_icon'] ?: '';
232 $onClickEvent = '';
233 switch ((string)$v[$k2i . '.']['mode']) {
234 case 'wrap':
235 $wrap = explode('|', $v[$k2i . '.']['content']);
236 $onClickEvent = 'wrapHTML(' . GeneralUtility::quoteJSvalue($wrap[0]) . ',' . GeneralUtility::quoteJSvalue($wrap[1]) . ',false);';
237 break;
238 case 'processor':
239 $script = trim($v[$k2i . '.']['submitToScript']);
240 if (substr($script, 0, 4) != 'http') {
241 $script = $this->siteUrl . $script;
242 }
243 if ($script) {
244 $onClickEvent = 'processSelection(' . GeneralUtility::quoteJSvalue($script) . ');';
245 }
246 break;
247 case 'insert':
248
249 default:
250 $onClickEvent = 'insertHTML(' . GeneralUtility::quoteJSvalue($v[$k2i . '.']['content']) . ');';
251 }
252 $A = array('<a href="#" onClick="' . $onClickEvent . 'return false;">', '</a>');
253 $subcats[$k2i] = '<tr>
254 <td></td>
255 <td>' . $A[0] . $logo . $A[1] . '</td>
256 <td>' . $A[0] . '<strong>' . $title . '</strong><br />' . $description . $A[1] . '</td>
257 </tr>';
258 }
259 }
260 ksort($subcats);
261 }
262 $categories[$ki] = implode('', $subcats);
263 }
264 }
265 ksort($categories);
266 // Render menu of the items:
267 $lines = array();
268 foreach ($categories as $k => $v) {
269 $title = trim($thisConfig['userElements.'][$k]);
270 $openK = $k;
271 if (!$title) {
272 $title = '[' . htmlspecialchars($GLOBALS['LANG']->getLL('noTitle')) . ']';
273 } else {
274 $title = htmlspecialchars($GLOBALS['LANG']->sL($title));
275 }
276
277 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
278 $url = (string)$uriBuilder->buildUriFromRoute('rtehtmlarea_wizard_user_elements', array('OC_key' => ($openKeys[$openK] ? 'C|' : 'O|') . $openK));
279
280 $lines[] = '<tr><td colspan="3"><a href="#" title="' . htmlspecialchars($GLOBALS['LANG']->getLL('expand')) . '" onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($url) . ');return false;"><i class="fa fa-caret-square-o-' . ($openKeys[$openK] ? 'left' : 'right') . '" title="' . htmlspecialchars($GLOBALS['LANG']->getLL('expand')) . '"></i><strong>' . $title . '</strong></a></td></tr>';
281 $lines[] = $v;
282 }
283 $content .= '<table class="table table-striped table-hover">' . implode('', $lines) . '</table>';
284 }
285 $content .= $this->doc->endPage();
286 return $content;
287 }
288 }