Follow-up to bug #15079: Add XCLASS-definitions to spritemanager classes
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_spritemanager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 *@var t3lib_spritemanager_SpriteIconGenerator Handler class instance
49 */
50 protected $handler = NULL;
51
52 /**
53 * @var array Register of valid icons
54 */
55 protected $iconNames = array();
56
57 /**
58 * @var string Name of current cache file
59 */
60 protected $tempFileName = '';
61
62 /**
63 * Check if the icon cache has to be rebuild, instantiate and call the handler class if so.
64 *
65 * @param boolean Suppress regeneration if false (useful for feediting)
66 * @return void
67 */
68 function __construct($allowRegeneration = TRUE) {
69 // Create temp directory if missing
70 if (!is_dir(PATH_site . self::$tempPath)) {
71 t3lib_div::mkdir(PATH_site . self::$tempPath);
72 }
73
74 // Backwards compatibility handling for API calls <= 4.3, will be removed in 4.7
75 $this->compatibilityCalls();
76
77 // Create cache filename, the hash includes all icons, registered CSS styles registered and the extension list
78 $this->tempFileName = PATH_site . self::$tempPath .
79 md5(serialize($GLOBALS['TBE_STYLES']['spritemanager']) .
80 md5(serialize($GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'])) .
81 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']) . '.inc';
82
83 // Regenerate cache file if not already existing
84 if (!@file_exists($this->tempFileName)) {
85 if ($allowRegeneration) {
86 $handlerClass = (
87 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
88 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
89 't3lib_spritemanager_SimpleHandler'
90 );
91 $this->handler = t3lib_div::makeInstance($handlerClass);
92
93 // Throw exception if handler class does not implement required interface
94 if (!$this->handler || !($this->handler instanceof t3lib_spritemanager_SpriteIconGenerator)) {
95 throw new Exception(
96 "class in TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] does not exist,
97 or does not implement t3lib_spritemanager_SpriteIconGenerator"
98 );
99 }
100
101 $this->rebuildCache();
102 } else {
103 // Set tempFileName to existing file if regeneration is not allowed
104 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
105 }
106 }
107 }
108
109 /**
110 * Call handler class, merge results with skin data and cache it.
111 *
112 * @return void
113 */
114 protected function rebuildCache() {
115 // Generate CSS and TCA files, build icon set register
116 $this->handler->generate();
117
118 // Get all icons registered from skins, merge with core icon list
119 $availableSkinIcons = (array)$GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
120 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinName => $skinData) {
121 $availableSkinIcons = array_merge($availableSkinIcons, (array)$skinData['availableSpriteIcons']);
122 }
123
124 // Merge icon names provided by the skin, with
125 // registered "complete sprites" and the handler class
126 $this->iconNames = array_merge(
127 $availableSkinIcons,
128 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
129 $this->handler->getAvailableIconNames()
130 );
131
132 // Create serialized cache data
133 $cacheString = addslashes(serialize($this->iconNames));
134 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
135
136 // Clean up cache directory
137 $oldFiles = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
138 foreach ($oldFiles as $file) {
139 @unlink($file);
140 }
141
142 // Write new cache file
143 t3lib_div::writeFile($this->tempFileName, $fileContent);
144 }
145
146 /**
147 * Backwards compatibility methods, log usage to deprecation log.
148 * Will be removed in 4.7
149 *
150 * @return void
151 */
152 private function compatibilityCalls() {
153 // Fallback for $TYPE_ICONS "contains-module" icons
154 foreach ((array) $GLOBALS['ICON_TYPES'] as $module => $icon) {
155 $iconFile = $icon['icon'];
156 t3lib_div::deprecationLog('Usage of $ICON_TYPES is deprecated since 4.4.' . LF .
157 'The extTables.php entry $ICON_TYPES[\'' . $module . '\'] = \'' . $iconFile . '\'; should be replaced with' . LF .
158 't3lib_SpriteManager::addTcaTypeIcon(\'pages\', \'contains-' . $module . '\', \'' . $iconFile . '\');' . LF .
159 'instead.'
160 );
161 t3lib_SpriteManager::addTcaTypeIcon('pages', 'contains-' . $module, $iconFile);
162 }
163
164 // Fallback for $PAGE_TYPES icons
165 foreach ((array) $GLOBALS['PAGES_TYPES'] as $type => $icon) {
166 if(isset($icon['icon'])) {
167 $iconFile = $icon['icon'];
168 t3lib_div::deprecationLog('Usage of $PAGES_TYPES[\'icon\'] is deprecated since 4.4.' . LF .
169 'The extTables.php entry $PAGE_TYPES[\'' . $type . '\'][\'icon\'] = \'' . $iconFile . '\'; should be replaced with' . LF .
170 't3lib_SpriteManager::addTcaTypeIcon(\'pages\', ' . $type . '\', \'' . $iconFile . '\');' . LF .
171 'instead.'
172 );
173 t3lib_SpriteManager::addTcaTypeIcon('pages', $module, $iconFile);
174 }
175 }
176 }
177
178 /**
179 * Include cache file if exists
180 *
181 * @return void
182 */
183 public function loadCacheFile() {
184 if (@file_exists($this->tempFileName)) {
185 include_once($this->tempFileName);
186 }
187 }
188
189 /**
190 * API for extensions to register own sprites.
191 *
192 * Get an array of icon names and the styleSheetFile with defined sprite icons.
193 * The stylesheet filename should contain the extension name to be unique.
194 *
195 * Naming conventions:
196 * - IconName: extensions-$extKey-$iconName
197 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
198 * - CSS class for single icons: t3-icon-$extKey-$iconName
199 *
200 * Do not use this for skins, stylesheets of skins will be included automatically.
201 * Skin icons should be added manually to $GLOBALS[TBE_STYLES][skins][skinName][availableIcons] via ext_tables.php
202 *
203 * @param array Icon names
204 * @param string Stylesheet filename relative to PATH_typo3
205 * @return void
206 */
207 public static function addIconSprite(array $icons, $styleSheetFile) {
208 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
209 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
210 $icons
211 );
212 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
213 }
214
215 /**
216 * API for extensions to register new sprite images which can be used with
217 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
218 *
219 * @param array Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
220 * @param string Extension key
221 * @return void
222 */
223 public static function addSingleIcons(array $icons, $extKey = '') {
224 foreach ($icons as $iconName => $iconFile) {
225 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
226 }
227 }
228
229 /**
230 * API to register new type icons for tables which use "typeicon_classes"
231 * Can be used to provide icons for "modules" in pages table
232 *
233 * @param string Table name to which the type icon should be added
234 * @param string Type column name of the table
235 * @param string Icon filename, relative to PATH_typo3
236 * @return void
237 */
238 public static function addTcaTypeIcon($table, $type, $iconFile) {
239 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
240 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
241 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
242 }
243 }
244 }
245
246 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php']) {
247 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php']);
248 }
249 ?>