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