[TASK] Remove deprecated parts marked for removal
[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 * @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 RuntimeException(
96 'Class in $TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] (' .
97 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] .
98 ') does not exist or does not implement t3lib_spritemanager_SpriteIconGenerator.',
99 1294586333
100 );
101 }
102
103 $this->rebuildCache();
104 } else {
105 // Set tempFileName to existing file if regeneration is not allowed
106 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
107 }
108 }
109 }
110
111 /**
112 * Call handler class, merge results with skin data and cache it.
113 *
114 * @return void
115 */
116 protected function rebuildCache() {
117 // Generate CSS and TCA files, build icon set register
118 $this->handler->generate();
119
120 // Get all icons registered from skins, merge with core icon list
121 $availableSkinIcons = (array) $GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
122 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinName => $skinData) {
123 $availableSkinIcons = array_merge($availableSkinIcons, (array) $skinData['availableSpriteIcons']);
124 }
125
126 // Merge icon names provided by the skin, with
127 // registered "complete sprites" and the handler class
128 $this->iconNames = array_merge(
129 $availableSkinIcons,
130 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
131 $this->handler->getAvailableIconNames()
132 );
133
134 // Create serialized cache data
135 $cacheString = addslashes(serialize($this->iconNames));
136 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
137
138 // Clean up cache directory
139 $oldFiles = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
140 foreach ($oldFiles as $file) {
141 @unlink($file);
142 }
143
144 // Write new cache file
145 t3lib_div::writeFile($this->tempFileName, $fileContent);
146 }
147
148 /**
149 * Backwards compatibility methods, log usage to deprecation log.
150 *
151 * @return void
152 * @deprecated since TYPO3 4.4: Will be removed in 4.7
153 */
154 private function compatibilityCalls() {
155 // Fallback for $TYPE_ICONS "contains-module" icons
156 foreach ((array) $GLOBALS['ICON_TYPES'] as $module => $icon) {
157 $iconFile = $icon['icon'];
158 t3lib_div::deprecationLog('Usage of $ICON_TYPES is deprecated since 4.4.' . LF .
159 'The extTables.php entry $ICON_TYPES[\'' . $module . '\'] = \'' . $iconFile . '\'; should be replaced with' . LF .
160 't3lib_SpriteManager::addTcaTypeIcon(\'pages\', \'contains-' . $module . '\', \'' . $iconFile . '\');' . LF .
161 'instead.'
162 );
163 t3lib_SpriteManager::addTcaTypeIcon('pages', 'contains-' . $module, $iconFile);
164 }
165
166 // Fallback for $PAGE_TYPES icons
167 foreach ((array) $GLOBALS['PAGES_TYPES'] as $type => $icon) {
168 if (isset($icon['icon'])) {
169 $iconFile = $icon['icon'];
170 t3lib_div::deprecationLog('Usage of $PAGES_TYPES[\'icon\'] is deprecated since 4.4.' . LF .
171 'The extTables.php entry $PAGE_TYPES[\'' . $type . '\'][\'icon\'] = \'' . $iconFile . '\'; should be replaced with' . LF .
172 't3lib_SpriteManager::addTcaTypeIcon(\'pages\', \'' . $type . '\', \'' . $iconFile . '\');' . LF .
173 'instead.'
174 );
175 t3lib_SpriteManager::addTcaTypeIcon('pages', $module, $iconFile);
176 }
177 }
178 }
179
180 /**
181 * Include cache file if exists
182 *
183 * @return void
184 */
185 public function loadCacheFile() {
186 if (@file_exists($this->tempFileName)) {
187 include_once($this->tempFileName);
188 }
189 }
190
191 /**
192 * API for extensions to register own sprites.
193 *
194 * Get an array of icon names and the styleSheetFile with defined sprite icons.
195 * The stylesheet filename should contain the extension name to be unique.
196 *
197 * Naming conventions:
198 * - IconName: extensions-$extKey-$iconName
199 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
200 * - CSS class for single icons: t3-icon-$extKey-$iconName
201 *
202 * @param array Icon names
203 * @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
204 * @return void
205 */
206 public static function addIconSprite(array $icons, $styleSheetFile = '') {
207 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
208 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
209 $icons
210 );
211 if ($styleSheetFile !== '') {
212 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
213 }
214 }
215
216 /**
217 * API for extensions to register new sprite images which can be used with
218 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
219 *
220 * @param array Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
221 * @param string Extension key
222 * @return void
223 */
224 public static function addSingleIcons(array $icons, $extKey = '') {
225 foreach ($icons as $iconName => $iconFile) {
226 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
227 }
228 }
229
230 /**
231 * API to register new type icons for tables which use "typeicon_classes"
232 * Can be used to provide icons for "modules" in pages table
233 *
234 * @param string Table name to which the type icon should be added
235 * @param string Type column name of the table
236 * @param string Icon filename, relative to PATH_typo3
237 * @return void
238 */
239 public static function addTcaTypeIcon($table, $type, $iconFile) {
240 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
241 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
242 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
243 }
244 }
245 }
246
247 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php'])) {
248 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php']);
249 }
250 ?>