[BUGFIX] Context menu for offline storages raises exception
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Sprite / AbstractSpriteHandler.php
1 <?php
2 namespace TYPO3\CMS\Backend\Sprite;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Steffen Ritter <info@steffen-ritter.net>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
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 */
35 abstract class AbstractSpriteHandler implements \TYPO3\CMS\Backend\Sprite\SpriteIconGeneratorInterface {
36
37 /**
38 * all "registered" icons available through sprite API will cumulated here
39 *
40 * @var array
41 */
42 protected $iconNames = array();
43
44 /**
45 * contains the content of the CSS file to write
46 *
47 * @var string
48 */
49 protected $styleSheetData = '';
50
51 /**
52 * path to CSS file for generated styles
53 *
54 * @var string
55 */
56 protected $cssTcaFile = '';
57
58 /**
59 * constructor just init's the temp-file-name
60 *
61 * @return void
62 */
63 public function __construct() {
64 // The file name is prefixed with "z" since the concatenator orders files per name
65 $this->cssTcaFile = PATH_site . \TYPO3\CMS\Backend\Sprite\SpriteManager::$tempPath . 'zextensions.css';
66 $this->styleSheetData = '/* Auto-Generated via ' . get_class($this) . ' */' . LF;
67 }
68
69 /**
70 * Loads all stylesheet files registered through
71 * t3lib_SpriteManager::addIconSprite
72 *
73 * In fact the stylesheet-files are copied to t3lib_SpriteManager::tempPath
74 * where they automatically will be included from via template.php and
75 * t3lib_compressor.
76 *
77 * @return void
78 */
79 protected function loadRegisteredSprites() {
80 // Saves which CSS Files are currently "allowed to be in place"
81 $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
82 // Process every registeres file
83 foreach ((array) $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
84 $fileName = basename($file);
85 // File should be present
86 $allowedCssFilesinTempDir[] = $fileName;
87 // get-Cache Filename
88 $unique = md5($fileName . filemtime((PATH_site . $file)) . filesize((PATH_site . $file)));
89 $cacheFile = PATH_site . \TYPO3\CMS\Backend\Sprite\SpriteManager::$tempPath . $fileName . $unique . '.css';
90 if (!file_exists($cacheFile)) {
91 copy(PATH_site . $file, $cacheFile);
92 }
93 }
94 // Get all .css files in dir
95 $cssFilesPresentInTempDir = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_site . \TYPO3\CMS\Backend\Sprite\SpriteManager::$tempPath, '.css', 0);
96 // and delete old ones which are not needed anymore
97 $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
98 foreach ($filesToDelete as $file) {
99 unlink(PATH_site . \TYPO3\CMS\Backend\Sprite\SpriteManager::$tempPath . $file);
100 }
101 }
102
103 /**
104 * Interface function. This will be called from the sprite manager to
105 * refresh all caches.
106 *
107 * @return void
108 */
109 public function generate() {
110 // Include registered Sprites
111 $this->loadRegisteredSprites();
112 // Cache results in the CSS file
113 \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($this->cssTcaFile, $this->styleSheetData);
114 }
115
116 /**
117 * Returns the detected icon-names which may be used through t3lib_iconWorks::getSpriteIcon.
118 *
119 * @return array all generated and registered sprite-icon-names, will be empty if there are none
120 */
121 public function getAvailableIconNames() {
122 return $this->iconNames;
123 }
124
125 /**
126 * this method creates sprite icon names for all tables in TCA (including their possible type-icons)
127 * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
128 *
129 * @return array Array as $iconName => $fileName
130 */
131 protected function collectTcaSpriteIcons() {
132 $tcaTables = array_keys($GLOBALS['TCA']);
133 $resultArray = array();
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 // check every table in the TCA, if an icon is needed
141 foreach ($tcaTables as $tableName) {
142 // This method is only needed for TCA tables where
143 // typeicon_classes are not configured
144 if (is_array($GLOBALS['TCA'][$tableName]) && !is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
145 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
146 // Adding the default Icon (without types)
147 if (isset($tcaCtrl['iconfile'])) {
148 // In CSS we need a path relative to the css file
149 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
150 if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
151 $icon = $tcaCtrl['iconfile'];
152 } else {
153 $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
154 }
155 $icon = \TYPO3\CMS\Core\Utility\GeneralUtility::resolveBackPath($icon);
156 $resultArray['tcarecords-' . $tableName . '-default'] = $icon;
157 }
158 // If records types are available, register them
159 if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
160 foreach ($tcaCtrl['typeicons'] as $type => $icon) {
161 // In CSS we need a path relative to the css file
162 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
163 if (strpos($icon, '/') === FALSE) {
164 $icon = $skinPath . 'gfx/i/' . $icon;
165 }
166 $icon = \TYPO3\CMS\Core\Utility\GeneralUtility::resolveBackPath($icon);
167 $resultArray['tcarecords-' . $tableName . '-' . $type] = $icon;
168 }
169 }
170 }
171 }
172 return $resultArray;
173 }
174
175 }
176
177
178 ?>