[TASK] Replace BrowseLinks.js with jQuery/RequireJS Module
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / SelectImage.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea;
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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Resource;
19
20 /**
21 * Script Class
22 *
23 * @author Kasper Skårhøj <kasper@typo3.com>
24 */
25 class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
26
27 /**
28 * These file extensions are allowed in the "plain" image selection mode.
29 *
30 * @const
31 */
32 const PLAIN_MODE_IMAGE_FILE_EXTENSIONS = 'jpg,jpeg,gif,png';
33
34 /**
35 * @var string
36 */
37 public $extKey = 'rtehtmlarea';
38
39 /**
40 * @var string
41 */
42 public $content;
43
44 public $allowedItems;
45
46 public $allowedFileTypes = array();
47
48 protected $defaultClass;
49
50 /**
51 * Relevant for RTE mode "plain": the maximum width an image must have to be selectable.
52 *
53 * @var int
54 */
55 protected $plainMaxWidth;
56
57 /**
58 * Relevant for RTE mode "plain": the maximum height an image must have to be selectable.
59 *
60 * @var int
61 */
62 protected $plainMaxHeight;
63
64 protected $imgPath;
65
66 public $editorNo;
67
68 public $sys_language_content;
69
70 public $thisConfig;
71
72 public $buttonConfig;
73
74 protected $imgObj;
75
76 /**
77 * Initialisation
78 *
79 * @return void
80 */
81 public function init() {
82 $this->initVariables();
83 $this->initConfiguration();
84 $this->initHookObjects('ext/rtehtmlarea/mod4/class.tx_rtehtmlarea_select_image.php');
85
86 $this->allowedItems = $this->getAllowedItems('magic,plain,image');
87 $this->insertImage();
88
89 $this->initDocumentTemplate();
90 }
91
92 /**
93 * Initialize class variables
94 *
95 * @return void
96 */
97 public function initVariables() {
98 parent::initVariables();
99 // Get "act"
100 $this->act = GeneralUtility::_GP('act');
101 if (!$this->act) {
102 $this->act = FALSE;
103 }
104 // Process bparams
105 $pArr = explode('|', $this->bparams);
106 $pRteArr = explode(':', $pArr[1]);
107 $this->editorNo = $pRteArr[0];
108 $this->sys_language_content = $pRteArr[1];
109 $this->RTEtsConfigParams = $pArr[2];
110 if (!$this->editorNo) {
111 $this->editorNo = GeneralUtility::_GP('editorNo');
112 $this->sys_language_content = GeneralUtility::_GP('sys_language_content');
113 $this->RTEtsConfigParams = GeneralUtility::_GP('RTEtsConfigParams');
114 }
115 $pArr[1] = implode(':', array($this->editorNo, $this->sys_language_content));
116 $pArr[2] = $this->RTEtsConfigParams;
117 if ($this->act == 'dragdrop' || $this->act == 'plain') {
118 $this->allowedFileTypes = explode(',', self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS);
119 }
120 $pArr[3] = implode(',', $this->allowedFileTypes);
121 $this->bparams = implode('|', $pArr);
122 }
123
124 /**
125 * Initialize document template object
126 *
127 * @return void
128 */
129 protected function initDocumentTemplate() {
130 parent::initDocumentTemplate();
131
132 $this->doc->bodyTagId = 'typo3-browse-links-php';
133 $this->doc->bodyTagAdditions = $this->getBodyTagAdditions();
134
135 $this->doc->JScode .= $this->doc->wrapScriptTags('
136 Tree.ajaxID = "SC_alt_file_navframe::expandCollapse";
137 ');
138 $this->doc->getPageRenderer()->addCssFile($this->doc->backPath . \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('t3skin') . 'rtehtmlarea/htmlarea.css');
139 $this->doc->getContextMenuCode();
140 }
141
142 /**
143 * Provide the additional parameters to be included in the template body tag
144 *
145 * @return string the body tag additions
146 */
147 public function getBodyTagAdditions() {
148 return 'onload="initEventListeners();"';
149 }
150
151 /**
152 * Insert the image in the editing area
153 *
154 * @return void
155 */
156 protected function insertImage() {
157 $table = htmlspecialchars(GeneralUtility::_GP('table'));
158 $uid = (int)GeneralUtility::_GP('uid');
159 if (GeneralUtility::_GP('insertImage') && $uid) {
160 /** @var $fileObject Resource\File */
161 $fileObject = Resource\ResourceFactory::getInstance()->getFileObject($uid);
162 // Get default values for alt and title attributes from file properties
163 $altText = $fileObject->getProperty('alternative');
164 $titleText = $fileObject->getProperty('title');
165 switch ($this->act) {
166 case 'magic':
167 $this->insertMagicImage($fileObject, $altText, $titleText, 'data-htmlarea-file-uid="' . $uid . '"');
168 die;
169 break;
170 case 'plain':
171 $this->insertPlainImage($fileObject, $altText, $titleText, 'data-htmlarea-file-uid="' . $uid . '"');
172 die;
173 break;
174 default:
175 // Call hook
176 foreach ($this->hookObjects as $hookObject) {
177 if (method_exists($hookObject, 'insertElement')) {
178 $hookObject->insertElement($this->act);
179 }
180 }
181 }
182 }
183 }
184
185 /**
186 * Insert a magic image
187 *
188 * @param Resource\File $fileObject: the image file
189 * @param string $altText: text for the alt attribute of the image
190 * @param string $titleText: text for the title attribute of the image
191 * @param string $additionalParams: text representing more HTML attributes to be added on the img tag
192 * @return void
193 */
194 public function insertMagicImage(Resource\File $fileObject, $altText = '', $titleText = '', $additionalParams = '') {
195 // Create the magic image service
196 /** @var $magicImageService Resource\Service\MagicImageService */
197 $magicImageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Service\MagicImageService::class);
198 $magicImageService->setMagicImageMaximumDimensions($this->thisConfig);
199 // Create the magic image
200 $imageConfiguration = array(
201 'width' => GeneralUtility::_GP('cWidth'),
202 'height' => GeneralUtility::_GP('cHeight')
203 );
204 $magicImage = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
205 $imageUrl = $magicImage->getPublicUrl();
206 // If file is local, make the url absolute
207 if (substr($imageUrl, 0, 4) !== 'http') {
208 $imageUrl = $this->siteURL . $imageUrl;
209 }
210 // Insert the magic image
211 $this->imageInsertJS($imageUrl, $magicImage->getProperty('width'), $magicImage->getProperty('height'), $altText, $titleText, $additionalParams);
212 }
213
214 /**
215 * Insert a plain image
216 *
217 * @param \TYPO3\CMS\Core\Resource\File $fileObject: the image file
218 * @param string $altText: text for the alt attribute of the image
219 * @param string $titleText: text for the title attribute of the image
220 * @param string $additionalParams: text representing more HTML attributes to be added on the img tag
221 * @return void
222 */
223 public function insertPlainImage(Resource\File $fileObject, $altText = '', $titleText = '', $additionalParams = '') {
224 $width = $fileObject->getProperty('width');
225 $height = $fileObject->getProperty('height');
226 if (!$width || !$height) {
227 $filePath = $fileObject->getForLocalProcessing(FALSE);
228 $imageInfo = @getimagesize($filePath);
229 $width = $imageInfo[0];
230 $height = $imageInfo[1];
231 }
232 $imageUrl = $fileObject->getPublicUrl();
233 // If file is local, make the url absolute
234 if (substr($imageUrl, 0, 4) !== 'http') {
235 $imageUrl = $this->siteURL . $imageUrl;
236 }
237 $this->imageInsertJS($imageUrl, $width, $height, $altText, $titleText, $additionalParams);
238 }
239
240 /**
241 * Echo the HTML page and JS that will insert the image
242 *
243 * @param string $url: the url of the image
244 * @param int $width: the width of the image
245 * @param int $height: the height of the image
246 * @param string $altText: text for the alt attribute of the image
247 * @param string $titleText: text for the title attribute of the image
248 * @param string $additionalParams: text representing more html attributes to be added on the img tag
249 * @return void
250 */
251 protected function imageInsertJS($url, $width, $height, $altText = '', $titleText = '', $additionalParams = '') {
252 echo '
253 <!DOCTYPE html>
254 <html>
255 <head>
256 <title>Untitled</title>
257 <script type="text/javascript">
258 /*<![CDATA[*/
259 var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("TYPO3Image");
260 function insertImage(file,width,height,alt,title,additionalParams) {
261 plugin.insertImage(\'<img src="\'+file+\'" width="\'+parseInt(width)+\'" height="\'+parseInt(height)+\'"\'' . ($this->defaultClass ? '+\' class="' . $this->defaultClass . '"\'' : '') . '+(alt?\' alt="\'+alt+\'"\':\'\')+(title?\' title="\'+title+\'"\':\'\')+(additionalParams?\' \'+additionalParams:\'\')+\' />\');
262 }
263 /*]]>*/
264 </script>
265 </head>
266 <body>
267 <script type="text/javascript">
268 /*<![CDATA[*/
269 insertImage(' . GeneralUtility::quoteJSvalue($url, 1) . ',' . $width . ',' . $height . ',' . GeneralUtility::quoteJSvalue($altText, 1) . ',' . GeneralUtility::quoteJSvalue($titleText, 1) . ',' . GeneralUtility::quoteJSvalue($additionalParams, 1) . ');
270 /*]]>*/
271 </script>
272 </body>
273 </html>';
274 }
275
276 /**
277 * Generate JS code to be used on the image insert/modify dialogue
278 *
279 * @param string $act: the action to be performed
280 * @param string $editorNo: the number of the RTE instance on the page
281 * @param string $sys_language_content: the language of the content element
282 * @return string the generated JS code
283 */
284 public function getJSCode($act, $editorNo, $sys_language_content) {
285 $removedProperties = array();
286 if (is_array($this->buttonConfig['properties.'])) {
287 if ($this->buttonConfig['properties.']['removeItems']) {
288 $removedProperties = GeneralUtility::trimExplode(',', $this->buttonConfig['properties.']['removeItems'], TRUE);
289 }
290 }
291 if ($this->buttonConfig['properties.']['class.']['allowedClasses']) {
292 $classesImageArray = GeneralUtility::trimExplode(',', $this->buttonConfig['properties.']['class.']['allowedClasses'], TRUE);
293 $classesImageJSOptions = '<option value=""></option>';
294 foreach ($classesImageArray as $class) {
295 $classesImageJSOptions .= '<option value="' . $class . '">' . $class . '</option>';
296 }
297 }
298 $lockPlainWidth = 'false';
299 $lockPlainHeight = 'false';
300 if (is_array($this->thisConfig['proc.']) && $this->thisConfig['proc.']['plainImageMode']) {
301 $plainImageMode = $this->thisConfig['proc.']['plainImageMode'];
302 $lockPlainWidth = $plainImageMode == 'lockDimensions' ? 'true' : 'false';
303 $lockPlainHeight = $lockPlainWidth || $plainImageMode == 'lockRatio' || $plainImageMode == 'lockRatioWhenSmaller' ? 'true' : 'false';
304 }
305 $JScode = '
306 var plugin = window.parent.RTEarea["' . $editorNo . '"].editor.getPlugin("TYPO3Image");
307 var HTMLArea = window.parent.HTMLArea;
308
309 HTMLArea.TYPO3Image.insertElement = function (table, uid, type, filename, filePath, fileExt, fileIcon) {
310 return jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'editorNo=') . ' + \'' . $editorNo . '\' + \'&insertImage=\' + filePath + \'&table=\' + table + \'&uid=\' + uid + \'&type=\' + type + \'bparams=\' + \'' . $this->bparams . '\');
311 }
312 function insertElement(table, uid, type, fileName, filePath, fileExt, fileIcon, action, close) {
313 return jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'editorNo=') . ' + \'' . $editorNo . '\' + \'&insertImage=\' + filePath + \'&table=\' + table + \'&uid=\' + uid + \'&type=\' + type + \'bparams=\' + \'' . $this->bparams . '\');
314 }
315 function initEventListeners() {
316 if (Ext.isWebKit) {
317 Ext.EventManager.addListener(window.document.body, "dragend", plugin.onDrop, plugin, { single: true });
318 }
319 }
320 function jumpToUrl(URL,anchor) {
321 var add_act = URL.indexOf("act=")==-1 ? "&act=' . $act . '" : "";
322 var add_editorNo = URL.indexOf("editorNo=")==-1 ? "&editorNo=' . $editorNo . '" : "";
323 var add_sys_language_content = URL.indexOf("sys_language_content=")==-1 ? "&sys_language_content=' . $sys_language_content . '" : "";
324 var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode($this->RTEtsConfigParams) . '";
325
326 var cur_width = selectedImageRef ? "&cWidth="+selectedImageRef.style.width : "";
327 var cur_height = selectedImageRef ? "&cHeight="+selectedImageRef.style.height : "";
328
329 var theLocation = URL+add_act+add_editorNo+add_sys_language_content+RTEtsConfigParams+cur_width+cur_height+(typeof(anchor)=="string"?anchor:"");
330 window.location.href = theLocation;
331 return false;
332 }
333 function insertImage(file,width,height) {
334 plugin.insertImage(\'<img src="\'+file+\'"' . ($this->defaultClass ? ' class="' . $this->defaultClass . '"' : '') . ' width="\'+parseInt(width)+\'" height="\'+parseInt(height)+\'" />\');
335 }
336 function launchView(url) {
337 var thePreviewWindow="";
338 thePreviewWindow = window.open("' . GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . 'show_item.php?table="+url,"ShowItem","height=300,width=410,status=0,menubar=0,resizable=0,location=0,directories=0,scrollbars=1,toolbar=0");
339 if (thePreviewWindow && thePreviewWindow.focus) {
340 thePreviewWindow.focus();
341 }
342 }
343 function getCurrentImageRef() {
344 if (plugin.image) {
345 return plugin.image;
346 } else {
347 return null;
348 }
349 }
350 function printCurrentImageOptions() {
351 var classesImage = ' . ($this->buttonConfig['properties.']['class.']['allowedClasses'] || $this->thisConfig['classesImage'] ? 'true' : 'false') . ';
352 if (classesImage) var styleSelector=\'<select id="iClass" name="iClass" style="width:140px;">' . $classesImageJSOptions . '</select>\';
353 var floatSelector=\'<select id="iFloat" name="iFloat"><option value="">' . $GLOBALS['LANG']->getLL('notSet') . '</option><option value="none">' . $GLOBALS['LANG']->getLL('nonFloating') . '</option><option value="left">' . $GLOBALS['LANG']->getLL('left') . '</option><option value="right">' . $GLOBALS['LANG']->getLL('right') . '</option></select>\';
354 if (plugin.getButton("Language")) {
355 var languageSelector = \'<select id="iLang" name="iLang">\';
356 plugin.getButton("Language").getStore().each(function (record) {
357 languageSelector +=\'<option value="\' + record.get("value") + \'">\' + record.get("text") + \'</option>\';
358 });
359 languageSelector += \'</select>\';
360 }
361 var sz="";
362 sz+=\'<form action="" name="imageData"><table class="htmlarea-window-table">\';
363 ' . (in_array('class', $removedProperties) ? '' : '
364 if(classesImage) {
365 sz+=\'<tr><td><label for="iClass">' . $GLOBALS['LANG']->getLL('class') . ': </label></td><td>\'+styleSelector+\'</td></tr>\';
366 }') . (in_array('width', $removedProperties) ? '' : '
367 if (!(selectedImageRef && selectedImageRef.src.indexOf("RTEmagic") == -1 && ' . $lockPlainWidth . ')) {
368 sz+=\'<tr><td><label for="iWidth">' . $GLOBALS['LANG']->getLL('width') . ': </label></td><td><input type="text" id="iWidth" name="iWidth" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';
369 }') . (in_array('height', $removedProperties) ? '' : '
370 if (!(selectedImageRef && selectedImageRef.src.indexOf("RTEmagic") == -1 && ' . $lockPlainHeight . ')) {
371 sz+=\'<tr><td><label for="iHeight">' . $GLOBALS['LANG']->getLL('height') . ': </label></td><td><input type="text" id="iHeight" name="iHeight" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';
372 }') . (in_array('border', $removedProperties) ? '' : '
373 sz+=\'<tr><td><label for="iBorder">' . $GLOBALS['LANG']->getLL('border') . ': </label></td><td><input type="checkbox" id="iBorder" name="iBorder" value="1" /></td></tr>\';') . (in_array('float', $removedProperties) ? '' : '
374 sz+=\'<tr><td><label for="iFloat">' . $GLOBALS['LANG']->getLL('float') . ': </label></td><td>\'+floatSelector+\'</td></tr>\';') . (in_array('paddingTop', $removedProperties) ? '' : '
375 sz+=\'<tr><td><label for="iPaddingTop">' . $GLOBALS['LANG']->getLL('padding_top') . ': </label></td><td><input type="text" id="iPaddingTop" name="iPaddingTop" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . '></td></tr>\';') . (in_array('paddingRight', $removedProperties) ? '' : '
376 sz+=\'<tr><td><label for="iPaddingRight">' . $GLOBALS['LANG']->getLL('padding_right') . ': </label></td><td><input type="text" id="iPaddingRight" name="iPaddingRight" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';') . (in_array('paddingBottom', $removedProperties) ? '' : '
377 sz+=\'<tr><td><label for="iPaddingBottom">' . $GLOBALS['LANG']->getLL('padding_bottom') . ': </label></td><td><input type="text" id="iPaddingBottom" name="iPaddingBottom" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';') . (in_array('paddingLeft', $removedProperties) ? '' : '
378 sz+=\'<tr><td><label for="iPaddingLeft">' . $GLOBALS['LANG']->getLL('padding_left') . ': </label></td><td><input type="text" id="iPaddingLeft" name="iPaddingLeft" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';') . (in_array('title', $removedProperties) ? '' : '
379 sz+=\'<tr><td><label for="iTitle">' . $GLOBALS['LANG']->getLL('title') . ': </label></td><td><input type="text" id="iTitle" name="iTitle"' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' /></td></tr>\';') . (in_array('alt', $removedProperties) ? '' : '
380 sz+=\'<tr><td><label for="iAlt">' . $GLOBALS['LANG']->getLL('alt') . ': </label></td><td><input type="text" id="iAlt" name="iAlt"' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' /></td></tr>\';') . (in_array('lang', $removedProperties) ? '' : '
381 if (plugin.getButton("Language")) {
382 sz+=\'<tr><td><label for="iLang">\' + plugin.editor.getPlugin("Language").localize(\'Language-Tooltip\') + \': </label></td><td>\' + languageSelector + \'</td></tr>\';
383 }') . (in_array('clickenlarge', $removedProperties) || in_array('data-htmlarea-clickenlarge', $removedProperties) ? '' : '
384 sz+=\'<tr><td><label for="iClickEnlarge">' . $GLOBALS['LANG']->sL('LLL:EXT:cms/locallang_ttc.xlf:image_zoom', TRUE) . ' </label></td><td><input type="checkbox" name="iClickEnlarge" id="iClickEnlarge" value="0" /></td></tr>\';') . '
385 sz+=\'<tr><td></td><td><input type="submit" value="' . $GLOBALS['LANG']->getLL('update') . '" onClick="return setImageProperties();"></td></tr>\';
386 sz+=\'</table></form>\';
387 return sz;
388 }
389 function setImageProperties() {
390 var classesImage = ' . ($this->buttonConfig['properties.']['class.']['allowedClasses'] || $this->thisConfig['classesImage'] ? 'true' : 'false') . ';
391 if (selectedImageRef) {
392 if (document.imageData.iWidth) {
393 if (document.imageData.iWidth.value && parseInt(document.imageData.iWidth.value)) {
394 selectedImageRef.style.width = "";
395 selectedImageRef.width = parseInt(document.imageData.iWidth.value);
396 }
397 }
398 if (document.imageData.iHeight) {
399 if (document.imageData.iHeight.value && parseInt(document.imageData.iHeight.value)) {
400 selectedImageRef.style.height = "";
401 selectedImageRef.height = parseInt(document.imageData.iHeight.value);
402 }
403 }
404 if (document.imageData.iPaddingTop) {
405 if (document.imageData.iPaddingTop.value != "" && !isNaN(parseInt(document.imageData.iPaddingTop.value))) {
406 selectedImageRef.style.paddingTop = parseInt(document.imageData.iPaddingTop.value) + "px";
407 } else {
408 selectedImageRef.style.paddingTop = "";
409 }
410 }
411 if (document.imageData.iPaddingRight) {
412 if (document.imageData.iPaddingRight.value != "" && !isNaN(parseInt(document.imageData.iPaddingRight.value))) {
413 selectedImageRef.style.paddingRight = parseInt(document.imageData.iPaddingRight.value) + "px";
414 } else {
415 selectedImageRef.style.paddingRight = "";
416 }
417 }
418 if (document.imageData.iPaddingBottom) {
419 if (document.imageData.iPaddingBottom.value != "" && !isNaN(parseInt(document.imageData.iPaddingBottom.value))) {
420 selectedImageRef.style.paddingBottom = parseInt(document.imageData.iPaddingBottom.value) + "px";
421 } else {
422 selectedImageRef.style.paddingBottom = "";
423 }
424 }
425 if (document.imageData.iPaddingLeft) {
426 if (document.imageData.iPaddingLeft.value != "" && !isNaN(parseInt(document.imageData.iPaddingLeft.value))) {
427 selectedImageRef.style.paddingLeft = parseInt(document.imageData.iPaddingLeft.value) + "px";
428 } else {
429 selectedImageRef.style.paddingLeft = "";
430 }
431 }
432 if (document.imageData.iTitle) {
433 selectedImageRef.title=document.imageData.iTitle.value;
434 }
435 if (document.imageData.iAlt) {
436 selectedImageRef.alt=document.imageData.iAlt.value;
437 }
438 if (document.imageData.iBorder) {
439 selectedImageRef.style.borderStyle = "";
440 selectedImageRef.style.borderWidth = "";
441 selectedImageRef.style.border = ""; // this statement ignored by Mozilla 1.3.1
442 selectedImageRef.style.borderTopStyle = "";
443 selectedImageRef.style.borderRightStyle = "";
444 selectedImageRef.style.borderBottomStyle = "";
445 selectedImageRef.style.borderLeftStyle = "";
446 selectedImageRef.style.borderTopWidth = "";
447 selectedImageRef.style.borderRightWidth = "";
448 selectedImageRef.style.borderBottomWidth = "";
449 selectedImageRef.style.borderLeftWidth = "";
450 if(document.imageData.iBorder.checked) {
451 selectedImageRef.style.borderStyle = "solid";
452 selectedImageRef.style.borderWidth = "thin";
453 }
454 selectedImageRef.removeAttribute("border");
455 }
456 if (document.imageData.iFloat) {
457 var iFloat = document.imageData.iFloat.options[document.imageData.iFloat.selectedIndex].value;
458 if (document.all) {
459 selectedImageRef.style.styleFloat = iFloat ? iFloat : "";
460 } else {
461 selectedImageRef.style.cssFloat = iFloat ? iFloat : "";
462 }
463 }
464 if (classesImage && document.imageData.iClass) {
465 var iClass;
466 if (document.imageData.iClass.options.length > 0) {
467 iClass = document.imageData.iClass.options[document.imageData.iClass.selectedIndex].value;
468 }
469 if (iClass || (selectedImageRef.attributes["class"] && selectedImageRef.attributes["class"].value)) {
470 selectedImageRef.className = iClass;
471 } else {
472 selectedImageRef.className = "";
473 }
474 }
475 if (document.imageData.iLang) {
476 var iLang = document.imageData.iLang.options[document.imageData.iLang.selectedIndex].value;
477 var languageObject = plugin.editor.getPlugin("Language");
478 if (iLang || languageObject.getLanguageAttribute(selectedImageRef)) {
479 languageObject.setLanguageAttributes(selectedImageRef, iLang);
480 } else {
481 languageObject.setLanguageAttributes(selectedImageRef, "none");
482 }
483 }
484 if (document.imageData.iClickEnlarge) {
485 if (document.imageData.iClickEnlarge.checked) {
486 selectedImageRef.setAttribute("data-htmlarea-clickenlarge","1");
487 } else {
488 selectedImageRef.removeAttribute("data-htmlarea-clickenlarge");
489 selectedImageRef.removeAttribute("clickenlarge");
490 }
491 }
492 plugin.close();
493 }
494 return false;
495 }
496 function insertImagePropertiesInForm() {
497 var classesImage = ' . ($this->buttonConfig['properties.']['class.']['allowedClasses'] || $this->thisConfig['classesImage'] ? 'true' : 'false') . ';
498 if (selectedImageRef) {
499 var styleWidth, styleHeight, padding;
500 if (document.imageData.iWidth) {
501 styleWidth = selectedImageRef.style.width ? selectedImageRef.style.width : selectedImageRef.width;
502 styleWidth = parseInt(styleWidth);
503 if (!(isNaN(styleWidth) || styleWidth == 0)) {
504 document.imageData.iWidth.value = styleWidth;
505 }
506 }
507 if (document.imageData.iHeight) {
508 styleHeight = selectedImageRef.style.height ? selectedImageRef.style.height : selectedImageRef.height;
509 styleHeight = parseInt(styleHeight);
510 if (!(isNaN(styleHeight) || styleHeight == 0)) {
511 document.imageData.iHeight.value = styleHeight;
512 }
513 }
514 if (document.imageData.iPaddingTop) {
515 var padding = selectedImageRef.style.paddingTop ? selectedImageRef.style.paddingTop : selectedImageRef.vspace;
516 var padding = parseInt(padding);
517 if (isNaN(padding) || padding <= 0) { padding = ""; }
518 document.imageData.iPaddingTop.value = padding;
519 }
520 if (document.imageData.iPaddingRight) {
521 padding = selectedImageRef.style.paddingRight ? selectedImageRef.style.paddingRight : selectedImageRef.hspace;
522 var padding = parseInt(padding);
523 if (isNaN(padding) || padding <= 0) { padding = ""; }
524 document.imageData.iPaddingRight.value = padding;
525 }
526 if (document.imageData.iPaddingBottom) {
527 var padding = selectedImageRef.style.paddingBottom ? selectedImageRef.style.paddingBottom : selectedImageRef.vspace;
528 var padding = parseInt(padding);
529 if (isNaN(padding) || padding <= 0) { padding = ""; }
530 document.imageData.iPaddingBottom.value = padding;
531 }
532 if (document.imageData.iPaddingLeft) {
533 var padding = selectedImageRef.style.paddingLeft ? selectedImageRef.style.paddingLeft : selectedImageRef.hspace;
534 var padding = parseInt(padding);
535 if (isNaN(padding) || padding <= 0) { padding = ""; }
536 document.imageData.iPaddingLeft.value = padding;
537 }
538 if (document.imageData.iTitle) {
539 document.imageData.iTitle.value = selectedImageRef.title;
540 }
541 if (document.imageData.iAlt) {
542 document.imageData.iAlt.value = selectedImageRef.alt;
543 }
544 if (document.imageData.iBorder) {
545 if((selectedImageRef.style.borderStyle && selectedImageRef.style.borderStyle != "none" && selectedImageRef.style.borderStyle != "none none none none") || selectedImageRef.border) {
546 document.imageData.iBorder.checked = 1;
547 }
548 }
549 if (document.imageData.iFloat) {
550 var fObj=document.imageData.iFloat;
551 var value = (selectedImageRef.style.cssFloat ? selectedImageRef.style.cssFloat : selectedImageRef.style.styleFloat);
552 var l=fObj.length;
553 for (var a=0;a<l;a++) {
554 if (fObj.options[a].value == value) {
555 fObj.selectedIndex = a;
556 }
557 }
558 }
559 if (classesImage && document.imageData.iClass) {
560 var fObj=document.imageData.iClass;
561 var value=selectedImageRef.className;
562 var l=fObj.length;
563 for (var a=0;a < l; a++) {
564 if (fObj.options[a].value == value) {
565 fObj.selectedIndex = a;
566 }
567 }
568 }
569 if (document.imageData.iLang) {
570 var fObj=document.imageData.iLang;
571 var value=plugin.editor.getPlugin("Language").getLanguageAttribute(selectedImageRef);
572 for (var i = 0, n = fObj.length; i < n; i++) {
573 if (fObj.options[i].value == value) {
574 fObj.selectedIndex = i;
575 if (i) {
576 fObj.options[0].text = plugin.editor.getPlugin("Language").localize("Remove language mark");
577 }
578 }
579 }
580 }
581 if (document.imageData.iClickEnlarge) {
582 if (selectedImageRef.getAttribute("data-htmlarea-clickenlarge") == "1" || selectedImageRef.getAttribute("clickenlarge") == "1") {
583 document.imageData.iClickEnlarge.checked = 1;
584 } else {
585 document.imageData.iClickEnlarge.checked = 0;
586 }
587 }
588 return false;
589 }
590 }
591
592 var selectedImageRef = getCurrentImageRef();';
593 // Setting this to a reference to the image object.
594 return $JScode;
595 }
596
597 /**
598 * Session data for this class can be set from outside with this method.
599 * Call after init()
600 *
601 * @param array $data Session data array
602 * @return array Session data and boolean which indicates that data needs to be stored in session because it's changed
603 */
604 public function processSessionData($data) {
605 $store = FALSE;
606 if ($this->act != 'image') {
607 if (isset($this->act)) {
608 $data['act'] = $this->act;
609 $store = TRUE;
610 } else {
611 $this->act = $data['act'];
612 }
613 }
614 if (isset($this->expandFolder)) {
615 $data['expandFolder'] = $this->expandFolder;
616 $store = TRUE;
617 } else {
618 $this->expandFolder = $data['expandFolder'];
619 }
620 return array($data, $store);
621 }
622
623 /**
624 * Rich Text Editor (RTE) image selector
625 *
626 * @param bool $wiz Not used here, kept for method signature compatibility with parent class
627 * @return string Modified content variable.
628 * @return string
629 */
630 public function main_rte($wiz = FALSE) {
631 // Starting content:
632 $this->content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Image', TRUE));
633
634 $this->content .= $this->doc->getTabMenuRaw($this->buildMenuArray($wiz, $this->allowedItems));
635 switch ($this->act) {
636 case 'image':
637 $JScode = '
638 document.write(printCurrentImageOptions());
639 insertImagePropertiesInForm();';
640 $this->content .= '<br />' . $this->doc->wrapScriptTags($JScode);
641 break;
642 case 'plain':
643
644 case 'magic':
645 // Create folder tree:
646 $foldertree = GeneralUtility::makeInstance(\TYPO3\CMS\Rtehtmlarea\FolderTree::class);
647 $foldertree->thisScript = $this->thisScript;
648 $tree = $foldertree->getBrowsableTree();
649 // Get currently selected folder
650 if (!$this->curUrlInfo['value'] || $this->curUrlInfo['act'] != $this->act) {
651 $cmpPath = '';
652 } else {
653 $cmpPath = $this->curUrlInfo['value'];
654 if (!isset($this->expandFolder)) {
655 $this->expandFolder = $cmpPath;
656 }
657 }
658 // Get the selected folder
659 $selectedFolder = FALSE;
660 if ($this->expandFolder) {
661 $fileOrFolderObject = NULL;
662 try {
663 $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->expandFolder);
664 } catch (\Exception $e) {
665 // No path is selected
666 }
667 if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
668 // it's a folder
669 $selectedFolder = $fileOrFolderObject;
670 } elseif ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
671 // it's a file
672 $selectedFolder = $fileOrFolderObject->getParentFolder();
673 }
674 }
675 // If no folder is selected, get the user's default upload folder
676 if (!$selectedFolder) {
677 try {
678 $selectedFolder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
679 } catch (\Exception $e) {
680 // The configured default user folder does not exist
681 }
682 }
683 // Build the file upload and folder creation form
684 $uploadForm = '';
685 $createFolder = '';
686 if ($selectedFolder) {
687 $uploadForm = $this->uploadForm($selectedFolder);
688 $createFolder = $this->createFolder($selectedFolder);
689 }
690 // Insert the upload form on top, if so configured
691 if ($GLOBALS['BE_USER']->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
692 $this->content .= $uploadForm;
693 }
694 // Render the filelist if there is a folder selected
695 $files = '';
696 if ($selectedFolder) {
697 $files = $this->TBE_expandFolder($selectedFolder, $this->act === 'plain' ? self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS : $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $GLOBALS['BE_USER']->getTSConfigVal('options.noThumbsInRTEimageSelect'));
698 }
699 // Setup filelist indexed elements:
700 $this->doc->JScode .= $this->doc->wrapScriptTags('
701 require(["TYPO3/CMS/Backend/BrowseLinks"], function(BrowseLinks) {
702 BrowseLinks.addElements(' . json_encode($this->elements) . ');
703 });');
704 // Wrap tree
705 $this->content .= '
706
707 <!--
708 Wrapper table for folder tree / file/folder list:
709 -->
710 <table border="0" cellpadding="0" cellspacing="0" id="typo3-linkFiles">
711 <tr>
712 <td class="c-wCell" valign="top">' . $this->barheader(($GLOBALS['LANG']->getLL('folderTree') . ':')) . $tree . '</td>
713 <td class="c-wCell" valign="top">' . $files . '</td>
714 </tr>
715 </table>
716 ';
717 // Add help message
718 $helpMessage = $this->getHelpMessage($this->act);
719 if ($helpMessage) {
720 $this->content .= $this->getMsgBox($helpMessage);
721 }
722 // Adding create folder + upload form if applicable
723 if (!$GLOBALS['BE_USER']->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
724 $this->content .= $uploadForm;
725 }
726 $this->content .= $createFolder;
727 $this->content .= '<br />';
728 break;
729 case 'dragdrop':
730 $foldertree = GeneralUtility::makeInstance(\TBE_FolderTree::class);
731 $foldertree->thisScript = $this->thisScript;
732 $foldertree->ext_noTempRecyclerDirs = TRUE;
733 $tree = $foldertree->getBrowsableTree();
734 // Get currently selected folder
735 if (!$this->curUrlInfo['value'] || $this->curUrlInfo['act'] != $this->act) {
736 $cmpPath = '';
737 } else {
738 $cmpPath = $this->curUrlInfo['value'];
739 if (!isset($this->expandFolder)) {
740 $this->expandFolder = $cmpPath;
741 }
742 }
743 $selectedFolder = FALSE;
744 if ($this->expandFolder) {
745 try {
746 $selectedFolder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($this->expandFolder);
747 } catch (\Exception $e) {
748 }
749 }
750 // Render the filelist if there is a folder selected
751 $files = '';
752 if ($selectedFolder) {
753 $files = $this->TBE_dragNDrop($selectedFolder, implode(',', $this->allowedFileTypes));
754 }
755 // Wrap tree
756 $this->content .= '<table border="0" cellpadding="0" cellspacing="0">
757 <tr>
758 <td style="vertical-align: top;">' . $this->barheader(($GLOBALS['LANG']->getLL('folderTree') . ':')) . $tree . '</td>
759 <td>&nbsp;</td>
760 <td style="vertical-align: top;">' . $files . '</td>
761 </tr>
762 </table>';
763 break;
764 default:
765 // Call hook
766 foreach ($this->hookObjects as $hookObject) {
767 $this->content .= $hookObject->getTab($this->act);
768 }
769 }
770 $this->content .= $this->doc->endPage();
771
772 // unset the default jumpToUrl() function
773 unset($this->doc->JScodeArray['jumpToUrl']);
774
775 $this->doc->JScodeArray['rtehtmlarea'] = $this->getJSCode($this->act, $this->editorNo, $this->sys_language_content);
776 $this->content = $this->doc->insertStylesAndJS($this->content);
777 return $this->content;
778 }
779
780 /**
781 * Returns an array definition of the top menu
782 *
783 * @param $wiz
784 * @param $allowedItems
785 * @return array
786 */
787 protected function buildMenuArray($wiz, $allowedItems) {
788 $menuDef = array();
789 if (in_array('image', $this->allowedItems) && ($this->act === 'image' || GeneralUtility::_GP('cWidth'))) {
790 $menuDef['image']['isActive'] = FALSE;
791 $menuDef['image']['label'] = $GLOBALS['LANG']->getLL('currentImage', TRUE);
792 $menuDef['image']['url'] = '#';
793 $menuDef['image']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=image&bparams=' . $this->bparams) . ');return false;"';
794 }
795 if (in_array('magic', $this->allowedItems)) {
796 $menuDef['magic']['isActive'] = FALSE;
797 $menuDef['magic']['label'] = $GLOBALS['LANG']->getLL('magicImage', TRUE);
798 $menuDef['magic']['url'] = '#';
799 $menuDef['magic']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=magic&bparams=' . $this->bparams) . ');return false;"';
800 }
801 if (in_array('plain', $this->allowedItems)) {
802 $menuDef['plain']['isActive'] = FALSE;
803 $menuDef['plain']['label'] = $GLOBALS['LANG']->getLL('plainImage', TRUE);
804 $menuDef['plain']['url'] = '#';
805 $menuDef['plain']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=plain&bparams=' . $this->bparams) . ');return false;"';
806 }
807 if (in_array('dragdrop', $this->allowedItems)) {
808 $menuDef['dragdrop']['isActive'] = FALSE;
809 $menuDef['dragdrop']['label'] = $GLOBALS['LANG']->getLL('dragDropImage', TRUE);
810 $menuDef['dragdrop']['url'] = '#';
811 $menuDef['dragdrop']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=dragdrop&bparams=' . $this->bparams) . ');return false;"';
812 }
813 // Call hook for extra options
814 foreach ($this->hookObjects as $hookObject) {
815 $menuDef = $hookObject->modifyMenuDefinition($menuDef);
816 }
817 // Order the menu items as specified in Page TSconfig
818 $menuDef = $this->orderMenuDefinition($menuDef);
819 // Set active menu item
820 reset($menuDef);
821 if ($this->act === FALSE || !in_array($this->act, $this->allowedItems)) {
822 $this->act = key($menuDef);
823 }
824 $menuDef[$this->act]['isActive'] = TRUE;
825 return $menuDef;
826 }
827
828 /**
829 * Initializes the configuration variables
830 *
831 * @return void
832 */
833 public function initConfiguration() {
834 $this->thisConfig = $this->getRTEConfig();
835 $this->buttonConfig = $this->getButtonConfig();
836 $this->imgPath = $this->getImgPath();
837 $this->defaultClass = $this->getDefaultClass();
838 $this->setMaximumPlainImageDimensions();
839 }
840
841 /**
842 * Get the path of the image to be inserted or modified
843 *
844 * @return string path to the image
845 */
846 protected function getImgPath() {
847 $RTEtsConfigParts = explode(':', $this->RTEtsConfigParams);
848 return $RTEtsConfigParts[6];
849 }
850
851 /**
852 * Get the configuration of the image button
853 *
854 * @return array the configuration array of the image button
855 */
856 protected function getButtonConfig() {
857 return is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['image.']) ? $this->thisConfig['buttons.']['image.'] : array();
858 }
859
860 /**
861 * Get the allowed items or tabs
862 *
863 * @param string $items: initial list of possible items
864 * @return array the allowed items
865 */
866 public function getAllowedItems($items) {
867 $allowedItems = explode(',', $items);
868 $clientInfo = GeneralUtility::clientInfo();
869 if ($clientInfo['BROWSER'] !== 'opera') {
870 $allowedItems[] = 'dragdrop';
871 }
872 // Call hook for extra options
873 foreach ($this->hookObjects as $hookObject) {
874 $allowedItems = $hookObject->addAllowedItems($allowedItems);
875 }
876 // Remove tab "image" if there is no current image
877 if ($this->act !== 'image') {
878 $allowedItems = array_diff($allowedItems, array('image'));
879 }
880 // Remove options according to RTE configuration
881 if (is_array($this->buttonConfig['options.']) && $this->buttonConfig['options.']['removeItems']) {
882 $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options.']['removeItems'], TRUE));
883 }
884 return $allowedItems;
885 }
886
887 /**
888 * Order the definition of menu items according to configured order
889 *
890 * @param array $menuDefinition: definition of menu items
891 * @return array ordered menu definition
892 */
893 public function orderMenuDefinition($menuDefinition) {
894 $orderedMenuDefinition = array();
895 if (is_array($this->buttonConfig['options.']) && $this->buttonConfig['options.']['orderItems']) {
896 $orderItems = GeneralUtility::trimExplode(',', $this->buttonConfig['options.']['orderItems'], TRUE);
897 $orderItems = array_intersect($orderItems, $this->allowedItems);
898 foreach ($orderItems as $item) {
899 $orderedMenuDefinition[$item] = $menuDefinition[$item];
900 }
901 } else {
902 $orderedMenuDefinition = $menuDefinition;
903 }
904 return $orderedMenuDefinition;
905 }
906
907 /**
908 * Get the default image class
909 *
910 * @return string the default class, if any
911 */
912 protected function getDefaultClass() {
913 $defaultClass = '';
914 if (is_array($this->buttonConfig['properties.'])) {
915 if (is_array($this->buttonConfig['properties.']['class.']) && trim($this->buttonConfig['properties.']['class.']['default'])) {
916 $defaultClass = trim($this->buttonConfig['properties.']['class.']['default']);
917 }
918 }
919 return $defaultClass;
920 }
921
922 /**
923 * Set variables for maximum plain image dimensions
924 *
925 * @return void
926 */
927 protected function setMaximumPlainImageDimensions() {
928 if (is_array($this->buttonConfig['options.']) && is_array($this->buttonConfig['options.']['plain.'])) {
929 if ($this->buttonConfig['options.']['plain.']['maxWidth']) {
930 $this->plainMaxWidth = $this->buttonConfig['options.']['plain.']['maxWidth'];
931 }
932 if ($this->buttonConfig['options.']['plain.']['maxHeight']) {
933 $this->plainMaxHeight = $this->buttonConfig['options.']['plain.']['maxHeight'];
934 }
935 }
936 if (!$this->plainMaxWidth) {
937 $this->plainMaxWidth = 640;
938 }
939 if (!$this->plainMaxHeight) {
940 $this->plainMaxHeight = 680;
941 }
942 }
943
944 /**
945 * Get the help message to be displayed on a given tab
946 *
947 * @param string $act: the identifier of the tab
948 * @return string the text of the message
949 */
950 public function getHelpMessage($act) {
951 switch ($act) {
952 case 'plain':
953 return sprintf($GLOBALS['LANG']->getLL('plainImage_msg'), $this->plainMaxWidth, $this->plainMaxHeight);
954 break;
955 case 'magic':
956 return sprintf($GLOBALS['LANG']->getLL('magicImage_msg'));
957 break;
958 default:
959 return '';
960 }
961 }
962
963 /**
964 * Checks if the given file is selectable in the file list.
965 *
966 * In "plain" RTE mode only image files with a maximum width and height are selectable.
967 *
968 * @param \TYPO3\CMS\Core\Resource\FileInterface $file
969 * @param array $imgInfo Image dimensions from \TYPO3\CMS\Core\Imaging\GraphicalFunctions::getImageDimensions()
970 * @return bool TRUE if file is selectable.
971 */
972 protected function fileIsSelectableInFileList(\TYPO3\CMS\Core\Resource\FileInterface $file, array $imgInfo) {
973 return (
974 $this->act !== 'plain'
975 || (
976 GeneralUtility::inList(self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS, strtolower($file->getExtension()))
977 && $imgInfo[0] <= $this->plainMaxWidth
978 && $imgInfo[1] <= $this->plainMaxHeight
979 )
980 );
981 }
982 }