[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZTemplateIncludeFunction class 4 // 5 // Created on: <05-Mar-2002 13:55:25 amos> 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 eZTemplateIncludeFunction eztemplateincludefunction.php 31 \ingroup eZTemplateFunctions 32 \brief Includes external template code using function "include" 33 34 Allows the template designer to include another template file 35 dynamically. This allows for reuse of commonly used template code. 36 The new template file will loaded into the current namespace or a 37 namspace specified by the template designer, any extra parameters 38 to this function is set as template variables for the template file 39 using the newly aquired namespace. 40 41 \code 42 // Example template code 43 {include uri=file:myfile.tpl} 44 45 {include name=new_namespace uri=/etc/test.tpl} 46 47 \endcode 48 */ 49 50 class eZTemplateIncludeFunction 51 { 52 /*! 53 Initializes the function with the function name $inc_name. 54 */ 55 function eZTemplateIncludeFunction( $inc_name = "include" ) 56 { 57 $this->IncludeName = $inc_name; 58 } 59 60 /*! 61 Returns an array of the function names, required for eZTemplate::registerFunctions. 62 */ 63 function functionList() 64 { 65 return array( $this->IncludeName ); 66 } 67 68 function functionTemplateHints() 69 { 70 return array( $this->IncludeName => array( 'parameters' => true, 71 'static' => false, 72 'transform-children' => true, 73 'tree-transformation' => true, 74 'transform-parameters' => true ) ); 75 } 76 77 function templateNodeTransformation( $functionName, &$node, 78 &$tpl, $parameters, $privateData ) 79 { 80 if ( $functionName != $this->IncludeName ) 81 return false; 82 $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node ); 83 if ( !isset( $parameters['uri'] ) ) 84 return false; 85 86 $uriData = $parameters['uri']; 87 if ( !eZTemplateNodeTool::isStaticElement( $uriData ) ) 88 return false; 89 90 $namespaceValue = false; 91 $namespaceName = '$currentNamespace'; 92 if ( isset( $parameters['name'] ) ) 93 { 94 $nameData = $parameters['name']; 95 if ( !eZTemplateNodeTool::isStaticElement( $nameData ) ) 96 return false; 97 $namespaceValue = eZTemplateNodeTool::elementStaticValue( $nameData ); 98 $namespaceName = '$namespace'; 99 } 100 101 $uriString = eZTemplateNodeTool::elementStaticValue( $uriData ); 102 103 $resourceName = ""; 104 $templateName = ""; 105 $resource =& $tpl->resourceFor( $uriString, $resourceName, $templateName ); 106 $resourceData = $tpl->resourceData( $resource, $uriString, $resourceName, $templateName ); 107 $resourceData['use-comments'] = eZTemplateCompiler::isCommentsEnabled(); 108 109 $includeNodes = $resource->templateNodeTransformation( $functionName, $node, $tpl, $resourceData, $parameters, $namespaceValue ); 110 if ( $includeNodes === false ) 111 return false; 112 113 $newNodes = array(); 114 115 $variableList = array(); 116 $uniqID = md5( uniqid('inc') ); 117 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$oldRestoreIncludeArray" . "_$uniqID = isset( \$restoreIncludeArray ) ? \$restoreIncludeArray : array();\n". 118 "\$restoreIncludeArray = array();\n"); 119 foreach ( array_keys( $parameters ) as $parameterName ) 120 { 121 if ( $parameterName == 'uri' or 122 $parameterName == 'name' ) 123 continue; 124 $parameterData =& $parameters[$parameterName]; 125 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( isset( $namespaceName ) and isset( \$vars[$namespaceName]['$parameterName'] ) )\n". 126 " \$restoreIncludeArray[] = array( $namespaceName, '$parameterName', \$vars[$namespaceName]['$parameterName'] );\n". 127 "elseif ( !isset( \$vars[( isset( $namespaceName ) ? $namespaceName : '' )]['$parameterName'] ) ) \n". 128 " \$restoreIncludeArray[] = array( ( isset( $namespaceName ) ? $namespaceName : '' ), '$parameterName', 'unset' );\n" ); 129 130 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameterData, false, array(), 131 array( $namespaceValue, EZ_TEMPLATE_NAMESPACE_SCOPE_RELATIVE, $parameterName ) ); 132 $variableList[] = $parameterName; 133 } 134 135 $newNodes = array_merge( $newNodes, $includeNodes ); 136 // Restore previous variables, before including 137 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "foreach ( \$restoreIncludeArray as \$element )\n". 138 "{\n". 139 " if ( \$element[2] === 'unset' )\n". 140 " {\n". 141 " unset( \$vars[\$element[0]][\$element[1]] );\n". 142 " continue;\n". 143 " }\n". 144 " \$vars[\$element[0]][\$element[1]] = \$element[2];\n". 145 "}\n". 146 "\$restoreIncludeArray = \$oldRestoreIncludeArray" . "_$uniqID;\n" ); 147 148 return $newNodes; 149 } 150 151 /*! 152 Loads the file specified in the parameter "uri" with namespace "name". 153 */ 154 function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace ) 155 { 156 $params = $functionParameters; 157 if ( !isset( $params["uri"] ) ) 158 { 159 $tpl->missingParameter( $this->IncludeName, "uri" ); 160 return false; 161 } 162 $uri = $tpl->elementValue( $params["uri"], $rootNamespace, $currentNamespace, $functionPlacement ); 163 $name = ""; 164 if ( isset( $params["name"] ) ) 165 $name = $tpl->elementValue( $params["name"], $rootNamespace, $currentNamespace, $functionPlacement ); 166 if ( $currentNamespace != "" ) 167 { 168 if ( $name != "" ) 169 $name = "$currentNamespace:$name"; 170 else 171 $name = $currentNamespace; 172 } 173 reset( $params ); 174 $whatParamsShouldBeUnset = array(); 175 $whatParamsShouldBeReplaced = array(); 176 while ( ( $key = key( $params ) ) !== null ) 177 { 178 $item =& $params[$key]; 179 switch ( $key ) 180 { 181 case "name": 182 case "uri": 183 break; 184 185 default: 186 { 187 if ( !$tpl->hasVariable( $key, $name ) ) 188 { 189 $whatParamsShouldBeUnset[] = $key; // Tpl vars should be removed after including 190 } 191 else 192 { 193 $whatParamsShouldBeReplaced[$key] = $tpl->variable( $key, $name ); // Tpl vars should be replaced after including 194 } 195 196 $item_value = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement ); 197 $tpl->setVariable( $key, $item_value, $name ); 198 } break; 199 } 200 next( $params ); 201 } 202 eZTemplateIncludeFunction::handleInclude( $textElements, $uri, $tpl, $rootNamespace, $name ); 203 // unset var 204 foreach ( $whatParamsShouldBeUnset as $key ) 205 { 206 $tpl->unsetVariable( $key, $name ); 207 } 208 // replace var 209 foreach ( $whatParamsShouldBeReplaced as $key => $item_value ) 210 { 211 $tpl->setVariable( $key, $item_value, $name ); 212 } 213 } 214 215 /*! 216 \static 217 Takes care of loading the template file and set it in the \a $text parameter. 218 */ 219 function handleInclude( &$textElements, &$uri, &$tpl, $rootNamespace, $name ) 220 { 221 $tpl->processURI( $uri, true, $extraParameters, $textElements, $name, $name ); 222 } 223 224 /*! 225 Returns false, telling the template parser that this is a single tag. 226 */ 227 function hasChildren() 228 { 229 return false; 230 } 231 232 /// \privatesection 233 /// The name of the include function 234 var $IncludeName; 235 } 236 237 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |