eZ Publish  [4.0]
ezsimpletagsoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSimpleTagsOperator class
00004 //
00005 // Created on: <07-Feb-2003 09:39:55 bf>
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 class eZSimpleTagsOperator
00032 {
00033     /*!
00034      */
00035     function eZSimpleTagsOperator( $name = 'simpletags' )
00036     {
00037         $this->Operators = array( $name );
00038     }
00039 
00040     /*!
00041      Returns the operators in this class.
00042     */
00043     function operatorList()
00044     {
00045         return $this->Operators;
00046     }
00047 
00048     /*!
00049      See eZTemplateOperator::namedParameterList()
00050     */
00051     function namedParameterList()
00052     {
00053         return array( 'listname' => array( 'type' => 'string',
00054                                            'required' => false,
00055                                            'default' => false ) );
00056     }
00057 
00058     /*!
00059      \private
00060 
00061      Makes sure extra includes are loaded (include_once) so extra functions can be used.
00062      \note This function will only run one time, if called multiple times it will simple return
00063     */
00064     function initializeIncludes()
00065     {
00066         // If we have this global variable we shouldn't do any processing
00067         if ( !empty( $GLOBALS['eZSimpleTagsInit'] ) )
00068             return;
00069 
00070         $GLOBALS['eZSimpleTagsInit'] = true;
00071         $ini = eZINI::instance( 'template.ini' );
00072         $extensions = $ini->variable( 'SimpleTagsOperator', 'Extensions' );
00073         //include_once( 'lib/ezutils/classes/ezextension.php' );
00074         $pathList = eZExtension::expandedPathList( $extensions, 'simpletags' );
00075         $includeList = $ini->variable( 'SimpleTagsOperator', 'IncludeList' );
00076 
00077         foreach ( $includeList as $includeFile )
00078         {
00079             foreach ( $pathList as $path )
00080             {
00081                 $file = $path . '/' . $includeFile;
00082                 if ( file_exists( $file ) )
00083                 {
00084                     include_once( $file );
00085                 }
00086             }
00087         }
00088     }
00089 
00090     /*!
00091      \reimp
00092     */
00093     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
00094     {
00095         $elements = preg_split( "#(</?[a-zA-Z0-9_-]+>)#",
00096                                 $operatorValue,
00097                                 false,
00098                                 PREG_SPLIT_DELIM_CAPTURE );
00099         $newElements = array();
00100         $i = 0;
00101         foreach ( $elements as $element )
00102         {
00103             if ( ( $i % 2 ) == 1 )
00104             {
00105                 $tagText = $element;
00106                 if ( preg_match( "#<(/?)([a-zA-Z0-9_-]+)>#", $tagText, $matches ) )
00107                 {
00108                     $isEndTag = false;
00109                     if ( $matches[1] )
00110                         $isEndTag = true;
00111                     $tag = $matches[2];
00112                     $element = array( $tag, $isEndTag, $tagText );
00113                 }
00114             }
00115             $newElements[] = $element;
00116             ++$i;
00117         }
00118 
00119         $tagListName = 'TagList';
00120         if ( $namedParameters['listname'] )
00121             $tagListName .= '_' . $namedParameters['listname'];
00122 
00123         $this->initializeIncludes();
00124 
00125         $tagMap = array();
00126         $ini = eZINI::instance( 'template.ini' );
00127         $tagList = $ini->variable( 'SimpleTagsOperator', $tagListName );
00128         foreach ( $tagList as $tag => $tagItem )
00129         {
00130             $elements = explode( ';', $tagItem );
00131             $pre = $elements[0];
00132             $post = $elements[1];
00133             $phpFunctions = array();
00134             if ( isset( $elements[2] ) )
00135             {
00136                 $phpFunctionList = explode( ',', $elements[2] );
00137                 $phpFunctions = array();
00138                 foreach ( $phpFunctionList as $phpFunction )
00139                 {
00140                     if ( function_exists( $phpFunction ) )
00141                         $phpFunctions[] = $phpFunction;
00142                 }
00143             }
00144             $tagMap[$tag] = array( 'pre' => $pre,
00145                                    'post' => $post,
00146                                    'phpfunctions' => $phpFunctions );
00147         }
00148 
00149         $textPHPFunctions = array( 'htmlspecialchars' );
00150         $textPre = false;
00151         $textPost = false;
00152         if ( isset( $tagMap['text']['pre'] ) )
00153             $textPre = $tagMap['text']['pre'];
00154         if ( isset( $tagMap['text']['post'] ) )
00155             $textPost = $tagMap['text']['post'];
00156         if ( isset( $tagMap['text']['phpfunctions'] ) )
00157             $textPHPFunctions = $tagMap['text']['phpfunctions'];
00158         $textElements = array();
00159         for ( $i = 0; $i < count( $newElements ); ++$i )
00160         {
00161             $element = $newElements[$i];
00162             if ( is_string( $element ) )
00163             {
00164                 $text = $element;
00165                 foreach ( $textPHPFunctions as $textPHPFunction )
00166                 {
00167                     $text = $textPHPFunction( $text );
00168                 }
00169                 $textElements[] = $textPre . $text . $textPost;
00170             }
00171             else if ( is_array( $element ) )
00172             {
00173                 $tag = $element[0];
00174                 $isEndTag = $element[1];
00175                 $originalText = $element[2];
00176                 if ( isset( $tagMap[$tag] ) )
00177                 {
00178                     $tagOptions = $tagMap[$tag];
00179                     $phpFunctions = $tagOptions['phpfunctions'];
00180                     if ( !$isEndTag )
00181                     {
00182                         $tagElements = array();
00183                         for ( $j = $i + 1; $j < count( $newElements ); ++$j )
00184                         {
00185                             $tagElement = $newElements[$j];
00186                             if ( is_string( $tagElement ) )
00187                             {
00188                                 $tagElements[] = $tagElement;
00189                             }
00190                             else if ( is_array( $tagElement ) )
00191                             {
00192                                 if ( $tagElement[0] == $tag and
00193                                      $tagElement[1] )
00194                                 {
00195                                     break;
00196                                 }
00197                                 $text = $tagElement[2];
00198                                 $tagElements[] = $text;
00199                             }
00200                         }
00201                         $i = $j;
00202                         $textElements[] = $tagOptions['pre'];
00203                         $text = implode( '', $tagElements );
00204                         foreach ( $phpFunctions as $phpFunction )
00205                         {
00206                             $text = $phpFunction( $text );
00207                         }
00208                         $textElements[] = $text;
00209                         $textElements[] = $tagOptions['post'];
00210                     }
00211                 }
00212                 else
00213                 {
00214                     $text = $originalText;
00215                     foreach ( $textPHPFunctions as $textPHPFunction )
00216                     {
00217                         $text = $textPHPFunction( $text );
00218                     }
00219                     $textElements[] = $textPre . $text . $textPost;
00220                 }
00221             }
00222         }
00223 
00224         $operatorValue = implode( '', $textElements );
00225     }
00226 
00227     /// \privatesection
00228     public $Operators;
00229 };
00230 
00231 ?>