Cleanup #16002: Refactoring of t3lib_spritemanager_SimpleHandler (Thanks to Steffen...
[Packages/TYPO3.CMS.git] / t3lib / spritemanager / class.t3lib_spritemanager_abstracthandler.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 * An abstract class implementing t3lib_spritemanager_SpriteIconGenerator.
31 * Provides base functionality for all handlers.
32 *
33 * @author Steffen Ritter <info@steffen-ritter.net>
34 * @package TYPO3
35 * @subpackage t3lib
36 */
37 abstract class t3lib_spritemanager_AbstractHandler implements t3lib_spritemanager_SpriteIconGenerator {
38 /**
39 * all "registered" icons available through sprite API will cmmulated here
40 * @var array
41 */
42 protected $iconNames = array();
43
44 /**
45 * contains the content of the CSS file to write
46 * @var String
47 */
48 protected $styleSheetData = '';
49
50 /**
51 * path to CSS file for generated styles
52 * @var String
53 */
54 protected $cssTcaFile = "";
55
56 /**
57 * constructor just init's the temp-file-name
58 * @return void
59 */
60 function __construct() {
61 // the file name is prefixed with "z" since the concatenator orders files per name
62 $this->cssTcaFile = PATH_site . t3lib_SpriteManager::$tempPath . 'zextensions.css';
63 $this->styleSheetData = '/* Auto-Generated via ' . get_class($this) . ' */' . LF;
64 }
65
66 /**
67 * Loads all stylesheet files registered through
68 * t3lib_SpriteManager::::addIconSprite
69 *
70 * In fact the stylesheet-files are copied to t3lib_SpriteManager::tempPath
71 * where they automatically will be included from via template.php and
72 * t3lib_compressor.
73 *
74 * @return void
75 */
76 protected function loadRegisteredSprites() {
77 // saves which CSS Files are currently "allowed to be in place"
78 $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
79 // process every registeres file
80 foreach ((array) $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
81 $fileName = basename($file);
82 // file should be present
83 $allowedCssFilesinTempDir[] = $fileName;
84 // get-Cache Filename
85 $unique = md5($fileName . filemtime(PATH_site . $file) . filesize(PATH_site . $file));
86 $cacheFile = PATH_site . t3lib_SpriteManager::$tempPath . $fileName . $unique . '.css';
87 if (!file_exists($cacheFile)) {
88 copy(PATH_site . $file, $cacheFile);
89 }
90 }
91 // get all .css files in dir
92 $cssFilesPresentInTempDir = t3lib_div::getFilesInDir(PATH_site . t3lib_SpriteManager::$tempPath , '.css', 0);
93 // and delete old ones which are not needed anymore
94 $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
95 foreach ($filesToDelete as $file) {
96 unlink(PATH_site . t3lib_SpriteManager::$tempPath . $file);
97 }
98 }
99
100 /**
101 * Interface function. This will be called from the sprite manager to
102 * refresh all caches.
103 *
104 * @return void
105 */
106 public function generate() {
107 // include registered Sprites
108 $this->loadRegisteredSprites();
109
110 // cache results in the CSS file
111 t3lib_div::writeFile($this->cssTcaFile, $this->styleSheetData);
112 }
113
114 /**
115 * Returns the detected icon-names which may be used through t3lib_iconWorks::getSpriteIcon.
116 *
117 * @return array all generated and registred sprite-icon-names, will be empty if there are none
118 */
119 public function getAvailableIconNames() {
120 return $this->iconNames;
121 }
122
123 /**
124 * this method creates sprite icon names for all tables in TCA (including their possible type-icons)
125 * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
126 *
127 * @return array array as $iconName => $fileName
128 */
129 protected function collectTcaSpriteIcons() {
130 $tcaTables = array_keys($GLOBALS['TCA']);
131
132 $resultArray = array();
133
134 // path (relative from typo3 dir) for skin-Images
135 if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
136 $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
137 } else {
138 $skinPath = '';
139 }
140
141 // check every table in the TCA, if an icon is needed
142 foreach ($tcaTables as $tableName) {
143
144 // this method is only needed for TCA tables where
145 // typeicon_classes are not configured
146 if (!is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
147 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
148
149 // adding the default Icon (without types)
150 if (isset($tcaCtrl['iconfile'])) {
151 // in CSS wie need a path relative to the css file
152 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
153 if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
154 $icon = $tcaCtrl['iconfile'];
155 } else {
156 $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
157 }
158
159 $icon = t3lib_div::resolveBackPath($icon);
160 $resultArray['tcarecords-' . $tableName . '-default'] = $icon;
161
162 }
163
164 // if records types are available, register them
165 if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
166 foreach ($tcaCtrl['typeicons'] as $type => $icon) {
167
168 // in CSS wie need a path relative to the css file
169 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
170 if (strpos($icon, '/') === FALSE) {
171 $icon = $skinPath . 'gfx/i/' . $icon;
172 }
173
174 $icon = t3lib_div::resolveBackPath($icon);
175
176 $resultArray['tcarecords-' . $tableName . '-' . $type] = $icon;
177 }
178 }
179 }
180 }
181 return $resultArray;
182 }
183 }
184
185 ?>