Removed some MS-DOS line endings from PHP files
[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 sprite manager.
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 file name 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 sprite manager to
92 * refresh 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(
125 array('extensions-', 'tcarecords-'), array('', ''), $iconName
126 ), $this->styleSheetTemplateExtIcons);
127 $css = str_replace('###IMAGE###', t3lib_div::resolveBackPath($iconPath . $iconFile), $css);
128
129 $this->iconNames[] = $iconName;
130 $this->styleSheetData .= $css;
131 }
132 }
133
134 /**
135 * Loads all StyleSheets Files registered through
136 * t3lib_SpriteManager::::addIconSprite
137 *
138 * In fact the stylesheet-files are copied to t3lib_SpriteManager::tempPath
139 * where they automatically will be included from via template.php and
140 * t3lib_compressor.
141 *
142 * @return void
143 */
144 protected function loadRegisteredSprites() {
145 // saves which CSS Files are currently "allowed to be in place"
146 $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
147 // process every registeres file
148 foreach((array) $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
149 $fileName = basename($file);
150 // file should be present
151 $allowedCssFilesinTempDir[] = $fileName;
152 // get-Cache Filename
153 $unique = md5($fileName . filemtime(PATH_site . $file) . filesize(PATH_site . $file));
154 $cacheFile = PATH_site . t3lib_SpriteManager::$tempPath . $fileName . $unique . '.css';
155 if(!file_exists($cacheFile)) {
156 copy(PATH_site . $file, $cacheFile);
157 }
158 }
159 // get all .css files in dir
160 $cssFilesPresentInTempDir = t3lib_div::getFilesInDir(PATH_site . t3lib_SpriteManager::$tempPath , '.css', 0);
161 // and delete old ones which are not needed anymore
162 $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
163 foreach ($filesToDelete as $file) {
164 unlink(PATH_site . t3lib_SpriteManager::$tempPath . $file);
165 }
166 }
167
168 /**
169 * public-interface function: getter for iconNames
170 * will return the detected icon-names which may be used throug t3lib_iconWorks::getSpriteIcon
171 *
172 * @return array all generated and registred sprite-icon-names
173 */
174 public function getAvailableIconNames() {
175 return $this->iconNames;
176 }
177
178 /**
179 * this method creates SpriteIcon names for all tables in TCA (including their possible type-icons)
180 * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
181 *
182 * @return void
183 */
184 protected function buildTcaSpriteIcons() {
185 $tcaTables = array_keys($GLOBALS['TCA']);
186
187 // delete old tempFiles
188 @unlink($this->cssTcaFile);
189
190 // backpath from the stylesheet file ($cssTcaFile) to typo3 dir
191 // in order to set the background-image URL paths correct
192 $iconPath = '../../' . TYPO3_mainDir;
193
194 // path (relative from typo3 dir) for skin-Images
195 if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
196 $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
197 } else {
198 $skinPath = '';
199 }
200
201 // check every table in the TCA, if an icon is needed
202 foreach ($tcaTables as $tableName) {
203
204 // this method is only needed for TCA tables where
205 // typeicon_classes are not configured
206 if (!is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
207 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
208
209 $template = str_replace('###TABLE###', $tableName, $this->styleSheetTemplateTca);
210
211 // adding the default Icon (without types)
212 if (isset($tcaCtrl['iconfile'])) {
213 // in CSS wie need a path relative to the css file
214 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
215 if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
216 $icon = $tcaCtrl['iconfile'];
217 } else {
218 $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
219 }
220
221 $icon = t3lib_div::resolveBackPath($iconPath . $icon);
222
223 // saving default icon
224 $stylesString = str_replace('###TYPE###', 'default', $template);
225 $stylesString = str_replace('###IMAGE###', $icon, $stylesString);
226 $this->styleSheetData .= $stylesString;
227 $this->iconNames[] = 'tcarecords-' . $tableName . '-default';
228 }
229
230 // if records types are available, register them
231 if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
232 foreach ($tcaCtrl['typeicons'] as $type => $icon) {
233
234 // in CSS wie need a path relative to the css file
235 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
236 if (strpos($icon, '/') === FALSE) {
237 $icon = $skinPath . 'gfx/i/' . $icon;
238 }
239
240 $icon = t3lib_div::resolveBackPath($iconPath . $icon);
241 $stylesString = str_replace('###TYPE###', $type, $template);
242 $stylesString = str_replace('###IMAGE###', $icon, $stylesString);
243 // saving type icon
244 $this->styleSheetData .= $stylesString;
245 $this->iconNames[] = 'tcarecords-' . $tableName . '-' . $type;
246 }
247 }
248 }
249 }
250 }
251 }
252
253 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/spritemanager/class.t3lib_spritemanager_simplehandler.php']) {
254 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/spritemanager/class.t3lib_spritemanager_simplehandler.php']);
255 }
256 ?>