TYPO3 API  SVNRelease
Befunc.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
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  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
00030  * @package Workspaces
00031  * @subpackage Service
00032  */
00033 class tx_Workspaces_Service_Befunc {
00034 
00035     protected static $pageCache = array();
00036 
00037     /**
00038      * Hooks into the t3lib_beFunc::viewOnClick and redirects to the workspace preview
00039      * only if we're in a workspace and if the frontend-preview is disabled.
00040      *
00041      * @param  $pageUid
00042      * @param  $backPath
00043      * @param  $rootLine
00044      * @param  $anchorSection
00045      * @param  $viewScript
00046      * @param  $additionalGetVars
00047      * @param  $switchFocus
00048      * @return void
00049      */
00050     public function preProcess(&$pageUid, $backPath, $rootLine, $anchorSection, &$viewScript, $additionalGetVars, $switchFocus) {
00051 
00052             // In case a $pageUid is submitted we need to make sure it points to a live-page
00053         if ($pageUid >  0) {
00054             $pageUid = $this->getLivePageUid($pageUid);
00055         }
00056 
00057         if ($GLOBALS['BE_USER']->workspace !== 0) {
00058             $ctrl = t3lib_div::makeInstance('Tx_Workspaces_Controller_PreviewController', FALSE);
00059             $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
00060             /** @var $uriBuilder Tx_Extbase_MVC_Web_Routing_UriBuilder */
00061             $uriBuilder = $objectManager->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
00062             /**
00063              *  This seems to be very harsh to set this directly to "/typo3 but the viewOnClick also
00064              *  has /index.php as fixed value here and dealing with the backPath is very error-prone
00065              *
00066              *  @todo make sure this would work in local extension installation too
00067              */
00068             $backPath = '/' . TYPO3_mainDir;
00069                 // @todo why do we need these additional params? the URIBuilder should add the controller, but he doesn't :(
00070             $additionalParams = '&tx_workspaces_web_workspacesworkspaces%5Bcontroller%5D=Preview&M=web_WorkspacesWorkspaces&id=';
00071             $viewScript = $backPath . $uriBuilder->uriFor('index', array(), 'Tx_Workspaces_Controller_PreviewController', 'workspaces', 'web_workspacesworkspaces') . $additionalParams;
00072         }
00073     }
00074 
00075     /**
00076      * Find the Live-Uid for a given page,
00077      * the results are cached at run-time to avoid too many database-queries
00078      *
00079      * @throws InvalidArgumentException
00080      * @param  $uid
00081      * @return void
00082      */
00083     protected function getLivePageUid($uid) {
00084         if (!isset(self::$pageCache[$uid])) {
00085             $rec = t3lib_beFunc::getRecord('pages', $uid);
00086             if (is_array($rec)) {
00087                 self::$pageCache[$uid] = $rec['t3ver_oid'] ? $rec['t3ver_oid'] : $uid;
00088             } else {
00089                 throw new InvalidArgumentException('uid is supposed to point to an existing page - given value was:' . $uid, 1290628113);
00090             }
00091         }
00092         return self::$pageCache[$uid];
00093     }
00094 }
00095 
00096 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/Service/Befunc.php'])) {
00097     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/Service/Befunc.php']);
00098 }
00099 ?>