eZ Publish  [4.0]
eztocoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTOCOperator class
00004 //
00005 // Created on: <24-Aug-2005 15:11:12 bf>
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 eztocoperator.php
00032 */
00033 
00034 /*!
00035   \class eZTOCOperator eztocoperator.php
00036   \brief The class eZTOCOperator does
00037 
00038 */
00039 
00040 class eZTOCOperator
00041 {
00042     /*!
00043      Constructor
00044     */
00045     function eZTOCOperator()
00046     {
00047     }
00048 
00049     /*!
00050      \return an array with the template operator name.
00051     */
00052     function operatorList()
00053     {
00054         return array( 'eztoc' );
00055     }
00056 
00057     /*!
00058      \return true to tell the template engine that the parameter list exists per operator type,
00059              this is needed for operator classes that have multiple operators.
00060     */
00061     function namedParameterPerOperator()
00062     {
00063         return true;
00064     }
00065 
00066     /*!
00067      See eZTemplateOperator::namedParameterList
00068     */
00069     function namedParameterList()
00070     {
00071         return array( 'eztoc' => array( 'dom' => array( 'type' => 'object',
00072                                                         'required' => true,
00073                                                         'default' => 0 ) ) );
00074     }
00075     /*!
00076      Executes the PHP function for the operator cleanup and modifies \a $operatorValue.
00077     */
00078     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
00079     {
00080         $dom = $namedParameters['dom'];
00081         if ( $dom instanceof eZContentObjectAttribute )
00082         {
00083             $this->ObjectAttributeId = $dom->attribute( 'id' );
00084             $content = $dom->attribute( 'content' );
00085             $xmlData = $content->attribute( 'xml_data' );
00086 
00087             $domTree = new DOMDocument( '1.0', 'utf-8' );
00088             $domTree->preserveWhiteSpace = false;
00089             $success = $domTree->loadXML( $xmlData );
00090 
00091             $tocText = '';
00092             if ( $success )
00093             {
00094                 $this->HeaderCounter = array();
00095                 $this->LastHeaderLevel = 0;
00096 
00097                 $rootNode = $domTree->documentElement;
00098                 $tocText .= $this->handleSection( $rootNode );
00099 
00100                 while ( $this->LastHeaderLevel > 0 )
00101                 {
00102                     $tocText .= "</li>\n</ul>\n";
00103                     $this->LastHeaderLevel--;
00104                 }
00105             }
00106         }
00107         $operatorValue = $tocText;
00108     }
00109 
00110     function handleSection( $sectionNode, $level = 0 )
00111     {
00112         // Reset next level counter
00113         if ( !isset( $this->HeaderCounter[$level + 1] ) )
00114         {
00115             $this->HeaderCounter[$level + 1] = 0;
00116         }
00117 
00118         $tocText = '';
00119         $children = $sectionNode->childNodes;
00120         foreach ( $children as $child )
00121         {
00122             if ( $child->nodeName == 'section' )
00123             {
00124                 $tocText .= $this->handleSection( $child, $level + 1 );
00125             }
00126 
00127             if ( $child->nodeName == 'header' )
00128             {
00129                 if ( $level > $this->LastHeaderLevel )
00130                 {
00131                     while ( $level > $this->LastHeaderLevel )
00132                     {
00133                         $tocText .= "\n<ul><li>";
00134                         $this->LastHeaderLevel++;
00135                     }
00136                 }
00137                 elseif ( $level == $this->LastHeaderLevel )
00138                 {
00139                     $tocText .= "</li>\n<li>";
00140                 }
00141                 else
00142                 {
00143                     $tocText .= "</li>\n";
00144                     while ( $level < $this->LastHeaderLevel )
00145                     {
00146                         $tocText .= "</ul></li>\n";
00147                         $this->LastHeaderLevel--;
00148                     }
00149                     $tocText .= "<li>";
00150                 }
00151                 $this->LastHeaderLevel = $level;
00152 
00153                 $this->HeaderCounter[$level] += 1;
00154                 $i = 1;
00155                 $headerAutoName = "";
00156                 while ( $i <= $level )
00157                 {
00158                     if ( $i > 1 )
00159                         $headerAutoName .= "_";
00160 
00161                     $headerAutoName .= $this->HeaderCounter[$i];
00162                     $i++;
00163                 }
00164                 $tocText .= '<a href="#eztoc' . $this->ObjectAttributeId . '_' . $headerAutoName . '">' . $child->textContent . '</a>';
00165             }
00166         }
00167 
00168         return $tocText;
00169     }
00170 
00171     public $HeaderCounter = array();
00172     public $LastHeaderLevel = 0;
00173 
00174     public $ObjectAttributeId;
00175 }
00176 
00177 ?>