eZ Publish  [4.0]
eztemplatetextoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateTextOperator class
00004 //
00005 // Created on: <01-Aug-2002 11:38:40 amos>
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 eztemplatetextoperator.php
00032 */
00033 
00034 /*!
00035   \class eZTemplateTextOperator eztemplatetextoperator.php
00036   \brief The class eZTemplateTextOperator does
00037 
00038 */
00039 
00040 class eZTemplateTextOperator
00041 {
00042     /*!
00043      Constructor
00044     */
00045     function eZTemplateTextOperator()
00046     {
00047         $this->Operators= array( 'concat', 'indent' );
00048 
00049         foreach ( $this->Operators as $operator )
00050         {
00051             $name = $operator . 'Name';
00052             $name[0] = $name[0] & "\xdf";
00053             $this->$name = $operator;
00054         }
00055     }
00056 
00057     /*!
00058      Returns the operators in this class.
00059     */
00060     function operatorList()
00061     {
00062         return $this->Operators;
00063     }
00064 
00065     function operatorTemplateHints()
00066     {
00067         return array( $this->ConcatName => array( 'input' => true,
00068                                                   'output' => true,
00069                                                   'parameters' => true,
00070                                                   'element-transformation' => true,
00071                                                   'transform-parameters' => true,
00072                                                   'input-as-parameter' => true,
00073                                                   'element-transformation-func' => 'concatTransformation'),
00074                       $this->IndentName => array( 'input' => true,
00075                                                   'output' => true,
00076                                                   'parameters' => 3,
00077                                                   'element-transformation' => true,
00078                                                   'transform-parameters' => true,
00079                                                   'input-as-parameter' => true,
00080                                                   'element-transformation-func' => 'indentTransformation') ) ;
00081     }
00082 
00083     /*!
00084      \return true to tell the template engine that the parameter list exists per operator type.
00085     */
00086     function namedParameterPerOperator()
00087     {
00088         return true;
00089     }
00090 
00091     /*!
00092      See eZTemplateOperator::namedParameterList
00093     */
00094     function namedParameterList()
00095     {
00096         return array( $this->IndentName => array( 'indent_count' => array( 'type' => 'integer',
00097                                                                            'required' => true,
00098                                                                            'default' => false ),
00099                                                   'indent_type' => array( 'type' => 'identifier',
00100                                                                           'required' => false,
00101                                                                           'default' => 'space' ),
00102                                                   'indent_filler' => array( 'type' => 'string',
00103                                                                             'required' => false,
00104                                                                             'default' => false ) ) );
00105     }
00106 
00107     function indentTransformation( $operatorName, &$node, $tpl, &$resourceData,
00108                                    $element, $lastElement, $elementList, $elementTree, &$parameters )
00109     {
00110         $values = array();
00111         $count = $type = $filler = false;
00112         $paramCount = count( $parameters );
00113 
00114         if ( $paramCount == 4 )
00115         {
00116             if ( eZTemplateNodeTool::isStaticElement( $parameters[3] ) )
00117             {
00118                 $filler = eZTemplateNodeTool::elementStaticValue( $parameters[3] );
00119             }
00120         }
00121         if ( $paramCount >= 3 )
00122         {
00123             if ( eZTemplateNodeTool::isStaticElement( $parameters[2] ) )
00124             {
00125                 $type = eZTemplateNodeTool::elementStaticValue( $parameters[2] );
00126                 if ( $type == 'space' )
00127                 {
00128                     $filler = ' ';
00129                 }
00130                 else if ( $type == 'tab' )
00131                 {
00132                     $filler = "\t";
00133                 }
00134                 else if ( $type != 'custom' )
00135                 {
00136                     $filler = ' ';
00137                 }
00138             }
00139         }
00140         if ( $paramCount >= 2 )
00141         {
00142             if ( eZTemplateNodeTool::isStaticElement( $parameters[1] ) )
00143             {
00144                 $count = eZTemplateNodeTool::elementStaticValue( $parameters[1] );
00145             }
00146             if ( $paramCount < 3 )
00147             {
00148                 $type = 'space';
00149                 $filler = ' ';
00150             }
00151         }
00152         $newElements = array();
00153 
00154         if ( $count and $type and $filler )
00155         {
00156             $tmpCount = 0;
00157             $values[] = $parameters[0];
00158             $indentation = str_repeat( $filler, $count );
00159             $code = ( "%output% = '$indentation' . str_replace( '\n', '\n$indentation', %1% );\n" );
00160         }
00161         else if ( $filler and $type )
00162         {
00163             $tmpCount = 1;
00164             $values[] = $parameters[0];
00165             $values[] = $parameters[1];
00166             $code = ( "%tmp1% = str_repeat( '$filler', %2% );\n" .
00167                       "%output% = %tmp1% . str_replace( '\n', '\n' . %tmp1%, %1% );\n" );
00168         }
00169         else
00170         {
00171             $tmpCount = 2;
00172             $code = "if ( %3% == 'tab' )\n{\n\t%tmp1% = \"\\t\";\n}\nelse ";
00173             $code .= "if ( %3% == 'space' )\n{\n\t%tmp1% = ' ';\n}\nelse\n";
00174             if ( count ( $parameters ) == 4 )
00175             {
00176                 $code .= "{\n\t%tmp1% = %4%;\n}\n";
00177             }
00178             else
00179             {
00180                 $code.= "{\n\t%tmp1% = ' ';\n}\n";
00181             }
00182             $code .= ( "%tmp2% = str_repeat( %tmp1%, %2% );\n" .
00183                        "%output% = %tmp2% . str_replace( '\n', '\n' . %tmp2%, %1% );\n" );
00184             foreach ( $parameters as $parameter )
00185             {
00186                 $values[] = $parameter;
00187             }
00188         }
00189 
00190         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values, 'false', $tmpCount );
00191         return $newElements;
00192     }
00193 
00194     function concatTransformation( $operatorName, &$node, $tpl, &$resourceData,
00195                                    $element, $lastElement, $elementList, $elementTree, &$parameters )
00196     {
00197         $values = array();
00198         $function = $operatorName;
00199 
00200         if ( ( count( $parameters ) < 1 ) )
00201         {
00202             return false;
00203         }
00204         if ( ( count( $parameters ) == 1 ) and
00205              eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
00206         {
00207             return array( eZTemplateNodeTool::createStaticElement( eZTemplateNodeTool::elementStaticValue( $parameters[0] ) ) );
00208         }
00209         $newElements = array();
00210 
00211         $counter = 1;
00212         $code = "%output% = ( ";
00213         foreach ( $parameters as $parameter )
00214         {
00215             $values[] = $parameter;
00216             if ( $counter > 1 )
00217             {
00218                 $code .= ' . ';
00219             }
00220             $code .= "%$counter%";
00221             $counter++;
00222         }
00223         $code .= " );\n";
00224 
00225         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00226         return $newElements;
00227     }
00228 
00229     /*!
00230      Handles concat and indent operators.
00231     */
00232     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
00233                      $placement )
00234     {
00235         switch ( $operatorName )
00236         {
00237             case $this->ConcatName:
00238             {
00239                 $operands = array();
00240                 if ( $operatorValue !== null )
00241                     $operands[] = $operatorValue;
00242                 for ( $i = 0; $i < count( $operatorParameters ); ++$i )
00243                 {
00244                     $operand = $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement );
00245                     if ( !is_object( $operand ) )
00246                         $operands[] = $operand;
00247                 }
00248                 $operatorValue = implode( '', $operands );
00249             } break;
00250             case $this->IndentName:
00251             {
00252                 $indentCount = $namedParameters['indent_count'];
00253                 $indentType = $namedParameters['indent_type'];
00254                 $filler = false;
00255                 switch ( $indentType )
00256                 {
00257                     case 'space':
00258                     default:
00259                     {
00260                         $filler = ' ';
00261                     } break;
00262                     case 'tab':
00263                     {
00264                         $filler = "\t";
00265                     } break;
00266                     case 'custom':
00267                     {
00268                         $filler = $namedParameters['indent_filler'];
00269                     } break;
00270                 }
00271                 $fillText = str_repeat( $filler, $indentCount );
00272                 $operatorValue = $fillText . str_replace( "\n", "\n" . $fillText, $operatorValue );
00273             } break;
00274         }
00275     }
00276 
00277     /// \privatesection
00278     public $ConcatName;
00279     public $Operators;
00280 }
00281 
00282 ?>