TYPO3 API  SVNRelease
class.t3lib_spritemanager_abstracthandler.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 
00029 /**
00030  * An abstract class implementing t3lib_spritemanager_SpriteIconGenerator.
00031  * Provides base functionality for all handlers.
00032  *
00033  * @author  Steffen Ritter <info@steffen-ritter.net>
00034  * @package TYPO3
00035  * @subpackage t3lib
00036  */
00037 abstract class t3lib_spritemanager_AbstractHandler implements t3lib_spritemanager_SpriteIconGenerator {
00038     /**
00039      * all "registered" icons available through sprite API will cmmulated here
00040      * @var array
00041      */
00042     protected $iconNames = array();
00043 
00044     /**
00045      * contains the content of the CSS file to write
00046      * @var String
00047      */
00048     protected $styleSheetData = '';
00049 
00050     /**
00051      * path to CSS file for generated styles
00052      * @var String
00053      */
00054     protected $cssTcaFile = "";
00055 
00056     /**
00057      * constructor just init's the temp-file-name
00058      * @return void
00059      */
00060     function __construct() {
00061             // the file name is prefixed with "z" since the concatenator orders files per name
00062         $this->cssTcaFile = PATH_site . t3lib_SpriteManager::$tempPath . 'zextensions.css';
00063         $this->styleSheetData = '/* Auto-Generated via ' . get_class($this) . ' */' . LF;
00064     }
00065 
00066     /**
00067      * Loads all stylesheet files registered through
00068      * t3lib_SpriteManager::::addIconSprite
00069      *
00070      * In fact the stylesheet-files are copied to t3lib_SpriteManager::tempPath
00071      * where they automatically will be included from via template.php and
00072      * t3lib_compressor.
00073      *
00074      * @return void
00075      */
00076     protected function loadRegisteredSprites() {
00077             // saves which CSS Files are currently "allowed to be in place"
00078         $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
00079             // process every registeres file
00080         foreach ((array) $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
00081             $fileName = basename($file);
00082                 // file should be present
00083             $allowedCssFilesinTempDir[] = $fileName;
00084                 // get-Cache Filename
00085             $unique = md5($fileName . filemtime(PATH_site . $file) . filesize(PATH_site . $file));
00086             $cacheFile = PATH_site . t3lib_SpriteManager::$tempPath . $fileName . $unique . '.css';
00087             if (!file_exists($cacheFile)) {
00088                 copy(PATH_site . $file, $cacheFile);
00089             }
00090         }
00091             // get all .css files in dir
00092         $cssFilesPresentInTempDir = t3lib_div::getFilesInDir(PATH_site . t3lib_SpriteManager::$tempPath, '.css', 0);
00093             // and delete old ones which are not needed anymore
00094         $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
00095         foreach ($filesToDelete as $file) {
00096             unlink(PATH_site . t3lib_SpriteManager::$tempPath . $file);
00097         }
00098     }
00099 
00100     /**
00101      * Interface function. This will be called from the sprite manager to
00102      * refresh all caches.
00103      *
00104      * @return void
00105      */
00106     public function generate() {
00107             // include registered Sprites
00108         $this->loadRegisteredSprites();
00109 
00110             // cache results in the CSS file
00111         t3lib_div::writeFile($this->cssTcaFile, $this->styleSheetData);
00112     }
00113 
00114     /**
00115      * Returns the detected icon-names which may be used through t3lib_iconWorks::getSpriteIcon.
00116      *
00117      * @return array all generated and registred sprite-icon-names, will be empty if there are none
00118      */
00119     public function getAvailableIconNames() {
00120         return $this->iconNames;
00121     }
00122 
00123     /**
00124      * this method creates sprite icon names for all tables in TCA (including their possible type-icons)
00125      * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
00126      *
00127      * @return array array as $iconName => $fileName
00128      */
00129     protected function collectTcaSpriteIcons() {
00130         $tcaTables = array_keys($GLOBALS['TCA']);
00131 
00132         $resultArray = array();
00133 
00134             // path (relative from typo3 dir) for skin-Images
00135         if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
00136             $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
00137         } else {
00138             $skinPath = '';
00139         }
00140 
00141             // check every table in the TCA, if an icon is needed
00142         foreach ($tcaTables as $tableName) {
00143 
00144                 // this method is only needed for TCA tables where
00145                 // typeicon_classes are not configured
00146             if (!is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
00147                 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
00148 
00149                     // adding the default Icon (without types)
00150                 if (isset($tcaCtrl['iconfile'])) {
00151                         // in CSS wie need a path relative to the css file
00152                         // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
00153                     if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
00154                         $icon = $tcaCtrl['iconfile'];
00155                     } else {
00156                         $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
00157                     }
00158 
00159                     $icon = t3lib_div::resolveBackPath($icon);
00160                     $resultArray['tcarecords-' . $tableName . '-default'] = $icon;
00161 
00162                 }
00163 
00164                     // if records types are available, register them
00165                 if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
00166                     foreach ($tcaCtrl['typeicons'] as $type => $icon) {
00167 
00168                             // in CSS wie need a path relative to the css file
00169                             // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
00170                         if (strpos($icon, '/') === FALSE) {
00171                             $icon = $skinPath . 'gfx/i/' . $icon;
00172                         }
00173 
00174                         $icon = t3lib_div::resolveBackPath($icon);
00175 
00176                         $resultArray['tcarecords-' . $tableName . '-' . $type] = $icon;
00177                     }
00178                 }
00179             }
00180         }
00181         return $resultArray;
00182     }
00183 }
00184 
00185 ?>