TYPO3 API  SVNRelease
FrontendConfigurationManager.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * A general purpose configuration manager used in frontend mode.
00027  *
00028  * Should NOT be singleton, as a new configuration manager is needed per plugin.
00029  *
00030  * @package Extbase
00031  * @subpackage Configuration
00032  * @version $ID:$
00033  */
00034 class Tx_Extbase_Configuration_FrontendConfigurationManager extends Tx_Extbase_Configuration_AbstractConfigurationManager {
00035 
00036     /**
00037      * Returns TypoScript Setup array from current Environment.
00038      *
00039      * @return array the raw TypoScript setup
00040      */
00041     public function getTypoScriptSetup() {
00042         return $GLOBALS['TSFE']->tmpl->setup;
00043     }
00044 
00045     /**
00046      * Returns the TypoScript configuration found in plugin.tx_yourextension_yourplugin
00047      * merged with the global configuration of your extension from plugin.tx_yourextension
00048      *
00049      * @param string $extensionName
00050      * @param string $pluginName
00051      * @return array
00052      */
00053     protected function getPluginConfiguration($extensionName, $pluginName) {
00054         $setup = $this->getTypoScriptSetup();
00055         $pluginConfiguration = array();
00056         if (is_array($setup['plugin.']['tx_' . strtolower($extensionName) . '.'])) {
00057             $pluginConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['plugin.']['tx_' . strtolower($extensionName) . '.']);
00058         }
00059         $pluginSignature = strtolower($extensionName . '_' . $pluginName);
00060         if (is_array($setup['plugin.']['tx_' . $pluginSignature . '.'])) {
00061             $pluginConfiguration = t3lib_div::array_merge_recursive_overrule($pluginConfiguration, Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($setup['plugin.']['tx_' . $pluginSignature . '.']));
00062         }
00063         return $pluginConfiguration;
00064     }
00065 
00066     /**
00067      * Returns the configured controller/action pairs of the specified plugin in the format
00068      * array(
00069      *  'Controller1' => array('action1', 'action2'),
00070      *  'Controller2' => array('action3', 'action4')
00071      * )
00072      *
00073      * @param string $extensionName
00074      * @param string $pluginName
00075      * @return array
00076      */
00077     protected function getSwitchableControllerActions($extensionName, $pluginName) {
00078         $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'];
00079         if (!is_array($switchableControllerActions)) {
00080             $switchableControllerActions = array();
00081         }
00082         return $switchableControllerActions;
00083     }
00084 
00085     /**
00086      * Get context specific framework configuration.
00087      * - Overrides storage PID with setting "Startingpoint"
00088      * - merge flexform configuration, if needed
00089      *
00090      * @param array $frameworkConfiguration The framework configuration to modify
00091      * @return array the modified framework configuration
00092      */
00093     protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration) {
00094         $frameworkConfiguration = $this->overrideStoragePidIfStartingPointIsSet($frameworkConfiguration);
00095         $frameworkConfiguration = $this->overrideConfigurationFromPlugin($frameworkConfiguration);
00096         $frameworkConfiguration = $this->overrideConfigurationFromFlexform($frameworkConfiguration);
00097 
00098         return $frameworkConfiguration;
00099     }
00100 
00101     /**
00102      * Overrides the storage PID settings, in case the "Startingpoint" settings
00103      * is set in the plugin configuration.
00104      *
00105      * @param array $frameworkConfiguration the framework configurations
00106      * @return array the framework configuration with overriden storagePid
00107      */
00108     protected function overrideStoragePidIfStartingPointIsSet(array $frameworkConfiguration) {
00109         $pages = $this->contentObject->data['pages'];
00110         if (is_string($pages) && strlen($pages) > 0) {
00111             $list = array();
00112             if($this->contentObject->data['recursive'] > 0) {
00113                 $explodedPages = t3lib_div::trimExplode(',', $pages);
00114                 foreach($explodedPages as $pid) {
00115                     $list[] = trim($this->contentObject->getTreeList($pid, $this->contentObject->data['recursive']), ',');
00116                 }
00117             }
00118             if (count($list) > 0) {
00119                 $pages = $pages . ',' . implode(',', $list);
00120             }
00121             $frameworkConfiguration = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration, array(
00122                 'persistence' => array(
00123                     'storagePid' => $pages
00124                 )
00125             ));
00126         }
00127         return $frameworkConfiguration;
00128     }
00129 
00130     /**
00131      * Overrides configuration settings from the plugin typoscript (plugin.tx_myext_pi1.)
00132      *
00133      * @param array the framework configuration
00134      * @return array the framework configuration with overridden data from typoscript
00135      */
00136     protected function overrideConfigurationFromPlugin(array $frameworkConfiguration) {
00137         $setup = $this->getTypoScriptSetup();
00138         $pluginSignature = strtolower($frameworkConfiguration['extensionName'] . '_' . $frameworkConfiguration['pluginName']);
00139         $pluginConfiguration = $setup['plugin.']['tx_' . $pluginSignature . '.'];
00140         if (is_array($pluginConfiguration)) {
00141             $pluginConfiguration = Tx_Extbase_Utility_TypoScript::convertTypoScriptArrayToPlainArray($pluginConfiguration);
00142             $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'settings');
00143             $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'persistence');
00144             $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $pluginConfiguration, 'view');
00145         }
00146         return $frameworkConfiguration;
00147     }
00148 
00149     /**
00150      * Overrides configuration settings from flexforms.
00151      * This merges the whole flexform data, and overrides switchable controller actions.
00152      *
00153      * @param array the framework configuration
00154      * @return array the framework configuration with overridden data from flexform
00155      */
00156     protected function overrideConfigurationFromFlexform(array $frameworkConfiguration) {
00157         if (strlen($this->contentObject->data['pi_flexform']) > 0) {
00158             $flexformConfiguration = $this->convertFlexformContentToArray($this->contentObject->data['pi_flexform']);
00159 
00160             $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'settings');
00161             $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'persistence');
00162             $frameworkConfiguration = $this->mergeConfigurationIntoFrameworkConfiguration($frameworkConfiguration, $flexformConfiguration, 'view');
00163 
00164             $frameworkConfiguration = $this->overrideSwitchableControllerActionsFromFlexform($frameworkConfiguration, $flexformConfiguration);
00165         }
00166         return $frameworkConfiguration;
00167     }
00168 
00169     /**
00170      * Parses the FlexForm content and converts it to an array
00171      * The resulting array will be multi-dimensional, as a value "bla.blubb"
00172      * results in two levels, and a value "bla.blubb.bla" results in three levels.
00173      *
00174      * Note: multi-language FlexForms are not supported yet
00175      *
00176      * @param string $flexFormContent FlexForm xml string
00177      * @return array the processed array
00178      */
00179     protected function convertFlexformContentToArray($flexFormContent) {
00180         $settings = array();
00181         $languagePointer = 'lDEF';
00182         $valuePointer = 'vDEF';
00183 
00184         $flexFormArray = t3lib_div::xml2array($flexFormContent);
00185         $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : array();
00186         foreach(array_values($flexFormArray) as $languages) {
00187             if (!is_array($languages[$languagePointer])) {
00188                 continue;
00189             }
00190 
00191             foreach($languages[$languagePointer] as $valueKey => $valueDefinition) {
00192                 if (strpos($valueKey, '.') === false) {
00193                     $settings[$valueKey] = $this->walkFlexformNode($valueDefinition, $valuePointer);
00194                 } else {
00195                     $valueKeyParts = explode('.', $valueKey);
00196                     $currentNode =& $settings;
00197                     foreach ($valueKeyParts as $valueKeyPart) {
00198                         $currentNode =& $currentNode[$valueKeyPart];
00199                     }
00200                     if (is_array($valueDefinition)) {
00201                         if (array_key_exists($valuePointer, $valueDefinition)) {
00202                             $currentNode = $valueDefinition[$valuePointer];
00203                         } else {
00204                             $currentNode = $this->walkFlexformNode($valueDefinition, $valuePointer);
00205                         }
00206                     } else {
00207                         $currentNode = $valueDefinition;
00208                     }
00209                 }
00210             }
00211         }
00212         return $settings;
00213     }
00214 
00215     /**
00216      * Parses a flexform node recursively and takes care of sections etc
00217      * @param array $nodeArray The flexform node to parse
00218      * @param string $valuePointer The valuePointer to use for value retrieval
00219      */
00220     protected function walkFlexformNode($nodeArray, $valuePointer = 'vDEF') {
00221         if (is_array($nodeArray)) {
00222             $return = array();
00223 
00224             foreach ($nodeArray as $nodeKey => $nodeValue) {
00225                 if ($nodeKey === $valuePointer) {
00226                     return $nodeValue;  
00227                 }
00228 
00229                 if (in_array($nodeKey, array('el', '_arrayContainer'))) {
00230                     return $this->walkFlexformNode($nodeValue, $valuePointer);
00231                 }
00232 
00233                 if (substr($nodeKey, 0, 1) === '_') {
00234                     continue;
00235                 }
00236 
00237                 if (strpos($nodeKey, '.')) {
00238                     $nodeKeyParts = explode('.', $nodeKey);
00239                     $currentNode =& $return;
00240                     for ($i = 0; $i < count($nodeKeyParts) - 1; $i++) {
00241                         $currentNode =& $currentNode[$nodeKeyParts[$i]];
00242                     }
00243                     $newNode = array(next($nodeKeyParts) => $nodeValue);
00244                     $currentNode = $this->walkFlexformNode($newNode, $valuePointer);
00245                 } else if (is_array($nodeValue)) {
00246                     if (array_key_exists($valuePointer, $nodeValue)) {
00247                         $return[$nodeKey] = $nodeValue[$valuePointer];
00248                     } else {
00249                         $return[$nodeKey] = $this->walkFlexformNode($nodeValue, $valuePointer);
00250                     }
00251                 } else {
00252                     $return[$nodeKey] = $nodeValue;
00253                 }
00254             }
00255             return $return;
00256         }
00257 
00258         return $nodeArray;
00259     }
00260 
00261     /**
00262      * Merge a configuration into the framework configuration.
00263      *
00264      * @param array $frameworkConfiguration the framework configuration to merge the data on
00265      * @param array $configuration The configuration
00266      * @param string $configurationPartName The name of the configuration part which should be merged.
00267      * @return array the processed framework configuration
00268      */
00269     protected function mergeConfigurationIntoFrameworkConfiguration(array $frameworkConfiguration, array $configuration, $configurationPartName) {
00270         if (is_array($frameworkConfiguration[$configurationPartName]) && is_array($configuration[$configurationPartName])) {
00271             $frameworkConfiguration[$configurationPartName] = t3lib_div::array_merge_recursive_overrule($frameworkConfiguration[$configurationPartName], $configuration[$configurationPartName]);
00272         }
00273         return $frameworkConfiguration;
00274     }
00275 
00276     /**
00277      * Overrides the switchable controller actions from the flexform.
00278      *
00279      * @param array $frameworkConfiguration The original framework configuration
00280      * @param array $flexformConfiguration The full flexform configuration
00281      * @return array the modified framework configuration, if needed
00282      */
00283     protected function overrideSwitchableControllerActionsFromFlexform(array $frameworkConfiguration, array $flexformConfiguration) {
00284         if (!isset($flexformConfiguration['switchableControllerActions']) || is_array($flexformConfiguration['switchableControllerActions'])) {
00285             return $frameworkConfiguration;
00286         }
00287 
00288             // As "," is the flexform field value delimiter, we need to use ";" as in-field delimiter. That's why we need to replace ; by  , first.
00289             // The expected format is: "Controller1->action2;Controller2->action3;Controller2->action1"
00290         $switchableControllerActionPartsFromFlexform = t3lib_div::trimExplode(',', str_replace(';', ',', $flexformConfiguration['switchableControllerActions']), TRUE);
00291 
00292         $newSwitchableControllerActionsFromFlexform = array();
00293         foreach ($switchableControllerActionPartsFromFlexform as $switchableControllerActionPartFromFlexform) {
00294             list($controller, $action) = t3lib_div::trimExplode('->', $switchableControllerActionPartFromFlexform);
00295             if (empty($controller) || empty($action)) {
00296                 throw new Tx_Extbase_Configuration_Exception_ParseError('Controller or action were empty when overriding switchableControllerActions from flexform.', 1257146403);
00297             }
00298             $newSwitchableControllerActionsFromFlexform[$controller][] = $action;
00299         }
00300         if (count($newSwitchableControllerActionsFromFlexform) > 0) {
00301             $this->overrideSwitchableControllerActions($frameworkConfiguration, $newSwitchableControllerActionsFromFlexform);
00302         }
00303         return $frameworkConfiguration;
00304     }
00305 }
00306 ?>