Added Feature #14324: Add spriteGeneratorAPI to support the new sprite-icon-API ...
[Packages/TYPO3.CMS.git] / t3lib / spriteManager / class.t3lib_spritemanager_simplehandler.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 * A class with an concrete implementation of t3lib_spriteManager_spriteIconGenerator.
31 * It is the standard / fallback handler of the spriteManager.
32 * This implementation won't generate sprites at all. It will just render css-definitions
33 * for all registered icons so that they may be used through t3lib_iconWorks::getSpriteIcon*
34 * Without the css classes generated here, icons of for example tca records would be empty.
35 *
36 * @author Steffen Ritter <info@steffen-ritter.net>
37 * @package TYPO3
38 * @subpackage t3lib
39 */
40 class t3lib_spriteManager_simpleHandler implements t3lib_spriteManager_spriteIconGenerator {
41 /**
42 * all "registered" Icons available through sprite-api will cumuluated within
43 * @var array
44 */
45 protected $iconNames = array();
46
47 /**
48 * contains the content of the CSS file to write
49 * @var String
50 */
51 protected $styleSheetData = "/* Auto-Generated via t3lib_spriteManager_simpleHandler */\n";
52
53 /**
54 * css-template for each sprite-icon of an tca-record-symbol
55 * @var String
56 */
57 protected $styleSheetTemplateTca = '
58 .t3-icon-###TABLE###-###TYPE### {
59 background-position: 0px 0px !important;
60 background-image: url(\'###IMAGE###\') !important;
61 }
62 ';
63
64 /**
65 * css template for single Icons registered by extension authors
66 * @var String
67 */
68 protected $styleSheetTemplateExtIcons = '
69 .t3-icon-###NAME### {
70 background-position: 0px 0px;
71 background-image: url(\'###IMAGE###\');
72 }
73 ';
74
75 /**
76 * path to css file for generated styles
77 * @var String
78 */
79 protected $cssTcaFile = "";
80
81 /**
82 * constructor just init's the temp-file-name
83 * @return void
84 */
85 function __construct() {
86 // the fileName is prefixed with "z" since the concatenator orders files per name
87 $this->cssTcaFile = PATH_site . t3lib_spriteManager::$tempPath . 'zextensions.css';
88 }
89
90 /**
91 * Interface function. This will be called from the spriteManager to refresh
92 * all caches.
93 *
94 * @return void
95 */
96 public function generate() {
97 // generate Icons for all TCA tables
98 $this->buildTcaSpriteIcons();
99
100 // generate IconData for single Icons registered
101 $this->buildExtensionSpriteIcons();
102
103 // include registered Sprites
104 $this->loadRegisteredSprites();
105
106 // cache results in the CSS file
107 t3lib_div::writeFile($this->cssTcaFile, $this->styleSheetData);
108 }
109
110
111 /**
112 * This function builds an css class for every single icon registered via
113 * t3lib_spriteManager::addSingleIcons to use them via t3lib_iconWorks::getSpriteIcon
114 * In the simpleHandler the icon just will be added as css-background-image.
115 *
116 * @return void
117 */
118 protected function buildExtensionSpriteIcons() {
119 // backpath from the stylesheet file ($cssTcaFile) to typo3 dir
120 // in order to set the background-image URL paths correct
121 $iconPath = '../../' . TYPO3_mainDir;
122
123 foreach((array) $GLOBALS['TBE_STYLES']['spriteManager']['singleIcons'] as $iconName => $iconFile) {
124 $css = str_replace('###NAME###', str_replace('extensions-', '', $iconName), $this->styleSheetTemplateExtIcons);
125 $css = str_replace('###IMAGE###', t3lib_div::resolveBackPath($iconPath . $iconFile), $css);
126
127 $this->iconNames[] = $iconName;
128 $this->styleSheetData .= $css;
129 }
130 }
131
132 /**
133 * loads all StyleSheets Files registered through t3lib_spriteManager::addIconSprite
134 * in fact the stylesheet-files are copied to t3lib_spriteManager::tempPath where they automatically
135 * will be included from via template.php and t3lib_compressor.
136 *
137 * @return void
138 */
139 protected function loadRegisteredSprites() {
140 // saves which CSS Files are currently "allowed to be in place"
141 $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
142 // process every registeres file
143 foreach((array)$GLOBALS['TBE_STYLES']['spriteManager']['cssFiles'] as $file) {
144 $fileName = basename($file);
145 // file should be present
146 $allowedCssFilesinTempDir[] = $fileName;
147 // get-Cache Filename
148 $unique = md5($fileName . filemtime(PATH_site . $file) . filesize(PATH_site . $file));
149 $cacheFile = PATH_site . t3lib_spriteManager::$tempPath . $fileName . $unique . '.css';
150 if(!file_exists($cacheFile)) {
151 copy(PATH_site . $file, $cacheFile);
152 }
153 }
154 // get all .css files in dir
155 $cssFilesPresentInTempDir = t3lib_div::getFilesInDir(PATH_site . t3lib_spriteManager::$tempPath , '.css', 0);
156 // and delete old ones which are not needed anymore
157 $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
158 foreach ($filesToDelete as $file) {
159 unlink(PATH_site . t3lib_spriteManager::$tempPath . $file);
160 }
161 }
162
163 /**
164 * public-interface function: getter for iconNames
165 * will return the detected icon-names which may be used throug t3lib_iconWorks::getSpriteIcon
166 *
167 * @return array all generated and registred sprite-icon-names
168 */
169 public function getAvailableIconNames() {
170 return $this->iconNames;
171 }
172
173 /**
174 * this method creates SpriteIcon names for all tables in TCA (including their possible type-icons)
175 * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
176 *
177 * @return void
178 */
179 protected function buildTcaSpriteIcons() {
180 $tcaTables = array_keys($GLOBALS['TCA']);
181
182 // delete old tempFiles
183 @unlink($this->cssTcaFile);
184
185 // backpath from the stylesheet file ($cssTcaFile) to typo3 dir
186 // in order to set the background-image URL paths correct
187 $iconPath = '../../' . TYPO3_mainDir;
188
189 // path (relative from typo3 dir) for skin-Images
190 if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
191 $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
192 } else {
193 $skinPath = '';
194 }
195
196 // check every table in the TCA, if an icon is needed
197 foreach ($tcaTables as $tableName) {
198
199 // this method is only needed for TCA tables where
200 // typeicon_classes are not configured
201 if (!is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
202 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
203
204 $template = str_replace('###TABLE###', $tableName, $this->styleSheetTemplateTca);
205
206 // adding the default Icon (without types)
207 if (isset($tcaCtrl['iconfile'])) {
208 // in CSS wie need a path relative to the css file
209 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
210 if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
211 $icon = $tcaCtrl['iconfile'];
212 } else {
213 $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
214 }
215
216 $icon = t3lib_div::resolveBackPath($iconPath . $icon);
217
218 // saving default icon
219 $stylesString = str_replace('###TYPE###', 'default', $template);
220 $stylesString = str_replace('###IMAGE###', $icon, $stylesString);
221 $this->styleSheetData .= $stylesString;
222 $this->iconNames[] = 'tcarecords-' . $tableName . '-default';
223 }
224
225 // if records types are available, register them
226 if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
227 foreach ($tcaCtrl['typeicons'] as $type => $icon) {
228
229 // in CSS wie need a path relative to the css file
230 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
231 if (strpos($icon, '/') === FALSE) {
232 $icon = $skinPath . 'gfx/i/' . $icon;
233 }
234
235 $icon = t3lib_div::resolveBackPath($iconPath . $icon);
236 $stylesString = str_replace('###TYPE###', $type, $template);
237 $stylesString = str_replace('###IMAGE###', $icon, $stylesString);
238 // saving type icon
239 $this->styleSheetData .= $stylesString;
240 $this->iconNames[] = 'tcarecords-' . $tableName . '-' . $type;
241 }
242 }
243 }
244 }
245 }
246 }
247
248 ?>