[TASK] Simplify sprite manager
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_spritemanager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
6 * All rights reserved
7 *
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.
13 *
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.
18 *
19 *
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.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * TYPO3 sprite manager, used in BE and in FE if a BE user is logged in.
31 *
32 * This class builds CSS definitions of registered icons, writes TCA definitions
33 * and registers sprite icons in a cache file.
34 *
35 * A configurable handler class does the business task.
36 *
37 * @author Steffen Ritter <info@steffen-ritter.net>
38 * @package TYPO3
39 * @subpackage t3lib
40 */
41 class t3lib_SpriteManager {
42 /**
43 * @var string Directory for cached sprite informations
44 */
45 public static $tempPath = 'typo3temp/sprites/';
46
47 /**
48 * Initialize sprite manager.
49 * Loads registered sprite configuration from cache, or
50 * rebuilds new cache before registration.
51 *
52 * @return void
53 */
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);
60 } else {
61 static::createSpriteCache();
62 $phpCodeCache->requireOnce($cacheIdentifier);
63 }
64 }
65
66 /**
67 * Compile sprite icon cache by calling the registered generator.
68 *
69 * Stuff the compiled $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
70 * global into php code cache
71 *
72 * @throws RuntimeException
73 * @return void
74 */
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);
79
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',
85 1294586333
86 );
87 }
88
89 // Create temp directory if missing
90 if (!is_dir(PATH_site . self::$tempPath)) {
91 t3lib_div::mkdir(PATH_site . self::$tempPath);
92 }
93
94 // Generate CSS and TCA files, build icon set register
95 $handler->generate();
96
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']);
101 }
102
103 // Merge icon names provided by the skin, with
104 // registered "complete sprites" and the handler class
105 $iconNames = array_merge(
106 $availableSkinIcons,
107 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
108 $handler->getAvailableIconNames()
109 );
110
111 $cacheString = addslashes(serialize($iconNames));
112 $cacheFileContent = '$GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\'));';
113
114 /** @var $phpCodeCache t3lib_cache_frontend_PhpFrontend */
115 $GLOBALS['typo3CacheManager']->getCache('cache_phpcode')->set(
116 static::getCacheIdentifier(),
117 $cacheFileContent,
118 array('t3lib_cachemanager', 'core')
119 );
120 }
121
122 /**
123 * Get cache identifier for $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
124 *
125 * @return string
126 */
127 protected static function getCacheIdentifier() {
128 return 'sprites_' . sha1(TYPO3_version . PATH_site . 'spriteManagement');
129 }
130
131 /**
132 * API for extensions to register own sprites.
133 *
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.
136 *
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
141 *
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
144 * @return void
145 */
146 public static function addIconSprite(array $icons, $styleSheetFile = '') {
147 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
148 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
149 $icons
150 );
151 if ($styleSheetFile !== '') {
152 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
153 }
154 }
155
156 /**
157 * API for extensions to register new sprite images which can be used with
158 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
159 *
160 * @param array $icons Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
161 * @param string $extKey Extension key
162 * @return void
163 */
164 public static function addSingleIcons(array $icons, $extKey = '') {
165 foreach ($icons as $iconName => $iconFile) {
166 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
167 }
168 }
169
170 /**
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
173 *
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
177 * @return void
178 */
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;
183 }
184 }
185 }
186 ?>