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