eZ Publish  [trunk]
eztreemenuoperator.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZTreeMenuOperator class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 class eZTreeMenuOperator
00012 {
00013     function eZTreeMenuOperator( $name = 'treemenu' )
00014     {
00015         $this->Operators = array( $name );
00016     }
00017 
00018     /*!
00019      Returns the operators in this class.
00020     */
00021     function operatorList()
00022     {
00023         return $this->Operators;
00024     }
00025 
00026     /*!
00027      See eZTemplateOperator::namedParameterList()
00028     */
00029     function namedParameterList()
00030     {
00031         return array( 'path' => array( 'type' => 'array',
00032                                        'required' => true,
00033                                        'default' => false ),
00034                       'node_id' => array( 'type' => 'int',
00035                                           'required' => false,
00036                                           'default' => false ),
00037                       'class_filter' => array( 'type' => 'array',
00038                                                'required' => false,
00039                                                'default' => false ),
00040                       'depth_skip' => array( 'type' => 'int',
00041                                              'required' => false,
00042                                              'default' => false ),
00043                       'max_level' => array( 'type' => 'int',
00044                                             'required' => false,
00045                                             'default' => false ),
00046                       'is_selected_method' => array( 'type' => 'string',
00047                                                      'required' => false,
00048                                                      'default' => 'tree' ),
00049                       'indentation_level' => array( 'type' => 'int',
00050                                                     'required' => false,
00051                                                     'default' => 15 ),
00052                       'language' => array( 'type' => 'string|array',
00053                                            'required' => false,
00054                                            'default' => false ),
00055                       'load_data_map' => array( 'type' => 'boolean',
00056                                            'required' => false,
00057                                            'default' => null ) );
00058     }
00059 
00060     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement )
00061     {
00062         $level = 0;
00063         $done = false;
00064         $i = 0;
00065         $pathArray = array();
00066         $tmpModulePath = $namedParameters['path'];
00067         $classFilter = $namedParameters['class_filter'];
00068         $language = $namedParameters['language'];
00069         $loadDataMap = $namedParameters['load_data_map'];
00070         // node_id is not used anymore
00071         if ( !empty( $namedParameters['node_id'] ) )
00072         {
00073             eZDebug::writeNotice( 'Deprecated parameter "node_id" in treemenu template operator' );
00074         }
00075 
00076         if ( $classFilter === false )
00077         {
00078             $classFilter = array();
00079         }
00080         else if ( count( $classFilter ) == 0 )
00081         {
00082             $classFilter = array( 1 );
00083         }
00084         $classFilter = ( count( $classFilter ) == 1 and !isset( $classFilter[0] ) ) ? array( 1 ) : $classFilter;
00085         $tmpModulePathCount = count( $tmpModulePath );
00086         if ( !$tmpModulePath[ $tmpModulePathCount -1 ]['url'] and isset( $tmpModulePath[ $tmpModulePathCount -1 ]['node_id'] ) )
00087             $tmpModulePath[ $tmpModulePathCount -1 ]['url'] = '/content/view/full/' . $tmpModulePath[ $tmpModulePathCount -1 ]['node_id'];
00088 
00089         $depthSkip = $namedParameters['depth_skip'];
00090         $indentationLevel = $namedParameters['indentation_level'];
00091 
00092         $maxLevel = $namedParameters['max_level'];
00093         $isSelectedMethod = $namedParameters['is_selected_method'];
00094         if ( $maxLevel === false )
00095             $maxLevel = 2;
00096 
00097         while ( !$done && isset( $tmpModulePath[$i+$depthSkip] ) )
00098         {
00099             // get node id
00100             $elements = explode( '/', $tmpModulePath[$i+$depthSkip]['url'] );
00101             $nodeID = false;
00102             if ( isset( $elements[4] ) )
00103                 $nodeID = $elements[4];
00104 
00105             $excludeNode = false;
00106 
00107             if ( isset( $elements[1] ) &&
00108                  isset( $elements[2] ) &&
00109                 $elements[1] == 'content' &&
00110                 $elements[2] == 'view' &&
00111                 is_numeric( $nodeID ) &&
00112                 $excludeNode == false &&
00113                 $level < $maxLevel )
00114             {
00115                 $node = eZContentObjectTreeNode::fetch( $nodeID );
00116                 if ( !isset( $node ) ) { $operatorValue = $pathArray; return; }
00117                 if ( isset( $tmpModulePath[$i+$depthSkip+1] ) )
00118                 {
00119                     $nextElements = explode( '/', $tmpModulePath[$i+$depthSkip+1]['url'] );
00120                     if ( isset( $nextElements[4] ) )
00121                     {
00122                         $nextNodeID = $nextElements[4];
00123                     }
00124                     else
00125                     {
00126                         $nextNodeID = false;
00127                     }
00128                 }
00129                 else
00130                     $nextNodeID = false;
00131 
00132                 $menuChildren = eZContentObjectTreeNode::subTreeByNodeID( array( 'Depth' => 1,
00133                                                                          'Offset' => 0,
00134                                                                          'SortBy' => $node->sortArray(),
00135                                                                          'Language' => $language,
00136                                                                          'ClassFilterType' => 'include',
00137                                                                          'ClassFilterArray' => $classFilter ),
00138                                                                   $nodeID );
00139 
00140                 /// Fill objects with attributes, speed boost, only use if load_data_map is true
00141                 // or if less then 16 nodes and param is not set (null)
00142                 if ( $loadDataMap || (  $loadDataMap === null && count( $menuChildren ) <= 15 ) )
00143                     eZContentObject::fillNodeListAttributes( $menuChildren );
00144 
00145                 $tmpPathArray = array();
00146                 foreach ( $menuChildren as $child )
00147                 {
00148                     $name = $child->attribute( 'name' );
00149                     $tmpNodeID = $child->attribute( 'node_id' );
00150 
00151                     $url = "/content/view/full/$tmpNodeID/";
00152                     $urlAlias = '/' . $child->attribute( 'url_alias' );
00153                     $hasChildren = $child->attribute( 'is_container' ) && $child->attribute( 'children_count' ) > 0;
00154                     $contentObject = $child->attribute( 'object' );
00155 
00156                     $indent = ($i - 1) * $indentationLevel;
00157 
00158                     $isSelected = false;
00159                     $nextNextElements = ( $isSelectedMethod == 'node' and isset( $tmpModulePath[$i+$depthSkip+2]['url'] ) ) ? explode( '/', $tmpModulePath[$i+$depthSkip+2]['url'] ) : null;
00160                     if ( $nextNodeID === $tmpNodeID and !isset( $nextNextElements[4] ) )
00161                     {
00162                         $isSelected = true;
00163                     }
00164 
00165                     $tmpPathArray[] = array( 'id' => $tmpNodeID,
00166                                              'level' => $i,
00167                                              'class_name' => $contentObject->classname(),
00168                                              'is_main_node' => $child->attribute( 'is_main' ),
00169                                              'has_children' => $hasChildren,
00170                                              'indent' => $indent,
00171                                              'url_alias' => $urlAlias,
00172                                              'url' => $url,
00173                                              'text' => $name,
00174                                              'is_selected' => $isSelected,
00175                                              'node' => $child );
00176                 }
00177 
00178                 // find insert pos
00179                 $j = 0;
00180                 $insertPos = 0;
00181                 foreach ( $pathArray as $path )
00182                 {
00183                     if ( $path['id'] == $nodeID )
00184                         $insertPos = $j;
00185                     $j++;
00186                 }
00187                 $restArray = array_splice( $pathArray, $insertPos + 1 );
00188 
00189                 $pathArray = array_merge( $pathArray, $tmpPathArray );
00190                 $pathArray = array_merge( $pathArray, $restArray );
00191             }
00192             else
00193             {
00194                 if ( $level == 0 )
00195                 {
00196                     $node = eZContentObjectTreeNode::fetch( 2 );
00197                     if ( !$node instanceof eZContentObjectTreeNode )
00198                     {
00199                         $operatorValue = $pathArray;
00200                         return;
00201                     }
00202 
00203                     $menuChildren = eZContentObjectTreeNode::subTreeByNodeID( array( 'Depth' => 1,
00204                                                                              'Offset' => 0,
00205                                                                              'SortBy' => $node->sortArray(),
00206                                                                              'Language' => $language,
00207                                                                              'ClassFilterType' => 'include',
00208                                                                              'ClassFilterArray' => $classFilter ),
00209                                                                       2 );
00210 
00211                     /// Fill objects with attributes, speed boost, only use if load_data_map is true
00212                     // or if less then 16 nodes and param is not set (null)
00213                     if ( $loadDataMap || (  $loadDataMap === null && count( $menuChildren ) < 16 ) )
00214                         eZContentObject::fillNodeListAttributes( $menuChildren );
00215 
00216                     $pathArray = array();
00217                     foreach ( $menuChildren as $child )
00218                     {
00219                         $name = $child->attribute( 'name' );
00220                         $tmpNodeID = $child->attribute( 'node_id' );
00221 
00222                         $url = "/content/view/full/$tmpNodeID/";
00223                         $urlAlias = '/' . $child->attribute( 'url_alias' );
00224 
00225                         $pathArray[] = array( 'id' => $tmpNodeID,
00226                                               'level' => $i,
00227                                               'is_main_node' => $child->attribute( 'is_main' ),
00228                                               'url_alias' => $urlAlias,
00229                                               'url' => $url,
00230                                               'text' => $name,
00231                                               'is_selected' => false,
00232                                               'node' => $child );
00233                     }
00234                 }
00235                 $done = true;
00236             }
00237             ++$level;
00238             ++$i;
00239         }
00240 
00241         $operatorValue = $pathArray;
00242     }
00243 
00244     /// \privatesection
00245     public $Operators;
00246 }
00247 
00248 ?>