eZ Publish  [trunk]
ezwordtoimageoperator.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZWordtoimageoperator class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZWordToImageOperator ezwordtoimageoperator.php
00013   \brief The class eZWordToImageOperator does
00014 
00015 */
00016 class eZWordToImageOperator
00017 {
00018     /*!
00019      Initializes the object with the name $name, default is "wash".
00020     */
00021     function eZWordToImageOperator()
00022     {
00023         $this->Operators = array( "wordtoimage",
00024                                   "mimetype_icon", "class_icon", "classgroup_icon", "action_icon", "icon",
00025                                   "flag_icon", "icon_info" );
00026         $this->IconInfo = array();
00027     }
00028 
00029     /*!
00030       Returns the template operators.
00031     */
00032     function operatorList()
00033     {
00034         return $this->Operators;
00035     }
00036 
00037     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement )
00038     {
00039         switch ( $operatorName )
00040         {
00041             case "wordtoimage":
00042             {
00043                 $ini = eZINI::instance("wordtoimage.ini");
00044                 $iconRoot = $ini->variable( 'WordToImageSettings', 'IconRoot' );
00045 
00046                 $replaceText = $ini->variable( 'WordToImageSettings', 'ReplaceText' );
00047                 $replaceIcon = $ini->variable( 'WordToImageSettings', 'ReplaceIcon' );
00048 
00049                 $wwwDirPrefix = "";
00050                 if ( strlen( eZSys::wwwDir() ) > 0 )
00051                     $wwwDirPrefix = eZSys::wwwDir() . "/";
00052                 foreach( $replaceIcon as $icon )
00053                 {
00054                     // Issue 015718, constructing alt text from icon name
00055                     $aReplaceIconName = explode( '.', $icon );
00056                     $altText = $aReplaceIconName[0];
00057                     $icons[] = '<img src="' . $wwwDirPrefix . $iconRoot .'/' . $icon . '" alt="'.$altText.'"/>';
00058                 }
00059 
00060                 $operatorValue = str_replace( $replaceText, $icons, $operatorValue );
00061             } break;
00062 
00063             // icon_info( <type> ) => array() containing:
00064             // - repository - Repository path
00065             // - theme - Theme name
00066             // - theme_path - Theme path
00067             // - size_path_list - Associative array of size paths
00068             // - size_info_list - Associative array of size info (width and height)
00069             // - icons - Array of icon files, relative to theme and size path
00070             // - default - Default icon file, relative to theme and size path
00071             case 'icon_info':
00072             {
00073                 if ( !isset( $operatorParameters[0] ) )
00074                 {
00075                     $tpl->missingParameter( $operatorName, 'type' );
00076                     return;
00077                 }
00078                 $type = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace );
00079 
00080                 // Check if we have it cached
00081                 if ( isset( $this->IconInfo[$type] ) )
00082                 {
00083                     $operatorValue = $this->IconInfo[$type];
00084                     return;
00085                 }
00086 
00087                 $ini = eZINI::instance( 'icon.ini' );
00088                 $repository = $ini->variable( 'IconSettings', 'Repository' );
00089                 $theme = $ini->variable( 'IconSettings', 'Theme' );
00090                 $groups = array( 'mimetype' => 'MimeIcons',
00091                                  'class' => 'ClassIcons',
00092                                  'classgroup' => 'ClassGroupIcons',
00093                                  'action' => 'ActionIcons',
00094                                  'icon' => 'Icons' );
00095                 $configGroup = $groups[$type];
00096                 $mapNames = array( 'mimetype' => 'MimeMap',
00097                                    'class' => 'ClassMap',
00098                                    'classgroup' => 'ClassGroupMap',
00099                                    'action' => 'ActionMap',
00100                                    'icon' => 'IconMap' );
00101                 $mapName = $mapNames[$type];
00102 
00103                 // Check if the specific icon type has a theme setting
00104                 if ( $ini->hasVariable( $configGroup, 'Theme' ) )
00105                 {
00106                     $theme = $ini->variable( $configGroup, 'Theme' );
00107                 }
00108 
00109                 // Load icon settings from the theme
00110                 $themeINI = eZINI::instance( 'icon.ini', $repository . '/' . $theme );
00111 
00112                 $sizes = $themeINI->variable( 'IconSettings', 'Sizes' );
00113                 if ( $ini->hasVariable( 'IconSettings', 'Sizes' ) )
00114                 {
00115                     $sizes = array_merge( $sizes,
00116                                           $ini->variable( 'IconSettings', 'Sizes' ) );
00117                 }
00118 
00119                 $sizePathList = array();
00120                 $sizeInfoList = array();
00121 
00122                 if ( is_array( $sizes ) )
00123                 {
00124                     foreach ( $sizes as $key => $size )
00125                     {
00126                         $pathDivider = strpos( $size, ';' );
00127                         if ( $pathDivider !== false )
00128                         {
00129                             $sizePath = substr( $size, $pathDivider + 1 );
00130                             $size = substr( $size, 0, $pathDivider );
00131                         }
00132                         else
00133                         {
00134                             $sizePath = $size;
00135                         }
00136 
00137                         $width = false;
00138                         $height = false;
00139                         $xDivider = strpos( $size, 'x' );
00140                         if ( $xDivider !== false )
00141                         {
00142                             $width = (int)substr( $size, 0, $xDivider );
00143                             $height = (int)substr( $size, $xDivider + 1 );
00144                         }
00145                         $sizePathList[$key] = $sizePath;
00146                         $sizeInfoList[$key] = array( $width, $height );
00147                     }
00148                 }
00149 
00150                 $map = array();
00151 
00152                 // Load mapping from theme
00153                 if ( $themeINI->hasVariable( $configGroup, $mapName ) )
00154                 {
00155                     $map = array_merge( $map,
00156                                         $themeINI->variable( $configGroup, $mapName ) );
00157                 }
00158                 // Load override mappings if they exist
00159                 if ( $ini->hasVariable( $configGroup, $mapName ) )
00160                 {
00161                     $map = array_merge( $map,
00162                                         $ini->variable( $configGroup, $mapName ) );
00163                 }
00164 
00165                 $default = false;
00166                 if ( $themeINI->hasVariable( $configGroup, 'Default' ) )
00167                     $default = $themeINI->variable( $configGroup, 'Default' );
00168                 if ( $ini->hasVariable( $configGroup, 'Default' ) )
00169                     $default = $ini->variable( $configGroup, 'Default' );
00170 
00171                 // Build return value
00172                 $iconInfo = array( 'repository' => $repository,
00173                                    'theme' => $theme,
00174                                    'theme_path' => $repository . '/' . $theme,
00175                                    'size_path_list' => $sizePathList,
00176                                    'size_info_list' => $sizeInfoList,
00177                                    'icons' => $map,
00178                                    'default' => $default );
00179 
00180                 $this->IconInfo[$type] = $iconInfo;
00181                 $operatorValue = $iconInfo;
00182             } break;
00183 
00184             case 'flag_icon':
00185             {
00186                 $ini = eZINI::instance( 'icon.ini' );
00187                 $repository = $ini->variable( 'FlagIcons', 'Repository' );
00188                 $theme = $ini->variable( 'FlagIcons', 'Theme' );
00189 
00190                 // Load icon settings from the theme
00191                 $themeINI = eZINI::instance( 'icon.ini', $repository . '/' . $theme );
00192 
00193                 $iconFormat = $themeINI->variable( 'FlagIcons', 'IconFormat' );
00194                 if ( $ini->hasVariable( 'FlagIcons', 'IconFormat' ) )
00195                 {
00196                     $iconFormat = $ini->variable( 'FlagIcons', 'IconFormat' );
00197                 }
00198 
00199                 $icon = $operatorValue . '.' . $iconFormat;
00200                 $iconPath = $repository . '/' . $theme . '/' . $icon;
00201                 if ( !is_readable( $iconPath ) )
00202                 {
00203                     $defaultIcon = $themeINI->variable( 'FlagIcons', 'DefaultIcon' );
00204                     $iconPath = $repository . '/' . $theme . '/' . $defaultIcon . '.' . $iconFormat;
00205                 }
00206                 if ( strlen( eZSys::wwwDir() ) > 0 )
00207                     $wwwDirPrefix = eZSys::wwwDir() . '/';
00208                 else
00209                     $wwwDirPrefix = '/';
00210                 $operatorValue = $wwwDirPrefix . $iconPath;
00211             } break;
00212 
00213             case 'mimetype_icon':
00214             case 'class_icon':
00215             case 'classgroup_icon':
00216             case 'action_icon':
00217             case 'icon':
00218             {
00219                 // Determine whether we should return only the image URI instead of the whole HTML code.
00220                 if ( isset( $operatorParameters[2] ) )
00221                     $returnURIOnly = $tpl->elementValue( $operatorParameters[2], $rootNamespace, $currentNamespace );
00222                 else
00223                     $returnURIOnly = false;
00224 
00225                 $ini = eZINI::instance( 'icon.ini' );
00226                 $repository = $ini->variable( 'IconSettings', 'Repository' );
00227                 $theme = $ini->variable( 'IconSettings', 'Theme' );
00228                 $groups = array( 'mimetype_icon' => 'MimeIcons',
00229                                  'class_icon' => 'ClassIcons',
00230                                  'classgroup_icon' => 'ClassGroupIcons',
00231                                  'action_icon' => 'ActionIcons',
00232                                  'icon' => 'Icons' );
00233                 $configGroup = $groups[$operatorName];
00234 
00235                 // Check if the specific icon type has a theme setting
00236                 if ( $ini->hasVariable( $configGroup, 'Theme' ) )
00237                 {
00238                     $theme = $ini->variable( $configGroup, 'Theme' );
00239                 }
00240 
00241                 // Load icon settings from the theme
00242                 $themeINI = eZINI::instance( 'icon.ini', $repository . '/' . $theme );
00243 
00244                 if ( isset( $operatorParameters[0] ) )
00245                 {
00246                     $sizeName = $tpl->elementValue( $operatorParameters[0], $rootNamespace, $currentNamespace );
00247                 }
00248                 else
00249                 {
00250                     $sizeName = $ini->variable( 'IconSettings', 'Size' );
00251                     // Check if the specific icon type has a size setting
00252                     if ( $ini->hasVariable( $configGroup, 'Size' ) )
00253                     {
00254                         $theme = $ini->variable( $configGroup, 'Size' );
00255                     }
00256                 }
00257 
00258                 $sizes = $themeINI->variable( 'IconSettings', 'Sizes' );
00259                 if ( $ini->hasVariable( 'IconSettings', 'Sizes' ) )
00260                 {
00261                     $sizes = array_merge( $sizes,
00262                                           $ini->variable( 'IconSettings', 'Sizes' ) );
00263                 }
00264 
00265                 if ( isset( $sizes[$sizeName] ) )
00266                 {
00267                     $size = $sizes[$sizeName];
00268                 }
00269                 else
00270                 {
00271                     $size = $sizes[0];
00272                 }
00273 
00274                 $pathDivider = strpos( $size, ';' );
00275                 if ( $pathDivider !== false )
00276                 {
00277                     $sizePath = substr( $size, $pathDivider + 1 );
00278                     $size = substr( $size, 0, $pathDivider );
00279                 }
00280                 else
00281                 {
00282                     $sizePath = $size;
00283                 }
00284 
00285                 $width = false;
00286                 $height = false;
00287                 $xDivider = strpos( $size, 'x' );
00288                 if ( $xDivider !== false )
00289                 {
00290                     $width = (int)substr( $size, 0, $xDivider );
00291                     $height = (int)substr( $size, $xDivider + 1 );
00292                 }
00293 
00294                 if ( isset( $operatorParameters[1] ) )
00295                 {
00296                     $altText = $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace );
00297                 }
00298                 else
00299                 {
00300                     $altText = $operatorValue;
00301                 }
00302 
00303                 if ( $operatorName == 'mimetype_icon' )
00304                 {
00305                     $icon = $this->iconGroupMapping( $ini, $themeINI,
00306                                                      'MimeIcons', 'MimeMap',
00307                                                      strtolower( $operatorValue ) );
00308                 }
00309                 else if ( $operatorName == 'class_icon' )
00310                 {
00311                     $icon = $this->iconDirectMapping( $ini, $themeINI,
00312                                                       'ClassIcons', 'ClassMap',
00313                                                       strtolower( $operatorValue ) );
00314                 }
00315                 else if ( $operatorName == 'classgroup_icon' )
00316                 {
00317                     $icon = $this->iconDirectMapping( $ini, $themeINI,
00318                                                       'ClassGroupIcons', 'ClassGroupMap',
00319                                                       strtolower( $operatorValue ) );
00320                 }
00321                 else if ( $operatorName == 'action_icon' )
00322                 {
00323                     $icon = $this->iconDirectMapping( $ini, $themeINI,
00324                                                       'ActionIcons', 'ActionMap',
00325                                                       strtolower( $operatorValue ) );
00326                 }
00327                 else if ( $operatorName == 'icon' )
00328                 {
00329                     $icon = $this->iconDirectMapping( $ini, $themeINI,
00330                                                       'Icons', 'IconMap',
00331                                                       strtolower( $operatorValue ) );
00332                 }
00333 
00334                 $iconPath = '/' . $repository . '/' . $theme;
00335                 $iconPath .= '/' . $sizePath;
00336                 $iconPath .= '/' . $icon;
00337 
00338                 $wwwDirPrefix = "";
00339                 if ( strlen( eZSys::wwwDir() ) > 0 )
00340                     $wwwDirPrefix = eZSys::wwwDir();
00341                 $sizeText = '';
00342                 if ( $width !== false and $height !== false )
00343                 {
00344                     $sizeText = ' width="' . $width . '" height="' . $height . '"';
00345                 }
00346 
00347                 // The class will be detected by ezpngfix.js, which will force alpha blending in IE.
00348                 if ( ( !isset( $sizeName ) || $sizeName == 'normal' || $sizeName == 'original' ) && strstr( strtolower( $iconPath ), ".png" ) )
00349                 {
00350                     $class = 'class="transparent-png-icon" ';
00351                 }
00352                 else
00353                 {
00354                     $class = '';
00355                 }
00356 
00357                 if ( $returnURIOnly )
00358                     $operatorValue = $wwwDirPrefix . $iconPath;
00359                 else
00360                     $operatorValue = '<img ' . $class . 'src="' . $wwwDirPrefix . $iconPath . '"' . $sizeText . ' alt="' .  htmlspecialchars( $altText ) . '" title="' . htmlspecialchars( $altText ) . '" />';
00361             } break;
00362 
00363             default:
00364             {
00365                 eZDebug::writeError( "Unknown operator: $operatorName", "ezwordtoimageoperator.php" );
00366             }
00367 
00368         }
00369 
00370     }
00371 
00372     /*!
00373      \private
00374      Tries to find icon file by considering \a $matchItem as a single value.
00375 
00376      It will first try to match the whole \a $matchItem value in the mapping table.
00377 
00378      \return The relative path to the icon file.
00379 
00380      Example
00381      \code
00382      $icon = $this->iconDirectMapping( $ini, $themeINI, 'ClassIcons', 'ClassMap', 'Folder' );
00383      \endcode
00384 
00385      \sa iconGroupMapping
00386     */
00387     function iconDirectMapping( &$ini, &$themeINI, $iniGroup, $mapName, $matchItem )
00388     {
00389         $map = array();
00390 
00391         // Load mapping from theme
00392         if ( $themeINI->hasVariable( $iniGroup, $mapName ) )
00393         {
00394             $map = array_merge( $map,
00395                                 $themeINI->variable( $iniGroup, $mapName ) );
00396         }
00397         // Load override mappings if they exist
00398         if ( $ini->hasVariable( $iniGroup, $mapName ) )
00399         {
00400             $map = array_merge( $map,
00401                                 $ini->variable( $iniGroup, $mapName ) );
00402         }
00403 
00404         $icon = false;
00405         if ( isset( $map[$matchItem] ) )
00406         {
00407             $icon = $map[$matchItem];
00408         }
00409         if ( $icon === false )
00410         {
00411             if ( $themeINI->hasVariable( $iniGroup, 'Default' ) )
00412                 $icon = $themeINI->variable( $iniGroup, 'Default' );
00413             if ( $ini->hasVariable( $iniGroup, 'Default' ) )
00414                 $icon = $ini->variable( $iniGroup, 'Default' );
00415         }
00416         return $icon;
00417     }
00418 
00419     /*!
00420      \private
00421      Tries to find icon file by considering \a $matchItem as a group,
00422      split into two parts and separated by a slash.
00423 
00424      It will first try to match the whole \a $matchItem value and then
00425      the group name.
00426 
00427      \return The relative path to the icon file.
00428 
00429      Example
00430      \code
00431      $icon = $this->iconGroupMapping( $ini, $themeINI, 'MimeIcons', 'MimeMap', 'image/jpeg' );
00432      \endcode
00433 
00434      \sa iconDirectMapping
00435     */
00436     function iconGroupMapping( &$ini, &$themeINI, $iniGroup, $mapName, $matchItem )
00437     {
00438         $map = array();
00439 
00440         // Load mapping from theme
00441         if ( $themeINI->hasVariable( $iniGroup, $mapName ) )
00442         {
00443             $map = array_merge( $map,
00444                                 $themeINI->variable( $iniGroup, $mapName ) );
00445         }
00446         // Load override mappings if they exist
00447         if ( $ini->hasVariable( $iniGroup, $mapName ) )
00448         {
00449             $map = array_merge( $map,
00450                                 $ini->variable( $iniGroup, $mapName ) );
00451         }
00452 
00453         $icon = false;
00454         // See if we have a match for the whole match item
00455         if ( isset( $map[$matchItem] ) )
00456         {
00457             $icon = $map[$matchItem];
00458         }
00459         else
00460         {
00461             // If not we have to check the group (first part)
00462             $pos = strpos( $matchItem, '/' );
00463             if ( $pos !== false )
00464             {
00465                 $mimeGroup = substr( $matchItem, 0, $pos );
00466                 if ( isset( $map[$mimeGroup] ) )
00467                 {
00468                     $icon = $map[$mimeGroup];
00469                 }
00470             }
00471         }
00472 
00473         // No icon? If so use default
00474         if ( $icon === false )
00475         {
00476             if ( $themeINI->hasVariable( $iniGroup, 'Default' ) )
00477                 $icon = $themeINI->variable( $iniGroup, 'Default' );
00478             if ( $ini->hasVariable( $iniGroup, 'Default' ) )
00479                 $icon = $ini->variable( $iniGroup, 'Default' );
00480         }
00481         return $icon;
00482     }
00483 
00484     /// \privatesection
00485     public $Operators;
00486     public $IconInfo;
00487 }
00488 ?>