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