eZ Publish  [4.0]
eztemplatesectioniterator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateSectionIterator class
00004 //
00005 // Created on: <26-Feb-2004 11:33:05 >
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 eztemplatesectioniterator.php
00032 */
00033 
00034 /*!
00035   \class eZTemplateSectionIterator eztemplatesectioniterator.php
00036   \ingroup eZTemplateFunctions
00037   \brief The iterator item in a section loop which works as a proxy.
00038 
00039   The iterator provides transparent access to iterator items. It will
00040   redirect all attribute calls to the iterator item with the exception
00041   of a few internal values. The internal values are
00042   - item - The actual item, provides backwards compatability
00043   - key - The current key
00044   - index - The current index value (starts at 0 and increases with 1 for each element)
00045   - number - The current index value + 1 (starts at 1 and increases with 1 for each element)
00046   - sequence - The current sequence value
00047   - last - The last iterated element item
00048 */
00049 
00050 class eZTemplateSectionIterator
00051 {
00052     /*!
00053      Initializes the iterator with empty values.
00054     */
00055     function eZTemplateSectionIterator()
00056     {
00057         $this->InternalAttributes = array( 'item' => false,
00058                                            'key' => false,
00059                                            'index' => false,
00060                                            'number' => false,
00061                                            'sequence' => false,
00062                                            'last' => false );
00063         $this->InternalAttributeNames = array_keys( $this->InternalAttributes );
00064     }
00065 
00066     /*!
00067      \return the value of the current item for the template system to use.
00068     */
00069     function templateValue()
00070     {
00071         $value = $this->InternalAttributes['item'];
00072         return $value;
00073     }
00074 
00075     /*!
00076      \return a merged list of attributes from both the internal attributes and the items attributes.
00077     */
00078     function attributes()
00079     {
00080         $attributes = array();
00081         $item = $this->InternalAttributes['item'];
00082         if ( is_array( $item ) )
00083         {
00084             $attributes = array_keys( $item );
00085         }
00086         else if ( is_object( $item ) and
00087                   method_exists( $item, 'attributes' ) )
00088         {
00089             $attributes = $item->attributes();
00090         }
00091         $attributes = array_merge( $this->InternalAttributes, $attributes );
00092         $attributes = array_unique( $attributes );
00093         return $attributes;
00094     }
00095 
00096     /*!
00097      \return \c true if the attribute \a $name exists either in
00098              the internal attributes or in the item value.
00099     */
00100     function hasAttribute( $name )
00101     {
00102         if ( in_array( $name, $this->InternalAttributeNames ) )
00103             return true;
00104         $item = $this->InternalAttributes['item'];
00105         if ( is_array( $item ) )
00106         {
00107             return in_array( $name, array_keys( $item ) );
00108         }
00109         else if ( is_object( $item ) and
00110                   method_exists( $item, 'hasAttribute' ) )
00111         {
00112             return $item->hasAttribute( $name );
00113         }
00114         return false;
00115     }
00116 
00117     /*!
00118      \return the attribute value of either the internal attributes or
00119              from the item value if the attribute exists for it.
00120     */
00121     function attribute( $name )
00122     {
00123         if ( in_array( $name, $this->InternalAttributeNames ) )
00124         {
00125             return $this->InternalAttributes[$name];
00126         }
00127         $item = $this->InternalAttributes['item'];
00128         if ( is_array( $item ) )
00129         {
00130             return $item[$name];
00131         }
00132         else if ( is_object( $item ) and
00133                   method_exists( $item, 'attribute' ) )
00134         {
00135             return $item->attribute( $name );
00136         }
00137         eZDebug::writeError( "Attribute '$name' does not exist", 'eZTemplateSectionIterator::attribute' );
00138         return null;
00139     }
00140 
00141     /*!
00142      Updates the iterator with the current iteration values.
00143     */
00144     function setIteratorValues( $item, $key, $index, $number, $sequence, &$last )
00145     {
00146         $this->InternalAttributes['item'] = $item;
00147         $this->InternalAttributes['key'] = $key;
00148         $this->InternalAttributes['index'] = $index;
00149         $this->InternalAttributes['number'] = $number;
00150         $this->InternalAttributes['sequence'] = $sequence;
00151         $this->InternalAttributes['last'] = $last;
00152     }
00153 
00154     /*!
00155      Updates the current sequence value to \a $sequence.
00156     */
00157     function setSequence( $sequence )
00158     {
00159         $this->InternalAttributes['sequence'] = $sequence;
00160     }
00161 }
00162 
00163 ?>