Change BSRouter::route() to BSRouter::dispatch()
[isso.git] / printer.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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
24 * printer.php
25 *
26 * @package ISSO
27 */
28
29 require_once('ISSO/PrinterElement.php');
30 require_once('ISSO/PrinterBaseElement.php');
31 require_once('ISSO/PrinterLabelElement.php');
32 require_once('ISSO/PrinterRootElement.php');
33 require_once('ISSO/PrinterRootElementPage.php');
34 require_once('ISSO/PrinterRootElementTable.php');
35 require_once('ISSO/PrinterTableElement.php');
36
37 /**
38 * Printer
39 *
40 * This framework generates standard HTML through various functions. The purpose
41 * is generally so that things like the admin system can be created without templates.
42 *
43 * Constants:
44 * ISSO_PRINTER_DONE_HEADER - An internal constant that is used to check to see
45 * if the page header has already been printed
46 * ISSO_PRINTER_HIDE_SETUP - Will stop the page footer data (copyright and debug
47 * box) from being printed
48 * ISSO_PRINTER_NO_NAVIGATION - Do not show the navigation frame from ISSO.Printer.Navigation
49 *
50 * @author Blue Static
51 * @copyright Copyright ©2002 - [#]year[#], Blue Static
52 * @version $Revision$
53 * @package ISSO
54 *
55 */
56 class Printer
57 {
58 /**
59 * Framework registry object
60 * @var object
61 */
62 private $registry = null;
63
64 /**
65 * Realm that we are operating in (displayed in the <title>)
66 * @var string
67 */
68 private $realm = '[UNDEFINED REALM]';
69
70 /**
71 * CSS to place in the page
72 * @var string
73 */
74 private $css = '';
75
76 /**
77 * Extra code to place
78 * @var sring
79 */
80 private $code = '';
81
82 /**
83 * Page-start hooko
84 * @var string
85 */
86 private $page_start_hook = ':=NO METHOD=:';
87
88 /**
89 * Language information array: ('langcode' =>, 'direction' =>, 'charset' =>)
90 * @var array
91 */
92 private $language = array('langcode' => 'en_US', 'direction' => 'ltr', 'charset' => 'utf-8');
93
94 // ###################################################################
95 /**
96 * Sets the realm (or title) of the printed output
97 *
98 * @param string Title/realm
99 */
100 public function setRealm($realm)
101 {
102 $this->realm = $realm;
103 }
104
105 // ###################################################################
106 /**
107 * Sets the language array information
108 *
109 * @param array Language array
110 */
111 public function setLanguageInformation($lang)
112 {
113 $this->language = $lang;
114 }
115
116 // ###################################################################
117 /**
118 * Creates a redirect to another page; constructs the header and footer
119 * (therefore execution stops)
120 *
121 * @param string Location to redirect to
122 * @param string Redirect message to be shown
123 * @param array An aray of POST variables to send through on the redirect
124 */
125 public function redirect($location, $message = null, $postvars = array())
126 {
127 $js = '
128 <script type="text/javascript">
129 <!--
130 var timeout = 2000;
131
132 if (timeout > 0)
133 {
134 setTimeout("redirect()", timeout);
135 }
136 else
137 {
138 redirect();
139 }
140
141 function redirect()
142 {
143 ' . ($postvars ? 'document.forms.postvars.submit();' : 'window.location = "' . $location . '";') . '
144 }
145
146 //-->
147 </script>';
148
149 if (!defined('ISSO_PRINTER_NO_NAVIGATION'))
150 {
151 define('ISSO_PRINTER_NO_NAVIGATION', 1);
152 }
153
154 $this->page_start(_('Redirect'));
155
156 if ($postvars)
157 {
158 $this->form_start($location, null, false, 'postvars');
159
160 foreach ($postvars AS $key => $value)
161 {
162 $this->form_hidden_field($key, $value);
163 }
164
165 $this->form_end();
166 }
167
168 $redir = sprintf(_('Please wait to be redirected. If you are not redirected in a few seconds, click <a href="%1$s">here</a>.'), $location);
169 $override = false;
170 if ($message == null)
171 {
172 $showmessage = $redir;
173 }
174 else
175 {
176 $showmessage = '<blockquote>' . $message . '</blockquote>';
177 $showmessage .= "\n<p>" . $redir . "</p>";
178 $override = true;
179 }
180
181 $this->page_message(_('Redirect'), $showmessage, $override);
182
183 $this->page_code($js);
184
185 $this->page_end();
186 }
187
188 // ###################################################################
189 /**
190 * Produces an entire page layout that asks the user whether or not
191 * they want to perform X action and provides a link to the YES and NO
192 * action
193 *
194 * @param string Message that asks if they want to do X
195 * @param string Location to go for YES
196 * @param string DO action to pass
197 * @param array Hidden parameters to pass to the next page
198 */
199 public function confirm($message, $location, $action, $params)
200 {
201 if (!defined('ISSO_PRINTER_NO_NAVIGATION'))
202 {
203 define('ISSO_PRINTER_NO_NAVIGATION', 1);
204 }
205
206 $this->page_start(_('Confirm'));
207
208 $this->form_start($location, $action);
209 foreach ($params AS $key => $value)
210 {
211 $this->form_hidden_field($key, $value);
212 }
213
214 $this->table_start(true, '75%');
215 $this->table_head(_('Confirm'), 1);
216 $this->row_span("<blockquote>$message</blockquote>", ':swap:', 'left', 1);
217 $this->row_submit('<input type="button" class="button" name="no" value=" ' . _('No') . ' " onclick="history.back(1); return false;" />', _('Yes'), '');
218 $this->table_end();
219
220 $this->form_end();
221
222 $this->page_end();
223 }
224
225 // ###################################################################
226 /**
227 * Throws a fatal error; constructs the header and footer
228 *
229 * @param string Error messsage text
230 */
231 public function error($message)
232 {
233 if (!defined('ISSO_PRINTER_NO_NAVIGATION'))
234 {
235 define('ISSO_PRINTER_NO_NAVIGATION', 1);
236 }
237
238 $this->page_start(_('Error'));
239 $this->page_message(_('Error'), $message);
240 $this->page_end();
241
242 exit;
243 }
244
245 // ###################################################################
246 /**
247 * Links CSS to the page from a given relative path
248 *
249 * @param string Relative path to the CSS file
250 */
251 public function css_link($path)
252 {
253 $this->css .= "\n\t<link rel=\"stylesheet\" href=\"$path\" />";
254 }
255
256 // ###################################################################
257 /**
258 * Imbeds actual CSS information into the page in <style> tags
259 *
260 * @param string Path of the CSS file to be imbeded
261 */
262 public function css_imbed($path)
263 {
264 $data = require_once($path);
265 $css = preg_replace('#/\*(.*?)\*/(\r|\n)*#s', '', $css);
266 $css = trim($css);
267 $this->css .= "\n\t<style type=\"text/css\">\n\t<!--\n" . $css . "\n\t//-->\n\t</style>";
268 }
269
270 // ###################################################################
271 /**
272 * Places raw HTML code directly into the documet at the current
273 * position
274 *
275 * @param string HTML code
276 */
277 public function page_code($code)
278 {
279 if (defined('ISSO_PRINTER_DONE_HEADER'))
280 {
281 echo "\n\n$code\n\n";
282 }
283 else
284 {
285 $this->code .= "\n\n$code\n\n";
286 }
287 }
288
289 // ###################################################################
290 /**
291 * A block function that produces a table with a message in it. This
292 * does not print the header and footer.
293 *
294 * @param string The title of the message (appears at the top of the block)
295 * @param string Content of the message
296 * @param bool Override the message: control the entire output (no <blockquote>)?
297 */
298 public function page_message($title, $message, $overridemessage = false)
299 {
300 $this->table_start(true, '75%');
301 $this->table_head($title, 1);
302 $this->row_span(($overridemessage ? $message : "<blockquote>$message</blockquote>"), ':swap:', 'left', 1);
303 $this->table_end();
304 }
305
306 // ###################################################################
307 /**
308 * Closes the HTML document structure an adds the copyright; this also
309 * stops execution of the page
310 */
311 public function page_end()
312 {
313 if (BSRegister::GetDebug() AND isset($_GET['query']))
314 {
315 ob_clean();
316 ob_end_clean();
317
318 if (is_array($this->registry->modules[ISSO_DB_LAYER]->history))
319 {
320 foreach ($this->registry->modules[ISSO_DB_LAYER]->history AS $query)
321 {
322 echo $this->registry->modules[ISSO_DB_LAYER]->construct_query_debug($query);
323 }
324 }
325 exit;
326 }
327
328 if ($this->registry->is_loaded('printer_navigation') AND (!defined('ISSO_PRINTER_NO_NAVIGATION') OR (defined('ISSO_PRINTER_NO_NAVIGATION') AND constant('ISSO_PRINTER_NO_NAVIGATION') != true)))
329 {
330 echo $this->registry->modules['printer_navigation']->generate_footer_html();
331 }
332
333 $copyright = "\n<br />\n<p align=\"center\" class=\"copyright\">\n\t<a href=\"http://www.bluestatic.org\" target=\"_blank\">" . BSRegister::GetApplication() . ' ' . BSRegister::GetAppVersion() . ", &copy;2002 - " . date('Y') . " Blue Static</a>\n</p>";
334
335 if (!defined('ISSO_PRINTER_HIDE_SETUP'))
336 {
337 echo "\n$copyright";
338 echo $this->registry->construct_debug_block(false);
339 }
340
341 echo "\n\n</body>\n</html>";
342
343 exit;
344 }
345
346 // -------------------------------------------------------------------
347
348 // ###################################################################
349 /**
350 * Opens a <table> tag with styling
351 *
352 * @param bool Whether to add a <br /> before the table
353 * @param string Value of the width attribute
354 */
355 public function table_start($break = true, $width = '90%')
356 {
357 if ($break)
358 {
359 echo '<br />';
360 }
361
362 echo "\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" align=\"center\" width=\"$width\" class=\"tborder\">\n";
363 }
364
365 // ###################################################################
366 /**
367 * Adds a table row that is sued to head the entire table
368 *
369 * @param string Title string
370 * @param integer Colspan attribute value
371 */
372 public function table_head($title, $colspan = 2)
373 {
374 echo "<tr>\n\t<td class=\"tcat\" align=\"center\" colspan=\"$colspan\">$title</td>\n</tr>\n";
375 }
376
377 // ###################################################################
378 /**
379 * Creates column headings; useful for a grid-style page. This uses a
380 * different styling than table_head() and is usually used directly
381 * after a table header.
382 *
383 * @param array Array of titles to print
384 */
385 public function table_column_head($columnarray)
386 {
387 if (is_array($columnarray))
388 {
389 $render = "<tr valign=\"top\" align=\"center\">\n";
390
391 foreach ($columnarray AS $header)
392 {
393 $render .= "\t<td class=\"thead\" align=\"center\">$header</td>\n";
394 }
395
396 $render .= "</tr>\n";
397
398 echo $render;
399 }
400 }
401
402 // ###################################################################
403 /**
404 * Closes a <table> tag
405 */
406 public function table_end()
407 {
408 echo "\n</table>\n";
409 }
410
411 // -------------------------------------------------------------------
412
413 // ###################################################################
414 /**
415 * Starts a <form> tag and adds the DO hidden input field
416 *
417 * @param string Action/name of the file to action to
418 * @param string Value of the DO parameter; used to do-branch
419 * @param bool Enctype attribute; used for mime/multi-part
420 * @param string Name of the form; this only matters for DOM access
421 * @param string Method to action as; POST or GET (default is POST)
422 */
423 public function form_start($action, $do, $enctype = false, $name = 'inputform', $submitmethod = 'post')
424 {
425 echo "\n<!-- input form -->\n<form name=\"$name\" action=\"$action\"" . ($enctype ? " enctype=\"$enctype\"" : '') . " method=\"$submitmethod\">\n";
426
427 if ($do !== null)
428 {
429 $this->form_hidden_field('do', $do);
430 }
431 }
432
433 // ###################################################################
434 /**
435 * Adds a hidden field at the current location
436 *
437 * @param string Name of the field
438 * @param string Value of the field
439 */
440 public function form_hidden_field($name, $value)
441 {
442 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />\n";
443 }
444
445 // ###################################################################
446 /**
447 * Closes a <form> tag
448 */
449 public function form_end()
450 {
451 echo "</form>\n<!-- / input form -->\n";
452 }
453
454 // -------------------------------------------------------------------
455
456 // ###################################################################
457 /**
458 * Creates a table row that has more than two columns; this is used in
459 * conjunction with table_column_head() usually; it takes an array of
460 * values
461 *
462 * @param array Array of values in form value => alignment key (c for center, l for left, and r for right)
463 */
464 public function row_multi_item($row_array)
465 {
466 BSFunctions::SwapCssClasses();
467
468 foreach ($row_array AS $item => $align)
469 {
470 $row_data["$align"][] = $item;
471 }
472
473 echo "<tr valign=\"top\">";
474
475 foreach ($row_data AS $align_key => $item_array)
476 {
477 if ($align_key == 'c')
478 {
479 $align = 'center';
480 }
481 else if ($align_key == 'l')
482 {
483 $align = 'left';
484 }
485 else if ($align_key == 'r')
486 {
487 $align = 'right';
488 }
489
490 foreach ($item_array AS $value)
491 {
492 echo "\n\t<td class=\"" . BSFunctions::$cssClass . "\" align=\"$align\">$value</td>";
493 }
494 }
495
496 echo "\n</tr>\n";
497 }
498
499 // ###################################################################
500 /**
501 * Generic row creation function that has two columns: label and value;
502 * this is used for many other form functions, but can also be used for
503 * non-editable fields
504 *
505 * @param string Label text
506 * @param string HTML or text to place in the value column
507 * @param string Vertical align (valign attribute) for the row
508 * @param integer Colspan attribute
509 * @param string Class to style the row with; default is to alternate
510 */
511 public function row_text($label, $value = '&nbsp;', $valign = 'top', $colspan = 2, $class = -1)
512 {
513 global $IS_SETTINGS;
514
515 if ($class == -1)
516 {
517 if (!$IS_SETTINGS)
518 {
519 BSFunctions::SwapCssClasses();
520 $row_class = BSFunctions::$cssClass;
521 }
522 else
523 {
524 $row_class = 'alt2';
525 }
526 }
527 else
528 {
529 $row_class = $class;
530 }
531
532 echo "<tr valign=\"$valign\">";
533 echo "\n\t<td class=\"$row_class\">$label</td>";
534 echo "\n\t<td class=\"$row_class\" colspan=\"" . ($colspan - 1) . "\">$value</td>";
535
536 echo "\n</tr>\n";
537 }
538
539 // ###################################################################
540 /**
541 * Creates a table row with a <textarea> as the value column
542 *
543 * @param string Label text
544 * @param string Name of the <textarea>
545 * @param string Value of the <textarea>
546 * @param integer Colspan attribute
547 * @param integer Number of rows in the <textarea>
548 * @param integer Number of colums in the <textarea>
549 * @param bool Whether or not to use monospacing font
550 * @param string Extra style attributes to apply to the <textarea>
551 */
552 public function row_textarea($label, $name, $value = '', $colspan = 2, $rows = 7, $cols = 50, $code = false, $style = '')
553 {
554 $this->row_text($label, "<textarea name=\"$name\" class=\"" . ($code ? 'code' : 'input') . "\" rows=\"$rows\" cols=\"$cols\"" . ($style ? ' style="' . $style . '"' : '') . ">$value</textarea>", 'top', $colspan);
555 }
556
557 // ###################################################################
558 /**
559 * Creates an upload row; you need to specify some other paramaters in
560 * form_start() for this to work
561 *
562 * @param string Label text
563 * @param string Upload name
564 * @param integer Colspan attribute
565 */
566 public function row_upload($label, $name, $colspan = 2)
567 {
568 $this->row_text($label, "<input type=\"file\" class=\"button\" name=\"$name\" size=\"35\" />", 'top', $colspan);
569 }
570
571 // ###################################################################
572 /**
573 * Adds a name-value pair to an array that is constructed into a
574 * <select> list
575 *
576 * @param string Text displayed for the option
577 * @param string Value of the option
578 * @param bool Whether or not to select this particluar option
579 */
580 public function list_item($name, $value, $selected = false)
581 {
582 global $listitem;
583
584 $listitem["$value"] = array('name' => $name, 'selected' => $selected);
585 }
586
587 // ###################################################################
588 /**
589 * Assembles a <select> table row from list_item() items
590 *
591 * @param string Label text
592 * @param string Name of the <select>
593 * @param bool Automatically submit the form on a change?
594 * @param integer Colspan attribute
595 */
596 public function row_list($label, $name, $is_jump = false, $colspan = 2)
597 {
598 global $listitem;
599
600 foreach ($listitem AS $value => $option)
601 {
602 $optionlist .= "\n\t<option value=\"$value\"" . ($option['selected'] == true ? ' selected="selected"' : '') . ">$option[name]</option>";
603 }
604
605 $listitem = array();
606
607 $this->row_text($label, "\n<select class=\"button\" name=\"$name\"" . ($is_jump ? " onchange=\"this.form.submit();\"" : '') . ">$optionlist\n</select>" . ($is_jump ? "\n<input type=\"submit\" class=\"button\" value=\" " . _('Go') . " \" accesskey=\"g\" />" : '') . "\n", $colspan);
608 }
609
610 // ###################################################################
611 /**
612 * Assembles a list of checkboxes from list_item() items
613 *
614 * @param string Label text
615 * @param string Name of the <input>s
616 * @param integer Colspan attribute
617 */
618 public function row_checkbox($label, $name, $colspan = 2)
619 {
620 global $listitem;
621
622 foreach ($listitem AS $value => $box)
623 {
624 $optionlist[] = "\n\t<input type=\"checkbox\" class=\"button\" name=\"{$name}[]\" value=\"$value\"" . ($box['selected'] == true ? ' checked="checked"' : '') . " /> $box[name]";
625 }
626
627 $listitem = array();
628
629 $this->row_text($label, "\n" . implode('<br />', $optionlist) . "\n", $colspan);
630 }
631
632 // ###################################################################
633 /**
634 * Creates a row with two radio buttons: yes and now
635 *
636 * @param string Label text
637 * @param string Name of the BOOL flag
638 * @param bool TRUE to select the YES by default; FALSE for NO
639 * @param integer Colspan attribute
640 */
641 public function row_yesno($label, $name, $value, $colspan = 2)
642 {
643 $this->row_text($label, "<input type=\"radio\" name=\"$name\" value=\"1\"" . ($value ? ' checked="checked"' : '') . " /> " . _('Yes') . " <input type=\"radio\" name=\"$name\" value=\"0\"" . (!$value ? ' checked="checked"' : '') . " /> " . _('No'), $colspan);
644 }
645 }
646
647 /*=====================================================================*\
648 || ###################################################################
649 || # $HeadURL$
650 || # $Id$
651 || ###################################################################
652 \*=====================================================================*/
653 ?>