TYPO3 API  SVNRelease
AbstractWidgetViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 declare(ENCODING = 'utf-8') ;
00003 
00004 /*                                                                        *
00005  * This script belongs to the FLOW3 package "Fluid".                      *
00006  *                                                                        *
00007  * It is free software; you can redistribute it and/or modify it under    *
00008  * the terms of the GNU Lesser General Public License as published by the *
00009  * Free Software Foundation, either version 3 of the License, or (at your *
00010  * option) any later version.                                             *
00011  *                                                                        *
00012  * This script is distributed in the hope that it will be useful, but     *
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00014  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00015  * General Public License for more details.                               *
00016  *                                                                        *
00017  * You should have received a copy of the GNU Lesser General Public       *
00018  * License along with the script.                                         *
00019  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00020  *                                                                        *
00021  * The TYPO3 project - inspiring people to share!                         *
00022  *                                                                        */
00023 
00024 /**
00025  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00026  * @api
00027  */
00028 abstract class Tx_Fluid_Core_Widget_AbstractWidgetViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper implements Tx_Fluid_Core_ViewHelper_Facets_ChildNodeAccessInterface {
00029 
00030     /**
00031      * The Controller associated to this widget.
00032      * This needs to be filled by the individual subclass by an @inject
00033      * annotation.
00034      *
00035      * @var Tx_Fluid_Core_Widget_AbstractWidgetController
00036      * @api
00037      */
00038     protected $controller;
00039 
00040     /**
00041      * If set to TRUE, it is an AJAX widget.
00042      *
00043      * @var boolean
00044      * @api
00045      */
00046     protected $ajaxWidget = FALSE;
00047 
00048     /**
00049      * @var Tx_Fluid_Core_Widget_AjaxWidgetContextHolder
00050      */
00051     private $ajaxWidgetContextHolder;
00052 
00053     /**
00054      * @var Tx_Extbase_Object_ObjectManagerInterface
00055      */
00056     private $objectManager;
00057 
00058     /**
00059      * @var Tx_Fluid_Core_Widget_WidgetContext
00060      */
00061     private $widgetContext;
00062 
00063     /**
00064      * @param Tx_Fluid_Core_Widget_AjaxWidgetContextHolder $ajaxWidgetContextHolder
00065      * @return void
00066      * @author Sebastian Kurfürst <sebastian@typo3.org>
00067      */
00068     public function injectAjaxWidgetContextHolder(Tx_Fluid_Core_Widget_AjaxWidgetContextHolder $ajaxWidgetContextHolder) {
00069         $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
00070     }
00071 
00072     /**
00073      * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
00074      * @return void
00075      * @author Sebastian Kurfürst <sebastian@typo3.org>
00076      */
00077     public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
00078         $this->objectManager = $objectManager;
00079         $this->widgetContext = $this->objectManager->create('Tx_Fluid_Core_Widget_WidgetContext');
00080     }
00081 
00082     /**
00083      * Initialize the arguments of the ViewHelper, and call the render() method of the ViewHelper.
00084      *
00085      * @param array $renderMethodParameters the parameters of the render() method.
00086      * @return string the rendered ViewHelper.
00087      * @author Sebastian Kurfürst <sebastian@typo3.org>
00088      */
00089     public function initializeArgumentsAndRender(array $renderMethodParameters) {
00090         $this->validateArguments();
00091         $this->initialize();
00092         $this->initializeWidgetContext();
00093 
00094         return $this->callRenderMethod($renderMethodParameters);
00095     }
00096 
00097     /**
00098      * Initialize the Widget Context, before the Render method is called.
00099      *
00100      * @return void
00101      * @author Sebastian Kurfürst <sebastian@typo3.org>
00102      */
00103     private function initializeWidgetContext() {
00104         $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
00105         $this->initializeWidgetIdentifier();
00106 
00107         $controllerObjectName = ($this->controller instanceof Tx_Fluid_AOP_ProxyInterface) ? $this->controller->FLOW3_AOP_Proxy_getProxyTargetClassName() : get_class($this->controller);
00108         $this->widgetContext->setControllerObjectName($controllerObjectName);
00109 
00110         $extensionName = $this->controllerContext->getRequest()->getControllerExtensionName();
00111         $pluginName = $this->controllerContext->getRequest()->getPluginName();
00112         $this->widgetContext->setParentExtensionName($extensionName);
00113         $this->widgetContext->setParentPluginName($pluginName);
00114         $pluginNamespace = Tx_Extbase_Utility_Extension::getPluginNamespace($extensionName, $pluginName);
00115         $this->widgetContext->setParentPluginNamespace($pluginNamespace);
00116 
00117         $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
00118         if ($this->ajaxWidget === TRUE) {
00119             $this->ajaxWidgetContextHolder->store($this->widgetContext);
00120         }
00121     }
00122 
00123     /**
00124      * Stores the syntax tree child nodes in the Widget Context, so they can be
00125      * rendered with <f:widget.renderChildren> lateron.
00126      *
00127      * @param array $childNodes The SyntaxTree Child nodes of this ViewHelper.
00128      * @return void
00129      * @author Sebastian Kurfürst <sebastian@typo3.org>
00130      */
00131     public function setChildNodes(array $childNodes) {
00132         $rootNode = $this->objectManager->create('Tx_Fluid_Core_Parser_SyntaxTree_RootNode');
00133         foreach ($childNodes as $childNode) {
00134             $rootNode->addChildNode($childNode);
00135         }
00136         $this->widgetContext->setViewHelperChildNodes($rootNode, $this->getRenderingContext());
00137     }
00138 
00139     /**
00140      * Generate the configuration for this widget. Override to adjust.
00141      *
00142      * @return array
00143      * @api
00144      * @author Sebastian Kurfürst <sebastian@typo3.org>
00145      */
00146     protected function getWidgetConfiguration() {
00147         return $this->arguments;
00148     }
00149 
00150     /**
00151      * Initiate a sub request to $this->controller. Make sure to fill $this->controller
00152      * via Dependency Injection.
00153      *
00154      * @return Tx_Extbase_MVC_Response the response of this request.
00155      * @api
00156      * @author Sebastian Kurfürst <sebastian@typo3.org>
00157      */
00158     protected function initiateSubRequest() {
00159         if (!($this->controller instanceof Tx_Fluid_Core_Widget_AbstractWidgetController)) {
00160             if (isset($this->controller)) {
00161                 throw new Tx_Fluid_Core_Widget_Exception_MissingControllerException('initiateSubRequest() can not be called if there is no valid controller extending Tx_Fluid_Core_Widget_AbstractWidgetController. Got "' . get_class($this->controller) . '" in class "' . get_class($this) . '".', 1289422564);
00162             }
00163             throw new Tx_Fluid_Core_Widget_Exception_MissingControllerException('initiateSubRequest() can not be called if there is no controller inside $this->controller. Make sure to add a corresponding injectController method to your WidgetViewHelper class "' . get_class($this) . '".', 1284401632);
00164         }
00165 
00166         $subRequest = $this->objectManager->create('Tx_Fluid_Core_Widget_WidgetRequest');
00167         $subRequest->setWidgetContext($this->widgetContext);
00168         $this->passArgumentsToSubRequest($subRequest);
00169 
00170         $subResponse = $this->objectManager->create('Tx_Extbase_MVC_Web_Response');
00171         $this->controller->processRequest($subRequest, $subResponse);
00172         return $subResponse;
00173     }
00174 
00175     /**
00176      * Pass the arguments of the widget to the subrequest.
00177      *
00178      * @param Tx_Fluid_Core_Widget_WidgetRequest $subRequest
00179      * @return void
00180      * @author Sebastian Kurfürst <sebastian@typo3.org>
00181      */
00182     private function passArgumentsToSubRequest(Tx_Fluid_Core_Widget_WidgetRequest $subRequest) {
00183         $arguments = $this->controllerContext->getRequest()->getArguments();
00184         $widgetIdentifier = $this->widgetContext->getWidgetIdentifier();
00185         if (isset($arguments[$widgetIdentifier])) {
00186             if (isset($arguments[$widgetIdentifier]['action'])) {
00187                 $subRequest->setControllerActionName($arguments[$widgetIdentifier]['action']);
00188                 unset($arguments[$widgetIdentifier]['action']);
00189             }
00190             $subRequest->setArguments($arguments[$widgetIdentifier]);
00191         }
00192     }
00193 
00194     /**
00195      * The widget identifier is unique on the current page, and is used
00196      * in the URI as a namespace for the widget's arguments.
00197      *
00198      * @return string the widget identifier for this widget
00199      * @author Sebastian Kurfürst <sebastian@typo3.org>
00200      * @return void
00201      * @todo clean up, and make it somehow more routing compatible.
00202      */
00203     private function initializeWidgetIdentifier() {
00204         if (!$this->viewHelperVariableContainer->exists('Tx_Fluid_Core_Widget_AbstractWidgetViewHelper', 'nextWidgetNumber')) {
00205             $widgetCounter = 0;
00206         } else {
00207             $widgetCounter = $this->viewHelperVariableContainer->get('Tx_Fluid_Core_Widget_AbstractWidgetViewHelper', 'nextWidgetNumber');
00208         }
00209         $widgetIdentifier = '__widget_' . $widgetCounter;
00210         $this->viewHelperVariableContainer->addOrUpdate('Tx_Fluid_Core_Widget_AbstractWidgetViewHelper', 'nextWidgetNumber', $widgetCounter + 1);
00211 
00212         $this->widgetContext->setWidgetIdentifier($widgetIdentifier);
00213     }
00214 }
00215 
00216 ?>