com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_menu.php

Documentation is available at lib_menu.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains everything that is connected to the site's menu.
  9. * @author Andreas Launila
  10. * @version $Revision: 1.16 $
  11. * @package com.lokorin.lokorin.lib
  12. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13. */
  14.  
  15. /**
  16. * For access to common constants and functions.
  17. */
  18. require_once('common.php');
  19. doInclude('lib_projects');
  20.  
  21. /**
  22. * The path relative to the site's root where the menu cache is located.
  23. * @var string
  24. * @access private
  25. */
  26. define('MENU_CACHE_PATH', FOLDER_CACHE.'menu.php');
  27.  
  28. /**
  29. * Updates the cache of the menu. Rebuilds the menu with the currently saved
  30. * configuration and then rewrites the cache.
  31. * @access public
  32. */
  33. function updateMenuCache() {
  34. global $db;
  35. $output = "<?php
  36. /**
  37. * A structured menu that allows users to navigate the site. This is a cache
  38. * file designed to avoid reconstructing the update boxes whenever the site
  39. * is displayed.
  40. * @author Andreas Launila
  41. * @package com.lokorin.lokorin.cache
  42. */?>";
  43. $output .= '<ul id="nav">';
  44. $items = getAllRootMenus();
  45. foreach($items as $item) {
  46. $output .= $item->getAsHtml();
  47. }
  48. $output .= '</ul>';
  49. //Write it to the cache file
  50. $cache = fopen(ROOT.MENU_CACHE_PATH, 'w');
  51. if(!$cache) {
  52. logException(new IOException('Error writing to the menu cache.'));
  53. }
  54. fputs($cache, $output);
  55. fclose($cache);
  56. }
  57.  
  58. /**
  59. * Gets all nodes that have the root as parent.
  60. * @return array An array with one MenuItem instance for every menu item with
  61. * root as parent.
  62. * @access private
  63. */
  64. function getAllRootMenus() {
  65. global $db;
  66. $rows = $db->querySelect(TABLE_MENU, array('id'),
  67. "WHERE parent_menu_id = 0 ORDER by id");
  68. $items = array();
  69. foreach($rows as $row) {
  70. try {
  71. $items[] = new MenuItem($row['id']);
  72. } catch(Exception $e) {}
  73. }
  74. return $items;
  75. }
  76.  
  77. /**
  78. * Inserts the menu from the cache.
  79. * @access public
  80. */
  81. function insertMenu() {
  82. if(!file_exists(DOC_ROOT.MENU_CACHE_PATH)) {
  83. updateMenuCache();
  84. }
  85. if(!file_exists(DOC_ROOT.MENU_CACHE_PATH)) {
  86. logFatalException(new FileNotFoundException('Could not locate the ' .
  87. 'menu cache.'));
  88. }
  89. include(ROOT.MENU_CACHE_PATH);
  90. }
  91.  
  92. /**
  93. * Gets the names of all menu items available.
  94. * @return array An array full of menu item names (as strings).
  95. * @access public
  96. */
  97. function getAllMenuNames() {
  98. global $db;
  99. //Get all menu items.
  100. $rows = $db->querySelect(TABLE_MENU, array('id', 'name'), "ORDER BY id");
  101. //Get their names.
  102. $names = array();
  103. foreach($rows as $row) {
  104. $names[$row['id']] = $row['name'];
  105. }
  106. return $names;
  107. }
  108.  
  109. /**
  110. * Describes an item in a menu. An menu item has a name and are ordered in a
  111. * tree where all nodes except the root are menu items. An menu item may have
  112. * any number of children.
  113. * @author Andreas Launila
  114. * @package com.lokorin.lokorin.lib
  115. */
  116. class MenuItem extends TableEntry {
  117. /**
  118. * The name that describes and is displayed for the menu item.
  119. * @var string
  120. */
  121. private $name;
  122. /**
  123. * An optional path that the menu item leads to. It's either an aboslute
  124. * url or one relative to the root path.
  125. * @var string
  126. */
  127. private $path;
  128. /**
  129. * Creator for MenuItem.
  130. * @param integer $id The unique identifier for the menu item.
  131. */
  132. public function __construct($id) {
  133. $fields = array('name', 'path');
  134. parent::__construct(TABLE_MENU, $id, $fields);
  135. }
  136. /**
  137. * @see com.lokorin.lokorin.includes.TableEntry.readRow($row)
  138. */
  139. protected function readRow($row) {
  140. $this->name = $row['name'];
  141. $this->path = $row['path'];
  142. }
  143.  
  144. /**
  145. * Gets all children of the menu item in alphabetical order based on their
  146. * names.
  147. * @return array An array of MenuItem instance where each instance
  148. * represents a child. The array may be empty if no children were
  149. * found.
  150. */
  151. public function getChildren() {
  152. global $db;
  153. $rows = $db->querySelect(TABLE_MENU, array('id'),
  154. "WHERE parent_menu_id=".$db->escape(parent::$this->getId())." ORDER BY name");
  155. $children = array();
  156. foreach($rows as $row) {
  157. try {
  158. $children[] = new MenuItem($row['id']);
  159. } catch(Exception $e) {}
  160. }
  161. return $children;
  162. }
  163. /**
  164. * Gets the menu item as XHTML compliant code. This function also includes
  165. * the children of the menu item (if there are any).
  166. * @return string XHTML compliant code representing the menu item. The menu
  167. * item is represented as a hypertext link in a list item containing
  168. * a list with all children.
  169. */
  170. public function getAsHtml() {
  171. $hyperLinkDest = '';
  172. if($this->path != '') {
  173. $hyperLinkDest = ' href="';
  174. if(strpos($this->path, 'http://') === false) {
  175. $hyperLinkDest .= '<?= PAGE_ROOT ?>';
  176. }
  177. $hyperLinkDest .= $this->path.'"';
  178. }
  179. $output = '';
  180. $children = $this->getChildren();
  181. $isProjectMenu = parent::$this->getId() == 4;
  182. $hasChildren = $isProjectMenu || count($children) > 0;
  183. if($hasChildren) {
  184. $output .= '<li><a'.$hyperLinkDest.' class="parent">'.
  185. $this->name.'</a><ul>';
  186. } else {
  187. $output .= '<li><a'.$hyperLinkDest.'>'.$this->name.'</a>';
  188. }
  189. //Project menu (dirty hack).
  190. if($isProjectMenu) {
  191. //Get the menus of all projects that should be displayed.
  192. $projects = getAllProjects();
  193. foreach($projects as $project) {
  194. if(!$project->isMenuDisplayable()) {
  195. continue;
  196. }
  197. $item = $project->getSubmenu();
  198. $output .= $item->getAsHtml();
  199. }
  200. }
  201. //Submenu.
  202. if(count($children) > 0) {
  203. foreach($children as $child) {
  204. $output .= $child->getAsHtml();
  205. }
  206. }
  207. //Close the tags.
  208. if($hasChildren) {
  209. $output .= '</ul></li>';
  210. } else {
  211. $output .= '</li>';
  212. }
  213. return $output."\n";
  214. }
  215. /**
  216. * Gets the name of the menu item.
  217. * @return string A name that describes the menu item.
  218. */
  219. public function getName() {
  220. return $this->name;
  221. }
  222. /**
  223. * Gets the menu as a sitemap entry.
  224. * @return string A well-formed XML url entry as specified by the google
  225. * sitemap schema.
  226. */
  227. public function getAsSitemapEntry() {
  228. doInclude('lib_sitemap');
  229. if($this->path == '' || strpos($this->path, 'http://') === 0) {
  230. //Nothing to display.
  231. return '';
  232. } else {
  233. return formatSitemapElement($this->path);
  234. }
  235. }
  236. }
  237.  
  238. /**
  239. * Describes a project menu. A menu that is connected to a project.
  240. * @author Andreas Launila
  241. * @package com.lokorin.lokorin.lib
  242. */
  243. class ProjectMenu {
  244. /**
  245. * The name that describes and is displayed for the menu item.
  246. * @var string
  247. */
  248. private $name;
  249. /**
  250. * An optional path that the menu item leads to. It's either an aboslute
  251. * url or one relative to the root path.
  252. * @var string
  253. */
  254. private $path;
  255. /**
  256. * All submenus that are connected to the menu. The key is the display name
  257. * for the submenu, the value is the path inside the project path.
  258. * @var array
  259. */
  260. private $subMenus;
  261. /**
  262. * Constructor for ProjectMenu. Constructs the entire menu system for the
  263. * specified project.
  264. * @param integer $projectId The unique identifier for the project that the
  265. * menu should be constructed for.
  266. */
  267. public function __construct($projectId) {
  268. global $db;
  269. //Set the base information.
  270. $project = new Project($projectId);
  271. $this->name = $project->getName();
  272. $this->path = $project->getPath();
  273. //Get all the submenus.
  274. $rows = $db->querySelect(TABLE_PROJECT_SUBMENUS, array('display',
  275. 'internal_path'),
  276. "WHERE project_id=".$projectId." ORDER BY display");
  277. $this->subMenus = array();
  278. foreach($rows as $row) {
  279. $this->subMenus[$row['display']] = $row['internal_path'];
  280. }
  281. }
  282. /**
  283. * Gets the menu item as XHTML compliant code. This function also includes
  284. * the children of the menu item (if there are any).
  285. * @return string XHTML compliant code representing the menu item. The menu
  286. * item is represented as a hypertext link in a list item containing
  287. * a list with all children.
  288. */
  289. public function getAsHtml() {
  290. $hyperLinkDest = '';
  291. if($this->path != '') {
  292. $hyperLinkDest = ' href="';
  293. if(strpos($this->path, 'http://') === false) {
  294. $hyperLinkDest .= '<?= PAGE_ROOT ?>';
  295. }
  296. $hyperLinkDest .= $this->path.'"';
  297. }
  298.  
  299. /*if(sizeof($this->subMenus) > 0) {
  300. $output = '<li><a'.$hyperLinkDest.' class="parent">'.
  301. $this->name.'</a>';
  302. $output .= "\n".'<ul>';
  303. $destinations = $this->getDestinations();
  304. foreach($destinations as $display => $path) {
  305. $hyperLinkDest = '';
  306. if($path != '') {
  307. $hyperLinkDest = ' href="';
  308. if(strpos($path, 'http://') === false) {
  309. $hyperLinkDest .= '<?= PAGE_ROOT ?>';
  310. }
  311. $hyperLinkDest .= $path.'"';
  312. }
  313. $output .= '<li><a'.$hyperLinkDest.'>'.$display.'</a></li>';
  314. }
  315. $output .= '</ul></li>';
  316. } else {
  317. $output = '<li><a'.$hyperLinkDest.'>'.$this->name.'</a></li>';
  318. }*/
  319. $output = '<li><a'.$hyperLinkDest.'>'.$this->name.'</a></li>';
  320. return $output;
  321. }
  322. /**
  323. * Gets all the destinations that the project menu has. I.e. all pages
  324. * associated with the project.
  325. * @return array All destination where the key is the display name and the
  326. * value is the associated path to the page from the root.
  327. */
  328. public function getDestinations() {
  329. $menus = array();
  330. foreach($this->subMenus as $name => $path) {
  331. $pathBase = '';
  332. if(strpos($path, 'http://') === false) {
  333. $pathBase .= $this->path;
  334. }
  335. $menus[$name] = $pathBase.$path;
  336. }
  337. return $menus;
  338. }
  339. }
  340. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:38 +0200 by phpDocumentor 1.3.0RC4