1: <?php
2: /**
3: * The Horde_Tree_Select:: class provides <option> tag rendering.
4: *
5: * Copyright 2005-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Ben Chavet <ben@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Tree
14: */
15: class Horde_Tree_Select extends Horde_Tree_Base
16: {
17: /**
18: * Allowed parameters for nodes.
19: *
20: * @var array
21: */
22: protected $_allowed = array(
23: 'selected'
24: );
25:
26: /**
27: * Should the tree be rendered statically?
28: *
29: * @var boolean
30: */
31: protected $_static = true;
32:
33: /**
34: * Returns the tree.
35: *
36: * @param boolean $static If true the tree nodes can't be expanded and
37: * collapsed and the tree gets rendered expanded.
38: * This option has no effect in this driver.
39: *
40: * @return string The HTML code of the rendered tree.
41: */
42: public function getTree($static = false)
43: {
44: $this->_buildIndents($this->_root_nodes);
45:
46: $tree = '';
47: foreach ($this->_root_nodes as $node_id) {
48: $tree .= $this->_buildTree($node_id);
49: }
50:
51: return $tree;
52: }
53:
54: /**
55: * Adds additional parameters to a node.
56: *
57: * @param string $id The unique node id.
58: * @param array $params Parameters to set (key/value pairs).
59: * <pre>
60: * selected - (boolean) Whether this node is selected.
61: * </pre>
62: */
63: public function addNodeParams($id, $params = array())
64: {
65: parent::addNodeParams($id, $params);
66: }
67:
68: /**
69: * Recursive function to walk through the tree array and build the output.
70: *
71: * @param string $node_id The Node ID.
72: *
73: * @return string The tree rendering.
74: */
75: protected function _buildTree($node_id)
76: {
77: $node = $this->_nodes[$node_id];
78:
79: $output = '<option value="' . htmlspecialchars($node_id) . '"' .
80: (empty($node['selected']) ? '' : ' selected="selected"') .
81: '>' .
82: str_repeat(' ', intval($node['indent']) * 2) .
83: htmlspecialchars($node['label']) .
84: '</option>';
85:
86: if (isset($node['children']) && $node['expanded']) {
87: foreach ($node['children'] as $val) {
88: $output .= $this->_buildTree($val);
89: }
90: }
91:
92: return $output;
93: }
94:
95: }
96: