Documentation TYPO3 par Ameos

class.t3lib_iconworks.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2006 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                         // Shadow:
00126                 if ($TCA[$table]['ctrl']['versioningWS'] && (int)$row['t3ver_state']===1)       {
00127                         return 'gfx/i/shadow_hide.png';
00128                 }
00129                 if ($TCA[$table]['ctrl']['versioningWS'] && (int)$row['t3ver_state']===2)       {
00130                         return 'gfx/i/shadow_delete.png';
00131                 }
00132 
00133                         // First, find the icon file name. This can depend on configuration in TCA, field values and more:
00134                 if ($table=='pages')    {
00135                         if ($row['nav_hide'] && ($row['doktype']==1||$row['doktype']==2))       $row['doktype']=5;      // Workaround to change the icon if "Hide in menu" was set
00136 
00137                         if (!$iconfile = $PAGES_TYPES[$row['doktype']]['icon']) {
00138                                 $iconfile = $PAGES_TYPES['default']['icon'];
00139                         }
00140                         if ($row['module'] && $ICON_TYPES[$row['module']]['icon'])      {
00141                                 $iconfile = $ICON_TYPES[$row['module']]['icon'];
00142                         }
00143                 } else {
00144                         if (!$iconfile = $TCA[$table]['ctrl']['typeicons'][$row[$TCA[$table]['ctrl']['typeicon_column']]])      {
00145                                 $iconfile = (($TCA[$table]['ctrl']['iconfile']) ? $TCA[$table]['ctrl']['iconfile'] : $table.'.gif');
00146                         }
00147                 }
00148 
00149                         // Setting path of iconfile if not already set. Default is "gfx/i/"
00150                 if (!strstr($iconfile,'/'))     {
00151                         $iconfile = 'gfx/i/'.$iconfile;
00152                 }
00153 
00154                         // Setting the absolute path where the icon should be found as a file:
00155                 if (substr($iconfile,0,3)=='../')       {
00156                         $absfile=PATH_site.substr($iconfile,3);
00157                 } else {
00158                         $absfile=PATH_typo3.$iconfile;
00159                 }
00160 
00161                         // Initializing variables, all booleans except otherwise stated:
00162                 $hidden = FALSE;
00163                 $timing = FALSE;
00164                 $futuretiming = FALSE;
00165                 $user = FALSE;                          // In fact an integer value...
00166                 $deleted = FALSE;
00167                 $protectSection = FALSE;        // Set, if a page-record (only pages!) has the extend-to-subpages flag set.
00168                 $noIconFound = $row['_NO_ICON_FOUND'] ? TRUE : FALSE;
00169                 // + $shaded which is also boolean!
00170 
00171                         // Icon state based on "enableFields":
00172                 if (is_array($TCA[$table]['ctrl']['enablecolumns']))    {
00173                         $enCols = $TCA[$table]['ctrl']['enablecolumns'];
00174                                 // If "hidden" is enabled:
00175                         if ($enCols['disabled'])        { if ($row[$enCols['disabled']]) { $hidden = TRUE; }}
00176                                 // If a "starttime" is set and higher than current time:
00177                         if ($enCols['starttime'])       { if (time() < intval($row[$enCols['starttime']]))      { $timing = TRUE; }}
00178                                 // If an "endtime" is set:
00179                         if ($enCols['endtime']) {
00180                                 if (intval($row[$enCols['endtime']]) > 0)       {
00181                                         if (intval($row[$enCols['endtime']]) < time())  {
00182                                                 $timing = TRUE; // End-timing applies at this point.
00183                                         } else {
00184                                                 $futuretiming = TRUE;           // End-timing WILL apply in the future for this element.
00185                                         }
00186                                 }
00187                         }
00188                                 // If a user-group field is set:
00189                         if ($enCols['fe_group'])        {
00190                                 $user = $row[$enCols['fe_group']];
00191                                 if ($user && $doNotRenderUserGroupNumber)       $user=100;      // Limit for user number rendering!
00192                         }
00193                 }
00194 
00195                         // If "deleted" flag is set (only when listing records which are also deleted!)
00196                 if ($col=$row[$TCA[$table]['ctrl']['delete']])  {
00197                         $deleted = TRUE;
00198                 }
00199                         // Detecting extendToSubpages (for pages only)
00200                 if ($table=='pages' && $row['extendToSubpages'] && ($hidden || $timing || $futuretiming || $user))      {
00201                         $protectSection = TRUE;
00202                 }
00203 
00204                         // If ANY of the booleans are set it means we have to alter the icon:
00205                 if ($hidden || $timing || $futuretiming || $user || $deleted || $shaded || $noIconFound)        {
00206                         $flags='';
00207                         $string='';
00208                         if ($deleted)   {
00209                                 $string='deleted';
00210                                 $flags='d';
00211                         } elseif ($noIconFound) {       // This is ONLY for creating icons with "?" on easily...
00212                                 $string='no_icon_found';
00213                                 $flags='x';
00214                         } else {
00215                                 if ($hidden) $string.='hidden';
00216                                 if ($timing) $string.='timing';
00217                                 if (!$string && $futuretiming) {
00218                                         $string='futuretiming';
00219                                 }
00220 
00221                                 $flags.=
00222                                         ($hidden ? 'h' : '').
00223                                         ($timing ? 't' : '').
00224                                         ($futuretiming ? 'f' : '').
00225                                         ($user ? 'u' : '').
00226                                         ($protectSection ? 'p' : '').
00227                                         ($shaded ? 's' : '');
00228                         }
00229 
00230                                 // Create tagged icon file name:
00231                         $iconFileName_stateTagged = ereg_replace('.([[:alnum:]]+)$','__'.$flags.'.\1',basename($iconfile));
00232 
00233                                 // 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)
00234                         if (@is_file(dirname($absfile).'/'.$iconFileName_stateTagged))  {       // Look for [iconname]_xxxx.[ext]
00235                                 return dirname($iconfile).'/'.$iconFileName_stateTagged;
00236                         } elseif ($doNotGenerateIcon)   {               // If no icon generation can be done, try to look for the _X icon:
00237                                 $iconFileName_X = ereg_replace('.([[:alnum:]]+)$','__x.\1',basename($iconfile));
00238                                 if (@is_file(dirname($absfile).'/'.$iconFileName_X))    {
00239                                         return dirname($iconfile).'/'.$iconFileName_X;
00240                                 } else {
00241                                         return 'gfx/i/no_icon_found.gif';
00242                                 }
00243                         } else {        // Otherwise, create the icon:
00244                                 $theRes = t3lib_iconWorks::makeIcon($GLOBALS['BACK_PATH'].$iconfile, $string, $user, $protectSection, $absfile, $iconFileName_stateTagged);
00245                                 return $theRes;
00246                         }
00247                 } else {
00248                         return $iconfile;
00249                 }
00250         }
00251 
00264         function skinImg($backPath,$src,$wHattribs='',$outputMode=0)    {
00265 
00266                         // Setting source key. If the icon is refered to inside an extension, we homogenize the prefix to "ext/":
00267                 $srcKey = ereg_replace('^(\.\.\/typo3conf\/ext|sysext|ext)\/','ext/',$src);
00268 #if ($src!=$srcKey)debug(array($src,$srcKey));
00269 
00270                         // LOOKING for alternative icons:
00271                 if ($GLOBALS['TBE_STYLES']['skinImg'][$srcKey]) {       // Slower or faster with is_array()? Could be used.
00272                         list($src,$wHattribs) = $GLOBALS['TBE_STYLES']['skinImg'][$srcKey];
00273                 } elseif ($GLOBALS['TBE_STYLES']['skinImgAutoCfg'])     {       // Otherwise, test if auto-detection is enabled:
00274 
00275                                 // Search for alternative icon automatically:
00276                         $fExt = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['forceFileExtension'];
00277                         $scaleFactor = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] ? $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['scaleFactor'] : 1;   // Scaling factor
00278                         $lookUpName = $fExt ? ereg_replace('\.[[:alnum:]]+$','',$srcKey).'.'.$fExt : $srcKey;   // Set filename to look for
00279 
00280                                 // If file is found:
00281                         if (@is_file($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName))   {       // If there is a file...
00282                                 $iInfo = @getimagesize($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['absDir'].$lookUpName); // Get width/height:
00283 
00284                                         // Set $src and $wHattribs:
00285                                 $src = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'].$lookUpName;
00286                                 $wHattribs = 'width="'.round($iInfo[0]*$scaleFactor).'" height="'.round($iInfo[1]*$scaleFactor).'"';
00287                         }
00288 
00289                                 // 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.
00290                         $GLOBALS['TBE_STYLES']['skinImg'][$srcKey] = array($src,$wHattribs);            // Set default...
00291                 }
00292 
00293                         // DEBUG: This doubles the size of all icons - for testing/debugging:
00294 #               if (ereg('^width="([0-9]+)" height="([0-9]+)"$',$wHattribs,$reg))       $wHattribs='width="'.($reg[1]*2).'" height="'.($reg[2]*2).'"';
00295 
00296 
00297                         // rendering disabled (greyed) icons using _i (inactive) as name suffix ("_d" is already used)
00298                 $matches = array();
00299                 $srcBasename = basename($src);
00300                 if (preg_match('/(.*)_i(\....)$/', $srcBasename, $matches)) {
00301                         $temp_path = dirname(PATH_thisScript).'/';
00302                         if(!@is_file($temp_path.$backPath.$src)) {
00303                                 $srcOrg = preg_replace('/_i'.preg_quote($matches[2]).'$/', $matches[2], $src);
00304                                 $src = t3lib_iconWorks::makeIcon($backPath.$srcOrg, 'disabled', 0, false, $temp_path.$backPath.$srcOrg, $srcBasename);
00305                         }
00306                 }
00307 
00308 
00309                         // Return icon source/wHattributes:
00310                 $output = '';
00311                 switch($outputMode)     {
00312                         case 0:
00313                                 $output = ' src="'.$backPath.$src.'" '.$wHattribs;
00314                         break;
00315                         case 1:
00316                                 $output = $backPath.$src;
00317                         break;
00318                         case 2:
00319                                 $output = $wHattribs;
00320                         break;
00321                 }
00322                 return $output;
00323         }
00324 
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335         /***********************************
00336          *
00337          * Other functions
00338          *
00339          ***********************************/
00340 
00353         function makeIcon($iconfile,$mode, $user, $protectSection,$absFile,$iconFileName_stateTagged)   {
00354                 $iconFileName = 'icon_'.t3lib_div::shortMD5($iconfile.'|'.$mode.'|-'.$user.'|'.$protectSection).'_'.$iconFileName_stateTagged.'.'.($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']?'png':'gif');
00355                 $mainpath = '../typo3temp/'.$iconFileName;
00356                 $path = PATH_site.'typo3temp/'.$iconFileName;
00357 
00358 
00359                 if (@file_exists(PATH_typo3.'icons/'.$iconFileName))    {       // Returns if found in typo3/icons/
00360                         return 'icons/'.$iconFileName;
00361                 } elseif (@file_exists($path))  {       // Returns if found in ../typo3temp/icons/
00362                         return $mainpath;
00363                 } else {        // Makes icon:
00364                         if (@file_exists($absFile))     {
00365                                 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'])        {
00366 
00367                                                 // Create image pointer, if possible
00368                                         $im = t3lib_iconworks::imagecreatefrom($absFile);
00369                                         if ($im<0)      return $iconfile;
00370 
00371                                                 // Converting to gray scale, dimming the icon:
00372                                         if (($mode=='disabled') OR ($mode!='futuretiming' && $mode!='no_icon_found' && !(!$mode && $user)))     {
00373                                                 for ($c=0; $c<ImageColorsTotal($im); $c++)      {
00374                                                         $cols = ImageColorsForIndex($im,$c);
00375                                                         $newcol = round(($cols['red']+$cols['green']+$cols['blue'])/3);
00376                                                         $lighten = ($mode=='disabled') ? 2.5 : 2;
00377                                                         $newcol = round(255-((255-$newcol)/$lighten));
00378                                                         ImageColorSet($im,$c,$newcol,$newcol,$newcol);
00379                                                 }
00380                                         }
00381                                                 // Applying user icon, if there are access control on the item:
00382                                         if ($user)      {
00383                                                 if ($user < 100)        {       // Apply user number only if lower than 100
00384                                                         $black = ImageColorAllocate($im, 0,0,0);
00385                                                         imagefilledrectangle($im, 0,0,(($user>10)?9:5),8,$black);
00386 
00387                                                         $white = ImageColorAllocate($im, 255,255,255);
00388                                                         imagestring($im, 1, 1, 1, $user, $white);
00389                                                 }
00390 
00391                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_group.gif');
00392                                                 if ($ol_im<0)   return $iconfile;
00393 
00394                                                 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
00395                                         }
00396                                                 // Applying overlay based on mode:
00397                                         if ($mode)      {
00398                                                 unset($ol_im);
00399                                                 switch($mode)   {
00400                                                         case 'deleted':
00401                                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_deleted.gif');
00402                                                         break;
00403                                                         case 'futuretiming':
00404                                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
00405                                                         break;
00406                                                         case 'timing':
00407                                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_timing.gif');
00408                                                         break;
00409                                                         case 'hiddentiming':
00410                                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden_timing.gif');
00411                                                         break;
00412                                                         case 'no_icon_found':
00413                                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_no_icon_found.gif');
00414                                                         break;
00415                                                         case 'disabled':
00416                                                                         // is already greyed - nothing more
00417                                                                 $ol_im = 0;
00418                                                         break;
00419                                                         case 'hidden':
00420                                                         default:
00421                                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_hidden.gif');
00422                                                         break;
00423                                                 }
00424                                                 if ($ol_im<0)   return $iconfile;
00425                                                 if ($ol_im) {
00426                                                         t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
00427                                                 }
00428                                         }
00429                                                 // Protect-section icon:
00430                                         if ($protectSection)    {
00431                                                 $ol_im = t3lib_iconworks::imagecreatefrom($GLOBALS['BACK_PATH'].'gfx/overlay_sub5.gif');
00432                                                 if ($ol_im<0)   return $iconfile;
00433                                                 t3lib_iconworks::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im));
00434                                         }
00435 
00436                                                 // Create the image as file, destroy GD image and return:
00437                                         @t3lib_iconWorks::imagemake($im, $path);
00438                                         t3lib_div::gif_compress($path, 'IM');
00439                                         ImageDestroy($im);
00440                                         return $mainpath;
00441                                 } else {
00442                                         return $iconfile;
00443                                 }
00444                         } else {
00445                                 return $GLOBALS['BACK_PATH'].'gfx/fileicons/default.gif';
00446                         }
00447                 }
00448         }
00449 
00475         function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)       {
00476                 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...
00477                         if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'])      {
00478 
00479                                 $tempBaseName = PATH_site.'typo3temp/ICRZ_'.md5(uniqid('.'));
00480 
00481                                 ImagePng($im, $tempBaseName.'_im.png');
00482                                 ImagePng($cpImg, $tempBaseName.'_cpImg.png');
00483 
00484                                 $cmd = t3lib_div::imageMagickCommand('combine', '-compose over '.$tempBaseName.'_cpImg.png '.$tempBaseName.'_im.png '.$tempBaseName.'_out.png ');
00485                                 exec($cmd);
00486 
00487                                 $im = imagecreatefrompng($tempBaseName.'_out.png');
00488                                 unlink($tempBaseName.'_im.png');
00489                                 unlink($tempBaseName.'_cpImg.png');
00490                                 unlink($tempBaseName.'_out.png');
00491                         }
00492                 } else {
00493                         imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
00494                 }
00495         }
00496 
00505         function imagecreatefrom($file) {
00506                 $file = t3lib_div::read_png_gif($file,$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']);
00507                 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])    {
00508                         return $file ? imagecreatefrompng($file) : -1;
00509                 } else {
00510                         return $file ? imagecreatefromgif($file) : -1;
00511                 }
00512         }
00513 
00522         function imagemake($im, $path)  {
00523                 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])    {
00524                         @ImagePng($im, $path);
00525                 } else {
00526                         @ImageGif($im, $path);
00527                 }
00528         }
00529 }
00530 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6