eZ Publish  [4.0]
eztreemenuoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTreeMenuOperator class
00004 //
00005 // Created on: <12-Feb-2003 09:17:07 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 //include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00032 
00033 class eZTreeMenuOperator
00034 {
00035     /*!
00036      */
00037     function eZTreeMenuOperator( $name = 'treemenu' )
00038     {
00039         $this->Operators = array( $name );
00040     }
00041 
00042     /*!
00043      Returns the operators in this class.
00044     */
00045     function operatorList()
00046     {
00047         return $this->Operators;
00048     }
00049 
00050     /*!
00051      See eZTemplateOperator::namedParameterList()
00052     */
00053     function namedParameterList()
00054     {
00055         return array( 'path' => array( 'type' => 'array',
00056                                        'required' => true,
00057                                        'default' => false ),
00058                       'node_id' => array( 'type' => 'int',
00059                                           'required' => false,
00060                                           'default' => false ),
00061                       'class_filter' => array( 'type' => 'array',
00062                                                'required' => false,
00063                                                'default' => false ),
00064                       'depth_skip' => array( 'type' => 'int',
00065                                              'required' => false,
00066                                              'default' => false ),
00067                       'max_level' => array( 'type' => 'int',
00068                                             'required' => false,
00069                                             'default' => false ),
00070                       'is_selected_method' => array( 'type' => 'string',
00071                                                      'required' => false,
00072                                                      'default' => 'tree' ),
00073                       'indentation_level' => array( 'type' => 'int',
00074                                                     'required' => false,
00075                                                     'default' => 15 ),
00076                       'language' => array( 'type' => 'string|array',
00077                                            'required' => false,
00078                                            'default' => false ) );
00079     }
00080 
00081     /*!
00082      \reimp
00083     */
00084     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
00085     {
00086         $level = 0;
00087         $done = false;
00088         $i = 0;
00089         $pathArray = array();
00090         $tmpModulePath = $namedParameters['path'];
00091         $classFilter = $namedParameters['class_filter'];
00092         $language = $namedParameters['language'];
00093         // node_id is not used anymore
00094         if ( !empty( $namedParameters['node_id'] ) )
00095         {
00096             eZDebug::writeNotice( 'Deprecated parameter "node_id" in treemenu template operator' );
00097         }
00098 
00099         if ( $classFilter === false )
00100         {
00101             $classFilter = array();
00102         }
00103         else if ( count( $classFilter ) == 0 )
00104         {
00105             $classFilter = array( 1 );
00106         }
00107         $classFilter = ( count( $classFilter ) == 1 and !isset( $classFilter[0] ) ) ? array( 1 ) : $classFilter;
00108         if ( !$tmpModulePath[count($tmpModulePath)-1]['url'] and isset( $tmpModulePath[count($tmpModulePath)-1]['node_id'] ) )
00109             $tmpModulePath[count($tmpModulePath)-1]['url'] = "/content/view/full/" . $tmpModulePath[count($tmpModulePath)-1]['node_id'];
00110 
00111         $depthSkip = $namedParameters['depth_skip'];
00112         $indentationLevel = $namedParameters['indentation_level'];
00113 
00114         $maxLevel = $namedParameters['max_level'];
00115         $isSelectedMethod = $namedParameters['is_selected_method'];
00116         if ( $maxLevel === false )
00117             $maxLevel = 2;
00118 
00119         while ( !$done && isset( $tmpModulePath[$i+$depthSkip] ) )
00120         {
00121             // get node id
00122             $elements = explode( "/", $tmpModulePath[$i+$depthSkip]['url'] );
00123             $nodeID = false;
00124             if ( isset( $elements[4] ) )
00125                 $nodeID = $elements[4];
00126 
00127             $excludeNode = false;
00128 
00129             if ( isset( $elements[1] ) &&
00130                  isset( $elements[2] ) &&
00131                 $elements[1] == 'content' &&
00132                 $elements[2] == 'view' &&
00133                 is_numeric( $nodeID ) &&
00134                 $excludeNode == false &&
00135                 $level < $maxLevel )
00136             {
00137                 $node = eZContentObjectTreeNode::fetch( $nodeID );
00138                 if ( !isset( $node ) ) { $operatorValue = $pathArray; return; }
00139                 if ( isset( $tmpModulePath[$i+$depthSkip+1] ) )
00140                 {
00141                     $nextElements = explode( "/", $tmpModulePath[$i+$depthSkip+1]['url'] );
00142                     if ( isset( $nextElements[4] ) )
00143                     {
00144                         $nextNodeID = $nextElements[4];
00145                     }
00146                     else
00147                     {
00148                         $nextNodeID = false;
00149                     }
00150                 }
00151                 else
00152                     $nextNodeID = false;
00153 
00154                 $menuChildren = eZContentObjectTreeNode::subTreeByNodeID( array( 'Depth' => 1,
00155                                                                          'Offset' => 0,
00156                                                                          'SortBy' => $node->sortArray(),
00157                                                                          'Language' => $language,
00158                                                                          'ClassFilterType' => 'include',
00159                                                                          'ClassFilterArray' => $classFilter ),
00160                                                                   $nodeID );
00161 
00162                 /// Fill objects with attributes, speed boost
00163                 eZContentObject::fillNodeListAttributes( $menuChildren );
00164 
00165                 $tmpPathArray = array();
00166                 foreach ( $menuChildren as $child )
00167                 {
00168                     $name = $child->attribute( 'name' );
00169                     $tmpNodeID = $child->attribute( 'node_id' );
00170 
00171                     $url = "/content/view/full/$tmpNodeID/";
00172                     $urlAlias = "/" . $child->attribute( 'url_alias' );
00173 
00174                     $mainNode = $child->attribute( 'main_node_id' );
00175                     $dataMap = $child->attribute( 'data_map' );
00176                     $childrenCount = $child->attribute( 'children_count' );
00177                     $contentObject = $child->attribute( 'object' );
00178                     $isMain = false;
00179                     if ( $mainNode == $tmpNodeID )
00180                         $isMain = true;
00181 
00182                     $hasChildren = false;
00183                     if ( $childrenCount > 0 )
00184                         $hasChildren = true;
00185 
00186                     $indent = ($i - 1) * $indentationLevel;
00187 
00188                     $isSelected = false;
00189                     $nextNextElements = ( $isSelectedMethod == 'node' and isset( $tmpModulePath[$i+$depthSkip+2]['url'] ) ) ? explode( "/", $tmpModulePath[$i+$depthSkip+2]['url'] ) : null;
00190                     if ( $nextNodeID === $tmpNodeID and !isset( $nextNextElements[4] ) )
00191                     {
00192                         $isSelected = true;
00193                     }
00194 
00195                     $tmpPathArray[] = array( 'id' => $tmpNodeID,
00196                                              'level' => $i,
00197                                              'data_map' => $dataMap,
00198                                              'class_name' => $contentObject->classname(),
00199                                              'is_main_node' => $isMain,
00200                                              'has_children' => $hasChildren,
00201                                              'indent' => $indent,
00202                                              'url_alias' => $urlAlias,
00203                                              'url' => $url,
00204                                              'text' => $name,
00205                                              'is_selected' => $isSelected,
00206                                              'node' => $child );
00207                 }
00208 
00209                 // find insert pos
00210                 $j = 0;
00211                 $insertPos = 0;
00212                 foreach ( $pathArray as $path )
00213                 {
00214                     if ( $path['id'] == $nodeID )
00215                         $insertPos = $j;
00216                     $j++;
00217                 }
00218                 $restArray = array_splice( $pathArray, $insertPos + 1 );
00219 
00220                 $pathArray = array_merge( $pathArray, $tmpPathArray );
00221                 $pathArray = array_merge( $pathArray, $restArray );
00222             }
00223             else
00224             {
00225                 if ( $level == 0 )
00226                 {
00227                     $node = eZContentObjectTreeNode::fetch( 2 );
00228                     if ( !isset( $node ) ) { $operatorValue = $pathArray; return; }
00229                     $menuChildren = eZContentObjectTreeNode::subTreeByNodeID( array( 'Depth' => 1,
00230                                                                              'Offset' => 0,
00231                                                                              'SortBy' => $node->sortArray(),
00232                                                                              'Language' => $language,
00233                                                                              'ClassFilterType' => 'include',
00234                                                                              'ClassFilterArray' => $classFilter ),
00235                                                                       2 );
00236 
00237                     /// Fill objects with attributes, speed boost
00238                     eZContentObject::fillNodeListAttributes( $menuChildren );
00239 
00240                     $pathArray = array();
00241                     foreach ( $menuChildren as $child )
00242                     {
00243                         $name = $child->attribute( 'name' );
00244                         $tmpNodeID = $child->attribute( 'node_id' );
00245 
00246                         $url = "/content/view/full/$tmpNodeID/";
00247                         $urlAlias = "/" . $child->attribute( 'url_alias' );
00248 
00249                         $pathArray[] = array( 'id' => $tmpNodeID,
00250                                               'level' => $i,
00251                                               'url_alias' => $urlAlias,
00252                                               'url' => $url,
00253                                               'text' => $name,
00254                                               'is_selected' => false,
00255                                               'node' => $child );
00256                     }
00257                 }
00258                 $done = true;
00259             }
00260             ++$level;
00261             ++$i;
00262         }
00263 
00264         $operatorValue = $pathArray;
00265     }
00266 
00267     /// \privatesection
00268     public $Operators;
00269 };
00270 
00271 ?>