TYPO3 API  SVNRelease
AjaxWidgetContextHolder.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * This object stores the WidgetContext for the currently active widgets
00025  * of the current user, to make sure the WidgetContext is available in
00026  * Widget AJAX requests.
00027  *
00028  * This class is only used internally by the widget framework.
00029  *
00030  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00031  */
00032 class Tx_Fluid_Core_Widget_AjaxWidgetContextHolder implements t3lib_Singleton {
00033 
00034     /**
00035      * An array $ajaxWidgetIdentifier => $widgetContext
00036      * which stores the widget context.
00037      *
00038      * @var array
00039      */
00040     protected $widgetContexts = array();
00041 
00042     /**
00043      * @var string
00044      */
00045     protected $widgetContextsStorageKey = 'Tx_Fluid_Core_Widget_AjaxWidgetContextHolder_widgetContexts';
00046 
00047     /**
00048      * Constructor
00049      */
00050     public function __construct() {
00051         $this->loadWidgetContexts();
00052     }
00053 
00054     /**
00055      * Loads the windget contexts from the TYPO3 user session
00056      *
00057      * @return void
00058      */
00059     protected function loadWidgetContexts() {
00060         if (TYPO3_MODE === 'FE') {
00061             $this->widgetContexts = unserialize($GLOBALS['TSFE']->fe_user->getKey('ses', $this->widgetContextsStorageKey));
00062         } else {
00063             $this->widgetContexts = unserialize($GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]);
00064             $GLOBALS['BE_USER']->writeUC();
00065         }
00066     }
00067 
00068     /**
00069      * Get the widget context for the given $ajaxWidgetId.
00070      *
00071      * @param string $ajaxWidgetId
00072      * @return Tx_Fluid_Core_Widget_WidgetContext
00073      * @author Sebastian Kurfürst <sebastian@typo3.org>
00074      */
00075     public function get($ajaxWidgetId) {
00076         if (!isset($this->widgetContexts[$ajaxWidgetId])) {
00077             throw new Tx_Fluid_Core_Widget_Exception_WidgetContextNotFoundException('No widget context was found for the Ajax Widget Identifier "' . $ajaxWidgetId . '". This only happens if AJAX URIs are called without including the widget on a page.', 1284793775);
00078         }
00079         return $this->widgetContexts[$ajaxWidgetId];
00080     }
00081 
00082     /**
00083      * Stores the WidgetContext inside the Context, and sets the
00084      * AjaxWidgetIdentifier inside the Widget Context correctly.
00085      *
00086      * @param Tx_Fluid_Core_Widget_WidgetContext $widgetContext
00087      * @return void
00088      * @author Sebastian Kurfürst <sebastian@typo3.org>
00089      */
00090     public function store(Tx_Fluid_Core_Widget_WidgetContext $widgetContext) {
00091         $ajaxWidgetId = md5(uniqid(mt_rand(), TRUE));
00092         $widgetContext->setAjaxWidgetIdentifier($ajaxWidgetId);
00093         $this->widgetContexts[$ajaxWidgetId] = $widgetContext;
00094         $this->storeWidgetContexts();
00095     }
00096 
00097     /**
00098      * Persists the widget contexts in the TYPO3 user session
00099      * @return void
00100      */
00101     protected function storeWidgetContexts() {
00102         if (TYPO3_MODE === 'FE') {
00103             $GLOBALS['TSFE']->fe_user->setKey(
00104                 'ses',
00105                 $this->widgetContextsStorageKey,
00106                 serialize($this->widgetContexts)
00107             );
00108             $GLOBALS['TSFE']->fe_user->storeSessionData();
00109         } else {
00110             $GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey] = serialize($this->widgetContexts);
00111             $GLOBALS['BE_USER']->writeUc();
00112         }
00113     }
00114 }
00115 
00116 ?>