eZ Publish  [4.0]
eztemplatestringoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateStringOperator class
00004 //
00005 // Created on: <17-Jul-2003 13:00:18 bh>
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 /*!
00032   \class eZTemplateStringOperator eztemplatestringoperator.php
00033   \ingroup eZTemplateOperators
00034 \code
00035 
00036 \endcode
00037 
00038 */
00039 
00040 class eZTemplateStringOperator
00041 {
00042     /*!
00043      Constructor.
00044     */
00045     function eZTemplateStringOperator()
00046     {
00047         $this->Operators = array( 'upcase', 'downcase',
00048                                   'count_words', 'count_chars',
00049                                   'trim', 'break', 'wrap', 'shorten', 'pad',
00050                                   'upfirst', 'upword',
00051                                   'simplify', 'wash',
00052                                   'chr', 'ord');
00053         foreach ( $this->Operators as $operator )
00054         {
00055             $name = $operator . 'Name';
00056             $name[0] = $name[0] & "\xdf";
00057             $this->$name = $operator;
00058         }
00059 
00060         $this->phpMap = array ('upcase' => 'mb_strtoupper,strtoupper',
00061                                'downcase' => 'mb_strtolower,strtolower',
00062                                'break' => 'nl2br',
00063                                'count_chars' => 'mb_strlen,strlen');
00064 
00065         $this->customMap = array ( 'count_words' => array( 'return' => 'int',
00066                                                           'code' => '$result = preg_match_all( "#(\w+)#", $staticValues[0], $dummy );'
00067                                                         ),
00068                                    'chr' => array( 'return' => 'string',
00069                                                    'code' => '$codec = eZTextCodec::instance( "unicode", false );' . "\n" .
00070                                                              '$result = $codec->convertString( $staticValues[0] );'
00071                                                  ),
00072                                    'ord' => array( 'return' => 'string',
00073                                                    'code' => '$codec = eZTextCodec::instance( false, "unicode" );' . "\n" .
00074                                                              '$result = $codec->convertString( $staticValues[0] );'
00075                                                  ),
00076                                    'pad' => array( 'return' => 'string',
00077                                                    'code' => '$result = $paramCount == 2 ? str_pad( $staticValues[0], $staticValues[1] ) : str_pad ( $staticValues[0], $staticValues[1], $staticValues[2] );',
00078                                                    'code2' => '$result = str_pad( $staticValues[0], $staticValues[1] );',
00079                                                    'code3' => '$result = str_pad( $staticValues[0], $staticValues[1], $staticValues[2] );',
00080                                                  ),
00081                                    'trim' => array( 'return' => 'string',
00082                                                     'code' => '$result = $paramCount == 1 ? trim( $staticValues[0] ) : trim ( $staticValues[0], $staticValues[1] );',
00083                                                     'code1' => '$result = trim( $staticValues[0] );',
00084                                                     'code2' => '$result = trim( $staticValues[0], $staticValues[1] );',
00085                                                  ),
00086                                    'wrap' => array( 'return' => 'string',
00087                                                     'code1' => '$result = wordwrap( $staticValues[0] );',
00088                                                     'code2' => '$result = wordwrap( $staticValues[0], $staticValues[1] );',
00089                                                     'code3' => '$result = wordwrap( $staticValues[0], $staticValues[1], $staticValues[2] );',
00090                                                     'code4' => '$result = wordwrap( $staticValues[0], $staticValues[1], $staticValues[2], $staticValues[3] );',
00091                                                  ),
00092                                    'simplify' => array( 'return' => 'string',
00093                                                         'code' => 'if ( $paramCount == 1 )
00094                                                                    {
00095                                                                        $replacer = " ";
00096                                                                    }
00097                                                                    else
00098                                                                    {
00099                                                                        $replacer = $staticValues[1];
00100                                                                    }
00101                                                                    $result = preg_replace( "/".$replacer."{2,}/", $replacer, $staticValues[0] );',
00102                                                         'code1' => '$result = preg_replace( "/ {2,}/", " ", $staticValues[0] );',
00103                                                         'code2' => '$result = preg_replace( "/".$staticValues[1]."{2,}/", $staticValues[1], $staticValues[0] );',
00104                                                       ),
00105                                    'shorten' => array( 'return' => 'string',
00106                                                        'code' => '$length = 80; $seq = "..."; $trimType = "right";
00107                                                                   if ( $paramCount > 1 )
00108                                                                   {
00109                                                                       $length = $staticValues[1];
00110                                                                   }
00111                                                                   if ( $paramCount > 2 )
00112                                                                   {
00113                                                                       $seq = $staticValues[2];
00114                                                                   }
00115                                                                   if ( $paramCount > 3 )
00116                                                                   {
00117                                                                       $trimType = $staticValues[3];
00118                                                                   }
00119                                                                   if ( $trimType === "middle" )
00120                                                                   {
00121                                                                       $appendedStrLen = $strlenFunc( $seq );
00122                                                                       if ( $length > $appendedStrLen && ( $strlenFunc( $staticValues[0] ) > $length ) )
00123                                                                       {
00124                                                                           $operatorValueLength = $strlenFunc( $staticValues[0] );
00125                                                                           $chop = $length - $appendedStrLen;
00126                                                                           $middlePos = (int)($chop / 2);
00127                                                                           $leftPartLength = $middlePos;
00128                                                                           $rightPartLength = $chop - $middlePos;
00129                                                                           $result = trim( $substrFunc( $staticValues[0], 0, $leftPartLength ) . $seq . $substrFunc( $staticValues[0], $operatorValueLength - $rightPartLength, $rightPartLength ) );
00130                                                                       }
00131                                                                       else
00132                                                                       {
00133                                                                           $result = $staticValues[0];
00134                                                                       }
00135                                                                   }
00136                                                                   else // default: trim_type === "right"
00137                                                                   {
00138                                                                       $maxLength = $length - $strlenFunc( $seq );
00139                                                                       if ( ( $strlenFunc( $staticValues[0] ) > $length ) && $strlenFunc( $staticValues[0] ) > $maxLength )
00140                                                                       {
00141                                                                           $result = trim( $substrFunc( $staticValues[0], 0, $maxLength) ) . $seq;
00142                                                                       }
00143                                                                       else
00144                                                                       {
00145                                                                           $result = $staticValues[0];
00146                                                                       }
00147                                                                   }'
00148                                                      ),
00149                                    'upfirst' => array( 'return' => 'string',
00150                                                        'code' => '$i18nIni = eZINI::instance( \'i18n.ini\' );
00151                                                                   $hasMBString = ( $i18nIni->variable( \'CharacterSettings\', \'MBStringExtension\' ) == \'enabled\' and
00152                                                                   function_exists( "mb_strtoupper" ) and
00153                                                                   function_exists( "mb_substr" ) and
00154                                                                   function_exists( "mb_strlen" ) );
00155 
00156                                                                   if ( $hasMBString )
00157                                                                   {
00158                                                                       $encoding = eZTextCodec::internalCharset();
00159                                                                       $firstLetter = mb_strtoupper( mb_substr( $staticValues[0], 0, 1, $encoding ), $encoding );
00160                                                                       $remainingText = mb_substr( $staticValues[0], 1, mb_strlen( $staticValues[0], $encoding ), $encoding );
00161                                                                       $result = $firstLetter . $remainingText;
00162                                                                   }
00163                                                                   else
00164                                                                   {
00165                                                                      $result = ucfirst( $staticValues[0] );
00166                                                                   }'
00167                                                      ),
00168                                    'upword' => array( 'return' => 'string',
00169                                                       'code' => ' $i18nIni = eZINI::instance( \'i18n.ini\' );
00170                                                                   $hasMBString = ( $i18nIni->variable( \'CharacterSettings\', \'MBStringExtension\' ) == \'enabled\' and
00171                                                                                    function_exists( "mb_strtoupper" ) and
00172                                                                                    function_exists( "mb_substr" ) and
00173                                                                                    function_exists( "mb_strlen" ) );
00174 
00175                                                                   if ( $hasMBString )
00176                                                                   {
00177                                                                       $encoding = eZTextCodec::internalCharset();
00178                                                                       $words = explode( " ", $staticValues[0] );
00179                                                                       $newString = array();
00180                                                                       foreach ( $words as $word )
00181                                                                       {
00182                                                                           $firstLetter = mb_strtoupper( mb_substr( $word, 0, 1, $encoding ), $encoding );
00183                                                                           $remainingText = mb_substr( $word, 1, mb_strlen( $word, $encoding ), $encoding );
00184                                                                           $newString[] = $firstLetter . $remainingText;
00185                                                                       }
00186                                                                       $result = implode( " ", $newString );
00187                                                                       unset( $newString, $words );
00188                                                                   }
00189                                                                   else
00190                                                                   {
00191                                                                      $result = ucwords( $staticValues[0] );
00192                                                                   }'
00193                                                     )
00194 
00195                                  );
00196     }
00197 
00198     /*!
00199      Returns the template operators.
00200     */
00201     function operatorList()
00202     {
00203         return $this->Operators;
00204     }
00205 
00206     function operatorTemplateHints()
00207     {
00208         $hints = array(
00209             $this->BreakName        => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
00210             $this->Count_charsName   => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
00211             $this->DowncaseName     => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
00212             $this->UpcaseName       => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
00213             $this->UpfirstName      => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00214             $this->UpwordName       => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00215 
00216             $this->Count_wordsName   => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00217             $this->ChrName          => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00218             $this->OrdName          => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00219             $this->PadName          => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00220             $this->ShortenName      => array( 'parameters' => 3    , 'element-transformation-func' => 'customMapTransformation' ),
00221             $this->SimplifyName     => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00222             $this->TrimName         => array( 'parameters' => 1    , 'element-transformation-func' => 'customMapTransformation' ),
00223             $this->WrapName         => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
00224 
00225             $this->WashName         => array( 'parameters' => 1, 'element-transformation-func' => 'washTransformation' ),
00226         );
00227 
00228         foreach ( $this->Operators as $operator )
00229         {
00230             $hints[$operator]['input'] = true;
00231             $hints[$operator]['output'] = true;
00232             $hints[$operator]['element-transformation'] = true;
00233             $hints[$operator]['transform-parameters'] = true;
00234             if ( !isset( $hints[$operator]['input-as-parameter'] ) )
00235                 $hints[$operator]['input-as-parameter'] = 'always';
00236         }
00237         return $hints;
00238     }
00239 
00240     /*!
00241      \return true to tell the template engine that the parameter list exists per operator type.
00242     */
00243     function namedParameterPerOperator()
00244     {
00245         return true;
00246     }
00247 
00248     /*!
00249      See eZTemplateOperator::namedParameterList()
00250     */
00251     function namedParameterList()
00252     {
00253         return array( $this->TrimName => array( 'chars_to_remove'  => array( "type" => "string",
00254                                                                              "required" => false,
00255                                                                              "default" => false ) ),
00256                       $this->WrapName => array( 'wrap_at_position' => array( "type" => "integer",
00257                                                                              "required" => false,
00258                                                                              "default" => false ),
00259                                                 'break_sequence'   => array( "type" => "string",
00260                                                                              "required" => false,
00261                                                                              "default" => false ),
00262                                                 'cut'              => array( "type" => "boolean",
00263                                                                              "required" => false,
00264                                                                              "default" => false ) ),
00265                       $this->WashName => array( 'type'             => array( "type" => "string",
00266                                                                              "required" => false,
00267                                                                              "default" => "xhtml" ) ),
00268                       $this->ShortenName => array( 'chars_to_keep' => array( "type" => "integer",
00269                                                                              "required" => false,
00270                                                                              "default" => 80 ),
00271                                                    'str_to_append' => array( "type" => "string",
00272                                                                              "required" => false,
00273                                                                              "default" => "..." ),
00274                                                    'trim_type'     => array( "type" => "string",
00275                                                                              "required" => false,
00276                                                                              "default" => "right" ) ),
00277                       $this->PadName => array(  'desired_length'   => array( "type"     => "integer",
00278                                                                              "required" => false,
00279                                                                              "default"  => 80 ),
00280                                                 'pad_sequence'     => array( "type" => "string",
00281                                                                              "required" => false,
00282                                                                              "default" => " " ) ),
00283                       $this->SimplifyName => array ( 'char'        => array( "type" => "string",
00284                                                                              "required" => false,
00285                                                                              "default" => false ) ) );
00286     }
00287 
00288     function phpMapTransformation( $operatorName, $node, $tpl, $resourceData,
00289                                    $element, $lastElement, $elementList, $elementTree, &$parameters )
00290     {
00291         $values = array();
00292         $phpFunctionList = explode( ',', $this->phpMap[$operatorName] );
00293         foreach ( $phpFunctionList as $element )
00294         {
00295             if ( function_exists( $element ) )
00296             {
00297                 $phpFunction = $element;
00298                 break;
00299             }
00300         }
00301         $newElements = array();
00302 
00303         if ( count ( $parameters ) != 1 )
00304         {
00305             return false;
00306         }
00307 
00308         if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
00309         {
00310             $text = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
00311             $text = $phpFunction( $text );
00312             $text = str_replace( array( "'" ), array( "\\'" ), $text );
00313             $code = "%output% = '" . $text . "' ;\n";
00314         }
00315         else
00316         {
00317             $values[] = $parameters[0];
00318             $code = "%output% = $phpFunction( %1% );\n";
00319         }
00320 
00321         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00322         return $newElements;
00323     }
00324 
00325     function customMapTransformation( $operatorName, $node, $tpl, $resourceData,
00326                                        $element, $lastElement, $elementList, $elementTree, &$parameters )
00327     {
00328         $values = array();
00329         $newElements = array();
00330         $mapEntry = $this->customMap[$operatorName];
00331         $paramCount = count( $parameters );
00332         $strlenFunc = 'strlen';
00333         $substrFunc = 'substr';
00334         $code = "\$strlenFunc = 'strlen'; \$substrFunc = 'substr';\n";
00335         if ( function_exists( 'mb_strlen' ) )
00336         {
00337             $strlenFunc = 'mb_strlen';
00338             $substrFunc = 'mb_substr';
00339             $code = "\$strlenFunc = 'mb_strlen'; \$substrFunc = 'mb_substr';\n";
00340         }
00341 
00342         if ( $paramCount < 1 )
00343         {
00344             return false;
00345         }
00346 
00347         $allStatic = true;
00348         $staticValues = array();
00349         $replaceMap = array('$result');
00350         $replacementMap = array('%output%');
00351         for ($i = 0; $i < $paramCount; $i++)
00352         {
00353             if ( eZTemplateNodeTool::isStaticElement( $parameters[$i] ) )
00354             {
00355                 $staticValues[$i] = eZTemplateNodeTool::elementStaticValue( $parameters[$i] );
00356             }
00357             else
00358             {
00359                 $allStatic = false;
00360             }
00361         }
00362 
00363         if ( $allStatic )
00364         {
00365             $result = false;
00366             if ( isset( $mapEntry['code'. $paramCount] ) )
00367             {
00368                 eval( $mapEntry['code' . $paramCount] );
00369             }
00370             else
00371             {
00372                 eval( $mapEntry['code'] );
00373             }
00374             return array( eZTemplateNodeTool::createStaticElement( $result ) );
00375         }
00376         else
00377         {
00378             $replaceMap = array( '$result', '$paramCount' );
00379             $replacementMap = array( '%output%', $paramCount );
00380             for ( $i = 0; $i < $paramCount; $i++ )
00381             {
00382                 $values[] = $parameters[$i];
00383                 $replaceMap[] = "\$staticValues[$i]";
00384                 $replacementMap[] = '%' . ( $i + 1 ) . '%';
00385             }
00386             if ( isset( $mapEntry['code'. $paramCount] ) )
00387             {
00388                 $code .= str_replace( $replaceMap, $replacementMap, $mapEntry['code' . $paramCount] ) . "\n";
00389             }
00390             else
00391             {
00392                 $code .= str_replace( $replaceMap, $replacementMap, $mapEntry['code'] ) . "\n";
00393             }
00394         }
00395 
00396         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00397         return $newElements;
00398     }
00399 
00400     /*!
00401      * \private
00402      */
00403     function wash( $operatorValue, $tpl, $type = 'xhtml' )
00404     {
00405         switch ( $type )
00406         {
00407             case "xhtml":
00408             {
00409                 $operatorValue = htmlspecialchars( $operatorValue );
00410             } break;
00411 
00412             case "email":
00413             {
00414                 $ini = $tpl->ini();
00415                 $dotText = $ini->variable( 'WashSettings', 'EmailDotText' );
00416                 $atText = $ini->variable( 'WashSettings', 'EmailAtText' );
00417                 $operatorValue = str_replace( array( '.',
00418                                                      '@' ),
00419                                               array( $dotText,
00420                                                      $atText ),
00421                                               $operatorValue );
00422             } break;
00423 
00424             case 'pdf':
00425             {
00426                 $operatorValue = str_replace( array( ' ', // use default callback functions in ezpdf library
00427                                                      "\r\n",
00428                                                      "\t" ),
00429                                               array( '<C:callSpace>',
00430                                                      '<C:callNewLine>',
00431                                                      '<C:callTab>' ),
00432                                               $operatorValue );
00433                 $operatorValue = str_replace( "\n", '<C:callNewLine>', $operatorValue );
00434             } break;
00435 
00436             case 'javascript':
00437             {
00438                 $operatorValue = str_replace( array( "\\", "\"", "'"),
00439                                               array( "\\\\", "\\042", "\\047" ) , $operatorValue );
00440             } break;
00441         }
00442         return $operatorValue;
00443     }
00444 
00445     function washTransformation( $operatorName, $node, $tpl, $resourceData,
00446                                  $element, $lastElement, $elementList, $elementTree, &$parameters )
00447     {
00448         $values = array();
00449         $tmpVarCount = 0;
00450         $newElements = array();
00451         $paramCount = count( $parameters );
00452 
00453         if ( $paramCount > 2 )
00454         {
00455             return false;
00456         }
00457 
00458         $allStatic = true;
00459         $staticValues = array();
00460         for ($i = 0; $i < $paramCount; $i++)
00461         {
00462             if ( eZTemplateNodeTool::isStaticElement( $parameters[$i] ) )
00463             {
00464                 $staticValues[$i] = eZTemplateNodeTool::elementStaticValue( $parameters[$i] );
00465             }
00466             else
00467             {
00468                 $allStatic = false;
00469             }
00470         }
00471 
00472         /* Do optimalizations for 'xhtml' case */
00473         if ( $allStatic ) {
00474             if ( $paramCount == 1 )
00475             {
00476                 $type = 'xhtml';
00477             }
00478             else
00479             {
00480                 $type = $staticValues[1];
00481             }
00482             $code = "%output% = '" . addcslashes( $this->wash( $staticValues[0], $tpl, $type ), "'\\" ) . "' ;\n";
00483         }
00484         /* XHTML: Type is static, input is not */
00485         else if ( ( $paramCount == 1 ) || ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'xhtml' ) ) )
00486         {
00487             $values[] = $parameters[0];
00488             $code = "%output% = htmlspecialchars( %1% );\n";
00489         }
00490         /* PDF: Type is static, input is not static */
00491         else if ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'pdf' ) )
00492         {
00493             $values[] = $parameters[0];
00494             $tmpVarCount = 1;
00495             $code = '%tmp1% = str_replace( array( " ", "\r\n", "\t" ), array( "<C:callSpace>", "<C:callNewLine>", "<C:callTab>" ), %1% );'. "\n";
00496             $code .= '%output% = str_replace( "\n", "<C:callNewLine>", %tmp1% );'."\n";
00497         }
00498         /* MAIL: Type is static, input is not static */
00499         else if ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'email' ) )
00500         {
00501             $ini = $tpl->ini();
00502             $dotText = addcslashes( $ini->variable( 'WashSettings', 'EmailDotText' ), "'" );
00503             $atText = addcslashes( $ini->variable( 'WashSettings', 'EmailAtText' ), "'" );
00504 
00505             $values[] = $parameters[0];
00506             $code = "%output% = str_replace( array( '.', '@' ), array( '$dotText', '$atText' ), %1% );\n";
00507         }
00508         /* JAVASCRIPT: Type is static, input is not static */
00509         else if ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'javascript' ) )
00510         {
00511             $values[] = $parameters[0];
00512             $code = '%output% = str_replace( array( "\\\\", "\"", "\'" ),
00513                                              array( "\\\\\\\\", "\\\\042", "\\\\047" ) , %1% ); ';
00514 
00515 
00516 
00517         }
00518         else /* No compiling for the rest cases */
00519         {
00520             return false;
00521         }
00522 
00523         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, false, $tmpVarCount );
00524         return $newElements;
00525     }
00526 
00527     /*
00528      The modify function takes care of the various operations.
00529     */
00530     function modify( $tpl,
00531                      $operatorName,
00532                      $operatorParameters,
00533                      $rootNamespace,
00534                      $currentNamespace,
00535                      &$operatorValue,
00536                      $namedParameters,
00537                      $placement )
00538     {
00539         switch ( $operatorName )
00540         {
00541             // Convert all alphabetical chars of operatorvalue to uppercase.
00542             case $this->UpcaseName:
00543             {
00544                 $funcName = function_exists( 'mb_strtoupper' ) ? 'mb_strtoupper' : 'strtoupper';
00545                 $operatorValue = $funcName( $operatorValue );
00546             } break;
00547 
00548             // Convert all alphabetical chars of operatorvalue to lowercase.
00549             case $this->DowncaseName:
00550             {
00551                 $funcName = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
00552                 $operatorValue = $funcName( $operatorValue );
00553             } break;
00554 
00555             // Count and return the number of words in operatorvalue.
00556             case $this->Count_wordsName:
00557             {
00558                 $operatorValue = preg_match_all( "#(\w+)#", $operatorValue, $dummy_match );
00559             }break;
00560 
00561             // Count and return the number of chars in operatorvalue.
00562             case $this->Count_charsName:
00563             {
00564                 $funcName = function_exists( 'mb_strlen' ) ? 'mb_strlen' : 'strlen';
00565                 $operatorValue = $funcName( $operatorValue );
00566             }break;
00567 
00568             // Insert HTML line breaks before newlines.
00569             case $this->BreakName:
00570             {
00571                 $operatorValue = nl2br( $operatorValue );
00572             }break;
00573 
00574             // Wrap line (insert newlines).
00575             case $this->WrapName:
00576             {
00577                 $parameters = array( $operatorValue );
00578                 if ( $namedParameters['wrap_at_position'] )
00579                 {
00580                     $parameters[] = $namedParameters['wrap_at_position'];
00581                     if ( $namedParameters['break_sequence'] )
00582                     {
00583                         $parameters[] = $namedParameters['break_sequence'];
00584                         if ( $namedParameters['cut'] )
00585                         {
00586                             $parameters[] = $namedParameters['cut'];
00587                         }
00588                     }
00589                 }
00590                 $operatorValue = call_user_func_array( 'wordwrap', $parameters );
00591             }break;
00592 
00593             // Convert the first character to uppercase.
00594             case $this->UpfirstName:
00595             {
00596                 $i18nIni = eZINI::instance( 'i18n.ini' );
00597                 $hasMBString = ( $i18nIni->variable( 'CharacterSettings', 'MBStringExtension' ) == 'enabled' and
00598                                  function_exists( "mb_strtoupper" ) and
00599                                  function_exists( "mb_substr" ) and
00600                                  function_exists( "mb_strlen" ) );
00601 
00602                 if ( $hasMBString )
00603                 {
00604                     $encoding = eZTextCodec::internalCharset();
00605                     $firstLetter = mb_strtoupper( mb_substr( $operatorValue, 0, 1, $encoding ), $encoding );
00606                     $remainingText = mb_substr( $operatorValue, 1, mb_strlen( $operatorValue, $encoding ), $encoding );
00607                     $operatorValue = $firstLetter . $remainingText;
00608                 }
00609                 else
00610                 {
00611                    $operatorValue = ucfirst( $operatorValue );
00612                 }
00613 
00614             }break;
00615 
00616             // Simplify / transform multiple consecutive characters into one.
00617             case $this->SimplifyName:
00618             {
00619                 $simplifyCharacter = $namedParameters['char'];
00620                 if ( $namedParameters['char'] === false )
00621                 {
00622                     $replace_this = "/\s{2,}/";
00623                     $simplifyCharacter = ' ';
00624                 }
00625                 else
00626                 {
00627                     $replace_this = "/". $simplifyCharacter ."{2,}/";
00628                 }
00629                 $operatorValue = preg_replace( $replace_this, $simplifyCharacter, $operatorValue );
00630             }break;
00631             // Convert all first characters [in all words] to uppercase.
00632             case $this->UpwordName:
00633             {
00634                 $i18nIni = eZINI::instance( 'i18n.ini' );
00635                 $hasMBString = ( $i18nIni->variable( 'CharacterSettings', 'MBStringExtension' ) == 'enabled' and
00636                                  function_exists( "mb_strtoupper" ) and
00637                                  function_exists( "mb_substr" ) and
00638                                  function_exists( "mb_strlen" ) );
00639 
00640                 if ( $hasMBString )
00641                 {
00642                     $encoding = eZTextCodec::internalCharset();
00643                     $words = explode( " ", $operatorValue );
00644                     $newString = array();
00645                     foreach ( $words as $word )
00646                     {
00647                         $firstLetter = mb_strtoupper( mb_substr( $word, 0, 1, $encoding ), $encoding );
00648                         $remainingText = mb_substr( $word, 1, mb_strlen( $word, $encoding ), $encoding );
00649                         $newString[]= $firstLetter . $remainingText;
00650                     }
00651                     $operatorValue = implode( " ", $newString );
00652                     unset( $newString, $words );
00653                 }
00654                 else
00655                 {
00656                    $operatorValue = ucwords( $operatorValue );
00657                 }
00658             }break;
00659 
00660             // Strip whitespace from the beginning and end of a string.
00661             case $this->TrimName:
00662             {
00663                 if ( $namedParameters['chars_to_remove'] === false )
00664                     $operatorValue = trim( $operatorValue );
00665                 else
00666                     $operatorValue = trim( $operatorValue, $namedParameters['chars_to_remove'] );
00667             }break;
00668 
00669             // Pad...
00670             case $this->PadName:
00671             {
00672                 if (strlen( $operatorValue ) < $namedParameters['desired_length'])
00673                 {
00674                     $operatorValue = str_pad( $operatorValue,
00675                                               $namedParameters['desired_length'],
00676                                               $namedParameters['pad_sequence'] );
00677                 }
00678             }break;
00679 
00680             // Shorten string [default or specified length, length=text+"..."] and add '...'
00681             case $this->ShortenName:
00682             {
00683                 $strlenFunc = function_exists( 'mb_strlen' ) ? 'mb_strlen' : 'strlen';
00684                 $substrFunc = function_exists( 'mb_substr' ) ? 'mb_substr' : 'substr';
00685                 if ( $strlenFunc( $operatorValue ) > $namedParameters['chars_to_keep'] )
00686                 {
00687                     $operatorLength = $strlenFunc( $operatorValue );
00688 
00689                     if ( $namedParameters['trim_type'] === 'middle' )
00690                     {
00691                         $appendedStrLen = $strlenFunc( $namedParameters['str_to_append'] );
00692 
00693                         if ( $namedParameters['chars_to_keep'] > $appendedStrLen )
00694                         {
00695                             $chop = $namedParameters['chars_to_keep'] - $appendedStrLen;
00696 
00697                             $middlePos = (int)($chop / 2);
00698                             $leftPartLength = $middlePos;
00699                             $rightPartLength = $chop - $middlePos;
00700 
00701                             $operatorValue = trim( $substrFunc( $operatorValue, 0, $leftPartLength ) . $namedParameters['str_to_append'] . $substrFunc( $operatorValue, $operatorLength - $rightPartLength, $rightPartLength ) );
00702                         }
00703                         else
00704                         {
00705                             $operatorValue = $namedParameters['str_to_append'];
00706                         }
00707                     }
00708                     else // default: trim_type === 'right'
00709                     {
00710                         $chop = $namedParameters['chars_to_keep'] - $strlenFunc( $namedParameters['str_to_append'] );
00711                         $operatorValue = $substrFunc( $operatorValue, 0, $chop );
00712                         $operatorValue = trim( $operatorValue );
00713                         if ( $operatorLength > $chop )
00714                             $operatorValue = $operatorValue.$namedParameters['str_to_append'];
00715                     }
00716                 }
00717             }break;
00718 
00719             // Wash (translate strings to non-spammable text):
00720             case $this->WashName:
00721             {
00722                 $type = $namedParameters['type'];
00723                 $operatorValue = $this->wash( $operatorValue, $tpl, $type );
00724             }break;
00725 
00726             // Ord (translate a unicode string to actual unicode id/numbers):
00727             case $this->OrdName:
00728             {
00729                 $codec = eZTextCodec::instance( false, 'unicode' );
00730                 $output = $codec->convertString( $operatorValue );
00731                 $operatorValue = $output;
00732             }break;
00733 
00734             // Chr (generate unicode characters based on input):
00735             case $this->ChrName:
00736             {
00737                 $codec = eZTextCodec::instance( 'unicode', false );
00738                 $output = $codec->convertString( $operatorValue );
00739                 $operatorValue = $output;
00740             }break;
00741 
00742             // Default case: something went wrong - unknown things...
00743             default:
00744             {
00745                 $tpl->warning( $operatorName, "Unknown string type '$type'", $placement );
00746             } break;
00747         }
00748     }
00749 
00750     /// The array of operators, used for registering operators
00751     public $Operators;
00752 }
00753 
00754 ?>