Updating the PrinterNavigation system to take an XML structure instead that is much...
[isso.git] / PrinterNavigation.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Printer: Navigation Generator (PrinterNavigation.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * Printer: Navigation Generator
30 *
31 * This framework works in conjunction with BSPrinter to generate a page header
32 * and sidebar to be used for navigation. You set the navigation array and then can
33 * specify a given key to highlight elements. Elements are broken into three types:
34 * tabs (top of the page), groups (blocks of link actions), and links
35 *
36 * @author Blue Static
37 * @copyright Copyright (c)2005 - 2008, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSPrinterNavigation
42 {
43 /**
44 * XML document structure
45 * @var SimpleXMLElement
46 */
47 private $structure;
48
49 /**
50 * Keys to set focus on
51 * @var array
52 */
53 private $focusKeys = array();
54
55 /**
56 * Sections to display
57 * @var array
58 */
59 private $displaySections = array();
60
61 /**
62 * Constructor
63 *
64 * @param string Navigation XML data
65 */
66 public function __construct($xml)
67 {
68 $this->structure = new SimpleXMLElement($xml);
69 }
70
71 /**
72 * Adds focus to a given key
73 *
74 * @param string Key
75 */
76 public function addFocus($key)
77 {
78 $this->focusKeys[] = $key;
79 }
80
81 /**
82 * Adds a section to display. The order in which sections are added is the order
83 * in which they are painted
84 *
85 * @param string Key
86 */
87 public function addSection($key)
88 {
89 $this->displaySections[] = $key;
90 }
91
92 /**
93 * Generates the header HTML that is called in BSPrinterRootElementPage
94 * to setup the navigation frame
95 *
96 * @return string Generated HTML content
97 */
98 public function constructHeaderHtml()
99 {
100 $output = '<!-- navigation start -->' . "\n\n";
101
102 // -------------------------------------------------------------------
103
104 $output2 = array();
105 foreach ($this->structure->links->link as $link)
106 {
107 $attrs = $link->attributes();
108 $output2[] = '<a href="' . $attrs->target . '">' . $link . '</a>';
109 }
110
111 $output .= "\n" . '<div id="toplinks">';
112 $output .= "\n\t" . '<div>' . BSPrinter::get_realm() . '</div>';
113 $output .= "\n\t" . '<div id="toplinks-links">' . implode(' &bull; ', $output2) . '</div>';
114 $output .= "\n" . '</div>';
115
116 // -------------------------------------------------------------------
117
118 $output .= "\n\n" . '<div id="wrapper">';
119
120 // -------------------------------------------------------------------
121
122 $output .= "\n" . '<div id="tabbar">';
123 foreach ($this->structure->tabs->tab as $tab)
124 {
125 $link = "\n\t" . '<a href="' . $tab['target'] . '"';
126 if (in_array($tab['key'], $this->focusKeys))
127 {
128 $link .= ' id="focustab"';
129 }
130
131 $link .= '><span>' . $tab . '</span></a>';
132
133 $output .= $link;
134 }
135 $output .= "\n" . '</div>';
136
137 // -------------------------------------------------------------------
138
139 $output .= "\n\n" . '<table id="contentbody" cellspacing="0" cellpadding="0" border="0">';
140 $output .= "\n" . '<tr>';
141
142 // -------------------------------------------------------------------
143
144 $output .= "\n" . '<td id="menu">';
145 foreach ($this->structure->sections->section as $section)
146 {
147 if (!in_array($section['key'], $this->displaySections))
148 {
149 continue;
150 }
151
152 // handle inherited sections
153 $links = array();
154 if ($section['inherits'])
155 {
156 $keys = explode(',', $section['inherits']);
157
158 foreach ($keys as $parKey)
159 {
160 $parLinks = $this->structure->xpath('/navigation/sections/section[@key="' . $parKey . '"]/link');
161 $links = array_merge($links, $parLinks);
162 }
163
164 // add the links from the hybrid section
165 if (is_array($section->link))
166 {
167 $links = array_merge($links, $section->link);
168 }
169 else
170 {
171 $links[] = $section->link;
172 }
173 }
174 // no inheritance, so the links are straight from the section
175 else
176 {
177 $links = $section->link;
178 }
179
180 $output .= "\n" . '<ul>';
181 $output .= "\n\t" . '<li class="header"><span>' . $section['name'] . '</span></li>';
182 foreach ($links as $link)
183 {
184 $output .= "\n\t" . '<li' . (in_array($link['key'], $this->focusKeys) ? ' class="focus"' : '') . '><a href="' . $link['target'] . '"><span>' . $link . '</span></a></li>';
185 }
186 $output .= "\n" . '</ul>' . "\n";
187 }
188 $output .= "\n" . '</td>';
189
190 // -------------------------------------------------------------------
191
192 $output .= "\n" . '<td id="mainbody">';
193 $output .= "\n\n" . '<!-- main content body -->' . "\n";
194
195 return $output;
196 }
197
198 /**
199 * Generates the HTML that is inserted in BSPrinterRootElementPage that
200 * closes all of the navigation HTML stuff
201 *
202 * @return string Generated HTML content
203 */
204 public function constructFooterHtml()
205 {
206 $output = '';
207
208 $output .= "\n" . '<!-- / main content body -->' . "\n";
209
210 $output .= "\n" . '</td>';
211 $output .= "\n" . '</tr>';
212
213 $output .= "\n\n" . '</table>';
214
215 $output .= "\n\n" . '</div>';
216
217 return $output;
218 }
219 }
220
221 ?>