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