[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/eztemplate/classes/ -> eztemplatestringoperator.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateStringOperator class
   4  //
   5  // Created on: <17-Jul-2003 13:00:18 bh>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZTemplateStringOperator eztemplatestringoperator.php
  31    \ingroup eZTemplateOperators
  32  \code
  33  
  34  \endcode
  35  
  36  */
  37  
  38  class eZTemplateStringOperator
  39  {
  40      /*!
  41       Constructor.
  42      */
  43      function eZTemplateStringOperator()
  44      {
  45          $this->Operators = array( 'upcase', 'downcase',
  46                                    'count_words', 'count_chars',
  47                                    'trim', 'break', 'wrap', 'shorten', 'pad',
  48                                    'upfirst', 'upword',
  49                                    'simplify', 'wash',
  50                                    'chr', 'ord');
  51          foreach ( $this->Operators as $operator )
  52          {
  53              $name = $operator . 'Name';
  54              $name[0] = $name[0] & "\xdf";
  55              $this->$name = $operator;
  56          }
  57  
  58          $this->phpMap = array ('upcase' => 'mb_strtoupper,strtoupper',
  59                                 'downcase' => 'mb_strtolower,strtolower',
  60                                 'break' => 'nl2br',
  61                                 'upfirst' => 'ucfirst',
  62                                 'upword' => 'ucwords',
  63                                 'count_chars' => 'mb_strlen,strlen');
  64  
  65          $this->customMap = array ( 'count_words' => array( 'return' => 'int',
  66                                                            'code' => '$result = preg_match_all( "#(\w+)#", $staticValues[0], $dummy );'
  67                                                          ),
  68                                     'chr' => array( 'return' => 'string',
  69                                                     'code' => '$codec =& eZTextCodec::instance( "unicode", false );' . "\n" .
  70                                                               '$result = $codec->convertString( $staticValues[0] );'
  71                                                   ),
  72                                     'ord' => array( 'return' => 'string',
  73                                                     'code' => '$codec =& eZTextCodec::instance( false, "unicode" );' . "\n" .
  74                                                               '$result = $codec->convertString( $staticValues[0] );'
  75                                                   ),
  76                                     'pad' => array( 'return' => 'string',
  77                                                     'code' => '$result = $paramCount == 2 ? str_pad( $staticValues[0], $staticValues[1] ) : str_pad ( $staticValues[0], $staticValues[1], $staticValues[2] );',
  78                                                     'code2' => '$result = str_pad( $staticValues[0], $staticValues[1] );',
  79                                                     'code3' => '$result = str_pad( $staticValues[0], $staticValues[1], $staticValues[2] );',
  80                                                   ),
  81                                     'trim' => array( 'return' => 'string',
  82                                                      'code' => '$result = $paramCount == 1 ? trim( $staticValues[0] ) : trim ( $staticValues[0], $staticValues[1] );',
  83                                                      'code1' => '$result = trim( $staticValues[0] );',
  84                                                      'code2' => '$result = trim( $staticValues[0], $staticValues[1] );',
  85                                                   ),
  86                                     'wrap' => array( 'return' => 'string',
  87                                                      'code1' => '$result = wordwrap( $staticValues[0] );',
  88                                                      'code2' => '$result = wordwrap( $staticValues[0], $staticValues[1] );',
  89                                                      'code3' => '$result = wordwrap( $staticValues[0], $staticValues[1], $staticValues[2] );',
  90                                                      'code4' => '$result = wordwrap( $staticValues[0], $staticValues[1], $staticValues[2], $staticValues[3] );',
  91                                                   ),
  92                                     'simplify' => array( 'return' => 'string',
  93                                                          'code' => 'if ( $paramCount == 1 )
  94                                                                     {
  95                                                                         $replacer = " ";
  96                                                                     }
  97                                                                     else
  98                                                                     {
  99                                                                         $replacer = $staticValues[1];
 100                                                                     }
 101                                                                     $result = preg_replace( "/".$replacer."{2,}/", $replacer, $staticValues[0] );',
 102                                                          'code1' => '$result = preg_replace( "/ {2,}/", " ", $staticValues[0] );',
 103                                                          'code2' => '$result = preg_replace( "/".$staticValues[1]."{2,}/", $staticValues[1], $staticValues[0] );',
 104                                                        ),
 105                                     'shorten' => array( 'return' => 'string',
 106                                                         'code' => '$length = 80; $seq = "..."; $trimType = "right";
 107                                                                    if ( $paramCount > 1 )
 108                                                                    {
 109                                                                        $length = $staticValues[1];
 110                                                                    }
 111                                                                    if ( $paramCount > 2 )
 112                                                                    {
 113                                                                        $seq = $staticValues[2];
 114                                                                    }
 115                                                                    if ( $paramCount > 3 )
 116                                                                    {
 117                                                                        $trimType = $staticValues[3];
 118                                                                    }
 119                                                                    if ( $trimType === "middle" )
 120                                                                    {
 121                                                                        $appendedStrLen = $strlenFunc( $seq );
 122                                                                        if ( $length > $appendedStrLen && ( $strlenFunc( $staticValues[0] ) > $length ) )
 123                                                                        {
 124                                                                            $operatorValueLength = $strlenFunc( $staticValues[0] );
 125                                                                            $chop = $length - $appendedStrLen;
 126                                                                            $middlePos = (int)($chop / 2);
 127                                                                            $leftPartLength = $middlePos;
 128                                                                            $rightPartLength = $chop - $middlePos;
 129                                                                            $result = trim( $substrFunc( $staticValues[0], 0, $leftPartLength ) . $seq . $substrFunc( $staticValues[0], $operatorValueLength - $rightPartLength, $rightPartLength ) );
 130                                                                        }
 131                                                                        else
 132                                                                        {
 133                                                                            $result = $staticValues[0];
 134                                                                        }
 135                                                                    }
 136                                                                    else // default: trim_type === "right"
 137                                                                    {
 138                                                                        $maxLength = $length - $strlenFunc( $seq );
 139                                                                        if ( ( $strlenFunc( $staticValues[0] ) > $length ) && $strlenFunc( $staticValues[0] ) > $maxLength )
 140                                                                        {
 141                                                                            $result = trim( $substrFunc( $staticValues[0], 0, $maxLength) ) . $seq;
 142                                                                        }
 143                                                                        else
 144                                                                        {
 145                                                                            $result = $staticValues[0];
 146                                                                        }
 147                                                                    }'
 148                                                       )
 149  
 150                                   );
 151      }
 152  
 153      /*!
 154       Returns the template operators.
 155      */
 156      function &operatorList()
 157      {
 158          return $this->Operators;
 159      }
 160  
 161      function operatorTemplateHints()
 162      {
 163          $hints = array(
 164              $this->BreakName        => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
 165              $this->Count_charsName   => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
 166              $this->DowncaseName     => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
 167              $this->UpcaseName       => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
 168              $this->UpfirstName      => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
 169              $this->UpwordName       => array( 'parameters' => false, 'element-transformation-func' => 'phpMapTransformation' ),
 170  
 171              $this->Count_wordsName   => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
 172              $this->ChrName          => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
 173              $this->OrdName          => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
 174              $this->PadName          => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
 175              $this->ShortenName      => array( 'parameters' => 3    , 'element-transformation-func' => 'customMapTransformation' ),
 176              $this->SimplifyName     => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
 177              $this->TrimName         => array( 'parameters' => 1    , 'element-transformation-func' => 'customMapTransformation' ),
 178              $this->WrapName         => array( 'parameters' => false, 'element-transformation-func' => 'customMapTransformation' ),
 179  
 180              $this->WashName         => array( 'parameters' => 1, 'element-transformation-func' => 'washTransformation' ),
 181          );
 182  
 183          foreach ( $this->Operators as $operator )
 184          {
 185              $hints[$operator]['input'] = true;
 186              $hints[$operator]['output'] = true;
 187              $hints[$operator]['element-transformation'] = true;
 188              $hints[$operator]['transform-parameters'] = true;
 189              if ( !isset( $hints[$operator]['input-as-parameter'] ) )
 190                  $hints[$operator]['input-as-parameter'] = 'always';
 191          }
 192          return $hints;
 193      }
 194  
 195      /*!
 196       \return true to tell the template engine that the parameter list exists per operator type.
 197      */
 198      function namedParameterPerOperator()
 199      {
 200          return true;
 201      }
 202  
 203      /*!
 204       See eZTemplateOperator::namedParameterList()
 205      */
 206      function namedParameterList()
 207      {
 208          return array( $this->TrimName => array( 'chars_to_remove'  => array( "type" => "string",
 209                                                                               "required" => false,
 210                                                                               "default" => false ) ),
 211                        $this->WrapName => array( 'wrap_at_position' => array( "type" => "integer",
 212                                                                               "required" => false,
 213                                                                               "default" => false ),
 214                                                  'break_sequence'   => array( "type" => "string",
 215                                                                               "required" => false,
 216                                                                               "default" => false ),
 217                                                  'cut'              => array( "type" => "boolean",
 218                                                                               "required" => false,
 219                                                                               "default" => false ) ),
 220                        $this->WashName => array( 'type'             => array( "type" => "string",
 221                                                                               "required" => false,
 222                                                                               "default" => "xhtml" ) ),
 223                        $this->ShortenName => array( 'chars_to_keep' => array( "type" => "integer",
 224                                                                               "required" => false,
 225                                                                               "default" => 80 ),
 226                                                     'str_to_append' => array( "type" => "string",
 227                                                                               "required" => false,
 228                                                                               "default" => "..." ),
 229                                                     'trim_type'     => array( "type" => "string",
 230                                                                               "required" => false,
 231                                                                               "default" => "right" ) ),
 232                        $this->PadName => array(  'desired_length'   => array( "type"     => "integer",
 233                                                                               "required" => false,
 234                                                                               "default"  => 80 ),
 235                                                  'pad_sequence'     => array( "type" => "string",
 236                                                                               "required" => false,
 237                                                                               "default" => " " ) ),
 238                        $this->SimplifyName => array ( 'char'        => array( "type" => "string",
 239                                                                               "required" => false,
 240                                                                               "default" => false ) ) );
 241      }
 242  
 243      function phpMapTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 244                                     &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 245      {
 246          $values = array();
 247          $phpFunctionList = explode( ',', $this->phpMap[$operatorName] );
 248          foreach ( $phpFunctionList as $element )
 249          {
 250              if ( function_exists( $element ) )
 251              {
 252                  $phpFunction = $element;
 253                  break;
 254              }
 255          }
 256          $newElements = array();
 257  
 258          if ( count ( $parameters ) != 1 )
 259          {
 260              return false;
 261          }
 262  
 263          if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
 264          {
 265              $text = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
 266              $code = "%output% = '" . $phpFunction( $text ) . "' ;\n";
 267          }
 268          else
 269          {
 270              $values[] = $parameters[0];
 271              $code = "%output% = $phpFunction( %1% );\n";
 272          }
 273  
 274          $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
 275          return $newElements;
 276      }
 277  
 278      function customMapTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 279                                         &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 280      {
 281          $values = array();
 282          $newElements = array();
 283          $mapEntry = $this->customMap[$operatorName];
 284          $paramCount = count( $parameters );
 285          $strlenFunc = 'strlen';
 286          $substrFunc = 'substr';
 287          $code = "\$strlenFunc = 'strlen'; \$substrFunc = 'substr';\n";
 288          if ( function_exists( 'mb_strlen' ) )
 289          {
 290              $strlenFunc = 'mb_strlen';
 291              $substrFunc = 'mb_substr';
 292              $code = "\$strlenFunc = 'mb_strlen'; \$substrFunc = 'mb_substr';\n";
 293          }
 294  
 295          if ( $paramCount < 1 )
 296          {
 297              return false;
 298          }
 299  
 300          $allStatic = true;
 301          $staticValues = array();
 302          $replaceMap = array('$result');
 303          $replacementMap = array('%output%');
 304          for ($i = 0; $i < $paramCount; $i++)
 305          {
 306              if ( eZTemplateNodeTool::isStaticElement( $parameters[$i] ) )
 307              {
 308                  $staticValues[$i] = eZTemplateNodeTool::elementStaticValue( $parameters[$i] );
 309              }
 310              else
 311              {
 312                  $allStatic = false;
 313              }
 314          }
 315  
 316          if ( $allStatic )
 317          {
 318              $result = false;
 319              if ( isset( $mapEntry['code'. $paramCount] ) )
 320              {
 321                  eval( $mapEntry['code' . $paramCount] );
 322              }
 323              else
 324              {
 325                  eval( $mapEntry['code'] );
 326              }
 327              return array( eZTemplateNodeTool::createStaticElement( $result ) );
 328          }
 329          else
 330          {
 331              $replaceMap = array( '$result', '$paramCount' );
 332              $replacementMap = array( '%output%', $paramCount );
 333              for ( $i = 0; $i < $paramCount; $i++ )
 334              {
 335                  $values[] = $parameters[$i];
 336                  $replaceMap[] = "\$staticValues[$i]";
 337                  $replacementMap[] = '%' . ( $i + 1 ) . '%';
 338              }
 339              if ( isset( $mapEntry['code'. $paramCount] ) )
 340              {
 341                  $code .= str_replace( $replaceMap, $replacementMap, $mapEntry['code' . $paramCount] ) . "\n";
 342              }
 343              else
 344              {
 345                  $code .= str_replace( $replaceMap, $replacementMap, $mapEntry['code'] ) . "\n";
 346              }
 347          }
 348  
 349          $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
 350          return $newElements;
 351      }
 352  
 353      /*!
 354       * \private
 355       */
 356      function wash( $operatorValue, &$tpl, $type = 'xhtml' )
 357      {
 358          switch ( $type )
 359          {
 360              case "xhtml":
 361              {
 362                  $operatorValue = htmlspecialchars( $operatorValue );
 363              } break;
 364  
 365              case "email":
 366              {
 367                  $ini =& $tpl->ini();
 368                  $dotText = $ini->variable( 'WashSettings', 'EmailDotText' );
 369                  $atText = $ini->variable( 'WashSettings', 'EmailAtText' );
 370                  $operatorValue = str_replace( array( '.',
 371                                                       '@' ),
 372                                                array( $dotText,
 373                                                       $atText ),
 374                                                $operatorValue );
 375              } break;
 376  
 377              case 'pdf':
 378              {
 379                  $operatorValue = str_replace( array( ' ', // use default callback functions in ezpdf library
 380                                                       "\r\n",
 381                                                       "\t" ),
 382                                                array( '<C:callSpace>',
 383                                                       '<C:callNewLine>',
 384                                                       '<C:callTab>' ),
 385                                                $operatorValue );
 386                  $operatorValue = str_replace( "\n", '<C:callNewLine>', $operatorValue );
 387              } break;
 388  
 389              case 'javascript':
 390              {
 391                  $operatorValue = str_replace( array( "\\", "\"", "'"),
 392                                                array( "\\\\", "\\042", "\\047" ) , $operatorValue );
 393              } break;
 394          }
 395          return $operatorValue;
 396      }
 397  
 398      function washTransformation( $operatorName, &$node, &$tpl, &$resourceData,
 399                                   &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
 400      {
 401          $values = array();
 402          $tmpVarCount = 0;
 403          $newElements = array();
 404          $paramCount = count( $parameters );
 405  
 406          if ( $paramCount > 2 )
 407          {
 408              return false;
 409          }
 410  
 411          $allStatic = true;
 412          $staticValues = array();
 413          for ($i = 0; $i < $paramCount; $i++)
 414          {
 415              if ( eZTemplateNodeTool::isStaticElement( $parameters[$i] ) )
 416              {
 417                  $staticValues[$i] = eZTemplateNodeTool::elementStaticValue( $parameters[$i] );
 418              }
 419              else
 420              {
 421                  $allStatic = false;
 422              }
 423          }
 424  
 425          /* Do optimalizations for 'xhtml' case */
 426          if ( $allStatic ) {
 427              if ( $paramCount == 1 )
 428              {
 429                  $type = 'xhtml';
 430              }
 431              else
 432              {
 433                  $type = $staticValues[1];
 434              }
 435              $code = "%output% = '" . addcslashes( $this->wash( $staticValues[0], $tpl, $type ), "'\\" ) . "' ;\n";
 436          }
 437          /* XHTML: Type is static, input is not */
 438          else if ( ( $paramCount == 1 ) || ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'xhtml' ) ) )
 439          {
 440              $values[] = $parameters[0];
 441              $code = "%output% = htmlspecialchars( %1% );\n";
 442          }
 443          /* PDF: Type is static, input is not static */
 444          else if ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'pdf' ) )
 445          {
 446              $values[] = $parameters[0];
 447              $tmpVarCount = 1;
 448              $code = '%tmp1% = str_replace( array( " ", "\r\n", "\t" ), array( "<C:callSpace>", "<C:callNewLine>", "<C:callTab>" ), %1% );'. "\n";
 449              $code .= '%output% = str_replace( "\n", "<C:callNewLine>", %tmp1% );'."\n";
 450          }
 451          /* MAIL: Type is static, input is not static */
 452          else if ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'email' ) )
 453          {
 454              $ini =& $tpl->ini();
 455              $dotText = addcslashes( $ini->variable( 'WashSettings', 'EmailDotText' ), "'" );
 456              $atText = addcslashes( $ini->variable( 'WashSettings', 'EmailAtText' ), "'" );
 457  
 458              $values[] = $parameters[0];
 459              $code = "%output% = str_replace( array( '.', '@' ), array( '$dotText', '$atText' ), %1% );\n";
 460          }
 461          /* JAVASCRIPT: Type is static, input is not static */
 462          else if ( ( $paramCount == 2 ) && isset( $staticValues[1] ) && ( $staticValues[1] == 'javascript' ) )
 463          {
 464              $values[] = $parameters[0];
 465              $code = '%output% = str_replace( array( "\\\\", "\"", "\'" ),
 466                                               array( "\\\\\\\\", "\\\\042", "\\\\047" ) , %1% ); ';
 467  
 468  
 469  
 470          }
 471          else /* No compiling for the rest cases */
 472          {
 473              return false;
 474          }
 475  
 476          $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, false, $tmpVarCount );
 477          return $newElements;
 478      }
 479  
 480      /*
 481       The modify function takes care of the various operations.
 482      */
 483      function modify( &$tpl,
 484                       &$operatorName,
 485                       &$operatorParameters,
 486                       &$rootNamespace,
 487                       &$currentNamespace,
 488                       &$operatorValue,
 489                       &$namedParameters,
 490                       $placement )
 491      {
 492          switch ( $operatorName )
 493          {
 494              // Convert all alphabetical chars of operatorvalue to uppercase.
 495              case $this->UpcaseName:
 496              {
 497                  $funcName = function_exists( 'mb_strtoupper' ) ? 'mb_strtoupper' : 'strtoupper';
 498                  $operatorValue = $funcName( $operatorValue );
 499              } break;
 500  
 501              // Convert all alphabetical chars of operatorvalue to lowercase.
 502              case $this->DowncaseName:
 503              {
 504                  $funcName = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
 505                  $operatorValue = $funcName( $operatorValue );
 506              } break;
 507  
 508              // Count and return the number of words in operatorvalue.
 509              case $this->Count_wordsName:
 510              {
 511                  $operatorValue = preg_match_all( "#(\w+)#", $operatorValue, $dummy_match );
 512              }break;
 513  
 514              // Count and return the number of chars in operatorvalue.
 515              case $this->Count_charsName:
 516              {
 517                  $funcName = function_exists( 'mb_strlen' ) ? 'mb_strlen' : 'strlen';
 518                  $operatorValue = $funcName( $operatorValue );
 519              }break;
 520  
 521              // Insert HTML line breaks before newlines.
 522              case $this->BreakName:
 523              {
 524                  $operatorValue = nl2br( $operatorValue );
 525              }break;
 526  
 527              // Wrap line (insert newlines).
 528              case $this->WrapName:
 529              {
 530                  $parameters = array( $operatorValue );
 531                  if ( $namedParameters['wrap_at_position'] )
 532                  {
 533                      $parameters[] = $namedParameters['wrap_at_position'];
 534                      if ( $namedParameters['break_sequence'] )
 535                      {
 536                          $parameters[] = $namedParameters['break_sequence'];
 537                          if ( $namedParameters['cut'] )
 538                          {
 539                              $parameters[] = $namedParameters['cut'];
 540                          }
 541                      }
 542                  }
 543                  $operatorValue = call_user_func_array( 'wordwrap', $parameters );
 544              }break;
 545  
 546              // Convert the first character to uppercase.
 547              case $this->UpfirstName:
 548              {
 549                  $operatorValue = ucfirst( $operatorValue );
 550              }break;
 551  
 552              // Simplify / transform multiple consecutive characters into one.
 553              case $this->SimplifyName:
 554              {
 555                  $simplifyCharacter = $namedParameters['char'];
 556                  if ( $namedParameters['char'] === false )
 557                  {
 558                      $replace_this = "/\s{2,}/";
 559                      $simplifyCharacter = ' ';
 560                  }
 561                  else
 562                  {
 563                      $replace_this = "/". $simplifyCharacter ."{2,}/";
 564                  }
 565                  $operatorValue = preg_replace( $replace_this, $simplifyCharacter, $operatorValue );
 566              }break;
 567              // Convert all first characters [in all words] to uppercase.
 568              case $this->UpwordName:
 569              {
 570                  $operatorValue = ucwords( $operatorValue );
 571              }break;
 572  
 573              // Strip whitespace from the beginning and end of a string.
 574              case $this->TrimName:
 575              {
 576                  if ( $namedParameters['chars_to_remove'] === false )
 577                      $operatorValue = trim( $operatorValue );
 578                  else
 579                      $operatorValue = trim( $operatorValue, $namedParameters['chars_to_remove'] );
 580              }break;
 581  
 582              // Pad...
 583              case $this->PadName:
 584              {
 585                  if (strlen( $operatorValue ) < $namedParameters['desired_length'])
 586                  {
 587                      $operatorValue = str_pad( $operatorValue,
 588                                                $namedParameters['desired_length'],
 589                                                $namedParameters['pad_sequence'] );
 590                  }
 591              }break;
 592  
 593              // Shorten string [default or specified length, length=text+"..."] and add '...'
 594              case $this->ShortenName:
 595              {
 596                  $strlenFunc = function_exists( 'mb_strlen' ) ? 'mb_strlen' : 'strlen';
 597                  $substrFunc = function_exists( 'mb_substr' ) ? 'mb_substr' : 'substr';
 598                  if ( $strlenFunc( $operatorValue ) > $namedParameters['chars_to_keep'] )
 599                  {
 600                      $operatorLength = $strlenFunc( $operatorValue );
 601  
 602                      if ( $namedParameters['trim_type'] === 'middle' )
 603                      {
 604                          $appendedStrLen = $strlenFunc( $namedParameters['str_to_append'] );
 605  
 606                          if ( $namedParameters['chars_to_keep'] > $appendedStrLen )
 607                          {
 608                              $chop = $namedParameters['chars_to_keep'] - $appendedStrLen;
 609  
 610                              $middlePos = (int)($chop / 2);
 611                              $leftPartLength = $middlePos;
 612                              $rightPartLength = $chop - $middlePos;
 613  
 614                              $operatorValue = trim( $substrFunc( $operatorValue, 0, $leftPartLength ) . $namedParameters['str_to_append'] . $substrFunc( $operatorValue, $operatorLength - $rightPartLength, $rightPartLength ) );
 615                          }
 616                          else
 617                          {
 618                              $operatorValue = $namedParameters['str_to_append'];
 619                          }
 620                      }
 621                      else // default: trim_type === 'right'
 622                      {
 623                          $chop = $namedParameters['chars_to_keep'] - $strlenFunc( $namedParameters['str_to_append'] );
 624                          $operatorValue = $substrFunc( $operatorValue, 0, $chop );
 625                          $operatorValue = trim( $operatorValue );
 626                          if ( $operatorLength > $chop )
 627                              $operatorValue = $operatorValue.$namedParameters['str_to_append'];
 628                      }
 629                  }
 630              }break;
 631  
 632              // Wash (translate strings to non-spammable text):
 633              case $this->WashName:
 634              {
 635                  $type = $namedParameters['type'];
 636                  $operatorValue = $this->wash( $operatorValue, $tpl, $type );
 637              }break;
 638  
 639              // Ord (translate a unicode string to actual unicode id/numbers):
 640              case $this->OrdName:
 641              {
 642                  $codec =& eZTextCodec::instance( false, 'unicode' );
 643                  $output = $codec->convertString( $operatorValue );
 644                  $operatorValue = $output;
 645              }break;
 646  
 647              // Chr (generate unicode characters based on input):
 648              case $this->ChrName:
 649              {
 650                  $codec =& eZTextCodec::instance( 'unicode', false );
 651                  $output = $codec->convertString( $operatorValue );
 652                  $operatorValue = $output;
 653              }break;
 654  
 655              // Default case: something went wrong - unknown things...
 656              default:
 657              {
 658                  $tpl->warning( $operatorName, "Unknown string type '$type'", $placement );
 659              } break;
 660          }
 661      }
 662  
 663      /// The array of operators, used for registering operators
 664      var $Operators;
 665  }
 666  
 667  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7