2 /***************************************************************
5 * (c) 2010 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 *@var t3lib_spritemanager_SpriteIconGenerator Handler class instance
50 protected $handler = NULL
;
53 * @var array Register of valid icons
55 protected $iconNames = array();
58 * @var string Name of current cache file
60 protected $tempFileName = '';
63 * Check if the icon cache has to be rebuild, instantiate and call the handler class if so.
65 * @param boolean Suppress regeneration if false (useful for feediting)
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);
74 // Backwards compatibility handling for API calls <= 4.3, will be removed in 4.7
75 $this->compatibilityCalls();
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';
83 // Regenerate cache file if not already existing
84 if (!@file_exists
($this->tempFileName
)) {
85 if ($allowRegeneration) {
87 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
88 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
89 't3lib_spritemanager_SimpleHandler'
91 $this->handler
= t3lib_div
::makeInstance($handlerClass);
93 // Throw exception if handler class does not implement required interface
94 if (!$this->handler ||
!($this->handler
instanceof t3lib_spritemanager_SpriteIconGenerator
)) {
96 "class in TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] does not exist,
97 or does not implement t3lib_spritemanager_SpriteIconGenerator"
101 $this->rebuildCache();
103 // Set tempFileName to existing file if regeneration is not allowed
104 list($this->tempFileName
) = t3lib_div
::getFilesInDir(PATH_site
. self
::$tempPath, 'inc', TRUE
);
110 * Call handler class, merge results with skin data and cache it.
114 protected function rebuildCache() {
115 // Generate CSS and TCA files, build icon set register
116 $this->handler
->generate();
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']);
124 // Merge icon names provided by the skin, with
125 // registered "complete sprites" and the handler class
126 $this->iconNames
= array_merge(
128 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
129 $this->handler
->getAvailableIconNames()
132 // Create serialized cache data
133 $cacheString = addslashes(serialize($this->iconNames
));
134 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
136 // Clean up cache directory
137 $oldFiles = t3lib_div
::getFilesInDir(PATH_site
. self
::$tempPath, 'inc', TRUE
);
138 foreach ($oldFiles as $file) {
142 // Write new cache file
143 t3lib_div
::writeFile($this->tempFileName
, $fileContent);
147 * Backwards compatibility methods, log usage to deprecation log.
148 * Will be removed in 4.7
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
.
161 t3lib_SpriteManager
::addTcaTypeIcon('pages', 'contains-' . $module, $iconFile);
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
.
173 t3lib_SpriteManager
::addTcaTypeIcon('pages', $module, $iconFile);
179 * Include cache file if exists
183 public function loadCacheFile() {
184 if (@file_exists
($this->tempFileName
)) {
185 include_once($this->tempFileName
);
190 * API for extensions to register own sprites.
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.
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
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
203 * @param array Icon names
204 * @param string Stylesheet filename relative to PATH_typo3
207 public static function addIconSprite(array $icons, $styleSheetFile) {
208 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
209 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
212 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
216 * API for extensions to register new sprite images which can be used with
217 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
219 * @param array Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
220 * @param string Extension key
223 public static function addSingleIcons(array $icons, $extKey = '') {
224 foreach ($icons as $iconName => $iconFile) {
225 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
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
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
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;
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']);