eZ Publish  [4.0]
eztemplateattributeoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateAttributeOperator class
00004 //
00005 // Created on: <01-Mar-2002 13:50:09 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 /*!
00032   \class eZTemplateAttributeOperator eztemplateattributeoperator.php
00033   \ingroup eZTemplateOperators
00034   \brief Display of variable attributes using operator "attribute"
00035 
00036   This class allows for displaying template variable attributes. The display
00037   is recursive and the number of levels can be maximized.
00038 
00039   The operator can take three parameters. The first is the maximum number of
00040   levels to recurse, if blank or omitted the maxium level is infinity.
00041   The second is the type of display, if set to "text" the output is as pure text
00042   otherwise as html.
00043   The third is whether to show variable values or not, default is to not show.
00044 
00045 \code
00046 // Example template code
00047 
00048 // Display attributes of $myvar
00049 {$myvar|attribute}
00050 // Display 2 levels of $tree
00051 {$tree|attribute(2)}
00052 // Display attributes and values of $item
00053 {$item|attribute(,,show)}
00054 \endcode
00055 
00056 */
00057 
00058 class eZTemplateAttributeOperator
00059 {
00060     /*!
00061      Initializes the object with the name $name, default is "attribute".
00062     */
00063     function eZTemplateAttributeOperator( $name = "attribute" )
00064     {
00065         $this->AttributeName = $name;
00066         $this->Operators = array( $name );
00067     }
00068 
00069     /*!
00070      Returns the template operators.
00071     */
00072     function operatorList()
00073     {
00074         return $this->Operators;
00075     }
00076 
00077     function operatorTemplateHints()
00078     {
00079         return array( $this->AttributeName => array( 'input' => true,
00080                                                      'output' => true,
00081                                                      'parameters' => 3 ) );
00082     }
00083 
00084     /*!
00085      See eZTemplateOperator::namedParameterList()
00086     */
00087     function namedParameterList()
00088     {
00089         return array( "show_values" => array( "type" => "string",
00090                                               "required" => false,
00091                                               "default" => "" ),
00092                       "max_val" => array( "type" => "numerical",
00093                                           "required" => false,
00094                                           "default" => 2 ),
00095                       "as_html" => array( "type" => "boolean",
00096                                           "required" => false,
00097                                           "default" => true ) );
00098     }
00099 
00100     /*!
00101      Display the variable.
00102     */
00103     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
00104     {
00105         $max = $namedParameters["max_val"];
00106         $as_html = $namedParameters["as_html"];
00107         $show_values = $namedParameters["show_values"] == "show";
00108         $txt = "";
00109         $this->displayVariable( $operatorValue, $as_html, $show_values, $max, 0, $txt );
00110         if ( $as_html )
00111         {
00112             $headers = "<th align=\"left\">Attribute</th>\n<th align=\"left\">Type</th>\n";
00113             if ( $show_values )
00114                 $headers .= "<th align=\"left\">Value</th>\n";
00115             $operatorValue = "<table><tr>$headers</tr>\n$txt</table>\n";
00116         }
00117         else
00118             $operatorValue = $txt;
00119     }
00120 
00121     /*!
00122      \private
00123      Helper function for recursive display of attributes.
00124      $value is the current variable, $as_html is true if display as html,
00125      $max is the maximum number of levels, $cur_level the current level
00126      and $txt is the output text which the function adds to.
00127     */
00128     function displayVariable( &$value, $as_html, $show_values, $max, $cur_level, &$txt )
00129     {
00130         if ( $max !== false and $cur_level >= $max )
00131             return;
00132         if ( is_array( $value ) )
00133         {
00134             foreach( $value as $key => $item )
00135             {
00136                 $type = gettype( $item );
00137                 if ( is_object( $item ) )
00138                     $type .= "[" . get_class( $item ) . "]";
00139 
00140                 if ( is_bool( $item ) )
00141                     $itemValue = $item ? "true" : "false";
00142                 else if ( is_array( $item ) )
00143                     $itemValue = 'Array(' . count( $item ) . ')';
00144                 else if ( is_string( $item ) )
00145                     $itemValue = "'" . $item . "'";
00146                 else if ( is_object( $item ) )
00147                     $itemValue = method_exists( $item, '__toString' ) ? (string)$item : 'Object';
00148                 else
00149                     $itemValue = $item;
00150                 if ( $as_html )
00151                 {
00152                     $spacing = str_repeat( ">", $cur_level );
00153                     if ( $show_values )
00154                         $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n<td>$itemValue</td>\n</tr>\n";
00155                     else
00156                         $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n</tr>\n";
00157                 }
00158                 else
00159                 {
00160                     $spacing = str_repeat( " ", $cur_level*4 );
00161                     if ( $show_values )
00162                         $txt .= "$spacing$key ($type=$itemValue)\n";
00163                     else
00164                         $txt .= "$spacing$key ($type)\n";
00165                 }
00166                 $this->displayVariable( $item, $as_html, $show_values, $max, $cur_level + 1, $txt );
00167             }
00168         }
00169         else if ( is_object( $value ) )
00170         {
00171             if ( !method_exists( $value, "attributes" ) or
00172                  !method_exists( $value, "attribute" ) )
00173                 return;
00174             $attrs = $value->attributes();
00175             foreach ( $attrs as $key )
00176             {
00177                 $item = $value->attribute( $key );
00178                 $type = gettype( $item );
00179                 if ( is_object( $item ) )
00180                     $type .= "[" . get_class( $item ) . "]";
00181 
00182                 if ( is_bool( $item ) )
00183                     $itemValue = $item ? "true" : "false";
00184                 else if ( is_array( $item ) )
00185                     $itemValue = 'Array(' . count( $item ) . ')';
00186                 else if ( is_numeric( $item ) )
00187                     $itemValue = $item;
00188                 else if ( is_string( $item ) )
00189                     $itemValue = "'" . $item . "'";
00190                 else if ( is_object( $item ) )
00191                     $itemValue = method_exists( $item, '__toString' ) ? (string)$item : 'Object';
00192                 else
00193                     $itemValue = $item;
00194                 if ( $as_html )
00195                 {
00196                     $spacing = str_repeat( ">", $cur_level );
00197                     if ( $show_values )
00198                         $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n<td>$itemValue</td>\n</tr>\n";
00199                     else
00200                         $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n</tr>\n";
00201                 }
00202                 else
00203                 {
00204                     $spacing = str_repeat( " ", $cur_level*4 );
00205                     if ( $show_values )
00206                         $txt .= "$spacing$key ($type=$itemValue)\n";
00207                     else
00208                         $txt .= "$spacing$key ($type)\n";
00209                 }
00210                 $this->displayVariable( $item, $as_html, $show_values, $max, $cur_level + 1, $txt );
00211             }
00212         }
00213     }
00214 
00215     /// The array of operators, used for registering operators
00216     public $Operators;
00217 }
00218 
00219 ?>