b93c0144dad267420ce7db1bece60382dc94fcca
[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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
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 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_userelementscontroller.xlf');
57 $GLOBALS['LANG']->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
58 }
59
60 /**
61 * @return void
62 */
63 public function init() {
64 $this->editorNo = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('editorNo');
65 $this->siteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
66 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
67 $this->doc->backPath = $GLOBALS['BACK_PATH'];
68 $this->doc->bodyTagAdditions = 'onload="Init();"';
69 $this->doc->form = '
70 <form action="" id="process" name="process" method="post">
71 <input type="hidden" name="processContent" value="" />
72 <input type="hidden" name="returnUrl" value="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REQUEST_URI')) . '" />
73 ';
74 $JScode = '
75 var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("UserElements");
76 var HTMLArea = window.parent.HTMLArea;
77 var editor = plugin.editor;
78
79 function Init() {
80 };
81 function insertHTML(content,noHide) {
82 plugin.restoreSelection();
83 editor.getSelection().insertHtml(content);
84 if(!noHide) plugin.close();
85 };
86 function wrapHTML(wrap1,wrap2,noHide) {
87 plugin.restoreSelection();
88 if(!editor.getSelection().isEmpty()) {
89 editor.getSelection().surroundHtml(wrap1,wrap2);
90 } else {
91 alert(' . GeneralUtility::quoteJSvalue($GLOBALS['LANG']->getLL('noTextSelection')) . ');
92 }
93 if(!noHide) plugin.close();
94 };
95 function processSelection(script) {
96 plugin.restoreSelection();
97 document.process.action = script;
98 document.process.processContent.value = editor.getSelection().getHtml();
99 document.process.submit();
100 };
101 function jumpToUrl(URL) {
102 var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams')) . '";
103 var editorNo = "&editorNo=' . rawurlencode($this->editorNo) . '";
104 theLocation = URL+RTEtsConfigParams+editorNo;
105 window.location.href = theLocation;
106 }
107 ';
108
109 // unset the default jumpToUrl() function
110 unset($this->doc->JScodeArray['jumpToUrl']);
111
112 $this->doc->JScode = $this->doc->wrapScriptTags($JScode);
113 $this->modData = $GLOBALS['BE_USER']->getModuleData('user.php', 'ses');
114 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key')) {
115 $parts = explode('|', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('OC_key'));
116 $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
117 $GLOBALS['BE_USER']->pushModuleData('user.php', $this->modData);
118 }
119 }
120
121 /**
122 * Main function
123 *
124 * @return void
125 */
126 public function main() {
127 $this->content = '';
128 $this->content .= $this->main_user($this->modData['openKeys']);
129 }
130
131 /**
132 * Print content
133 *
134 * @return void
135 */
136 public function printContent() {
137 echo $this->content;
138 }
139
140 /********************************
141 *
142 * Other functions
143 *
144 *********************************/
145 /**
146 * @param array $imgInfo
147 * @param int $maxW
148 * @param int $maxH
149 * @return array
150 */
151 public function calcWH($imgInfo, $maxW = 380, $maxH = 500) {
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 // Starting content:
174 $content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Custom Element', TRUE));
175 $RTEtsConfigParts = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('RTEtsConfigParams'));
176 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
177 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
178 if (is_array($thisConfig['userElements.'])) {
179 $categories = array();
180 foreach ($thisConfig['userElements.'] as $k => $value) {
181 $ki = (int)$k;
182 $v = $thisConfig['userElements.'][$ki . '.'];
183 if (substr($k, -1) == '.' && is_array($v)) {
184 $subcats = array();
185 $openK = $ki;
186 if ($openKeys[$openK]) {
187 $mArray = '';
188 if ($v['load'] === 'images_from_folder') {
189 $mArray = array();
190 if ($v['path'] && @is_dir((PATH_site . $v['path']))) {
191 $files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . $v['path'], 'gif,jpg,jpeg,png', 0, '');
192 if (is_array($files)) {
193 $c = 0;
194 foreach ($files as $filename) {
195 $iInfo = @getimagesize((PATH_site . $v['path'] . $filename));
196 $iInfo = $this->calcWH($iInfo, 50, 100);
197 $ks = (string)(100 + $c);
198 $mArray[$ks] = $filename;
199 $mArray[$ks . '.'] = array(
200 'content' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" />',
201 '_icon' => '<img src="' . $this->siteUrl . $v['path'] . $filename . '" ' . $iInfo[3] . ' />',
202 '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]
203 );
204 $c++;
205 }
206 }
207 }
208 }
209 if (is_array($mArray)) {
210 if ($v['merge']) {
211 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($mArray, $v);
212 $v = $mArray;
213 } else {
214 $v = $mArray;
215 }
216 }
217 foreach ($v as $k2 => $dummyValue) {
218 $k2i = (int)$k2;
219 if (substr($k2, -1) == '.' && is_array($v[$k2i . '.'])) {
220 $title = trim($v[$k2i]);
221 if (!$title) {
222 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', TRUE) . ']';
223 } else {
224 $title = $GLOBALS['LANG']->sL($title, TRUE);
225 }
226 $description = $GLOBALS['LANG']->sL($v[($k2i . '.')]['description'], TRUE) . '<br />';
227 if (!$v[($k2i . '.')]['dontInsertSiteUrl']) {
228 $v[$k2i . '.']['content'] = str_replace('###_URL###', $this->siteUrl, $v[$k2i . '.']['content']);
229 }
230 $logo = $v[$k2i . '.']['_icon'] ?: '';
231 $onClickEvent = '';
232 switch ((string)$v[($k2i . '.')]['mode']) {
233 case 'wrap':
234 $wrap = explode('|', $v[$k2i . '.']['content']);
235 $onClickEvent = 'wrapHTML(' . GeneralUtility::quoteJSvalue($wrap[0]) . ',' . GeneralUtility::quoteJSvalue($wrap[1]) . ',false);';
236 break;
237 case 'processor':
238 $script = trim($v[$k2i . '.']['submitToScript']);
239 if (substr($script, 0, 4) != 'http') {
240 $script = $this->siteUrl . $script;
241 }
242 if ($script) {
243 $onClickEvent = 'processSelection(' . GeneralUtility::quoteJSvalue($script) . ');';
244 }
245 break;
246 case 'insert':
247
248 default:
249 $onClickEvent = 'insertHTML(' . GeneralUtility::quoteJSvalue($v[($k2i . '.')]['content']) . ');';
250 }
251 $A = array('<a href="#" onClick="' . $onClickEvent . 'return false;">', '</a>');
252 $subcats[$k2i] = '<tr>
253 <td></td>
254 <td class="bgColor4" valign="top">' . $A[0] . $logo . $A[1] . '</td>
255 <td class="bgColor4" valign="top">' . $A[0] . '<strong>' . $title . '</strong><br />' . $description . $A[1] . '</td>
256 </tr>';
257 }
258 }
259 ksort($subcats);
260 }
261 $categories[$ki] = implode('', $subcats);
262 }
263 }
264 ksort($categories);
265 // Render menu of the items:
266 $lines = array();
267 foreach ($categories as $k => $v) {
268 $title = trim($thisConfig['userElements.'][$k]);
269 $openK = $k;
270 if (!$title) {
271 $title = '[' . $GLOBALS['LANG']->getLL('noTitle', TRUE) . ']';
272 } else {
273 $title = $GLOBALS['LANG']->sL($title, TRUE);
274 }
275 $lines[] = '<tr><td colspan="3" class="bgColor5"><a href="#" title="' . $GLOBALS['LANG']->getLL('expand', TRUE) . '" onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl($GLOBALS['MCONF']['name'], array('OC_key' => ($openKeys[$openK] ? 'C|' : 'O|') . $openK))) . ');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>';
276 $lines[] = $v;
277 }
278 $content .= '<table border="0" cellpadding="1" cellspacing="1">' . implode('', $lines) . '</table>';
279 }
280 $content .= $this->doc->endPage();
281 return $content;
282 }
283
284 }