TYPO3 API  SVNRelease
class.t3lib_spritemanager.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
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 /**
00030  * TYPO3 sprite manager, used in BE and in FE if a BE user is logged in.
00031  *
00032  * This class builds CSS definitions of registered icons, writes TCA definitions
00033  * and registers sprite icons in a cache file.
00034  *
00035  * A configurable handler class does the business task.
00036  *
00037  * @author Steffen Ritter <info@steffen-ritter.net>
00038  * @package TYPO3
00039  * @subpackage t3lib
00040  */
00041 class t3lib_SpriteManager {
00042     /**
00043      * @var string Directory for cached sprite informations
00044      */
00045     public static $tempPath = 'typo3temp/sprites/';
00046 
00047     /**
00048      * @var t3lib_spritemanager_SpriteIconGenerator Handler class instance
00049      */
00050     protected $handler = NULL;
00051 
00052     /**
00053      * @var array Register of valid icons
00054      */
00055     protected $iconNames = array();
00056 
00057     /**
00058      * @var string Name of current cache file
00059      */
00060     protected $tempFileName = '';
00061 
00062     /**
00063      * Check if the icon cache has to be rebuild, instantiate and call the handler class if so.
00064      *
00065      * @param boolean Suppress regeneration if false (useful for feediting)
00066      * @return void
00067      */
00068     function __construct($allowRegeneration = TRUE) {
00069             // Create temp directory if missing
00070         if (!is_dir(PATH_site . self::$tempPath)) {
00071             t3lib_div::mkdir(PATH_site . self::$tempPath);
00072         }
00073 
00074             // Backwards compatibility handling for API calls <= 4.3, will be removed in 4.7
00075         $this->compatibilityCalls();
00076 
00077             // Create cache filename, the hash includes all icons, registered CSS styles registered and the extension list
00078         $this->tempFileName = PATH_site . self::$tempPath .
00079                               md5(serialize($GLOBALS['TBE_STYLES']['spritemanager']) .
00080                                   md5(serialize($GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'])) .
00081                                   $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']) . '.inc';
00082 
00083             // Regenerate cache file if not already existing
00084         if (!@file_exists($this->tempFileName)) {
00085             if ($allowRegeneration) {
00086                 $handlerClass = (
00087                 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
00088                         $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
00089                         't3lib_spritemanager_SimpleHandler'
00090                 );
00091                 $this->handler = t3lib_div::makeInstance($handlerClass);
00092 
00093                     // Throw exception if handler class does not implement required interface
00094                 if (!$this->handler || !($this->handler instanceof t3lib_spritemanager_SpriteIconGenerator)) {
00095                     throw new Exception(
00096                         "class in TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] does not exist,
00097                         or does not implement t3lib_spritemanager_SpriteIconGenerator"
00098                     );
00099                 }
00100 
00101                 $this->rebuildCache();
00102             } else {
00103                     // Set tempFileName to existing file if regeneration is not allowed
00104                 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
00105             }
00106         }
00107     }
00108 
00109     /**
00110      * Call handler class, merge results with skin data and cache it.
00111      *
00112      * @return void
00113      */
00114     protected function rebuildCache() {
00115             // Generate CSS and TCA files, build icon set register
00116         $this->handler->generate();
00117 
00118             // Get all icons registered from skins, merge with core icon list
00119         $availableSkinIcons = (array) $GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
00120         foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinName => $skinData) {
00121             $availableSkinIcons = array_merge($availableSkinIcons, (array) $skinData['availableSpriteIcons']);
00122         }
00123 
00124             // Merge icon names provided by the skin, with
00125             // registered "complete sprites" and the handler class
00126         $this->iconNames = array_merge(
00127             $availableSkinIcons,
00128             (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
00129             $this->handler->getAvailableIconNames()
00130         );
00131 
00132             // Create serialized cache data
00133         $cacheString = addslashes(serialize($this->iconNames));
00134         $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
00135 
00136             // Clean up cache directory
00137         $oldFiles = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
00138         foreach ($oldFiles as $file) {
00139             @unlink($file);
00140         }
00141 
00142             // Write new cache file
00143         t3lib_div::writeFile($this->tempFileName, $fileContent);
00144     }
00145 
00146     /**
00147      * Backwards compatibility methods, log usage to deprecation log.
00148      * Will be removed in 4.7
00149      *
00150      * @return void
00151      */
00152     private function compatibilityCalls() {
00153             // Fallback for $TYPE_ICONS "contains-module" icons
00154         foreach ((array) $GLOBALS['ICON_TYPES'] as $module => $icon) {
00155             $iconFile = $icon['icon'];
00156             t3lib_div::deprecationLog('Usage of $ICON_TYPES is deprecated since 4.4.' . LF .
00157                                       'The extTables.php entry $ICON_TYPES[\'' . $module . '\'] = \'' . $iconFile . '\'; should be replaced with' . LF .
00158                                       't3lib_SpriteManager::addTcaTypeIcon(\'pages\', \'contains-' . $module . '\', \'' . $iconFile . '\');' . LF .
00159                                       'instead.'
00160             );
00161             t3lib_SpriteManager::addTcaTypeIcon('pages', 'contains-' . $module, $iconFile);
00162         }
00163 
00164             // Fallback for $PAGE_TYPES icons
00165         foreach ((array) $GLOBALS['PAGES_TYPES'] as $type => $icon) {
00166             if (isset($icon['icon'])) {
00167                 $iconFile = $icon['icon'];
00168                 t3lib_div::deprecationLog('Usage of $PAGES_TYPES[\'icon\'] is deprecated since 4.4.' . LF .
00169                                           'The extTables.php entry $PAGE_TYPES[\'' . $type . '\'][\'icon\'] = \'' . $iconFile . '\'; should be replaced with' . LF .
00170                                           't3lib_SpriteManager::addTcaTypeIcon(\'pages\', \'' . $type . '\', \'' . $iconFile . '\');' . LF .
00171                                           'instead.'
00172                 );
00173                 t3lib_SpriteManager::addTcaTypeIcon('pages', $module, $iconFile);
00174             }
00175         }
00176     }
00177 
00178     /**
00179      * Include cache file if exists
00180      *
00181      * @return void
00182      */
00183     public function loadCacheFile() {
00184         if (@file_exists($this->tempFileName)) {
00185             include_once($this->tempFileName);
00186         }
00187     }
00188 
00189     /**
00190      * API for extensions to register own sprites.
00191      *
00192      * Get an array of icon names and the styleSheetFile with defined sprite icons.
00193      * The stylesheet filename should contain the extension name to be unique.
00194      *
00195      * Naming conventions:
00196      * - IconName: extensions-$extKey-$iconName
00197      * - CSS class for loading the sprite: t3-icon-extensions-$extKey
00198      * - CSS class for single icons: t3-icon-$extKey-$iconName
00199      *
00200      * @param array Icon names
00201      * @param string Stylesheet filename relative to PATH_typo3. Skins do not need to supply the $styleSheetFile, if the CSS file is within the registered stylesheet folders
00202      * @return void
00203      */
00204     public static function addIconSprite(array $icons, $styleSheetFile = '') {
00205         $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
00206             (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
00207             $icons
00208         );
00209         if ($styleSheetFile !== '') {
00210             $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
00211         }
00212     }
00213 
00214     /**
00215      * API for extensions to register new sprite images which can be used with
00216      * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
00217      *
00218      * @param array Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
00219      * @param string Extension key
00220      * @return void
00221      */
00222     public static function addSingleIcons(array $icons, $extKey = '') {
00223         foreach ($icons as $iconName => $iconFile) {
00224             $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
00225         }
00226     }
00227 
00228     /**
00229      * API to register new type icons for tables which use "typeicon_classes"
00230      * Can be used to provide icons for "modules" in pages table
00231      *
00232      * @param string Table name to which the type icon should be added
00233      * @param string Type column name of the table
00234      * @param string Icon filename, relative to PATH_typo3
00235      * @return void
00236      */
00237     public static function addTcaTypeIcon($table, $type, $iconFile) {
00238         $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
00239         if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
00240             $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
00241         }
00242     }
00243 }
00244 
00245 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php'])) {
00246     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php']);
00247 }
00248 ?>