1: <?php
2: /**
3: * The Horde_Core_Ui_Tabs:: class manages and renders a tab-like interface.
4: *
5: * Copyright 2001-2003 Robert E. Coyle <robertecoyle@hotmail.com>
6: * Copyright 2003-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 Jason M. Felice <jason.m.felice@gmail.com>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package Core
15: */
16: class Horde_Core_Ui_Tabs extends Horde_Core_Ui_Widget
17: {
18: /**
19: * The array of tabs.
20: *
21: * @var array
22: */
23: protected $_tabs = array();
24:
25: /**
26: * Adds a tab to the interface.
27: *
28: * @param string $title The text which appears on the tab.
29: * @param Horde_Url $link The target page.
30: * @param mixed $params Either a string value to set the tab variable to,
31: * or a hash of parameters. If an array, the tab
32: * variable can be set by the 'tabname' key.
33: */
34: public function addTab($title, $link, $params = array())
35: {
36: if (!is_array($params)) {
37: $params = array('tabname' => $params);
38: }
39:
40: $this->_tabs[] = array_merge(array('title' => $title,
41: 'link' => $link->copy(),
42: 'tabname' => null),
43: $params);
44: }
45:
46: /**
47: * Returns the title of the tab with the specified name.
48: *
49: * @param string $tabname The name of the tab.
50: *
51: * @return string The tab's title.
52: */
53: public function getTitleFromAction($tabname)
54: {
55: foreach ($this->_tabs as $tab) {
56: if ($tab['tabname'] == $tabname) {
57: return $tab['title'];
58: }
59: }
60:
61: return null;
62: }
63:
64: /**
65: * Renders the tabs.
66: *
67: * @param string $active_tab If specified, the name of the active tab. If
68: * not, the active tab is determined
69: * automatically.
70: */
71: public function render($active_tab = null)
72: {
73: $html = "<div class=\"tabset\"><ul>\n";
74:
75: $first = true;
76: $active = $_SERVER['PHP_SELF'] . $this->_vars->get($this->_name);
77:
78: foreach ($this->_tabs as $tab) {
79: $link = $this->_addPreserved($tab['link']);
80: if (!is_null($this->_name) && !is_null($tab['tabname'])) {
81: $link->add($this->_name, $tab['tabname']);
82: }
83:
84: $class = '';
85: if ((!is_null($active_tab) && $active_tab == $tab['tabname']) ||
86: ($active == $tab['link'] . $tab['tabname'])) {
87: $class = ' class="activeTab"';
88: }
89:
90: $id = '';
91: if (!empty($tab['id'])) {
92: $id = ' id="' . htmlspecialchars($tab['id']) . '"';
93: }
94:
95: if (!isset($tab['target'])) {
96: $tab['target'] = '';
97: }
98:
99: if (!isset($tab['onclick'])) {
100: $tab['onclick'] = '';
101: }
102:
103: $accesskey = Horde::getAccessKey($tab['title']);
104:
105: $html .= '<li' . $class . $id . '>'
106: . $link->link(array('target' => $tab['target'], 'onclick' => $tab['onclick'], 'accesskey' => $accesskey))
107: . Horde::highlightAccessKey(str_replace(' ', ' ', $tab['title']), $accesskey)
108: . "</a> </li>\n";
109: }
110:
111: return $html . "</ul></div><br class=\"clear\" />\n";
112: }
113:
114: }
115: