Fixing a bug in BSPrinterRootElementPage that would cause an error whenever we tried...
[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 if (isset($this->structure->links))
105 {
106 $links = array();
107 foreach ($this->structure->links->link as $link)
108 {
109 $attrs = $link->attributes();
110 $links[] = '<a href="' . $attrs->target . '">' . $link . '</a>';
111 }
112 }
113
114 $output .= "\n" . '<div id="toplinks">';
115 $output .= "\n\t" . '<div>' . BSPrinter::get_realm() . '</div>';
116 if (isset($links))
117 {
118 $output .= "\n\t" . '<div id="toplinks-links">' . implode(' &bull; ', $links) . '</div>';
119 }
120 $output .= "\n" . '</div>';
121
122 // -------------------------------------------------------------------
123
124 $output .= "\n\n" . '<div id="wrapper">';
125
126 // -------------------------------------------------------------------
127
128 if (isset($this->structure->tabs))
129 {
130 $output .= "\n" . '<div id="tabbar">';
131 foreach ($this->structure->tabs->tab as $tab)
132 {
133 $link = "\n\t" . '<a href="' . $tab['target'] . '"';
134 if (in_array($tab['key'], $this->focusKeys))
135 {
136 $link .= ' id="focustab"';
137 }
138
139 $link .= '><span>' . $tab . '</span></a>';
140
141 $output .= $link;
142 }
143 $output .= "\n" . '</div>';
144 }
145
146 // -------------------------------------------------------------------
147
148 $output .= "\n\n" . '<table id="contentbody" cellspacing="0" cellpadding="0" border="0">';
149 $output .= "\n" . '<tr>';
150
151 // -------------------------------------------------------------------
152
153 $output .= "\n" . '<td id="menu">';
154 foreach ($this->structure->sections->section as $section)
155 {
156 if (!in_array($section['key'], $this->displaySections))
157 {
158 continue;
159 }
160
161 // handle inherited sections
162 $links = array();
163 if ($section['inherits'])
164 {
165 $keys = explode(',', $section['inherits']);
166
167 foreach ($keys as $parKey)
168 {
169 $parLinks = $this->structure->xpath('/navigation/sections/section[@key="' . $parKey . '"]/link');
170 $links = array_merge($links, $parLinks);
171 }
172
173 // add the links from the hybrid section
174 if (is_array($section->link))
175 {
176 $links = array_merge($links, $section->link);
177 }
178 else
179 {
180 $links[] = $section->link;
181 }
182 }
183 // no inheritance, so the links are straight from the section
184 else
185 {
186 $links = $section->link;
187 }
188
189 $output .= "\n" . '<ul>';
190 $output .= "\n\t" . '<li class="header"><span>' . $section['name'] . '</span></li>';
191 foreach ($links as $link)
192 {
193 $output .= "\n\t" . '<li' . (in_array($link['key'], $this->focusKeys) ? ' class="focus"' : '') . '><a href="' . $link['target'] . '"><span>' . $link . '</span></a></li>';
194 }
195 $output .= "\n" . '</ul>' . "\n";
196 }
197 $output .= "\n" . '</td>';
198
199 // -------------------------------------------------------------------
200
201 $output .= "\n" . '<td id="mainbody">';
202 $output .= "\n\n" . '<!-- main content body -->' . "\n";
203
204 return $output;
205 }
206
207 /**
208 * Generates the HTML that is inserted in BSPrinterRootElementPage that
209 * closes all of the navigation HTML stuff
210 *
211 * @return string Generated HTML content
212 */
213 public function constructFooterHtml()
214 {
215 $output = '';
216
217 $output .= "\n" . '<!-- / main content body -->' . "\n";
218
219 $output .= "\n" . '</td>';
220 $output .= "\n" . '</tr>';
221
222 $output .= "\n\n" . '</table>';
223
224 $output .= "\n\n" . '</div>';
225
226 return $output;
227 }
228 }
229
230 ?>