[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/template/menu/ -> menu.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/xml/tree/Tree.php" );
   4      lt_include( PLOG_CLASS_PATH."class/template/menu/menuentry.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   6      
   7      define( "DEFAULT_MENU_FILE", "/admin/menus.xml" );
   8  
   9      /**
  10       *  \ingroup Template
  11       *
  12       * Implements support for dealing with xml tree-based menus.
  13       *
  14       * There is more information about the menu API and how to use it here:
  15       * http://wiki.lifetype.net/index.php/Menu_api
  16       *
  17       * If you need to get the global instance of the menu, use the Menu::getMenu() singleton method. 
  18       *
  19       * New entries can be dynamically added to the menu via the Menu::addEntry() method, and the whole
  20       * menu can be rendered by using the MenuRenderer class.
  21       *
  22       * @see MenuEntry
  23       * @see MenuRenderer
  24       */
  25      class Menu extends XML_Tree
  26      {
  27      
  28          /**
  29           * constructor
  30           *
  31           * @param menuFile
  32           * @return nothing
  33           */
  34          function Menu( $menuFile = "")
  35          {
  36              // generate the path to the menu file
  37              if( $menuFile == "" ) {
  38                  $config =& Config::getConfig();
  39                  $menuFile = $config->getValue( "template_folder" ).DEFAULT_MENU_FILE;            
  40              }
  41              
  42              // generate the tree
  43              $this->XML_Tree( $menuFile );
  44              $this->getTreeFromFile();
  45          }
  46          
  47          /**
  48           * returns a valid instance of the global menu
  49           *
  50           * @static
  51           * @return A valid Menu object
  52           */
  53          function &getMenu( $menuFile = "" )
  54          {
  55              static $instance;
  56              
  57              // create a new instance of the menu if it does not exist yet...
  58              if( $instance == null )
  59                  $instance = new Menu( $menuFile );
  60                  
  61              // once we have it, return this instance
  62              return $instance;
  63          }
  64          
  65          /**
  66           * @private
  67           * @see findNode
  68           * Helper function that helps to find a certain node in the menu tree
  69           */
  70          function _findNode( $node, $nodeId )
  71          {
  72              //print("node = $node->name<br/>");
  73              if( $node->name == $nodeId )
  74                  return $node;
  75              else {
  76                  $i=0;
  77                  $found=false;
  78                  while( !$found && $i < count($node->children)) {
  79                      $found = $this->_findNode( $node->children[$i], $nodeId );
  80                      $i++;
  81                  }
  82              }
  83  
  84              return $found;                
  85          }
  86          
  87          /**
  88           * finds a node in the tree
  89           *
  90           * @param nodeId
  91           * @return Returns an XML_Tree_Node object or null if not found
  92           */
  93          function findEntry( $nodeId ) 
  94          {
  95              $root = $this->getRoot();
  96              return $this->_findNode( $root, $this->_prepareEntryPath($nodeId));
  97          }
  98          
  99          /**
 100           * @private
 101           * @see nodePath
 102           */
 103          function _nodePath( $node, $nodeId, $stack = Array())
 104          {
 105              //print("node = $node->name<br/>");
 106              if( $node->name == $nodeId ) {
 107                  while( $tmp = array_pop($stack))
 108                      $path = $tmp->name."/".$path;
 109                  $path = $path.$node->name;
 110  
 111                  return $path;
 112              }
 113              else {
 114                  $i=0;
 115                  $found=false;
 116                  while( !$found && $i < count($node->children)) {
 117                      //print("i=$i<br/>");
 118                      array_push( $stack, $node );
 119                      $found = $this->_nodePath( $node->children[$i], $nodeId, $stack );
 120                      $i++;
 121                      array_pop( $stack );
 122                  }
 123              }
 124  
 125              return $found;
 126          }
 127          
 128          /**
 129           * @private
 130           * Removes the "/" from the beginning of a path, if it exists at all
 131           * @return The cleaned up version of the path
 132           */
 133          function _prepareEntryPath( $entryPath )
 134          {
 135              if( $entryPath[0] == "/" )
 136                  $entryPath = substr($entryPath, 1, strlen($entryPath)-1);
 137                  
 138              return $entryPath;
 139          }
 140  
 141          /**
 142           * returns the path to a menu entry
 143           *
 144           * @param nodeId The id of the node
 145           * @param node Optional, a name of the starting node if it's not the root
 146           * @return An XML_Tree_Node object or null if not found
 147           */
 148          function entryPath( $nodeId, $node = null )
 149          {
 150              if( $node == null )
 151                  $node = $this->getRoot();
 152  
 153              return $this->_nodePath( $node, $nodeId );
 154          }
 155          
 156          /**
 157           * adds a menu entry to the menu
 158           * 
 159           * @param entryPath
 160           * @param entryId
 161           * @param entryAttrs
 162           * @param entryOrder
 163           */
 164          function addEntry( $entryPath, $entry, $entryOrder = -1 )
 165          {
 166              // insertChild will return a reference to the node or PEAR_Error if there was
 167              // a problem
 168              $node = $this->insertChild( $this->_prepareEntryPath($entryPath), // path to the entry
 169                                          $entryOrder,  // order, use '-1' to specify that it should be the last
 170                                          $entry // object 
 171                                          );
 172  
 173              //
 174              // this is a bit tricky, but the problem is that when we adde a new menu entry (a new node to the tree)
 175              // the nodes above it should also inherit the new permission that is required to view this entry. Otherwise
 176              // if a new node is added with let's say permission 'manage_plugins' but the nodes above it only have 'view_links' and 
 177              // 'edit_links', then that entry and all the ones above it would not rendered by the MenuRenderer class. 
 178              // By means of the code below, we're replicating the needed permissions to all the parent nodes so that we can get the
 179              // upper level entries to be displayed
 180              //
 181              $path = split( "/", $entryPath );
 182              $currentPath = "";
 183              foreach( $path as $step ) {
 184                  if( $step != "" ) {
 185                      //print($currentPath."/".$step."<br/>");
 186                      $currentPath = $currentPath."/".$step;
 187                      $node =& $this->getNodeAt( $this->_prepareEntryPath( $currentPath ));
 188                      // add the new node's orPerms and andPerms
 189                      $curOrPerms = $node->getAttribute( "orPerms" );
 190                      if( $curOrPerms != "" ) {
 191                          $node->setAttribute( "orPerms", $curOrPerms.",".$entry->getAttribute( "orPerms" ));
 192                      }
 193                      $curAndPerms = $node->getAttribute( "andPerms" );                    
 194                      if( $curAndPerms != "" ) {
 195                          $node->setAttribute( "andPerms", $curAndPerms.",".$entry->getAttribute( "andPerms" ));
 196                      }
 197                  }
 198              }
 199  
 200              
 201              $ok = $this->isError( $node );
 202              
 203              return $ok;
 204          }
 205          
 206          /**
 207           * alias for getNodeAt
 208           *
 209           * @param path
 210           */
 211          function getEntryAt( $path )
 212          {
 213              return $this->getNodeAt( $path );
 214          }
 215          
 216          /**
 217           * returns all the "brother" nodes or in other words, all the nodes that have
 218           * the same parent
 219           *
 220           * @param path
 221           * @return An array of nodes
 222           */
 223          function getEntryBrothers( $path )
 224          {
 225              // no brothers or sister if the path is pointing to the root!
 226              if( $path == $this->root->name )
 227                  return Array();
 228                  
 229              // if not, then we can proceed...
 230              $node = $this->getEntryAt( $path );
 231              
 232              if( $node )
 233                  return $node->parent->children;
 234              else
 235                  return Array();
 236          }
 237          
 238          /**
 239           * returns whether there is an entry in the given path or not
 240           *
 241           * @param entryPath
 242           * @return True if it exists or false otherwise
 243           */
 244  		function entryExists( $entryPath )
 245          {
 246              $entryExists = ( $this->getNodeAt( $this->_prepareEntryPath($entryPath)) != null );
 247              
 248              return $entryExists;
 249          }
 250      }
 251  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics