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