1: <?php
2: /**
3: * The Horde_Tree_Simplehtml:: class provides simple HTML rendering of a tree
4: * (no graphics).
5: *
6: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Michael Slusarz <slusarz@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package Tree
15: */
16: class Horde_Tree_Simplehtml extends Horde_Tree_Base
17: {
18: /**
19: * Allowed parameters for nodes.
20: *
21: * @var array
22: */
23: protected $_allowed = array(
24: 'class',
25: 'url'
26: );
27:
28: /**
29: * Should the tree be rendered statically?
30: *
31: * @var boolean
32: */
33: protected $_static = true;
34:
35: /**
36: * Returns the tree.
37: *
38: * @return string The HTML code of the rendered tree.
39: */
40: public function getTree($static = false)
41: {
42: $this->_buildIndents($this->_root_nodes);
43:
44: $tree = '';
45: foreach ($this->_root_nodes as $node_id) {
46: $tree .= $this->_buildTree($node_id);
47: }
48:
49: return $tree;
50: }
51:
52: /**
53: * Adds additional parameters to a node.
54: *
55: * @param string $id The unique node id.
56: * @param array $params Parameters to set (key/value pairs).
57: * <pre>
58: * class - CSS class to use with this node
59: * url - URL to link the node to
60: * </pre>
61: */
62: public function addNodeParams($id, $params = array())
63: {
64: parent::addNodeParams($id, $params);
65: }
66:
67: /**
68: * Recursive function to walk through the tree array and build the output.
69: *
70: * @param string $node_id The Node ID.
71: *
72: * @return string The tree rendering.
73: */
74: protected function _buildTree($node_id)
75: {
76: $node = $this->_nodes[$node_id];
77:
78: $output = '<div' .
79: (empty($node['class']) ? '' : ' class="' . $node['class'] . '"') .
80: '>';
81: if (isset($node['extra'][Horde_Tree::EXTRA_LEFT])) {
82: $output .= implode(' ', $node['extra'][Horde_Tree::EXTRA_LEFT]);
83: }
84: $output .= str_repeat(' ', $node['indent'] * 2);
85:
86: $output .= empty($node['url'])
87: ? $node['label']
88: : '<a href="' . strval($node['url']) . '">' . $node['label'] . '</a>';
89: if (isset($node['extra'][Horde_Tree::EXTRA_RIGHT])) {
90: $output .= implode(' ', $node['extra'][Horde_Tree::EXTRA_RIGHT]);
91: }
92:
93: if (isset($node['children'])) {
94: $output .= ' [' .
95: $this->_generateUrlTag($node_id) .
96: ($node['expanded'] ? '-' : '+') .
97: '</a>]';
98: }
99:
100: $output .= '</div>';
101:
102: if (isset($node['children']) && $node['expanded']) {
103: foreach ($node['children'] as $val) {
104: $output .= $this->_buildTree($val);
105: }
106: }
107:
108: return $output;
109: }
110:
111: /**
112: * Generate a link URL.
113: *
114: * @param string $node_id The node ID.
115: *
116: * @return string The link tag.
117: */
118: protected function _generateUrlTag($node_id)
119: {
120: $url = new Horde_Url($_SERVER['PHP_SELF']);
121: return $url->add(Horde_Tree::TOGGLE . $this->_instance, $node_id)->link();
122: }
123:
124: }
125: