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

Source for file lib_links.php

Documentation is available at lib_links.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 related to the site's system for
  9. * external links.
  10. * @author Andreas Launila
  11. * @version $Revision: 1.12 $
  12. * @package com.lokorin.lokorin.lib
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14. */
  15.  
  16. /**
  17. * For access to common constants and functions.
  18. */
  19. require_once('common.php');
  20. CssHandler::getInstance()->includeCss('links');
  21.  
  22. /**
  23. * Gets all categories that have the root as parent.
  24. * @return array All top level categories.
  25. * @access public
  26. */
  27. function getRootCategories() {
  28. global $db;
  29. //Get all root categories.
  30. $idFields = array(
  31. "parent_id" => 0
  32. );
  33. $rows = $db->querySelect(TABLE_LINK_CATEGORIES, array('id'),
  34. $db->buildSqlSelector($idFields));
  35. $rootCategories = array();
  36. foreach($rows as $row) {
  37. $rootCategories[] = new Category($row['id']);
  38. }
  39.  
  40. return $rootCategories;
  41. }
  42.  
  43. /**
  44. * Gets a list of all category names.
  45. * @return array Key is the id, value is the corresponding category name.
  46. * @access public
  47. */
  48. function getAllCategoryNames() {
  49. /**
  50. * Internal recursive function. Gets the names of the subcategories of a
  51. * specified category. The function recurses into the subcategories of the
  52. * subcategories and so on.
  53. * @param Category The category from which the names should be retrieved.
  54. * @param string The base name that should be put in front of all retrieved
  55. * names.
  56. * @return array All retrieved category names.
  57. * @access private
  58. */
  59. function getCategoryNames($category, $base = '') {
  60. $result = array();
  61. $result[$category->getId()] = $base.$category->getName();
  62. $children = $category->getChildren();
  63. if(sizeof($children) == 0) {
  64. return $result;
  65. }
  66. foreach($children as $cat) {
  67. $result += getCategoryNames($cat, $base.$category->getName().'->');
  68. }
  69. return $result;
  70. }
  71. $categories = getRootCategories();
  72. $names = array();
  73. foreach($categories as $cat) {
  74. $names += getCategoryNames($cat);
  75. }
  76. return $names;
  77. }
  78.  
  79. /**
  80. * Describes a link category. A category that contains links but that also
  81. * has a hierarchy so a category can contain child categories and so on.
  82. * @author Andreas Launila
  83. * @package com.lokorin.lokorin.lib
  84. */
  85. class Category extends TableEntry {
  86. /**
  87. * The name of the category.
  88. * @var string
  89. */
  90. private $name;
  91. /**
  92. * The description of the category.
  93. * @var string
  94. */
  95. private $description;
  96. /**
  97. * The Category instances that are direct children to this instance.
  98. * @var array
  99. */
  100. private $children;
  101. /**
  102. * The Link instances that are connected to the category.
  103. * @var array
  104. */
  105. private $links;
  106. /**
  107. * Constructor for Category.
  108. * @param integer $id The unique identifier to be used.
  109. */
  110. public function __construct($id) {
  111. global $db;
  112. $fields = array(
  113. "name",
  114. "description"
  115. );
  116. parent::__construct(TABLE_LINK_CATEGORIES, $id, $fields);
  117. //Get the category's children.
  118. $this->children = array();
  119. $idFields = array(
  120. "parent_id" => parent::$this->getId()
  121. );
  122. $rows = $db->querySelect(TABLE_LINK_CATEGORIES, array("id"),
  123. $db->buildSqlSelector($idFields));
  124. foreach($rows as $row) {
  125. $this->children[] = new Category($row["id"]);
  126. }
  127. //Get the links associated with the category.
  128. $this->links = array();
  129. $idFields = array(
  130. "category_id" => parent::$this->getId()
  131. );
  132. $rows = $db->querySelect(TABLE_LINKS, array("id"),
  133. $db->buildSqlSelector($idFields));
  134. foreach($rows as $row) {
  135. $this->links[] = new Link($row["id"]);
  136. }
  137. }
  138. /**
  139. * @see com.lokorin.lokorin.includes.TableEntry.readRow($row)
  140. */
  141. protected function readRow($row) {
  142. $this->name = $row["name"];
  143. $this->description = $row["description"];
  144. }
  145. /**
  146. * Gets the name for the specific instance (not including parents).
  147. * @return string A single category name.
  148. */
  149. public function getName() {
  150. return $this->name;
  151. }
  152. /**
  153. * Gets the description of the category.
  154. * @return sring A category description.
  155. */
  156. public function getDescription() {
  157. return $this->description;
  158. }
  159. /**
  160. * Gets all the direct children instances.
  161. * @return array Key is irrelevant, value is a Category instance.
  162. */
  163. public function getChildren() {
  164. return $this->children;
  165. }
  166.  
  167. /**
  168. * Gets all Link instances connected to the category.
  169. * @return array Key is irrelevant, value is a Link instance.
  170. */
  171. public function getLinks() {
  172. return $this->links;
  173. }
  174. /**
  175. * Recursive method for creating the body contents with the links.
  176. * @param integer $depth The depth of the category (1 being the first after
  177. * root).
  178. * @return string The resulting output as XHTML.
  179. */
  180. public function getAsHtmlList($depth = 1) {
  181. $out = '<div class="depth'.$depth.'">';
  182. $out .= '<a name="'.$this->getId().'"></a>';
  183. $out .= '<h'.($depth+1).'>'.$this->getName().'</h'.($depth+1).'>'."\n";
  184. $out .= $this->getDescription();
  185. //Add the links.
  186. $links = $this->getLinks();
  187. sort($links);
  188. if(count($links) > 0) {
  189. $out .= '<ul>'."\n";
  190. foreach($links as $link) {
  191. $out .= '<li>'.$link->getLinkAsHTML().'</li>'."\n";
  192. }
  193. $out .= '</ul>'."\n";
  194. }
  195. //Add the children categories.
  196. $children = $this->getChildren();
  197. sort($children);
  198. foreach($children as $child) {
  199. $out .= $child->getAsHtmlList($depth + 1);
  200. }
  201. $out .= '</div>';
  202. return $out;
  203. }
  204. /**
  205. * Recursive method for creating a navigation structure where each link
  206. * is an internal page link to the categories id. The navigation is
  207. * structured according to the tree in list elements.
  208. * @param string $hrefPattern The printf pattern that should be used to
  209. * format the href attribute of the links.
  210. * @param integer $maxDepth The maximum number of levels that may be taken
  211. * down the tree. If it's 0 then only the current category will be
  212. * included, if it's 1 then the category and its subcategories will
  213. * be included and so on. By default an large max depth is used.
  214. * @param boolean $includeDescriptions Whether or not the descriptions of
  215. * each category should be included in the navigation. If true then
  216. * the descriptions are included, otherwise they are not.
  217. * @return string The resulting XHTML for the navigation.
  218. */
  219. public function getHtmlNavigation($hrefPattern, $maxDepth = 1000,
  220. $includeDescriptions = true) {
  221. //Create the link.
  222. $out = '<a href="'.sprintf($hrefPattern, $this->getId()).'">'.
  223. $this->getName().'</a>'."\n";
  224. if($includeDescriptions) {
  225. //Add the description.
  226. $out .= '<br />'.$this->getDescription();
  227. }
  228. if($maxDepth == 0) {
  229. //Not allowed to go any further.
  230. return $out;
  231. }
  232. //Add the next level.
  233. $children = $this->getChildren();
  234. sort($children);
  235. if(count($children) > 0) {
  236. $out .= '<ul>';
  237. foreach($children as $child) {
  238. $out .= '<li>'.
  239. $child->getHtmlNavigation($hrefPattern, $maxDepth - 1,
  240. $includeDescriptions).
  241. "</li>\n";
  242. }
  243. $out .= '</ul>';
  244. }
  245. return $out;
  246. }
  247. }
  248.  
  249. /**
  250. * Describes a link.
  251. * @author Andreas Launila
  252. * @package com.lokorin.lokorin.lib
  253. */
  254. class Link extends TableEntry {
  255. /**
  256. * The title of the page that the link refers to.
  257. * @var string
  258. */
  259. private $title;
  260. /**
  261. * The URL that the link points to.
  262. * @var string
  263. */
  264. private $url;
  265. /**
  266. * A description of what the link points to.
  267. * @var string
  268. */
  269. private $description;
  270. /**
  271. * A timstamp describing the point in time which the link was added.
  272. * @var integer
  273. */
  274. private $timeAdded;
  275. /**
  276. * The unique identifier of the category that the link belongs to.
  277. * @var integer
  278. */
  279. private $categoryId;
  280. /**
  281. * Constructor for Link.
  282. * @param string $id The unique identifier for the link that should be
  283. * constructed.
  284. */
  285. public function __construct($id) {
  286. $fields = array(
  287. "title",
  288. "url",
  289. "description",
  290. "time_added",
  291. "category_id"
  292. );
  293. parent::__construct(TABLE_LINKS, $id, $fields);
  294. }
  295. /**
  296. * @see com.lokorin.lokorin.includes.TableEntry.readRow($row)
  297. */
  298. protected function readRow($row) {
  299. $this->title = $row["title"];
  300. $this->url = $row["url"];
  301. $this->description = $row["description"];
  302. $this->timeAdded = $row["time_added"];
  303. $this->categoryId = $row["category_id"];
  304. }
  305. /**
  306. * Gets the link as XHTML, ready to be used.
  307. * @return string XHTML compliant code that represents the link.
  308. */
  309. public function getLinkAsHTML() {
  310. return '<a href="'.$this->url.'">'.$this->title.'</a>' .
  311. '<span class="linkDescription">'.
  312. htmlspecialchars($this->description).'</span>';
  313. }
  314. }
  315. ?>

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