- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains everything that is connected to the site's menu.
- * @author Andreas Launila
- * @version $Revision: 1.16 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
- doInclude('lib_projects');
-
- /**
- * The path relative to the site's root where the menu cache is located.
- * @var string
- * @access private
- */
- define('MENU_CACHE_PATH', FOLDER_CACHE.'menu.php');
-
- /**
- * Updates the cache of the menu. Rebuilds the menu with the currently saved
- * configuration and then rewrites the cache.
- * @access public
- */
- function updateMenuCache() {
- global $db;
-
- $output = "<?php
- /**
- * A structured menu that allows users to navigate the site. This is a cache
- * file designed to avoid reconstructing the update boxes whenever the site
- * is displayed.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.cache
- */?>";
-
- $output .= '<ul id="nav">';
- $items = getAllRootMenus();
- foreach($items as $item) {
- $output .= $item->getAsHtml();
- }
- $output .= '</ul>';
- //Write it to the cache file
- $cache = fopen(ROOT.MENU_CACHE_PATH, 'w');
- if(!$cache) {
- logException(new IOException('Error writing to the menu cache.'));
- }
- fputs($cache, $output);
- fclose($cache);
- }
-
- /**
- * Gets all nodes that have the root as parent.
- * @return array An array with one MenuItem instance for every menu item with
- * root as parent.
- * @access private
- */
- function getAllRootMenus() {
- global $db;
-
- $rows = $db->querySelect(TABLE_MENU, array('id'),
- "WHERE parent_menu_id = 0 ORDER by id");
- $items = array();
- foreach($rows as $row) {
- try {
- $items[] = new MenuItem($row['id']);
- } catch(Exception $e) {}
- }
- return $items;
- }
-
- /**
- * Inserts the menu from the cache.
- * @access public
- */
- function insertMenu() {
- if(!file_exists(DOC_ROOT.MENU_CACHE_PATH)) {
- updateMenuCache();
- }
- if(!file_exists(DOC_ROOT.MENU_CACHE_PATH)) {
- logFatalException(new FileNotFoundException('Could not locate the ' .
- 'menu cache.'));
- }
- include(ROOT.MENU_CACHE_PATH);
- }
-
- /**
- * Gets the names of all menu items available.
- * @return array An array full of menu item names (as strings).
- * @access public
- */
- function getAllMenuNames() {
- global $db;
-
- //Get all menu items.
- $rows = $db->querySelect(TABLE_MENU, array('id', 'name'), "ORDER BY id");
- //Get their names.
- $names = array();
- foreach($rows as $row) {
- $names[$row['id']] = $row['name'];
- }
- return $names;
- }
-
- /**
- * Describes an item in a menu. An menu item has a name and are ordered in a
- * tree where all nodes except the root are menu items. An menu item may have
- * any number of children.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class MenuItem extends TableEntry {
- /**
- * The name that describes and is displayed for the menu item.
- * @var string
- */
- private $name;
- /**
- * An optional path that the menu item leads to. It's either an aboslute
- * url or one relative to the root path.
- * @var string
- */
- private $path;
-
- /**
- * Creator for MenuItem.
- * @param integer $id The unique identifier for the menu item.
- */
- public function __construct($id) {
- $fields = array('name', 'path');
- parent::__construct(TABLE_MENU, $id, $fields);
- }
-
- /**
- * @see com.lokorin.lokorin.includes.TableEntry.readRow($row)
- */
- protected function readRow($row) {
- $this->name = $row['name'];
- $this->path = $row['path'];
- }
-
- /**
- * Gets all children of the menu item in alphabetical order based on their
- * names.
- * @return array An array of MenuItem instance where each instance
- * represents a child. The array may be empty if no children were
- * found.
- */
- public function getChildren() {
- global $db;
-
- $rows = $db->querySelect(TABLE_MENU, array('id'),
- "WHERE parent_menu_id=".$db->escape(parent::$this->getId())." ORDER BY name");
- $children = array();
- foreach($rows as $row) {
- try {
- $children[] = new MenuItem($row['id']);
- } catch(Exception $e) {}
- }
- return $children;
- }
-
- /**
- * Gets the menu item as XHTML compliant code. This function also includes
- * the children of the menu item (if there are any).
- * @return string XHTML compliant code representing the menu item. The menu
- * item is represented as a hypertext link in a list item containing
- * a list with all children.
- */
- public function getAsHtml() {
- $hyperLinkDest = '';
- if($this->path != '') {
- $hyperLinkDest = ' href="';
- if(strpos($this->path, 'http://') === false) {
- $hyperLinkDest .= '<?= PAGE_ROOT ?>';
- }
- $hyperLinkDest .= $this->path.'"';
- }
-
- $output = '';
- $children = $this->getChildren();
- $isProjectMenu = parent::$this->getId() == 4;
- $hasChildren = $isProjectMenu || count($children) > 0;
-
-
- if($hasChildren) {
- $output .= '<li><a'.$hyperLinkDest.' class="parent">'.
- $this->name.'</a><ul>';
- } else {
- $output .= '<li><a'.$hyperLinkDest.'>'.$this->name.'</a>';
- }
-
- //Project menu (dirty hack).
- if($isProjectMenu) {
- //Get the menus of all projects that should be displayed.
- $projects = getAllProjects();
- foreach($projects as $project) {
- if(!$project->isMenuDisplayable()) {
- continue;
- }
- $item = $project->getSubmenu();
- $output .= $item->getAsHtml();
- }
- }
-
- //Submenu.
- if(count($children) > 0) {
- foreach($children as $child) {
- $output .= $child->getAsHtml();
- }
- }
-
- //Close the tags.
- if($hasChildren) {
- $output .= '</ul></li>';
- } else {
- $output .= '</li>';
- }
-
- return $output."\n";
- }
-
- /**
- * Gets the name of the menu item.
- * @return string A name that describes the menu item.
- */
- public function getName() {
- return $this->name;
- }
-
- /**
- * Gets the menu as a sitemap entry.
- * @return string A well-formed XML url entry as specified by the google
- * sitemap schema.
- */
- public function getAsSitemapEntry() {
- doInclude('lib_sitemap');
-
- if($this->path == '' || strpos($this->path, 'http://') === 0) {
- //Nothing to display.
- return '';
- } else {
- return formatSitemapElement($this->path);
- }
- }
- }
-
- /**
- * Describes a project menu. A menu that is connected to a project.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class ProjectMenu {
- /**
- * The name that describes and is displayed for the menu item.
- * @var string
- */
- private $name;
- /**
- * An optional path that the menu item leads to. It's either an aboslute
- * url or one relative to the root path.
- * @var string
- */
- private $path;
- /**
- * All submenus that are connected to the menu. The key is the display name
- * for the submenu, the value is the path inside the project path.
- * @var array
- */
- private $subMenus;
-
- /**
- * Constructor for ProjectMenu. Constructs the entire menu system for the
- * specified project.
- * @param integer $projectId The unique identifier for the project that the
- * menu should be constructed for.
- */
- public function __construct($projectId) {
- global $db;
-
- //Set the base information.
- $project = new Project($projectId);
- $this->name = $project->getName();
- $this->path = $project->getPath();
-
- //Get all the submenus.
- $rows = $db->querySelect(TABLE_PROJECT_SUBMENUS, array('display',
- 'internal_path'),
- "WHERE project_id=".$projectId." ORDER BY display");
- $this->subMenus = array();
- foreach($rows as $row) {
- $this->subMenus[$row['display']] = $row['internal_path'];
- }
- }
-
- /**
- * Gets the menu item as XHTML compliant code. This function also includes
- * the children of the menu item (if there are any).
- * @return string XHTML compliant code representing the menu item. The menu
- * item is represented as a hypertext link in a list item containing
- * a list with all children.
- */
- public function getAsHtml() {
- $hyperLinkDest = '';
- if($this->path != '') {
- $hyperLinkDest = ' href="';
- if(strpos($this->path, 'http://') === false) {
- $hyperLinkDest .= '<?= PAGE_ROOT ?>';
- }
- $hyperLinkDest .= $this->path.'"';
- }
-
- /*if(sizeof($this->subMenus) > 0) {
- $output = '<li><a'.$hyperLinkDest.' class="parent">'.
- $this->name.'</a>';
- $output .= "\n".'<ul>';
- $destinations = $this->getDestinations();
- foreach($destinations as $display => $path) {
- $hyperLinkDest = '';
- if($path != '') {
- $hyperLinkDest = ' href="';
- if(strpos($path, 'http://') === false) {
- $hyperLinkDest .= '<?= PAGE_ROOT ?>';
- }
- $hyperLinkDest .= $path.'"';
- }
- $output .= '<li><a'.$hyperLinkDest.'>'.$display.'</a></li>';
- }
- $output .= '</ul></li>';
- } else {
- $output = '<li><a'.$hyperLinkDest.'>'.$this->name.'</a></li>';
- }*/
- $output = '<li><a'.$hyperLinkDest.'>'.$this->name.'</a></li>';
- return $output;
- }
-
- /**
- * Gets all the destinations that the project menu has. I.e. all pages
- * associated with the project.
- * @return array All destination where the key is the display name and the
- * value is the associated path to the page from the root.
- */
- public function getDestinations() {
- $menus = array();
- foreach($this->subMenus as $name => $path) {
- $pathBase = '';
- if(strpos($path, 'http://') === false) {
- $pathBase .= $this->path;
- }
- $menus[$name] = $pathBase.$path;
- }
- return $menus;
- }
- }
- ?>