[ 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/ -> eztemplatecontroloperator.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateControlOperator class
   4  //
   5  // Created on: <18-Apr-2002 12:15:07 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 eZTemplateControlOperator eztemplatetypeoperator.php
  31    \ingroup eZTemplateOperators
  32    \brief Operators for checking variable type
  33  
  34  Usage:
  35  // Evalue condition and if true return body
  36  cond(is_set($var),$var,
  37       true(),2)
  38  // Return first element that is set
  39  first_set($var1,$var2,$var3,0)
  40  
  41  */
  42  
  43  class eZTemplateControlOperator
  44  {
  45      /*!
  46       Initializes the operator class with the various operator names.
  47      */
  48      function eZTemplateControlOperator(  /*! The name array */
  49          $condName = 'cond',
  50          $firstSetName = 'first_set' )
  51      {
  52          $this->Operators = array( $condName, $firstSetName );
  53          $this->CondName = $condName;
  54          $this->FirstSetName = $firstSetName;
  55      }
  56  
  57      /*!
  58       Returns the operators in this class.
  59      */
  60      function &operatorList()
  61      {
  62          return $this->Operators;
  63      }
  64  
  65      function operatorTemplateHints()
  66      {
  67          return array( $this->CondName => array( 'input' => false,
  68                                                  'output' => true,
  69                                                  'parameters' => true,
  70                                                  'element-transformation' => true,
  71                                                  'transform-parameters' => true,
  72                                                  'input-as-parameter' => false,
  73                                                  'element-transformation-func' => 'condTransform' ),
  74                        $this->FirstSetName => array( 'input' => false,
  75                                                      'output' => true,
  76                                                      'parameters' => true,
  77                                                      'element-transformation' => true,
  78                                                      'transform-parameters' => true,
  79                                                      'input-as-parameter' => false,
  80                                                      'element-transformation-func' => 'condTransform' ) );
  81      }
  82  
  83      /*!
  84       \reimp
  85      */
  86      function condTransform( $operatorName, &$node, &$tpl, &$resourceData,
  87                              &$element, &$lastElement, &$elementList, &$elementTree, &$parameters )
  88      {
  89          switch( $operatorName )
  90          {
  91              case $this->CondName:
  92              {
  93                  $paramCount = count( $parameters );
  94                  $clauseCount = floor( $paramCount / 2 );
  95                  $hasDefaultClause = ( $paramCount % 2 ) != 0;
  96  
  97                  if ( $paramCount == 1 )
  98                  {
  99                      return $parameters[0];
 100                  }
 101  
 102                  $values = array();
 103                  $code = '';
 104                  $spacing = 0;
 105                  $spacingCode = '';
 106                  for ( $i = 0; $i < $clauseCount; ++$i )
 107                  {
 108                      $prevSpacingCode = $spacingCode;
 109                      $spacingCode = str_repeat( " ", $spacing*4 );
 110                      if ( $i > 0 )
 111                      {
 112                          $code .= $prevSpacingCode . "else\n" . $prevSpacingCode . "{\n";
 113                      }
 114  
 115                      $values[] = $parameters[$i*2];
 116                      if ( $i > 0 )
 117                      {
 118                          $code .= $spacingCode . "%code" . count( $values ) . "%\n";
 119                      }
 120                      $code .= $spacingCode. 'if ( %' . count( $values ) . "% )\n" . $spacingCode . "{\n";
 121  
 122                      if ( !eZTemplateNodeTool::isStaticElement( $parameters[$i*2 + 1] ) )
 123                      {
 124                          $values[] = $parameters[$i*2 + 1];
 125                          $code .= ( $spacingCode . "    %code" . count( $values ) . "%\n" .
 126                                     $spacingCode . "    %output% = %" . count( $values ) . "%;\n" );
 127                      }
 128                      else
 129                      {
 130                          $code .= $spacingCode . '    %output% = ' . eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameters[$i*2 + 1] ), 0, 0, false ) . ';' . "\n";
 131                      }
 132                      $code .= $spacingCode . "}\n";
 133                      ++$spacing;
 134                  }
 135                  $bracketCount = $clauseCount - 1;
 136                  if ( $hasDefaultClause )
 137                  {
 138                      ++$bracketCount;
 139                      $values[] = $parameters[$paramCount - 1];
 140                      if ( $clauseCount > 0 )
 141                      {
 142                          $code .= $spacingCode . "else\n" . $spacingCode . "{\n" . $spacingCode . "    %code" . count( $values ) . "%\n    ";
 143                      }
 144  
 145                      $code .= $spacingCode . '%output% = %' . count( $values ) . "%;\n";
 146                  }
 147                  for ( $clauseIndex = 0; $clauseIndex < $bracketCount; ++$clauseIndex )
 148                  {
 149                      $spacingCode = str_repeat( " ", ( $bracketCount - $clauseIndex - 1 ) *4 );
 150                      $code .= $spacingCode . "}\n";
 151                  }
 152  
 153                  return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
 154              } break;
 155  
 156              case $this->FirstSetName:
 157              {
 158                  $values = array();
 159                  $code = '';
 160                  $spacing = 0;
 161                  $spacingCode = '';
 162                  $nestCount = 0;
 163                  for( $i = 0; $i < count( $parameters ); ++$i )
 164                  {
 165                      if ( $i != 0 )
 166                      {
 167                          $code .= "$spacingCode}\n" . $spacingCode . "else\n$spacingCode{\n";
 168                      }
 169                      $spacingCode = str_repeat( ' ', $spacing*4 );
 170                      ++$spacing;
 171  
 172                      if ( eZTemplateNodeTool::isStaticElement( $parameters[$i] ) )
 173                      {
 174                          $code .= "$spacingCode%output% = " . eZPHPCreator::variableText( eZTemplateNodeTool::elementStaticValue( $parameters[$i] ), 0, 0, false ) . ";\n";
 175                          break;
 176                      }
 177                      ++$nestCount;
 178  
 179                      $values[] = $parameters[$i];
 180                      $code .= ( $spacingCode . "%code" . count( $values ) . "%\n" .
 181                                 $spacingCode . 'if ( isset( %' . count( $values ) . "% ) )\n" .
 182                                 $spacingCode . "{\n" .
 183                                 $spacingCode . "    %output% = %" . count( $values ) . '%;' . "\n" );
 184                  }
 185                  for ( $i = 0; $i < $nestCount; ++$i )
 186                  {
 187                      $spacing = $nestCount - $i - 1;
 188                      $spacingCode = str_repeat( ' ', $spacing*4 );
 189                      $code .= $spacingCode . "}\n";
 190                  }
 191  
 192                  return array( eZTemplateNodeTool::createCodePieceElement( $code, $values ) );
 193              } break;
 194          }
 195      }
 196  
 197      /*!
 198       \return true to tell the template engine that the parameter list exists per operator type.
 199      */
 200      function namedParameterPerOperator()
 201      {
 202          return true;
 203      }
 204  
 205      /*!
 206       See eZTemplateOperator::namedParameterList
 207      */
 208      function namedParameterList()
 209      {
 210          return array();
 211      }
 212  
 213      /*!
 214       Examines the input value and outputs a boolean value. See class documentation for more information.
 215      */
 216      function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace, &$currentNamespace, &$value, &$namedParameters,
 217                       $placement )
 218      {
 219          switch ( $operatorName )
 220          {
 221              case $this->CondName:
 222              {
 223                  $parameterCount = count( $operatorParameters );
 224                  $clauseCount = floor( $parameterCount / 2 );
 225                  $clauseMod = $parameterCount % 2;
 226                  $conditionSuccess = false;
 227                  for ( $i = 0; $i < $clauseCount; ++$i )
 228                  {
 229                      $condition =& $tpl->elementValue( $operatorParameters[$i*2], $rootNamespace, $currentNamespace, $placement );
 230                      if ( $condition )
 231                      {
 232                          $body =& $tpl->elementValue( $operatorParameters[$i*2 + 1], $rootNamespace, $currentNamespace, $placement );
 233                          $conditionSuccess = true;
 234                          $value = $body;
 235                          break;
 236                      }
 237                  }
 238                  if ( !$conditionSuccess and
 239                       $clauseMod > 0 )
 240                  {
 241                      $condition =& $tpl->elementValue( $operatorParameters[count($operatorParameters) - 1], $rootNamespace, $currentNamespace, $placement );
 242                      if ( $condition )
 243                      {
 244                          $conditionSuccess = true;
 245                          $value = $condition;
 246                      }
 247                  }
 248              } break;
 249              case $this->FirstSetName:
 250              {
 251                  if ( count( $operatorParameters ) > 0 )
 252                  {
 253                      for ( $i = 0; $i < count( $operatorParameters ); ++$i )
 254                      {
 255                          $operand =& $tpl->elementValue( $operatorParameters[$i], $rootNamespace, $currentNamespace, $placement, true );
 256                          if ( $operand !== null )
 257                          {
 258                              $value = $operand;
 259                              return;
 260                          }
 261                      }
 262                  }
 263                  $value = null;
 264              } break;
 265          }
 266      }
 267  
 268      /// The array of operators
 269      var $Operators;
 270  };
 271  
 272  ?>


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