2 /***************************************************************
5 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
30 * TYPO3 sprite manager, used in BE and in FE if a BE user is logged in.
32 * This class builds CSS definitions of registered icons, writes TCA definitions
33 * and registers sprite icons in a cache file.
35 * A configurable handler class does the business task.
37 * @author Steffen Ritter <info@steffen-ritter.net>
41 class t3lib_SpriteManager
{
43 * @var string Directory for cached sprite informations
45 public static $tempPath = 'typo3temp/sprites/';
48 * Initialize sprite manager.
49 * Loads registered sprite configuration from cache, or
50 * rebuilds new cache before registration.
54 public static function initialize() {
55 $cacheIdentifier = static::getCacheIdentifier();
56 /** @var $phpCodeCache t3lib_cache_frontend_PhpFrontend */
57 $phpCodeCache = $GLOBALS['typo3CacheManager']->getCache('cache_phpcode');
58 if ($phpCodeCache->has($cacheIdentifier)) {
59 $phpCodeCache->requireOnce($cacheIdentifier);
61 static::createSpriteCache();
62 $phpCodeCache->requireOnce($cacheIdentifier);
67 * Compile sprite icon cache by calling the registered generator.
69 * Stuff the compiled $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
70 * global into php code cache
72 * @throws RuntimeException
75 protected static function createSpriteCache() {
76 $handlerClass = $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'];
77 /** @var $handler t3lib_spritemanager_SpriteIconGenerator */
78 $handler = t3lib_div
::makeInstance($handlerClass);
80 // Throw exception if handler class does not implement required interface
81 if (!$handler instanceof t3lib_spritemanager_SpriteIconGenerator
) {
82 throw new RuntimeException(
83 'Class ' . $handlerClass . ' in $TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] ' .
84 ' does not implement t3lib_spritemanager_SpriteIconGenerator',
89 // Create temp directory if missing
90 if (!is_dir(PATH_site
. self
::$tempPath)) {
91 t3lib_div
::mkdir(PATH_site
. self
::$tempPath);
94 // Generate CSS and TCA files, build icon set register
97 // Get all icons registered from skins, merge with core icon list
98 $availableSkinIcons = (array) $GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
99 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinData) {
100 $availableSkinIcons = array_merge($availableSkinIcons, (array) $skinData['availableSpriteIcons']);
103 // Merge icon names provided by the skin, with
104 // registered "complete sprites" and the handler class
105 $iconNames = array_merge(
107 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
108 $handler->getAvailableIconNames()
111 $cacheString = addslashes(serialize($iconNames));
112 $cacheFileContent = '$GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\'));';
114 /** @var $phpCodeCache t3lib_cache_frontend_PhpFrontend */
115 $GLOBALS['typo3CacheManager']->getCache('cache_phpcode')->set(
116 static::getCacheIdentifier(),
118 array('t3lib_cachemanager', 'core')
123 * Get cache identifier for $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
127 protected static function getCacheIdentifier() {
128 return 'sprites_' . sha1(TYPO3_version
. PATH_site
. 'spriteManagement');
132 * API for extensions to register own sprites.
134 * Get an array of icon names and the styleSheetFile with defined sprite icons.
135 * The stylesheet filename should contain the extension name to be unique.
137 * Naming conventions:
138 * - IconName: extensions-$extKey-$iconName
139 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
140 * - CSS class for single icons: t3-icon-$extKey-$iconName
142 * @param array $icons Icon names
143 * @param string $styleSheetFile Stylesheet filename relative to PATH_typo3. Skins do not need to supply the $styleSheetFile, if the CSS file is within the registered stylesheet folders
146 public static function addIconSprite(array $icons, $styleSheetFile = '') {
147 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
148 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
151 if ($styleSheetFile !== '') {
152 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
157 * API for extensions to register new sprite images which can be used with
158 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
160 * @param array $icons Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
161 * @param string $extKey Extension key
164 public static function addSingleIcons(array $icons, $extKey = '') {
165 foreach ($icons as $iconName => $iconFile) {
166 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
171 * API to register new type icons for tables which use "typeicon_classes"
172 * Can be used to provide icons for "modules" in pages table
174 * @param string $table Table name to which the type icon should be added
175 * @param string $type Type column name of the table
176 * @param string $iconFile Icon filename, relative to PATH_typo3
179 public static function addTcaTypeIcon($table, $type, $iconFile) {
180 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
181 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
182 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;