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