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