- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains everything related to the site's system for
- * external links.
- * @author Andreas Launila
- * @version $Revision: 1.12 $
- * @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');
- CssHandler::getInstance()->includeCss('links');
-
- /**
- * Gets all categories that have the root as parent.
- * @return array All top level categories.
- * @access public
- */
- function getRootCategories() {
- global $db;
-
- //Get all root categories.
- $idFields = array(
- "parent_id" => 0
- );
- $rows = $db->querySelect(TABLE_LINK_CATEGORIES, array('id'),
- $db->buildSqlSelector($idFields));
- $rootCategories = array();
- foreach($rows as $row) {
- $rootCategories[] = new Category($row['id']);
- }
-
- return $rootCategories;
- }
-
- /**
- * Gets a list of all category names.
- * @return array Key is the id, value is the corresponding category name.
- * @access public
- */
- function getAllCategoryNames() {
- /**
- * Internal recursive function. Gets the names of the subcategories of a
- * specified category. The function recurses into the subcategories of the
- * subcategories and so on.
- * @param Category The category from which the names should be retrieved.
- * @param string The base name that should be put in front of all retrieved
- * names.
- * @return array All retrieved category names.
- * @access private
- */
- function getCategoryNames($category, $base = '') {
- $result = array();
- $result[$category->getId()] = $base.$category->getName();
-
- $children = $category->getChildren();
- if(sizeof($children) == 0) {
- return $result;
- }
- foreach($children as $cat) {
- $result += getCategoryNames($cat, $base.$category->getName().'->');
- }
- return $result;
- }
-
- $categories = getRootCategories();
- $names = array();
- foreach($categories as $cat) {
- $names += getCategoryNames($cat);
- }
-
- return $names;
- }
-
- /**
- * Describes a link category. A category that contains links but that also
- * has a hierarchy so a category can contain child categories and so on.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class Category extends TableEntry {
- /**
- * The name of the category.
- * @var string
- */
- private $name;
- /**
- * The description of the category.
- * @var string
- */
- private $description;
- /**
- * The Category instances that are direct children to this instance.
- * @var array
- */
- private $children;
- /**
- * The Link instances that are connected to the category.
- * @var array
- */
- private $links;
-
- /**
- * Constructor for Category.
- * @param integer $id The unique identifier to be used.
- */
- public function __construct($id) {
- global $db;
-
- $fields = array(
- "name",
- "description"
- );
- parent::__construct(TABLE_LINK_CATEGORIES, $id, $fields);
-
- //Get the category's children.
- $this->children = array();
- $idFields = array(
- "parent_id" => parent::$this->getId()
- );
- $rows = $db->querySelect(TABLE_LINK_CATEGORIES, array("id"),
- $db->buildSqlSelector($idFields));
- foreach($rows as $row) {
- $this->children[] = new Category($row["id"]);
- }
-
- //Get the links associated with the category.
- $this->links = array();
- $idFields = array(
- "category_id" => parent::$this->getId()
- );
- $rows = $db->querySelect(TABLE_LINKS, array("id"),
- $db->buildSqlSelector($idFields));
- foreach($rows as $row) {
- $this->links[] = new Link($row["id"]);
- }
- }
-
- /**
- * @see com.lokorin.lokorin.includes.TableEntry.readRow($row)
- */
- protected function readRow($row) {
- $this->name = $row["name"];
- $this->description = $row["description"];
- }
-
- /**
- * Gets the name for the specific instance (not including parents).
- * @return string A single category name.
- */
- public function getName() {
- return $this->name;
- }
-
- /**
- * Gets the description of the category.
- * @return sring A category description.
- */
- public function getDescription() {
- return $this->description;
- }
-
- /**
- * Gets all the direct children instances.
- * @return array Key is irrelevant, value is a Category instance.
- */
- public function getChildren() {
- return $this->children;
- }
-
- /**
- * Gets all Link instances connected to the category.
- * @return array Key is irrelevant, value is a Link instance.
- */
- public function getLinks() {
- return $this->links;
- }
-
- /**
- * Recursive method for creating the body contents with the links.
- * @param integer $depth The depth of the category (1 being the first after
- * root).
- * @return string The resulting output as XHTML.
- */
- public function getAsHtmlList($depth = 1) {
- $out = '<div class="depth'.$depth.'">';
- $out .= '<a name="'.$this->getId().'"></a>';
- $out .= '<h'.($depth+1).'>'.$this->getName().'</h'.($depth+1).'>'."\n";
- $out .= $this->getDescription();
-
- //Add the links.
- $links = $this->getLinks();
- sort($links);
- if(count($links) > 0) {
- $out .= '<ul>'."\n";
- foreach($links as $link) {
- $out .= '<li>'.$link->getLinkAsHTML().'</li>'."\n";
- }
- $out .= '</ul>'."\n";
- }
-
- //Add the children categories.
- $children = $this->getChildren();
- sort($children);
- foreach($children as $child) {
- $out .= $child->getAsHtmlList($depth + 1);
- }
- $out .= '</div>';
- return $out;
- }
-
- /**
- * Recursive method for creating a navigation structure where each link
- * is an internal page link to the categories id. The navigation is
- * structured according to the tree in list elements.
- * @param string $hrefPattern The printf pattern that should be used to
- * format the href attribute of the links.
- * @param integer $maxDepth The maximum number of levels that may be taken
- * down the tree. If it's 0 then only the current category will be
- * included, if it's 1 then the category and its subcategories will
- * be included and so on. By default an large max depth is used.
- * @param boolean $includeDescriptions Whether or not the descriptions of
- * each category should be included in the navigation. If true then
- * the descriptions are included, otherwise they are not.
- * @return string The resulting XHTML for the navigation.
- */
- public function getHtmlNavigation($hrefPattern, $maxDepth = 1000,
- $includeDescriptions = true) {
- //Create the link.
- $out = '<a href="'.sprintf($hrefPattern, $this->getId()).'">'.
- $this->getName().'</a>'."\n";
- if($includeDescriptions) {
- //Add the description.
- $out .= '<br />'.$this->getDescription();
- }
- if($maxDepth == 0) {
- //Not allowed to go any further.
- return $out;
- }
-
- //Add the next level.
- $children = $this->getChildren();
- sort($children);
- if(count($children) > 0) {
- $out .= '<ul>';
- foreach($children as $child) {
- $out .= '<li>'.
- $child->getHtmlNavigation($hrefPattern, $maxDepth - 1,
- $includeDescriptions).
- "</li>\n";
- }
- $out .= '</ul>';
- }
- return $out;
- }
- }
-
- /**
- * Describes a link.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class Link extends TableEntry {
- /**
- * The title of the page that the link refers to.
- * @var string
- */
- private $title;
- /**
- * The URL that the link points to.
- * @var string
- */
- private $url;
- /**
- * A description of what the link points to.
- * @var string
- */
- private $description;
- /**
- * A timstamp describing the point in time which the link was added.
- * @var integer
- */
- private $timeAdded;
- /**
- * The unique identifier of the category that the link belongs to.
- * @var integer
- */
- private $categoryId;
-
- /**
- * Constructor for Link.
- * @param string $id The unique identifier for the link that should be
- * constructed.
- */
- public function __construct($id) {
- $fields = array(
- "title",
- "url",
- "description",
- "time_added",
- "category_id"
- );
- parent::__construct(TABLE_LINKS, $id, $fields);
- }
-
- /**
- * @see com.lokorin.lokorin.includes.TableEntry.readRow($row)
- */
- protected function readRow($row) {
- $this->title = $row["title"];
- $this->url = $row["url"];
- $this->description = $row["description"];
- $this->timeAdded = $row["time_added"];
- $this->categoryId = $row["category_id"];
- }
-
- /**
- * Gets the link as XHTML, ready to be used.
- * @return string XHTML compliant code that represents the link.
- */
- public function getLinkAsHTML() {
- return '<a href="'.$this->url.'">'.$this->title.'</a>' .
- '<span class="linkDescription">'.
- htmlspecialchars($this->description).'</span>';
- }
- }
- ?>