[BUGFIX] Missing column in t3lib_TCEmain::getPreviousLocalizedRecordUid
[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 // Create cache filename, the hash includes all icons, registered CSS styles registered and the extension list
75 $this->tempFileName = PATH_site . self::$tempPath .
76 md5(serialize($GLOBALS['TBE_STYLES']['spritemanager']) .
77 md5(serialize($GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'])) .
78 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']) . '.inc';
79
80 // Regenerate cache file if not already existing
81 if (!@file_exists($this->tempFileName)) {
82 if ($allowRegeneration) {
83 $handlerClass = (
84 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
85 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
86 't3lib_spritemanager_SimpleHandler'
87 );
88 $this->handler = t3lib_div::makeInstance($handlerClass);
89
90 // Throw exception if handler class does not implement required interface
91 if (!$this->handler || !($this->handler instanceof t3lib_spritemanager_SpriteIconGenerator)) {
92 throw new RuntimeException(
93 'Class in $TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] (' .
94 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] .
95 ') does not exist or does not implement t3lib_spritemanager_SpriteIconGenerator.',
96 1294586333
97 );
98 }
99
100 $this->rebuildCache();
101 } else {
102 // Set tempFileName to existing file if regeneration is not allowed
103 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
104 }
105 }
106 }
107
108 /**
109 * Call handler class, merge results with skin data and cache it.
110 *
111 * @return void
112 */
113 protected function rebuildCache() {
114 // Generate CSS and TCA files, build icon set register
115 $this->handler->generate();
116
117 // Get all icons registered from skins, merge with core icon list
118 $availableSkinIcons = (array) $GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
119 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinName => $skinData) {
120 $availableSkinIcons = array_merge($availableSkinIcons, (array) $skinData['availableSpriteIcons']);
121 }
122
123 // Merge icon names provided by the skin, with
124 // registered "complete sprites" and the handler class
125 $this->iconNames = array_merge(
126 $availableSkinIcons,
127 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
128 $this->handler->getAvailableIconNames()
129 );
130
131 // Create serialized cache data
132 $cacheString = addslashes(serialize($this->iconNames));
133 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
134
135 // Clean up cache directory
136 $oldFiles = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
137 foreach ($oldFiles as $file) {
138 @unlink($file);
139 }
140
141 // Write new cache file
142 t3lib_div::writeFile($this->tempFileName, $fileContent);
143 }
144
145 /**
146 * Include cache file if exists
147 *
148 * @return void
149 */
150 public function loadCacheFile() {
151 if (@file_exists($this->tempFileName)) {
152 include_once($this->tempFileName);
153 }
154 }
155
156 /**
157 * API for extensions to register own sprites.
158 *
159 * Get an array of icon names and the styleSheetFile with defined sprite icons.
160 * The stylesheet filename should contain the extension name to be unique.
161 *
162 * Naming conventions:
163 * - IconName: extensions-$extKey-$iconName
164 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
165 * - CSS class for single icons: t3-icon-$extKey-$iconName
166 *
167 * @param array Icon names
168 * @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
169 * @return void
170 */
171 public static function addIconSprite(array $icons, $styleSheetFile = '') {
172 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
173 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
174 $icons
175 );
176 if ($styleSheetFile !== '') {
177 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
178 }
179 }
180
181 /**
182 * API for extensions to register new sprite images which can be used with
183 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
184 *
185 * @param array Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
186 * @param string Extension key
187 * @return void
188 */
189 public static function addSingleIcons(array $icons, $extKey = '') {
190 foreach ($icons as $iconName => $iconFile) {
191 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
192 }
193 }
194
195 /**
196 * API to register new type icons for tables which use "typeicon_classes"
197 * Can be used to provide icons for "modules" in pages table
198 *
199 * @param string Table name to which the type icon should be added
200 * @param string Type column name of the table
201 * @param string Icon filename, relative to PATH_typo3
202 * @return void
203 */
204 public static function addTcaTypeIcon($table, $type, $iconFile) {
205 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
206 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
207 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
208 }
209 }
210 }
211
212 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php'])) {
213 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_spritemanager.php']);
214 }
215 ?>