Followup to #14324: Feature: Add spriteGeneratorAPI to support the new sprite-icon...
[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, it is initiated from BE and FE if BE-User ist active
31 * Its task will be to build css-definitions for registered Icons of Extensions,
32 * TCA-Tables and so on, so that they will be usuable through sprite-icon-api.
33 * An special configurable handler-class will process the "real" task so that
34 * the user may differ between details of generation and their caching.
35 *
36 * @author Steffen Ritter <info@steffen-ritter.net>
37 * @package TYPO3
38 * @subpackage t3lib
39 */
40 class t3lib_SpriteManager {
41 /**
42 * @var string
43 */
44 public static $tempPath = 'typo3temp/sprites/';
45
46 /**
47 *@var t3lib_spritemanager_SpriteIconGenerator
48 */
49 protected $handler = NULL;
50
51 /**
52 * @var array
53 */
54 protected $iconNames = array();
55
56 /**
57 * @var string the file name the current cache file
58 */
59 protected $tempFileName = '';
60
61 /**
62 * class constructor checks if cache has to be rebuild and initiates the rebuild
63 * instantiates the handler class
64 *
65 * @param boolean $regenerate with set to false, cache won't be regenerated if needed (useful for feediting)
66 * @return void
67 */
68 function __construct($regenerate = TRUE) {
69 // we check for existance of our targetDirectory
70 if (!is_dir(PATH_site . self::$tempPath)) {
71 t3lib_div::mkdir(PATH_site . self::$tempPath);
72 }
73 // create a fileName, the hash includes all icons and css-styles registered and the extlist
74 $this->tempFileName = PATH_site . self::$tempPath .
75 md5(serialize($GLOBALS['TBE_STYLES']['spritemanager']) .
76 md5(serialize($GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'])) .
77 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']) . '.inc';
78 // if no cache-file for the current config ist present, regenerate it
79 if(!@file_exists($this->tempFileName)) {
80 // regenerate if allowed
81 if($regenerate) {
82 $handlerClass = (
83 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
84 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
85 't3lib_spritemanager_SimpleHandler'
86 );
87 $this->handler = t3lib_div::makeInstance($handlerClass);
88 // check if the handler could be loaded and implements the needed interface
89 if (!$this->handler || !($this->handler instanceof t3lib_spritemanager_SpriteIconGenerator)) {
90 throw new Exception(
91 "class in TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] does not exist,
92 or does not implement t3lib_spritemanager_SpriteIconGenerator"
93 );
94 }
95 // all went good? to go for rebuild
96 $this->rebuildCache();
97 } else {
98 // use old file if present
99 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', 1);
100 }
101 }
102 }
103
104 /**
105 * this method calls the main methods from the handler classes
106 * merges the results with the data from the skin, and cache it
107 *
108 * @return void
109 */
110 protected function rebuildCache() {
111 // ask the handlerClass to kindly rebuild our data
112 $this->handler->generate();
113
114 // get all Icons registered from skins, merge with core-Icon-List
115 $availableSkinIcons = (array)$GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
116 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinName => $skinData) {
117 $availableSkinIcons = array_merge($availableSkinIcons, (array)$skinData['availableSpriteIcons']);
118 }
119
120 // merge icon names whith them provided by the skin,
121 // registered from "complete sprites" and the ones detected
122 // by the handlerclass
123 $this->iconNames = array_merge(
124 $availableSkinIcons,
125 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
126 $this->handler->getAvailableIconNames()
127 );
128
129 // serialize found icons, and cache them to file
130 $cacheString = addslashes(serialize($this->iconNames));
131 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
132
133 // delete old cache files
134 $oldFiles = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', 1);
135 foreach ($oldFiles as $file) {
136 @unlink($file);
137 }
138 // and write the new one
139 t3lib_div::writeFile($this->tempFileName, $fileContent);
140 }
141
142
143 /**
144 * includes the generated cacheFile, if present
145 *
146 * @return void
147 */
148 public function loadCacheFile() {
149 if (@file_exists($this->tempFileName)) {
150 include_once($this->tempFileName);
151 }
152 }
153
154 /**
155 * if an extension has an pregenerated sprite, it might register it here.
156 * Giving the "available" iconNames and the styleSheetFile where the sprite icons are defined (make shure the css filename contains the extname to be unique).
157 * the iconnames and the stylesheet must follow the conventions as follows:
158 * IconName: extensions-$extKey-$iconName.
159 * Class for loading the sprite: t3-icon-extensions-$extKey
160 * Class for single icons: t3-icon-$extKey-$iconName
161 * NOTE: do not use this for skins, stylesheets of skins will be included automatically.
162 * Available icons of skins should be located manually (extTables) to $GLOBALS[TBE_STYLES][skins][skinName][availableIcons]
163 *
164 * @param array icons the names of the introduced icons
165 * @parram string $styleSheetFile the name of the styleshet file relative to PATH_site
166 */
167 public static function addIconSprite(array $icons, $styleSheetFile) {
168 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
169 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
170 $icons
171 );
172
173 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
174 }
175
176 /**
177 * will allow Ext-Developers to register their icons to get included in sprites,
178 * they may use them afterwards with t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
179 * @param array $icons array which contains the adding icons array ( $iconname => $iconFile) $iconFile relative to PATH_typo3
180 * @param string $extKey string of the extension which adds the icons
181 * @return void
182 */
183 public static function addSingleIcons(array $icons, $extKey = '') {
184 foreach ($icons as $iconName => $iconFile) {
185 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
186 }
187 }
188
189 /**
190 * static function to add a type-with icon to an already existent table which makes use of "typeicon_classes"
191 * feature or to provide icon for "modules" in pages table
192 * @param string $table the table the type has been added
193 * @param string $type the type - must equal the value of the column in the table
194 * @param string $iconFile relative to PATH_typo3
195 */
196 public static function addTcaTypeIcon($table, $type, $iconFile) {
197 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
198 if(isset($GLOBALS['TCA'][$table]['typeicon_classes'])) {
199 $GLOBALS['TCA'][$table]['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
200 }
201 }
202 }
203
204 ?>