[CLEANUP] main_rte function of the ElementBrowser
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / SelectImage.php
index af9622e..895c92a 100644 (file)
@@ -1,34 +1,19 @@
 <?php
 namespace TYPO3\CMS\Rtehtmlarea;
 
-/***************************************************************
- *  Copyright notice
- *
- *  (c) 1999-2013 Kasper Skårhøj (kasper@typo3.com)
- *  (c) 2004-2013 Stanislas Rolland <typo3(arobas)jbr.ca>
- *  All rights reserved
- *
- *  This script is part of the TYPO3 project. The TYPO3 project is
- *  free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  The GNU General Public License can be found at
- *  http://www.gnu.org/copyleft/gpl.html.
- *  A copy is found in the text file GPL.txt and important notices to the license
- *  from the author is found in LICENSE.txt distributed with these scripts.
+/**
+ * This file is part of the TYPO3 CMS project.
  *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
  *
- *  This script is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
  *
- *  This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
+ * The TYPO3 project - inspiring people to share!
+ */
 
-use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Resource;
 
@@ -76,10 +61,6 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
         */
        protected $plainMaxHeight;
 
-       protected $magicMaxWidth;
-
-       protected $magicMaxHeight;
-
        protected $imgPath;
 
        public $editorNo;
@@ -191,7 +172,7 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                        $fileObject = Resource\ResourceFactory::getInstance()->getFileObject($uid);
                        // Get default values for alt and title attributes from file properties
                        $altText = $fileObject->getProperty('alternative');
-                       $titleText = $fileObject->getProperty('name');
+                       $titleText = $fileObject->getProperty('title');
                        switch ($this->act) {
                                case 'magic':
                                        $this->insertMagicImage($fileObject, $altText, $titleText, 'data-htmlarea-file-uid="' . $uid . '" data-htmlarea-file-table="' . $table . '"');
@@ -225,12 +206,11 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                // Create the magic image service
                /** @var $magicImageService Resource\Service\MagicImageService */
                $magicImageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\MagicImageService');
+               $magicImageService->setMagicImageMaximumDimensions($this->thisConfig);
                // Create the magic image
                $imageConfiguration = array(
                        'width' => GeneralUtility::_GP('cWidth'),
-                       'height' => GeneralUtility::_GP('cHeight'),
-                       'maxW' => $this->magicMaxWidth,
-                       'maxH' => $this->magicMaxHeight
+                       'height' => GeneralUtility::_GP('cHeight')
                );
                $magicImage = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
                $imageUrl = $magicImage->getPublicUrl();
@@ -651,51 +631,17 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
        }
 
        /**
+        * Rich Text Editor (RTE) image selector
+        *
+        * @param boolean $wiz Not used here, kept for method signature compatibility with parent class
+        * @return string Modified content variable.
         * @return string
-        * @todo Define visibility
         */
