class_Tree.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:8k
- <?php
- // $Id: class_Tree.php,v 1.5 2004/02/06 04:46:59 deskpro Exp $
- /**
- * HTML_TreeMenu Class
- * @author Richard Heyes
- * @author Harald Radi
- * @access public
- * @package HTML_TreeMenu
- */
- class HTML_TreeMenu
- {
- /**
- * Indexed array of subnodes
- * @var array
- */
- var $items;
- /**
- * The layer ID
- * @var string
- */
- var $layer;
- /**
- * Path to the images
- * @var string
- */
- var $images;
- /**
- * Name of the object
- * This should not be changed without changing
- * the javascript.
- * @var string
- */
- var $menuobj;
- /**
- * Constructor
- *
- * @access public
- * @param string $layer The name of the layer to add the HTML to.
- * In browsers that do not support document.all
- * or document.getElementById(), document.write()
- * is used, and thus this layer name has no effect.
- * @param string $images The path to the images folder.
- * @param string $linkTarget The target for the link. Defaults to "_self"
- * @param string $usePersistence Whether to use clientside persistence. This option
- * only affects ie5+.
- */
- function HTML_TreeMenu($layer, $images, $linkTarget = '_self', $usePersistence = true)
- {
- $this->menuobj = 'objTreeMenu';
- $this->layer = $layer;
- $this->images = $images;
- $this->linkTarget = $linkTarget;
- $this->usePersistence = $usePersistence;
- }
- /**
- * This function adds an item to the the tree.
- *
- * @access public
- * @param object $menu The node to add. This object should be
- * a HTML_TreeNode object.
- * @return object Returns a reference to the new node inside
- * the tree.
- */
- function &addItem(&$menu)
- {
- $this->items[] = &$menu;
- return $this->items[count($this->items) - 1];
- }
- /**
- * This function prints the menu Jabbascript code. Should
- * be called *AFTER* your layer tag has been printed. In the
- * case of older browsers, eg Navigator 4, The menu HTML will
- * appear where this function is called.
- *
- * @access public
- */
- function printMenu()
- {
- echo "n";
- echo '<script language="javascript" type="text/javascript">' . "nt";
- echo sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s");',
- $this->menuobj,
- $this->layer,
- $this->images,
- $this->menuobj,
- $this->linkTarget);
-
- echo "n";
- if (isset($this->items)) {
- for ($i=0; $i<count($this->items); $i++) {
- $this->items[$i]->_printMenu($this->menuobj . ".n[$i]");
- }
- }
- echo sprintf("nt%s.drawMenu();", $this->menuobj);
- if ($this->usePersistence) {
- echo sprintf("nt%s.resetBranches();", $this->menuobj);
- }
- echo "n</script>";
- }
- } // HTML_TreeMenu
- /**
- * HTML_TreeNode class
- *
- * This class is supplementary to the above and provides a way to
- * add nodes to the tree. A node can have other nodes added to it.
- *
- * @author Richard Heyes
- * @author Harald Radi
- * @access public
- * @package HTML_TreeMenu
- */
- class HTML_TreeNode
- {
- /**
- * The text for this node.
- * @var string
- */
- var $text;
- /**
- * The link for this node.
- * @var string
- */
- var $link;
- /**
- * The icon for this node.
- * @var string
- */
- var $icon;
- /**
- * Indexed array of subnodes
- * @var array
- */
- var $items;
- /**
- * Whether this node is expanded or not
- * @var bool
- */
- var $expanded;
- /**
- * Constructor
- *
- * @access public
- * @param string $text The description text for this node
- * @param string $link The link for the text
- * @param string $icon Optional icon to appear to the left of the text
- * @param bool $expanded Whether this node is expanded or not (IE only)
- * @param bool $isDynamic Whether this node is dynamic or not (no affect on non-supportive browsers)
- */
- function HTML_TreeNode($text = null, $link = null, $icon = null, $expanded = false, $isDynamic = true)
- {
- $this->text = (string)$text;
- $this->link = (string)$link;
- $this->icon = (string)$icon;
- $this->expanded = $expanded;
- $this->isDynamic = $isDynamic;
- }
- /**
- * Adds a new subnode to this node.
- *
- * @access public
- * @param object $node The new node
- */
- function &addItem(&$node)
- {
- $this->items[] = &$node;
- return $this->items[count($this->items) - 1];
- }
- /**
- * Prints jabbascript for this particular node.
- *
- * @access private
- * @param string $prefix The jabbascript object to assign this node to.
- */
- function _printMenu($prefix)
- {
- echo sprintf("t%s = new TreeNode('%s', %s, %s, %s, %s);n",
- $prefix,
- htmlspecialchars($this->text),
- !empty($this->icon) ? "'" . $this->icon . "'" : 'null',
- !empty($this->link) ? "'" . $this->link . "'" : 'null',
- $this->expanded ? 'true' : 'false',
- $this->isDynamic ? 'true' : 'false');
- if (!empty($this->items)) {
- for ($i=0; $i<count($this->items); $i++) {
- $this->items[$i]->_printMenu($prefix . ".n[$i]");
- }
- }
- }
- }
- // +-----------------------------------------------------------------------+
- // | END OF PEAR COPYRIGHT |
- // +-----------------------------------------------------------------------+
- /**
- * Builds a tree menu object out of two
- * mysql tables.
- *
- * EXPECTS
- * o A global variable $db (database object from Deskpro)
- *
- */
- class categoryTree
- {
- var $catLinkFormat;
- var $artLinkFormat;
- var $catNameFormat;
- var $artNameFormat;
- var $categories;
- var $articles;
- /**
- * Constructor. Autostarts the build process
- */
- function categoryTree()
- {
- $this->catLinkFormat = 'index.php?catid={id}';
- $this->artLinkFormat = 'view.php?do=view&articleid={id}';
- $this->catNameFormat = '{name} ({totalarticles})';
- $this->artNameFormat = '{title}';
- $this->_getData();
- $this->tree = &new HTML_TreeMenu('fooLayer', 'treeMenu/images/');
- $this->_buildTree($this->_getCategories(0), $this->tree);
- }
-
- /**
- * Prints the menu
- */
- function printMenu()
- {
- $this->tree->printMenu();
- }
-
- /**
- * Builds the menu
- */
- function _buildTree($categories, &$treenode)
- {
- for ($i=0; $i<count($categories); $i++) {
- $newnode = &$treenode->addItem(new HTML_TreeNode($this->_parseLinkFormat($this->catNameFormat, $categories[$i]), $this->_parseLinkFormat($this->catLinkFormat, $categories[$i]), 'folder.gif'));
-
- // Handle subcategories
- $subcats = $this->_getCategories($categories[$i]['id']);
- if (!empty($subcats)) {
- $this->_buildTree($subcats, $newnode);
- }
-
- // Handle articles
- $articles = $this->_getArticles($categories[$i]['id']);
- if (!empty($articles)) {
- foreach ($articles as $article) {
- $newnode->addItem(new HTML_TreeNode($this->_parseLinkFormat($this->artNameFormat, $article), $this->_parseLinkFormat($this->artLinkFormat, $article), 'article.gif'));
- }
- }
- }
- }
- /**
- * Gets categories using the given parent id
- */
- function _getCategories($id)
- {
- $return = array();
- if (is_array($this->categories)) {
- foreach ($this->categories as $key => $value) {
- if ($value['parent'] == $id) {
- $return[] = $value;
- }
- }
- }
-
- return $return;
- }
- /**
- * Gets articles using the given category id
- */
- function _getArticles($category)
- {
- return !empty($this->articles[$category]) ? $this->articles[$category] : array();
- }
- /**
- * Parses the data from the db
- */
- function _getData()
- {
- $GLOBALS['db']->query('SELECT * FROM faq_cats ORDER BY parent, show_order, id');
- while ($row = $GLOBALS['db']->row_array()) {
- $row['name'] = addslashes($row['name']);
- $this->categories[] = $row;
- }
- $GLOBALS['db']->query('SELECT id, title, category FROM faq_articles ORDER BY category, show_order, id');
- while ($row = $GLOBALS['db']->row_array()) {
- $row['title'] = addslashes($row['title']);
- $this->articles[$row['category']][] = $row;
- }
- }
- /**
- * Parses a link (though not only used for links)
- */
- function _parseLinkFormat($format, $data)
- {
- return preg_replace('/{(.+)}/Ue', "@$data['\1'];", $format);
- }
- }
- ?>