TYPO3 API  SVNRelease
CObjectViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script is part of the TYPO3 project - inspiring people to share!  *
00005  *                                                                        *
00006  * TYPO3 is free software; you can redistribute it and/or modify it under *
00007  * the terms of the GNU General Public License version 2 as published by  *
00008  * the Free Software Foundation.                                          *
00009  *                                                                        *
00010  * This script is distributed in the hope that it will be useful, but     *
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00012  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
00013  * Public License for more details.                                       *
00014  *                                                                        */
00015 
00016 /**
00017  * This ViewHelper renders CObjects from the global TypoScript configuration.
00018  *
00019  * = Examples =
00020  *
00021  * <code title="Render lib object">
00022  * <f:cObject typoscriptObjectPath="lib.someLibObject" />
00023  * </code>
00024  * <output>
00025  * // rendered lib.someLibObject
00026  * </output>
00027  *
00028  * <code title="Specify cObject data & current value">
00029  * <f:cObject typoscriptObjectPath="lib.customHeader" data="{article}" current="{article.title}" />
00030  * </code>
00031  * <output>
00032  * // rendered lib.customHeader. data and current value will be available in TypoScript
00033  * </output>
00034  *
00035  * <code title="inline notation">
00036  * {article -> f:cObject(typoscriptObjectPath: 'lib.customHeader')}
00037  * </code>
00038  * <output>
00039  * // rendered lib.customHeader. data will be available in TypoScript
00040  * </output>
00041  *
00042  */
00043 class Tx_Fluid_ViewHelpers_CObjectViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00044 
00045     /**
00046      * @var tslib_cObj
00047      */
00048     protected $contentObject;
00049 
00050     /**
00051      * @var array
00052      */
00053     protected $typoScriptSetup;
00054 
00055     /**
00056      * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
00057      */
00058     protected $tsfeBackup;
00059 
00060     /**
00061      * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
00062      */
00063     protected $configurationManager;
00064 
00065     /**
00066      * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
00067      * @return void
00068      */
00069     public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
00070         $this->configurationManager = $configurationManager;
00071         $this->contentObject = $this->configurationManager->getContentObject();
00072         $this->typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
00073     }
00074 
00075     /**
00076      * Renders the TypoScript object in the given TypoScript setup path.
00077      *
00078      * @param string $typoscriptObjectPath the TypoScript setup path of the TypoScript object to render
00079      * @param mixed $data the data to be used for rendering the cObject. Can be an object, array or string. If this argument is not set, child nodes will be used
00080      * @param string $currentValueKey
00081      * @return string the content of the rendered TypoScript object
00082      * @author Bastian Waidelich <bastian@typo3.org>
00083      * @author Niels Pardon <mail@niels-pardon.de>
00084      */
00085     public function render($typoscriptObjectPath, $data = NULL, $currentValueKey = NULL) {
00086         if (TYPO3_MODE === 'BE') {
00087             $this->simulateFrontendEnvironment();
00088         }
00089 
00090         if ($data === NULL) {
00091             $data = $this->renderChildren();
00092         }
00093         $currentValue = NULL;
00094         if (is_object($data)) {
00095             $data = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($data);
00096         } elseif (is_string($data)) {
00097             $currentValue = $data;
00098             $data = array($data);
00099         }
00100         $this->contentObject->start($data);
00101         if ($currentValue !== NULL) {
00102             $this->contentObject->setCurrentVal($currentValue);
00103         } elseif ($currentValueKey !== NULL && isset($data[$currentValueKey])) {
00104             $this->contentObject->setCurrentVal($data[$currentValueKey]);
00105         }
00106 
00107         $pathSegments = t3lib_div::trimExplode('.', $typoscriptObjectPath);
00108         $lastSegment = array_pop($pathSegments);
00109         $setup = $this->typoScriptSetup;
00110         foreach ($pathSegments as $segment) {
00111             if (!array_key_exists($segment . '.', $setup)) {
00112                 throw new Tx_Fluid_Core_ViewHelper_Exception('TypoScript object path "' . htmlspecialchars($typoscriptObjectPath) . '" does not exist' , 1253191023);
00113             }
00114             $setup = $setup[$segment . '.'];
00115         }
00116         $content = $this->contentObject->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.']);
00117 
00118         if (TYPO3_MODE === 'BE') {
00119             $this->resetFrontendEnvironment();
00120         }
00121 
00122         return $content;
00123     }
00124 
00125     /**
00126      * Sets the $TSFE->cObjectDepthCounter in Backend mode
00127      * This somewhat hacky work around is currently needed because the cObjGetSingle() function of tslib_cObj relies on this setting
00128      *
00129      * @return void
00130      * @author Bastian Waidelich <bastian@typo3.org>
00131      */
00132     protected function simulateFrontendEnvironment() {
00133         $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
00134         $GLOBALS['TSFE'] = new stdClass();
00135         $GLOBALS['TSFE']->cObjectDepthCounter = 100;
00136     }
00137 
00138     /**
00139      * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
00140      *
00141      * @return void
00142      * @author Bastian Waidelich <bastian@typo3.org>
00143      * @see simulateFrontendEnvironment()
00144      */
00145     protected function resetFrontendEnvironment() {
00146         $GLOBALS['TSFE'] = $this->tsfeBackup;
00147     }
00148 }
00149 
00150 ?>