-       public function main_rte() {
+       public function main_rte($wiz = FALSE) {
                // Starting content:
                $this->content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Image', TRUE));
-               // Making menu in top:
-               $menuDef = array();
-               if (in_array('image', $this->allowedItems) && ($this->act === 'image' || GeneralUtility::_GP('cWidth'))) {
-                       $menuDef['image']['isActive'] = FALSE;
-                       $menuDef['image']['label'] = $GLOBALS['LANG']->getLL('currentImage', TRUE);
-                       $menuDef['image']['url'] = '#';
-                       $menuDef['image']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=image&bparams=' . $this->bparams) . ');return false;"';
-               }
-               if (in_array('magic', $this->allowedItems)) {
-                       $menuDef['magic']['isActive'] = FALSE;
-                       $menuDef['magic']['label'] = $GLOBALS['LANG']->getLL('magicImage', TRUE);
-                       $menuDef['magic']['url'] = '#';
-                       $menuDef['magic']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=magic&bparams=' . $this->bparams) . ');return false;"';
-               }
-               if (in_array('plain', $this->allowedItems)) {
-                       $menuDef['plain']['isActive'] = FALSE;
-                       $menuDef['plain']['label'] = $GLOBALS['LANG']->getLL('plainImage', TRUE);
-                       $menuDef['plain']['url'] = '#';
-                       $menuDef['plain']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=plain&bparams=' . $this->bparams) . ');return false;"';
-               }
-               if (in_array('dragdrop', $this->allowedItems)) {
-                       $menuDef['dragdrop']['isActive'] = FALSE;
-                       $menuDef['dragdrop']['label'] = $GLOBALS['LANG']->getLL('dragDropImage', TRUE);
-                       $menuDef['dragdrop']['url'] = '#';
-                       $menuDef['dragdrop']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=dragdrop&bparams=' . $this->bparams) . ');return false;"';
-               }
-               // Call hook for extra options
-               foreach ($this->hookObjects as $hookObject) {
-                       $menuDef = $hookObject->modifyMenuDefinition($menuDef);
-               }
-               // Order the menu items as specified in Page TSconfig
-               $menuDef = $this->orderMenuDefinition($menuDef);
-               // Set active menu item
-               reset($menuDef);
-               if ($this->act === FALSE || !in_array($this->act, $this->allowedItems)) {
-                       $this->act = key($menuDef);
-               }
-               $menuDef[$this->act]['isActive'] = TRUE;
-               $this->content .= $this->doc->getTabMenuRaw($menuDef);
+
+               $this->content .= $this->doc->getTabMenuRaw($this->buildMenuArray($wiz, $this->allowedItems));
                switch ($this->act) {
                        case 'image':
                                $JScode = '
@@ -722,21 +668,27 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                                // Get the selected folder
                                $selectedFolder = FALSE;
                                if ($this->expandFolder) {
-                                       $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->expandFolder);
+                                       $fileOrFolderObject = NULL;
+                                       try {
+                                               $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->expandFolder);
+                                       } catch (\Exception $e) {
+                                               // No path is selected
+                                       }
                                        if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
                                                // it's a folder
                                                $selectedFolder = $fileOrFolderObject;
                                        } elseif ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
                                                // it's a file
-                                               // @todo: find the parent folder, right now done a bit ugly, because the file does not
-                                               // support finding the parent folder of a file on purpose
-                                               $folderIdentifier = dirname($fileOrFolderObject->getIdentifier());
-                                               $selectedFolder = $fileOrFolderObject->getStorage()->getFolder($folderIdentifier);
+                                               $selectedFolder = $fileOrFolderObject->getParentFolder();
                                        }
                                }
                                // If no folder is selected, get the user's default upload folder
                                if (!$selectedFolder) {
-                                       $selectedFolder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
+                                       try {
+                                               $selectedFolder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
+                                       } catch (\Exception $e) {
+                                               // The configured default user folder does not exist
+                                       }
                                }
                                // Build the file upload and folder creation form
                                $uploadForm = '';
