HTML_Menu
[ class tree: HTML_Menu ] [ index: HTML_Menu ] [ all elements ]

Source for file MenuBrowser.php

Documentation is available at MenuBrowser.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Ulf Wendel <ulf.wendel@phpdoc.de>                            |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: MenuBrowser.php,v 1.4 2003/09/15 17:24:38 avb Exp $
  20.  
  21. /**
  22. * Simple filesystem browser that can be used to generated menu (3) hashes based on the directory structure.
  23. *
  24. * Together with menu (3) and the (userland) cache you can use this
  25. * browser to generate simple fusebox like applications / content systems.
  26. *
  27. * Let the menubrowser scan your document root and generate a menu (3) structure
  28. * hash which maps the directory structure, pass it to menu's setMethod() and optionally
  29. * wrap the cache around all this to save script runs. If you do so, it looks
  30. * like this:
  31. *
  32. * // document root directory
  33. * define('DOC_ROOT', '/home/server/www.example.com/');
  34. *
  35. * // instantiate the menubrowser
  36. * $browser = new menubrowser(DOC_ROOT);
  37. *
  38. * // instantiate menu (3)
  39. * $menu = new menu($browser->getMenu());
  40. *
  41. * // output the sitemap
  42. * $menu->show('sitemap');
  43. *
  44. * Now, use e.g. simple XML files to store your content and additional menu informations
  45. * (title!). Subclass exploreFile() depending on your file format.
  46. *
  47. @author   Ulf Wendel <ulf.wendel@phpdoc.de>
  48. @version  $Revision: 1.4 $
  49. @package  HTML_Menu
  50. */
  51. {
  52.    /**
  53.     * Filesuffix of your XML files.
  54.     *
  55.     * @var  string 
  56.     * @see  HTML_MenuBrowser()
  57.     */
  58.     var $file_suffix = 'xml';
  59.  
  60.    /**
  61.     * Number of characters of the file suffix.
  62.     *
  63.     * @var  int 
  64.     * @see  HTML_MenuBrowser()
  65.     */
  66.     var $file_suffix_length = 3;
  67.  
  68.    /**
  69.     * Filename (without suffix) of your index / start pages.
  70.     *
  71.     * @var  string 
  72.     * @see  HTML_MenuBrowser()
  73.     */
  74.     var $index = 'index';
  75.  
  76.    /**
  77.     * Full filename of your index / start pages.
  78.     *
  79.     * @var  string 
  80.     * @see  $file_suffix, $index
  81.     */
  82.     var $index_file = '';
  83.  
  84.    /**
  85.     * Directory to scan.
  86.     *
  87.     * @var  string 
  88.     * @see  setDirectory()
  89.     */
  90.     var $dir = '';
  91.  
  92.    /**
  93.     * Prefix for every menu hash entry.
  94.     *
  95.     * Set the ID prefix if you want to merge the browser menu
  96.     * hash with another (static) menu hash so that there're no
  97.     * name clashes with the ids.
  98.     *
  99.     * @var  string 
  100.     * @see  setIDPrefix()
  101.     */
  102.     var $id_prefix = '';
  103.  
  104.     /**
  105.     * Menu (3)'s setMenu() hash.
  106.     *
  107.     * @var  array 
  108.     */
  109.     var $menu = array();
  110.  
  111.    /**
  112.     * Creates the object and optionally sets the directory to scan.
  113.     *
  114.     * @param    string  Directory to scan
  115.     * @param    string  Filename of index pages
  116.     * @param    string  Suffix for files containing the additional data
  117.     * @see      $dir
  118.     */
  119.     function HTML_MenuBrowser($dir ''$index ''$file_suffix '')
  120.     {
  121.         if ($dir{
  122.             $this->dir = $dir;
  123.         }
  124.         if ($index{
  125.             $this->index = $index;
  126.         }
  127.         if ($file_suffix{
  128.             $this->file_suffix = $file_suffix;
  129.         }
  130.  
  131.         $this->index_file = $this->index . '.' $this->file_suffix;
  132.         $this->file_suffix_length = strlen($this->file_suffix);
  133.     }
  134.  
  135.  
  136.    /**
  137.     * Sets the directory to scan.
  138.     *
  139.     * @param    string  directory to scan
  140.     * @access   public
  141.     */
  142.     function setDirectory($dir
  143.     {
  144.         $this->dir = $dir;
  145.     }
  146.  
  147.  
  148.    /**
  149.     * Sets the prefix for every id in the menu hash.
  150.     *
  151.     * @param    string 
  152.     * @access   public
  153.     */
  154.     function setIDPrefix($prefix
  155.     {
  156.         $this->id_prefix = $prefix;
  157.     }
  158.  
  159.  
  160.    /**
  161.     * Returns a hash to be used with menu(3)'s setMenu().
  162.     *
  163.     * @param    string  directory to scan
  164.     * @param    string  id prefix
  165.     * @access   public
  166.     */
  167.     function getMenu($dir ''$prefix ''
  168.     {
  169.         if ($dir{
  170.             $this->setDirectory($dir);
  171.         }
  172.         if ($prefix{
  173.             $this->setIDPrefix($prefix);
  174.         }
  175.  
  176.         // drop the result of previous runs
  177.         $this->files = array();
  178.  
  179.         $this->menu = $this->browse($this->dir);
  180.         $this->menu = $this->addFileInfo($this->menu);
  181.  
  182.         return $this->menu;
  183.     }
  184.  
  185.  
  186.    /**
  187.     * Recursive function that does the scan and builds the menu (3) hash.
  188.     *
  189.     * @param    string  directory to scan
  190.     * @param    integer entry id - used only for recursion
  191.     * @param    boolean ??? - used only for recursion
  192.     * @return   array 
  193.     */
  194.     function browse($dir$id = 0$noindex = false)
  195.     {
  196.         $struct = array();
  197.         $dh opendir($dir);
  198.         while ($file readdir($dh)) {
  199.             if ('.' == $file || '..' == $file{
  200.                 continue;
  201.             }
  202.             $ffile $dir $file;
  203.             if (is_dir($ffile)) {
  204.                 $ffile .= '/';
  205.                 if (file_exists($ffile $this->index_file)) {
  206.                     $id++;
  207.                     $struct[$this->id_prefix . $id]['url'$ffile $this->index_file;
  208.  
  209.                     $sub $this->browse($ffile$id + 1true);
  210.                     if (0 != count($sub)) {
  211.                         $struct[$this->id_prefix . $id]['sub'$sub;
  212.                     }
  213.                 }
  214.             else {
  215.                 if ($this->file_suffix == substr($filestrlen($file$this->file_suffix_length$this->file_suffix_length)
  216.                     && !($noindex && $this->index_file == $file) )
  217.                 {
  218.                     $id++;
  219.                     $struct[$this->id_prefix . $id]['url'$dir $file;
  220.                 }
  221.             }
  222.         }
  223.         return $struct;
  224.     }
  225.  
  226.  
  227.    /**
  228.     * Adds further informations to the menu hash gathered from the files in it
  229.     *
  230.     * @param    array   Menu hash to examine
  231.     * @return   array   Modified menu hash with the new informations
  232.     */
  233.     function addFileInfo($menu
  234.     {
  235.         // no foreach - it works on a copy - the recursive
  236.         // structure requires already lots of memory
  237.         reset($menu);
  238.         while (list($id$dataeach($menu)) {
  239.             $menu[$idarray_merge($data$this->exploreFile($data['url']));
  240.             if (isset($data['sub'])) {
  241.                 $menu[$id]['sub'$this->addFileInfo($data['sub']);
  242.             }
  243.         }
  244.  
  245.         return $menu;
  246.     }
  247.  
  248.  
  249.    /**
  250.     * Returns additional menu informations decoded in the file that appears in the menu.
  251.     *
  252.     * You should subclass this method to make it work with your own
  253.     * file formats. I used a simple XML format to store the content.
  254.     *
  255.     * @param    string  filename
  256.     */
  257.     function exploreFile($file
  258.     {
  259.         $xml join(''@file($file));
  260.         if (!$xml{
  261.             return array();
  262.         }
  263.  
  264.         $doc = xmldoc($xml);
  265.         $xpc = xpath_new_context($doc);
  266.  
  267.         $menu = xpath_eval($xpc'//menu');
  268.         $node &$menu->nodeset[0];
  269.  
  270.         return array('title' => $node->content);
  271.     }
  272. }
  273. ?>

Documentation generated on Mon, 11 Mar 2019 10:16:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.