[BUGFIX] Fix various TCA issues
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Sprite / AbstractSpriteHandler.php
1 <?php
2 namespace TYPO3\CMS\Backend\Sprite;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\PathUtility;
19
20 /**
21 * An abstract class implementing SpriteIconGeneratorInterface.
22 * Provides base functionality for all handlers.
23 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
24 */
25 abstract class AbstractSpriteHandler implements SpriteIconGeneratorInterface
26 {
27 /**
28 * all "registered" icons available through sprite API will cumulated here
29 *
30 * @var array
31 */
32 protected $iconNames = array();
33
34 /**
35 * contains the content of the CSS file to write
36 *
37 * @var string
38 */
39 protected $styleSheetData = '';
40
41 /**
42 * path to CSS file for generated styles
43 *
44 * @var string
45 */
46 protected $cssTcaFile = '';
47
48 /**
49 * constructor just init's the temp-file-name
50 *
51 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
52 */
53 public function __construct()
54 {
55 // The file name is prefixed with "z" since the concatenator orders files per name
56 $this->cssTcaFile = PATH_site . SpriteManager::$tempPath . 'zextensions.css';
57 $this->styleSheetData = '/* Auto-Generated via ' . get_class($this) . ' */' . LF;
58 }
59
60 /**
61 * Loads all stylesheet files registered through
62 * \TYPO3\CMS\Backend\Sprite\SpriteManager::addIconSprite
63 *
64 * In fact the stylesheet-files are copied to \TYPO3\CMS\Backend\Sprite\SpriteManager::tempPath
65 * where they automatically will be included from via
66 * \TYPO3\CMS\Backend\Template\DocumentTemplate and
67 * \TYPO3\CMS\Core\Resource\ResourceCompressor
68 *
69 * @return void
70 */
71 protected function loadRegisteredSprites()
72 {
73 // Saves which CSS Files are currently "allowed to be in place"
74 $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
75 // Process every registeres file
76 foreach ((array)$GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
77 $fileName = basename($file);
78 // File should be present
79 $allowedCssFilesinTempDir[] = $fileName;
80 // get-Cache Filename
81 $fileStatus = stat(PATH_site . $file);
82 $unique = md5($fileName . $fileStatus['mtime'] . $fileStatus['size']);
83 $cacheFile = PATH_site . SpriteManager::$tempPath . $fileName . $unique . '.css';
84 if (!file_exists($cacheFile)) {
85 copy(PATH_site . $file, $cacheFile);
86 }
87 }
88 // Get all .css files in dir
89 $cssFilesPresentInTempDir = GeneralUtility::getFilesInDir(PATH_site . SpriteManager::$tempPath, '.css', 0);
90 // and delete old ones which are not needed anymore
91 $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
92 foreach ($filesToDelete as $file) {
93 unlink(PATH_site . SpriteManager::$tempPath . $file);
94 }
95 }
96
97 /**
98 * Interface function. This will be called from the sprite manager to
99 * refresh all caches.
100 *
101 * @return void
102 */
103 public function generate()
104 {
105 // Include registered Sprites
106 $this->loadRegisteredSprites();
107 // Cache results in the CSS file
108 GeneralUtility::writeFile($this->cssTcaFile, $this->styleSheetData);
109 }
110
111 /**
112 * Returns the detected icon-names which may be used through
113 * \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon.
114 *
115 * @return array all generated and registered sprite-icon-names, will be empty if there are none
116 */
117 public function getAvailableIconNames()
118 {
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
125 * (moved form \TYPO3\CMS\Backend\Utility\IconUtility)
126 *
127 * @return array Array as $iconName => $fileName
128 */
129 protected function collectTcaSpriteIcons()
130 {
131 $tcaTables = array_keys($GLOBALS['TCA']);
132 $resultArray = array();
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 // check every table in the TCA, if an icon is needed
140 foreach ($tcaTables as $tableName) {
141 // This method is only needed for TCA tables where
142 // typeicon_classes are not configured
143 if (is_array($GLOBALS['TCA'][$tableName]) && !is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
144 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
145 // Adding the default Icon (without types)
146 if (isset($tcaCtrl['iconfile'])) {
147 // In CSS we need a path relative to the css file
148 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
149 if (\TYPO3\CMS\Core\Utility\StringUtility::beginsWith($tcaCtrl['iconfile'], 'EXT:')) {
150 list($extensionKey, $relativePath) = explode('/', substr($tcaCtrl['iconfile'], 4), 2);
151 $pathInfo = PathUtility::pathinfo(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($extensionKey) . $relativePath);
152 $path = PathUtility::getRelativePathTo($pathInfo['dirname']);
153 $icon = $path . $pathInfo['basename'];
154 } elseif (strpos($tcaCtrl['iconfile'], '/') !== false) {
155 $icon = $tcaCtrl['iconfile'];
156 $icon = GeneralUtility::resolveBackPath($icon);
157 } else {
158 $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
159 $icon = GeneralUtility::resolveBackPath($icon);
160 }
161
162 $resultArray['tcarecords-' . $tableName . '-default'] = $icon;
163 }
164 }
165 }
166 return $resultArray;
167 }
168 }