eZ Publish  [4.0]
eztemplatetoolbarfunction.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateToolbarFunction class
00004 //
00005 // Created on: <04-Mar-2004 13:22:32 wy>
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 eztemplatetoolbarfunction.php
00032 */
00033 
00034 /*!
00035   \class eZTemplateToolbarFunction eztemplatetoolbarfunction.php
00036   \brief The class eZTemplateToolarFunction does
00037 
00038 */
00039 
00040 class eZTemplateToolbarFunction
00041 {
00042 
00043     /*!
00044      Initializes the object with names.
00045     */
00046     function eZTemplateToolbarFunction( $blockName = 'tool_bar' )
00047     {
00048         $this->BlockName = $blockName;
00049     }
00050 
00051     /*!
00052      Returns an array containing the name of the block function, default is "block".
00053      The name is specified in the constructor.
00054     */
00055     function functionList()
00056     {
00057         return array( $this->BlockName );
00058     }
00059 
00060     function functionTemplateHints()
00061     {
00062         return array( $this->BlockName => array( 'parameters' => true,
00063                                                  'static' => false,
00064                                                  'transform-children' => false,
00065                                                  'tree-transformation' => true,
00066                                                  'transform-parameters' => true ) );
00067     }
00068 
00069     function templateNodeTransformation( $functionName, &$node,
00070                                          $tpl, $parameters, $privateData )
00071     {
00072         if ( $functionName != $this->BlockName )
00073             return false;
00074 
00075         $parameters = eZTemplateNodeTool::extractFunctionNodeParameters( $node );
00076 
00077         if ( !isset( $parameters['name'] ) )
00078             return false;
00079 
00080         // Read ini file
00081         $toolbarIni = eZINI::instance( "toolbar.ini" );
00082 
00083         if ( isset( $parameters["view"] ) )
00084         {
00085             $viewData = $parameters["view"];
00086             $viewMode = eZTemplateNodeTool::elementStaticValue( $viewData );
00087         }
00088         else
00089         {
00090             $viewMode = "full";
00091         }
00092 
00093         $params = $parameters;
00094         $namespaceValue = false;
00095         if ( isset( $parameters["name"] ) )
00096         {
00097             $nameData = $parameters["name"];
00098             if ( !eZTemplateNodeTool::isStaticElement( $nameData ) )
00099                 return false;
00100 
00101             $nameValue = eZTemplateNodeTool::elementStaticValue( $nameData );
00102 
00103             $toolbarPosition = $nameValue;
00104             $toolbarName = "Toolbar_" . $toolbarPosition;
00105             $toolArray = $toolbarIni->variable( $toolbarName, 'Tool' );
00106             if ( !is_array( $toolArray ) )
00107                 $toolArray = array();
00108 
00109             $newNodes = array();
00110             foreach ( array_keys( $toolArray ) as $toolKey )
00111             {
00112                 $tool = $toolArray[$toolKey];
00113                 $placement = $toolKey + 1;
00114 
00115                 $uriString = "design:toolbar/$viewMode/$tool.tpl";
00116 
00117                 $placementValue = "";
00118                 $firstValue = false;
00119                 $lastValue = false;
00120                 if ( $placement == 1 )
00121                 {
00122                     if ( $placement == count( $toolArray ) )
00123                     {
00124                         $firstValue = true;
00125                         $lastValue = true;
00126                         $placementValue = "last";
00127                     }
00128                     else
00129                     {
00130                         $firstValue = true;
00131                         $placementValue = "first";
00132                     }
00133                 }
00134                 else if ( $placement == count( $toolArray ) )
00135                 {
00136                     $lastValue = true;
00137                     $placementValue = "last";
00138                 }
00139 
00140                 $resourceName = "";
00141                 $templateName = "";
00142                 $resource = $tpl->resourceFor( $uriString, $resourceName, $templateName );
00143                 $resourceData = $tpl->resourceData( $resource, $uriString, $resourceName, $templateName );
00144                 $resourceData['use-comments'] = eZTemplateCompiler::isCommentsEnabled();
00145 
00146                 $includeNodes = $resource->templateNodeTransformation( $functionName, $node, $tpl, $resourceData, $parameters, $namespaceValue );
00147                 if ( $includeNodes === false )
00148                     return false;
00149 
00150                 $uniqID = md5( uniqid('inc') );
00151                 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$oldRestoreIncludeArray" . "_$uniqID = isset( \$restoreIncludeArray ) ? \$restoreIncludeArray : array();\n".
00152                                                                        "\$restoreIncludeArray = array();\n");
00153                 $variableList = array();
00154                 foreach ( array_keys( $parameters ) as $parameterName )
00155                 {
00156                     if ( $parameterName == 'name' or
00157                          $parameterName == 'view' )
00158                         continue;
00159                     $parameterData =& $parameters[$parameterName];
00160                     $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( isset( \$vars['']['$parameterName'] ) )\n".
00161                                                                            "    \$restoreIncludeArray[] = array( '', '$parameterName', \$vars['']['$parameterName'] );\n".
00162                                                                            "elseif ( !isset( \$vars['']['$parameterName'] ) ) \n".
00163                                                                            "    \$restoreIncludeArray[] = array( '', '$parameterName', 'unset' );\n" );
00164 
00165                     $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameterData, false, array(),
00166                                                                           array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, $parameterName ) );
00167                     $variableList[] = $parameterName;
00168                 }
00169 
00170                 $actionParameters = array();
00171                 if ( $toolbarIni->hasGroup( "Tool_" . $tool ) )
00172                 {
00173                     $actionParameters = $toolbarIni->group( "Tool_" . $tool );
00174                 }
00175                 if ( $toolbarIni->hasGroup( "Tool_" . $toolbarPosition . "_" . $tool . "_" . $placement ) )
00176                 {
00177                     $actionParameters = array_merge( $actionParameters, $toolbarIni->group( "Tool_" . $toolbarPosition . "_" . $tool . "_" . $placement ) );
00178                 }
00179                 foreach ( array_keys( $actionParameters ) as $key )
00180                 {
00181                     $itemValue = $actionParameters[$key];
00182                     $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $itemValue, false, array(),
00183                                                                           array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, $key ) );
00184                     $variableList[] = $key;
00185                 }
00186 
00187                 // Add parameter tool_id and offset
00188                 $toolIDValue =  "Tool_" . $toolbarPosition . "_" . $tool . "_" . $placement;
00189                 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $toolIDValue, false, array(),
00190                                                                       array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, "tool_id" ) );
00191                 $variableList[] = "tool_id";
00192 
00193                 $toolOffset = $placement;
00194                 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $toolOffset, false, array(),
00195                                                                       array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, "offset" ) );
00196                 $variableList[] = "offset";
00197 
00198                 // Add parameter first, last and placement
00199                 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $firstValue, false, array(),
00200                                                                       array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, "first" ) );
00201                 $variableList[] = "first";
00202 
00203                 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $lastValue, false, array(),
00204                                                                       array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, "last" ) );
00205                 $variableList[] = "last";
00206 
00207                 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $placementValue, false, array(),
00208                                                                       array( $namespaceValue, eZTemplate::NAMESPACE_SCOPE_RELATIVE, "placement" ) );
00209                 $variableList[] = "placement";
00210 
00211                 $newNodes = array_merge( $newNodes, $includeNodes );
00212 
00213                 // Restore previous variables, before including
00214                 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "foreach ( \$restoreIncludeArray as \$element )\n".
00215                                                                        "{\n".
00216                                                                        "    if ( \$element[2] === 'unset' )\n".
00217                                                                        "    {\n".
00218                                                                        "        unset( \$vars[\$element[0]][\$element[1]] );\n".
00219                                                                        "        continue;\n".
00220                                                                        "    }\n".
00221                                                                        "    \$vars[\$element[0]][\$element[1]] = \$element[2];\n".
00222                                                                        "}\n".
00223                                                                        "\$restoreIncludeArray = \$oldRestoreIncludeArray" . "_$uniqID;\n" );
00224             }
00225         }
00226         return $newNodes;
00227     }
00228 
00229     /*!
00230      Processes the function with all it's children.
00231     */
00232     function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00233     {
00234         $params = $functionParameters;
00235         switch ( $functionName )
00236         {
00237             case $this->BlockName:
00238             {
00239                 $viewMode = "full";
00240                 $name = $currentNamespace;
00241                 // Read ini file
00242                 $toolbarIni = eZINI::instance( "toolbar.ini" );
00243 
00244                 if ( isset( $functionParameters["view"] ) )
00245                 {
00246                     $viewMode = $tpl->elementValue( $functionParameters["view"], $rootNamespace, $currentNamespace, $functionPlacement );
00247                 }
00248 
00249                 if ( isset( $functionParameters["name"] ) )
00250                 {
00251                     reset( $params );
00252                     $whatParamsShouldBeUnset = array();
00253                     $whatParamsShouldBeReplaced = array();
00254                     while ( ( $key = key( $params ) ) !== null )
00255                     {
00256                         $item =& $params[$key];
00257                         switch ( $key )
00258                         {
00259                             case "name":
00260                             case "view":
00261                                 break;
00262 
00263                             default:
00264                             {
00265                                 if ( !$tpl->hasVariable( $key, $name ) )
00266                                 {
00267                                     $whatParamsShouldBeUnset[] = $key; // Tpl vars should be removed after including
00268                                 }
00269                                 else
00270                                 {
00271                                     $whatParamsShouldBeReplaced[$key] = $tpl->variable( $key, $name ); // Tpl vars should be replaced after including
00272                                 }
00273 
00274                                 $item_value = $tpl->elementValue( $item, $rootNamespace, $currentNamespace, $functionPlacement );
00275                                 $tpl->setVariable( $key, $item_value, $name );
00276                             } break;
00277                         }
00278                         next( $params );
00279                     }
00280                     $toolbarPosition = $tpl->elementValue( $functionParameters["name"], $rootNamespace, $currentNamespace, $functionPlacement );
00281                     $definedVariables = array();
00282                     $toolbarName = "Toolbar_" . $toolbarPosition;
00283                     $toolArray = $toolbarIni->variable( $toolbarName, 'Tool' );
00284 
00285                     if ( !is_array( $toolArray ) )
00286                         $toolArray = array();
00287 
00288                     $actionParameters = array();
00289                     foreach ( array_keys( $toolArray ) as $toolKey )
00290                     {
00291                         $definedVariables = array();
00292                         $tool = $toolArray[$toolKey];
00293                         $placement = $toolKey + 1;
00294                         if ( $toolbarIni->hasGroup( "Tool_" . $tool ) )
00295                         {
00296                             $actionParameters = $toolbarIni->group( "Tool_" . $tool );
00297                         }
00298                         if ( $toolbarIni->hasGroup( "Tool_" . $toolbarPosition . "_" . $tool . "_" . $placement ) )
00299                         {
00300                             $actionParameters = array_merge( $actionParameters, $toolbarIni->group( "Tool_" . $toolbarPosition . "_" . $tool . "_" . $placement ) );
00301                         }
00302                         foreach ( array_keys( $actionParameters ) as $key )
00303                         {
00304                             $itemValue = $actionParameters[$key];
00305                             $tpl->setVariable( $key, $itemValue, $name );
00306                             $definedVariables[] = $key;
00307                         }
00308                         $toolIDValue =  "Tool_" . $toolbarPosition . "_" . $tool . "_" . $placement;
00309                         $tpl->setVariable( "tool_id", $toolIDValue, $name );
00310                         $definedVariables[] = "tool_id";
00311                         $toolOffset = $placement;
00312                         $tpl->setVariable( "offset", $toolOffset, $name );
00313                         $definedVariables[] = "offset";
00314                         $uri = "design:toolbar/$viewMode/$tool.tpl";
00315 
00316                         if ( $placement == 1 )
00317                         {
00318                             if ( count( $toolArray ) == 1 )
00319                             {
00320                                 $tpl->setVariable( "first", true );
00321                                 $tpl->setVariable( "last", true );
00322                                 $tpl->setVariable( "placement", "last" );
00323                                 $definedVariables[] = "first";
00324                                 $definedVariables[] = "last";
00325                                 $definedVariables[] = "placement";
00326                             }
00327                             else
00328                             {
00329                                 $tpl->setVariable( "first", true );
00330                                 $tpl->setVariable( "last", false );
00331                                 $tpl->setVariable( "placement", "first" );
00332                                 $definedVariables[] = "first";
00333                                 $definedVariables[] = "last";
00334                                 $definedVariables[] = "placement";
00335                             }
00336                         }
00337                         else if ( $placement == count( $toolArray ) )
00338                         {
00339                             $tpl->setVariable( "first", false );
00340                             $tpl->setVariable( "last", true );
00341                             $tpl->setVariable( "placement", "last" );
00342                             $definedVariables[] = "first";
00343                             $definedVariables[] = "last";
00344                             $definedVariables[] = "placement";
00345                         }
00346                         else
00347                         {
00348                             $tpl->setVariable( "first", false );
00349                             $tpl->setVariable( "last",  false );
00350                             $tpl->setVariable( "placement", "" );
00351                             $definedVariables[] = "placement";
00352                         }
00353                         $tpl->processURI( $uri, true, $extraParameters, $textElements, $name, $name );
00354                         // unset var
00355                         foreach ( $whatParamsShouldBeUnset as $key )
00356                         {
00357                             $tpl->unsetVariable( $key, $name );
00358                         }
00359                         // replace var
00360                         foreach ( $whatParamsShouldBeReplaced as $key => $item_value )
00361                         {
00362                             $tpl->setVariable( $key, $item_value, $name );
00363                         }
00364                     }
00365                 }
00366             }
00367         }
00368     }
00369 
00370     /*!
00371      Returns false.
00372     */
00373     function hasChildren()
00374     {
00375         return false;
00376     }
00377 }
00378 
00379 ?>