Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

class.t3lib_iconworks.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
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 ***************************************************************/
00085 class t3lib_iconWorks   {
00086 
00100    function getIconImage($table,$row=array(),$backPath,$params='',$shaded=FALSE) {
00101       $str='<img'.t3lib_iconWorks::skinImg($backPath,t3lib_iconWorks::getIcon($table,$row,$shaded),'width="18" height="16"').(trim($params)?' '.trim($params):'');
00102       if (!stristr($str,'alt="'))   $str.=' alt=""';
00103       $str.=' />';
00104       return $str;
00105    }
00106 
00118    function getIcon($table,$row=array(),$shaded=FALSE)   {
00119       global $TCA, $PAGES_TYPES, $ICON_TYPES;
00120 
00121          // Flags:
00122       $doNotGenerateIcon = $GLOBALS['TYPO3_CONF_VARS']['GFX']['noIconProc'];           // If set, the icon will NOT be generated with GDlib. Rather the icon will be looked for as [iconfilename]_X.[extension]
00123       $doNotRenderUserGroupNumber = TRUE;    // If set, then the usergroup number will NOT be printed unto the icon. NOTICE. the icon is generated only if a default icon for groups is not found... So effectively this is ineffective...
00124 
00125          // First, find the icon file name. This can depend on configuration in TCA, field values and more:
00126       if ($table=='pages') {
00127          if (!$iconfile = $PAGES_TYPES[$row['doktype']]['icon'])  {
00128             $iconfile = $PAGES_TYPES['default']['icon'];
00129          }
00130          if ($row['module'] && $ICON_TYPES[$row['module']]['icon'])  {
00131             $iconfile = $ICON_TYPES[$row['module']]['icon'];
00132          }
00133       } else {
00134          if (!$iconfile = $TCA[$table]['ctrl']['typeicons'][$row[$TCA[$table]['ctrl']['typeicon_column']]]) {
00135             $iconfile = (($TCA[$table]['ctrl']['iconfile']) ? $TCA[$table]['ctrl']['iconfile'] : $table.'.gif');
00136          }
00137       }
00138 
00139          // Setting path of iconfile if not already set. Default is "gfx/i/"
00140       if (!strstr($iconfile,'/'))   {
00141          $iconfile = 'gfx/i/'.$iconfile;
00142       }
00143 
00144          // Setting the absolute path where the icon should be found as a file:
00145       if (substr($iconfile,0,3)=='../')   {
00146          $absfile=PATH_site.substr($iconfile,3);
00147       } else {
00148          $absfile=PATH_typo3.$iconfile;
00149       }
00150 
00151          // Initializing variables, all booleans except otherwise stated:
00152       $hidden = FALSE;
00153       $timing = FALSE;
00154       $futuretiming = FALSE;
00155       $user = FALSE;          // In fact an integer value...
00156       $deleted = FALSE;
00157       $protectSection = FALSE;   // Set, if a page-record (only pages!) has the extend-to-subpages flag set.
00158       $noIconFound = $row['_NO_ICON_FOUND'] ? TRUE : FALSE;
00159       // + $shaded which is also boolean!
00160 
00161          // Icon state based on "enableFields":
00162       if (is_array($TCA[$table]['ctrl']['enablecolumns']))  {
00163          $enCols = $TCA[$table]['ctrl']['enablecolumns'];
00164             // If "hidden" is enabled:
00165          if ($enCols['disabled'])   { if ($row[$enCols['disabled']]) { $hidden = TRUE; }}
00166             // If a "starttime" is set and higher than current time:
00167          if ($enCols['starttime'])  { if (time() < intval($row[$enCols['starttime']])) { $timing = TRUE; }}
00168             // If an "endtime" is set:
00169          if ($enCols['endtime']) {
00170             if (intval($row[$enCols['endtime']]) > 0) {
00171                if (intval($row[$enCols['endtime']]) < time())  {
00172                   $timing = TRUE;   // End-timing applies at this point.
00173                } else {
00174                   $futuretiming = TRUE;      // End-timing WILL apply in the future for this element.
00175                }
00176             }
00177          }
00178             // If a user-group field is set:
00179          if ($enCols['fe_group'])   {
00180             $user = $row[$enCols['fe_group']];
00181             if ($user && $doNotRenderUserGroupNumber) $user=100;  // Limit for user number rendering!
00182          }
00183       }
00184 
00185          // If "deleted" flag is set (only when listing records which are also deleted!)
00186       if ($col=$row[$TCA[$table]['ctrl']['delete']])  {
00187          $deleted = TRUE;
00188       }
00189          // Detecting extendToSubpages (for pages only)
00190       if ($table=='pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user)) {
00191          $protectSection = TRUE;
00192       }
00193 
00194          // If ANY of the booleans are set it means we have to alter the icon:
00195       if ($hidden || $timing || $futuretiming || $user || $deleted || $shaded || $noIconFound)  {
00196          $flags='';
00197          $string='';
00198          if ($deleted)  {
00199             $string='deleted';
00200             $flags='d';
00201          } elseif ($noIconFound) {  // This is ONLY for creating icons with "?" on easily...
00202             $string='no_icon_found';
00203             $flags='x';
00204          } else {
00205             if ($hidden) $string.='hidden';
00206             if ($timing) $string.='timing';
00207             if (!$string && $futuretiming) {
00208                $string='futuretiming';
00209             }
00210 
00211             $flags.=
00212                ($hidden ? 'h' : '').
00213                ($timing ? 't' : '').
00214                ($futuretiming ? 'f' : '').
00215                ($user ? 'u' : '').
00216                ($protectSection ? 'p' : '').
00217                ($shaded ? 's' : '');
00218          }
00219 
00220             // Create tagged icon file name:
00221          $iconFileName_stateTagged = ereg_replace('.([[:alnum:]]+)$','__'.$flags.'.\1',basename($iconfile));
00222 
00223             // Check if tagged icon file name exists (a tagget icon means the icon base name with the flags added between body and extension of the filename, prefixed with underscore)
00224          if (@is_file(dirname($absfile).'/'.$iconFileName_stateTagged)) {  // Look for [iconname]_xxxx.[ext]
00225             return dirname($iconfile).'/'.$iconFileName_stateTagged;
00226          } elseif ($doNotGenerateIcon) {     // If no icon generation can be done, try to look for the _X icon:
00227             $iconFileName_X = ereg_replace('.([[:alnum:]]+)$','__x.\1',basename($iconfile));
00228             if (@is_file(dirname($absfile).'/'.$iconFileName_X))  {
00229                return dirname($iconfile).'/'.$iconFileName_X;
00230             } else {
00231                return 'gfx/i/no_icon_found.gif';
00232             }
00233          } else { // Otherwise, create the icon:
00234             $theRes= t3lib_iconWorks::makeIcon($GLOBALS['BACK_PATH'].$iconfile, $string, $user, $protectSection, $absfile, $iconFileName_stateTagged);
00235             return $theRes;
00236          }
00237       } else {
00238          return $iconfile;
00239       }
00240    }
00241 
00254    function skinImg($backPath,$src,$wHattribs='',$outputMode=0)   {
00255 
00256          // Setting source key. If the icon is refered to inside an extension, we homogenize the prefix to "ext/":
00257       $srcKey = ereg_replace('^(\.\.\/typo3conf\/ext|sysext|ext)\/','ext/',$src);
00258 #if ($src!=$srcKey)debug(array($src,$srcKey));
00259 
00260          // LOOKING for alternative icons:
00261       if ($GLOBALS['TBE_STYLES']['skinImg'][$srcKey]) {  // Slower or faster with is_array()? Could be used.
00262          list($src,$wHattribs) = $GLOBALS['TBE_STYLES']['skinImg'][$srcKey];
00263       } elseif ($GLOBALS['TBE_STYLES']['skinImgAutoCfg'])   {  // Otherwise, test if auto-detection is enabled:
00264 
00265             // Search for alternative icon automatically:
00266          $fExt = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['forceFileExtension'];
00267          $scaleFactor = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] ? $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] : 1;  // Scaling factor
00268          $lookUpName = $fExt ? ereg_replace('\.[[:alnum:]]+$','',$srcKey).'.'.$fExt : $srcKey;  // Set filename to look for
00269 
00270             // If file is found:
00271          if (@is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName)) {  // If there is a file...
00272             $iInfo = @getimagesize($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName);   // Get width/height:
00273 
00274                // Set $src and $wHattribs:
00275             $src = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'].$lookUpName;
00276             $wHattribs = 'width="'.round($iInfo[0]*$scaleFactor).'" height="'.round($iInfo[1]*$scaleFactor).'"';
00277          }
00278 
00279             // In anycase, set currect src / wHattrib - this way we make sure that an entry IS found next time we hit the function, regardless of whether it points to a alternative icon or just the current.
00280          $GLOBALS['TBE_STYLES']['skinImg'][$srcKey] = array($src,$wHattribs);    // Set default...
00281       }
00282 
00283          // DEBUG: This doubles the size of all icons - for testing/debugging:
00284 #     if (ereg('^width="([0-9]+)" height="([0-9]+)"$',$wHattribs,$reg)) $wHattribs='width="'.($reg[1]*2).'" height="'.($reg[2]*2).'"';
00285 
00286          // Return icon source/wHattributes:
00287       switch($outputMode)  {
00288          case 0:
00289             return ' src="'.$backPath.$src.'" '.$wHattribs;
00290          break;
00291          case 1:
00292             return $backPath.$src;
00293          break;
00294          case 2:
00295             return $wHattribs;
00296          break;
00297       }
00298    }
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308 
00309 
00310    /***********************************
00311     *
00312     * Other functions
00313     *
00314     ***********************************/
00315 
00328    function makeIcon($iconfile,$mode, $user, $protectSection,$absFile,$iconFileName_stateTagged)   {
00329       $iconFileName = 'icon_'.t3lib_div::shortMD5($iconfile.'|'.$mode.'|-'.$user.'|'.$protectSection).'_'.$iconFileName_stateTagged.'.'.($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']?'png':'gif');
00330       $mainpath = '../typo3temp/'.$iconFileName;
00331       $path = PATH_site.'typo3temp/'.$iconFileName;
00332 
00333 
00334       if (@file_exists(PATH_typo3.'icons/'.$iconFileName))  {  // Returns if found in typo3/icons/
00335          return 'icons/'.$iconFileName;
00336       } elseif (@file_exists($path))   {  // Returns if found in ../typo3temp/icons/
00337          return $mainpath;
00338       } else { // Makes icon:
00339          if (@file_exists($absFile))   {
00340             if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'])   {
00341 
00342                   // Create image pointer, if possible:
00343                $im = t3lib_iconworks::imagecreatefrom($absFile);
00344                if ($im<0)  return $iconfile;
00345 
00346                   // Converting to gray scale, dimming the icon:
00347                if ($mode!='futuretiming' && $mode!='no_icon_found' && !(!$mode && $user)) {
00348                   for ($c=0; $c<ImageColorsTotal($im); $c++)   {
00349                      $cols = ImageColorsForIndex($im,$c);
00350                      $newcol = round(($cols['red']+$cols['green']+$cols['blue'])/3);
00351                      $lighten = 2;
00352                      $newcol = round(255-((255-$newcol)/$lighten));
00353                      ImageColorSet($im,$c,$newcol,$newcol,$newcol);
00354                   }
00355                }
00356                   // Applying user icon, if there are access control on the item:
00357                if ($user)  {
00358                   if ($user < 100)  {  // Apply user number only if lower than 100
00359                      $black = ImageColorAllocate($im, 0,0,0);
00360                      imagefilledrectangle($im, 0,0,(($user>10)?9:5),8,$black);
00361 
00362                      $white = ImageColorAllocate($im, 255,255,255);
00363                      imagestring($im, 1, 1, 1, $user, $white);
00364                   }
00365 
00366                   $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_group.gif');
00367                   if ($ol_im<0)  return $iconfile;
00368 
00369                   t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
00370                }
00371                   // Applying overlay based on mode:
00372                if ($mode)  {
00373                   unset($ol_im);
00374                   switch($mode)  {
00375                      case 'deleted':
00376                         $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_deleted.gif');
00377                      break;
00378                      case 'futuretiming':
00379                         $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
00380                      break;
00381                      case 'timing':
00382                         $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
00383                      break;
00384                      case 'hiddentiming':
00385                         $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden_timing.gif');
00386                      break;
00387                      case 'no_icon_found':
00388                         $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_no_icon_found.gif');
00389                      break;
00390                      case 'hidden':
00391                      default:
00392                         $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden.gif');
00393                      break;
00394                   }
00395                   if ($ol_im<0)  return $iconfile;
00396                   t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
00397                }
00398                   // Protect-section icon:
00399                if ($protectSection) {
00400                   $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_sub5.gif');
00401                   if ($ol_im<0)  return $iconfile;
00402                   t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
00403                }
00404 
00405                   // Create the image as file, destroy GD image and return:
00406                @t3lib_iconWorks::imagemake($im, $path);
00407                t3lib_div::gif_compress($path, 'IM');
00408                ImageDestroy($im);
00409                return $mainpath;
00410             } else {
00411                return $iconfile;
00412             }
00413          } else {
00414             return $GLOBALS['BACK_PATH'].'gfx/fileicons/default.gif';
00415          }
00416       }
00417    }
00418 
00444    function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)  {
00445       if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_2'] && $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {  // Maybe I'll have to change this if GD2/gif does not work either...
00446          if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']) {
00447             $cmd=$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'].
00448                   ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']?$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']:'combine').
00449                   ' -compose over ';
00450             $tempBaseName = PATH_site.'typo3temp/ICRZ_'.md5(uniqid('.'));
00451 
00452             ImagePng($im, $tempBaseName.'_im.png');
00453             ImagePng($cpImg, $tempBaseName.'_cpImg.png');
00454             exec($cmd.
00455                $tempBaseName.'_cpImg.png '.
00456                $tempBaseName.'_im.png '.
00457                $tempBaseName.'_out.png '
00458             );
00459             $im = imagecreatefrompng($tempBaseName.'_out.png');
00460             unlink($tempBaseName.'_im.png');
00461             unlink($tempBaseName.'_cpImg.png');
00462             unlink($tempBaseName.'_out.png');
00463          }
00464       } else {
00465          imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
00466       }
00467    }
00468 
00477    function imagecreatefrom($file)  {
00478       $file = t3lib_div::read_png_gif($file,$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
00479       if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])  {
00480          return $file ? imagecreatefrompng($file) : -1;
00481       } else {
00482          return $file ? imagecreatefromgif($file) : -1;
00483       }
00484    }
00485 
00494    function imagemake($im, $path)   {
00495       if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])  {
00496          @ImagePng($im, $path);
00497       } else {
00498          @ImageGif($im, $path);
00499       }
00500    }
00501 }
00502 ?>

Generated on Sun Oct 3 01:05:48 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913