@@ -833,6 +785,54 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
        }
 
        /**
+        * Returns an array definition of the top menu
+        *
+        * @param $wiz
+        * @param $allowedItems
+        * @return array
+        */
+       protected function buildMenuArray($wiz, $allowedItems) {
+               $menuDef = array();
+               if (in_array('image', $this->allowedItems) && ($this->act === 'image' || GeneralUtility::_GP('cWidth'))) {
+                       $menuDef['image']['isActive'] = FALSE;
+                       $menuDef['image']['label'] = $GLOBALS['LANG']->getLL('currentImage', TRUE);
+                       $menuDef['image']['url'] = '#';
+                       $menuDef['image']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=image&bparams=' . $this->bparams) . ');return false;"';
+               }
+               if (in_array('magic', $this->allowedItems)) {
+                       $menuDef['magic']['isActive'] = FALSE;
+                       $menuDef['magic']['label'] = $GLOBALS['LANG']->getLL('magicImage', TRUE);
+                       $menuDef['magic']['url'] = '#';
+                       $menuDef['magic']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=magic&bparams=' . $this->bparams) . ');return false;"';
+               }
+               if (in_array('plain', $this->allowedItems)) {
+                       $menuDef['plain']['isActive'] = FALSE;
+                       $menuDef['plain']['label'] = $GLOBALS['LANG']->getLL('plainImage', TRUE);
+                       $menuDef['plain']['url'] = '#';
+                       $menuDef['plain']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=plain&bparams=' . $this->bparams) . ');return false;"';
+               }
+               if (in_array('dragdrop', $this->allowedItems)) {
+                       $menuDef['dragdrop']['isActive'] = FALSE;
+                       $menuDef['dragdrop']['label'] = $GLOBALS['LANG']->getLL('dragDropImage', TRUE);
+                       $menuDef['dragdrop']['url'] = '#';
+                       $menuDef['dragdrop']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=dragdrop&bparams=' . $this->bparams) . ');return false;"';
+               }
+               // Call hook for extra options
+               foreach ($this->hookObjects as $hookObject) {
+                       $menuDef = $hookObject->modifyMenuDefinition($menuDef);
+               }
+               // Order the menu items as specified in Page TSconfig
+               $menuDef = $this->orderMenuDefinition($menuDef);
+               // Set active menu item
+               reset($menuDef);
+               if ($this->act === FALSE || !in_array($this->act, $this->allowedItems)) {
+                       $this->act = key($menuDef);
+               }
+               $menuDef[$this->act]['isActive'] = TRUE;
+               return $menuDef;
+       }
+
+       /**
         * Initializes the configuration variables
         *
         * @return      void
@@ -842,7 +842,7 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                $this->buttonConfig = $this->getButtonConfig();
                $this->imgPath = $this->getImgPath();
                $this->defaultClass = $this->getDefaultClass();
-               $this->setMaximumImageDimensions();
+               $this->setMaximumPlainImageDimensions();
        }
 
        /**
@@ -867,8 +867,8 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
        /**
         * Get the allowed items or tabs
         *
-        * @param       string          $items: initial list of possible items
-        * @return      array           the allowed items
+        * @param string $items: initial list of possible items
+        * @return array the allowed items
         */
        public function getAllowedItems($items) {
                $allowedItems = explode(',', $items);
@@ -927,11 +927,11 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
        }
 
        /**
-        * Set variables for maximum image dimensions
+        * Set variables for maximum plain image dimensions
         *
         * @return      void
         */
-       protected function setMaximumImageDimensions() {
+       protected function setMaximumPlainImageDimensions() {
                if (is_array($this->buttonConfig['options.']) && is_array($this->buttonConfig['options.']['plain.'])) {
                        if ($this->buttonConfig['options.']['plain.']['maxWidth']) {
                                $this->plainMaxWidth = $this->buttonConfig['options.']['plain.']['maxWidth'];
@@ -946,21 +946,6 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                if (!$this->plainMaxHeight) {
                        $this->plainMaxHeight = 680;
                }
-               if (is_array($this->buttonConfig['options.']) && is_array($this->buttonConfig['options.']['magic.'])) {
-                       if ($this->buttonConfig['options.']['magic.']['maxWidth']) {
-                               $this->magicMaxWidth = $this->buttonConfig['options.']['magic.']['maxWidth'];
-                       }
-                       if ($this->buttonConfig['options.']['magic.']['maxHeight']) {
-                               $this->magicMaxHeight = $this->buttonConfig['options.']['magic.']['maxHeight'];
-                       }
-               }
-               // These defaults allow images to be based on their width - to a certain degree - by setting a high height. Then we're almost certain the image will be based on the width
-               if (!$this->magicMaxWidth) {
-                       $this->magicMaxWidth = 300;
-               }
-               if (!$this->magicMaxHeight) {
-                       $this->magicMaxHeight = 1000;
-               }
        }
 
        /**