PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/pear/HTML/MenuBrowser.php

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