[CLEANUP] main_rte function of the ElementBrowser
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / SelectImage.php
index fe61014..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;
@@ -100,44 +81,27 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
        public function init() {
                $this->initVariables();
                $this->initConfiguration();
-               $this->initHookObjects();
-               // init fileProcessor
-               $this->fileProcessor = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\BasicFileUtility');
-               $this->fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
+               $this->initHookObjects('ext/rtehtmlarea/mod4/class.tx_rtehtmlarea_select_image.php');
+
                $this->allowedItems = $this->getAllowedItems('magic,plain,image');
                $this->insertImage();
-               // Creating backend template object:
-               $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
-               // Apply the same styles as those of the base script
-               $this->doc->bodyTagId = 'typo3-browse-links-php';
-               $this->doc->bodyTagAdditions = $this->getBodyTagAdditions();
-               $this->doc->backPath = $GLOBALS['BACK_PATH'];
-               // Load the Prototype library and browse_links.js
-               $this->doc->getPageRenderer()->loadPrototype();
-               $this->doc->loadJavascriptLib('js/tree.js');
-               $this->doc->getPageRenderer()->addInlineSetting('Tree.SC_alt_db_navframe', 'ajaxUrl', BackendUtility::getAjaxUrl('SC_alt_db_navframe::expandCollapse'));
-               $this->doc->getPageRenderer()->addInlineSetting('Tree.SC_alt_file_navframe', 'ajaxUrl', BackendUtility::getAjaxUrl('SC_alt_file_navframe::expandCollapse'));
-               $this->doc->loadJavascriptLib('js/browse_links.js');
-               $this->doc->JScode .= $this->doc->wrapScriptTags('
-                       Tree.ajaxID = "SC_alt_file_navframe::expandCollapse";
-               ');
-               $this->doc->getPageRenderer()->addCssFile($this->doc->backPath . \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('t3skin') . 'rtehtmlarea/htmlarea.css');
-               $this->doc->getContextMenuCode();
+
+               $this->initDocumentTemplate();
        }
 
        /**
         * Initialize class variables
         *
-        * @return      void
+        * @return void
         */
        public function initVariables() {
+               parent::initVariables();
                // Get "act"
                $this->act = GeneralUtility::_GP('act');
                if (!$this->act) {
                        $this->act = FALSE;
                }
                // Process bparams
-               $this->bparams = GeneralUtility::_GP('bparams');
                $pArr = explode('|', $this->bparams);
                $pRteArr = explode(':', $pArr[1]);
                $this->editorNo = $pRteArr[0];
@@ -148,8 +112,6 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                        $this->sys_language_content = GeneralUtility::_GP('sys_language_content');
                        $this->RTEtsConfigParams = GeneralUtility::_GP('RTEtsConfigParams');
                }
-               $this->expandPage = GeneralUtility::_GP('expandPage');
-               $this->expandFolder = GeneralUtility::_GP('expandFolder');
                $pArr[1] = implode(':', array($this->editorNo, $this->sys_language_content));
                $pArr[2] = $this->RTEtsConfigParams;
                if ($this->act == 'dragdrop' || $this->act == 'plain') {
@@ -157,34 +119,24 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                }
                $pArr[3] = implode(',', $this->allowedFileTypes);
                $this->bparams = implode('|', $pArr);
-               // Find "mode"
-               $this->mode = GeneralUtility::_GP('mode');
-               if (!$this->mode) {
-                       $this->mode = 'rte';
-               }
-               // Site URL
-               $this->siteURL = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
-               $this->determineScriptUrl();
        }
 
        /**
-        * Initialize hook objects implementing the hook interface
+        * Initialize document template object
         *
-        * @return      void
-        * @throws \UnexpectedValueException
+        * @return void
         */
-       protected function initHookObjects() {
-               if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/rtehtmlarea/mod4/class.tx_rtehtmlarea_select_image.php']['browseLinksHook'])) {
-                       foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/rtehtmlarea/mod4/class.tx_rtehtmlarea_select_image.php']['browseLinksHook'] as $classData) {
-                               $processObject = GeneralUtility::getUserObj($classData);
-                               if (!$processObject instanceof \TYPO3\CMS\Core\ElementBrowser\ElementBrowserHookInterface) {
-                                       throw new \UnexpectedValueException('$processObject must implement interface TYPO3\\CMS\\Core\\ElementBrowser\\ElementBrowserHookInterface', 1195115652);
-                               }
-                               $parameters = array();
-                               $processObject->init($this, $parameters);
-                               $this->hookObjects[] = $processObject;
-                       }
-               }
+       protected function initDocumentTemplate() {
+               parent::initDocumentTemplate();
+
+               $this->doc->bodyTagId = 'typo3-browse-links-php';
+               $this->doc->bodyTagAdditions = $this->getBodyTagAdditions();
+
+               $this->doc->JScode .= $this->doc->wrapScriptTags('
+                       Tree.ajaxID = "SC_alt_file_navframe::expandCollapse";
+               ');
+               $this->doc->getPageRenderer()->addCssFile($this->doc->backPath . \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('t3skin') . 'rtehtmlarea/htmlarea.css');
+               $this->doc->getContextMenuCode();
        }
 
        /**
@@ -220,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 . '"');
@@ -254,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();
@@ -680,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 = '
@@ -751,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 = '';
@@ -852,12 +775,64 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                                }
                }
                $this->content .= $this->doc->endPage();
+
+               // unset the default jumpToUrl() function
+               unset($this->doc->JScodeArray['jumpToUrl']);
+
                $this->doc->JScodeArray['rtehtmlarea'] = $this->getJSCode($this->act, $this->editorNo, $this->sys_language_content);
                $this->content = $this->doc->insertStylesAndJS($this->content);
                return $this->content;
        }
 
        /**
+        * 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
@@ -867,18 +842,7 @@ class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
                $this->buttonConfig = $this->getButtonConfig();
                $this->imgPath = $this->getImgPath();
                $this->defaultClass = $this->getDefaultClass();
-               $this->setMaximumImageDimensions();
-       }
-
-       /**
-        * Get the RTE configuration from Page TSConfig
-        *
-        * @return      array           RTE configuration array
-        */
-       protected function getRTEConfig() {
-               $RTEtsConfigParts = explode(':', $this->RTEtsConfigParams);
-               $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
-               return \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
+               $this->setMaximumPlainImageDimensions();
        }
 
        /**
@@ -903,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);
@@ -963,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'];
@@ -982,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;
-               }
        }
 
        /**