Documenting hooks
[isso.git] / printer.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 /**
30 * Printer
31 *
32 * This framework generates standard HTML through various functions. The purpose
33 * is generally so that things like the admin system can be created without templates.
34 *
35 * Constants:
36 * ISSO_PRINTER_DONE_HEADER - An internal constant that is used to check to see
37 * if the page header has already been printed
38 * ISSO_PRINTER_HIDE_SETUP - Will stop the page footer data (copyright and debug
39 * box) from being printed
40 *
41 * Hooks:
42 * _printer_page_start() - Define function to echo() data after the page header
43 * has been outputted
44 *
45 * @author Iris Studios, Inc.
46 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
47 * @version $Revision$
48 * @package ISSO
49 *
50 */
51 class Printer
52 {
53 /**
54 * Framework registry object
55 * @var object
56 */
57 var $registry = null;
58
59 /**
60 * Realm that we are operating in (displayed in the <title>)
61 * @var string
62 */
63 var $realm = '[UNDEFINED REALM]';
64
65 /**
66 * CSS to place in the page
67 * @var string
68 */
69 var $css = '';
70
71 // ###################################################################
72 /**
73 * Constructor
74 */
75 function __construct(&$registry)
76 {
77 $this->registry =& $registry;
78 }
79
80 // ###################################################################
81 /**
82 * (PHP 4) Constructor
83 */
84 function Printer(&$registry)
85 {
86 $this->__construct($registry);
87 }
88
89 // ###################################################################
90 /**
91 * Creates a redirect to another page; constructs the header and footer
92 * (therefore execution stops)
93 *
94 * @access public
95 *
96 * @param string Location to redirect to
97 * @param integer Time to wait before the redirect
98 * @param array An aray of POST variables to send through on the redirect
99 */
100 function redirect($location, $timeout = 10, $postvars = array())
101 {
102 $timeout = $timeout * 200;
103
104 if ($postvars)
105 {
106 $js = <<<JS
107 <script type="text/javascript">
108 <!--
109 var timeout = $timeout;
110
111 if (timeout > 0)
112 {
113 setTimeout("redirect()", $timeout);
114 }
115 else
116 {
117 redirect();
118 }
119
120 function redirect()
121 {
122 document.forms.postvars.submit();
123 }
124
125 //-->
126 </script>
127 JS;
128 }
129 else
130 {
131 $js = <<<JS
132 <script type="text/javascript">
133 <!--
134 var timeout = $timeout;
135
136 if (timeout > 0)
137 {
138 setTimeout("redirect()", $timeout);
139 }
140 else
141 {
142 redirect();
143 }
144
145 function redirect()
146 {
147 window.location = "$location";
148 }
149 //-->
150 </script>
151 JS;
152 }
153
154 $this->page_start($this->registry->modules['localize']->string('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 $this->page_message($this->registry->modules['localize']->string('Redirect'), sprintf($this->registry->modules['localize']->string('Please wait to be redirected. If you are not redirected in a few seconds, click <a href="%1$s">here</a>.'), $location));
169
170 $this->page_code($js);
171
172 $this->page_end();
173 }
174
175 // ###################################################################
176 /**
177 * Throws a fatal error; constructs the header and footer
178 *
179 * @access public
180 *
181 * @param string Error messsage text
182 */
183 function error($message)
184 {
185 $this->page_start($this->registry->modules['localize']->string('Error'));
186 $this->page_message($this->registry->modules['localize']->string('Error'), $message);
187 $this->page_end();
188
189 exit;
190 }
191
192 // ###################################################################
193 /**
194 * Outputs the header of the page: doctype, <html>, <head>, <title>,
195 * <body> and imbeds the style information
196 *
197 * @access public
198 *
199 * @param string Title of the page
200 * @param string Class of the page to be applied to the body
201 * @param integer Margin of the <div> that all content is placed in
202 * @param string Extra HTML to imbed in the <head> tag
203 * @param string <body> onLoad action to imbed
204 * @param integer Margin of the actual <body > tag
205 * @param string Relative path where the CSS data is stored
206 * @param bool Will force re-print the header if it has already been printed
207 */
208 function page_start($actiontitle, $pageclass = ':default:', $pagemargin = 15, $extra = '', $onload = false, $margin = 0, $dotpath = '.', $override = false)
209 {
210 if ($this->registry->debug AND isset($_GET['query']))
211 {
212 ob_start();
213 }
214
215 if (defined('ISSO_PRINTER_DONE_HEADER') AND !$override)
216 {
217 if (constant('ISSO_PRINTER_DONE_HEADER') AND !$override)
218 {
219 return;
220 }
221 }
222
223 $title = sprintf($this->registry->modules['localize']->string('%1$s - %2$s - %3$s'), $this->registry->application, $this->realm, $actiontitle);
224
225 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
226 echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>";
227 echo "\n\t<title>$title</title>";
228 echo "\n\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
229 echo $this->css;
230 echo ($extra ? "\n$extra" : '');
231 echo "\n</head>\n<body style=\"margin: {$margin}px;\"" . (($pageclass !== ':default:') ? " class=\"$pageclass\"" : '') . (($onload) ? " onload=\"$onload\"" : '') . ">\n";
232
233 if (!defined('ISSO_PRINTER_HIDE_SETUP') AND function_exists('_printer_page_start'))
234 {
235 _printer_page_start();
236 }
237
238 echo "<div style=\"margin: {$pagemargin}px;\">\n<!-- / page head -->\n\n";
239
240 if (!defined('ISSO_PRINTER_DONE_HEADER'))
241 {
242 define('ISSO_PRINTER_DONE_HEADER', 1);
243 }
244 }
245
246 // ###################################################################
247 /**
248 * Links CSS to the page from a given relative path
249 *
250 * @access public
251 *
252 * @param string Relative path to the CSS file
253 */
254 function css_link($path)
255 {
256 $this->css .= "\n\t<link rel=\"stylesheet\" href=\"$path\" />";
257 }
258
259 // ###################################################################
260 /**
261 * Imbeds actual CSS information into the page in <style> tags
262 *
263 * @access public
264 *
265 * @param string Path of the CSS file to be imbeded
266 */
267 function css_imbed($path)
268 {
269 $data = require_once($path);
270 $css = preg_replace('#/\*(.*?)\*/(\r|\n)*#s', '', $css);
271 $css = trim($css);
272 $this->css .= "\n\t<style type=\"text/css\">\n\t<!--\n" . $css . "\n\t//-->\n\t</style>";
273 }
274
275 // ###################################################################
276 /**
277 * Places raw HTML code directly into the documet at the current
278 * position
279 *
280 * @access public
281 *
282 * @param string HTML code
283 */
284 function page_code($code)
285 {
286 echo "\n\n$code\n\n";
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 * @access public
295 *
296 * @param string The title of the message (appears at the top of the block)
297 * @param string Content of the message
298 */
299 function page_message($title, $message)
300 {
301 $this->table_start(true, '75%');
302 $this->table_head($title, 1);
303 $this->row_span("<blockquote>$message</blockquote>", ':swap:', 'left', 1);
304 $this->table_end();
305 }
306
307 // ###################################################################
308 /**
309 * Produces an entire page layout that asks the user whether or not
310 * they want to perform X action and provides a link to the YES and NO
311 * action
312 *
313 * @access public
314 *
315 * @param string Message that asks if they want to do X
316 * @param string Location to go for YES
317 * @param string DO action to pass
318 * @param array Hidden parameters to pass to the next page
319 */
320 function page_confirm($message, $location, $action, $params)
321 {
322 $this->page_start($this->registry->modules['localize']->string('Confirm'));
323
324 $this->form_start($location, $action);
325 foreach ($params AS $key => $value)
326 {
327 $this->form_hidden_field($key, $value);
328 }
329
330 $this->table_start(true, '75%');
331 $this->table_head($this->registry->modules['localize']->string('Confirm'), 1);
332 $this->row_span("<blockquote>$message</blockquote>", ':swap:', 'left', 1);
333 $this->row_submit('<input type="button" class="button" name="no" value=" ' . $this->registry->modules['localize']->string('No') . ' " onclick="history.back(1); return false;" />', $this->registry->modules['localize']->string('Yes'), '');
334 $this->table_end();
335
336 $this->form_end();
337
338 $this->page_end();
339 }
340
341 // ###################################################################
342 /**
343 * Closes the HTML document structure an adds the copyright; this also
344 * stops execution of the page
345 *
346 * @access public
347 */
348 function page_end()
349 {
350 if ($this->registry->debug AND isset($_GET['query']))
351 {
352 ob_clean();
353 ob_end_clean();
354
355 if (is_array($this->registry->modules[ISSO_DB_LAYER]->history))
356 {
357 echo '<pre>';
358 foreach ($this->registry->modules[ISSO_DB_LAYER]->history AS $query)
359 {
360 echo $query . "\n\n<hr />\n\n";
361 }
362 echo '</pre>';
363 }
364 exit;
365 }
366
367 $copyright = "\n<br />\n<p align=\"center\" class=\"copyright\">\n\t<a href=\"http://www.iris-studios.com\" target=\"_blank\">" . $this->registry->application . ' ' . $this->registry->appversion . ", &copy; 2002 - " . date('Y') . " Iris Studios, Inc.</a>\n</p>";
368
369 if (!defined('ISSO_PRINTER_HIDE_SETUP'))
370 {
371 echo "\n<!-- page end -->\n</div>\n$copyright";
372 echo $this->registry->construct_debug_block(false);
373 }
374 else
375 {
376 echo "\n<!-- page end -->\n</div>";
377 }
378
379 echo "\n\n</body>\n</html>";
380
381 exit;
382 }
383
384 // -------------------------------------------------------------------
385
386 // ###################################################################
387 /**
388 * Opens a <table> tag with styling
389 *
390 * @access public
391 *
392 * @param bool Whether to add a <br /> before the table
393 * @param string Value of the width attribute
394 */
395 function table_start($break = true, $width = '90%')
396 {
397 if ($break)
398 {
399 echo '<br />';
400 }
401
402 echo "\n<table cellpadding=\"4\" cellspacing=\"0\" border=\"0\" align=\"center\" width=\"$width\" class=\"tborder\">\n";
403 }
404
405 // ###################################################################
406 /**
407 * Adds a table row that is sued to head the entire table
408 *
409 * @access public
410 *
411 * @param string Title string
412 * @param integer Colspan attribute value
413 * @param bool Whether to bold the title
414 */
415 function table_head($title, $colspan = 2, $strong = true)
416 {
417 echo "<tr>\n\t<td class=\"tcat\" align=\"center\" colspan=\"$colspan\">" . (($strong) ? "<strong>$title</strong>" : $title) . "</td>\n</tr>\n";
418 }
419
420 // ###################################################################
421 /**
422 * Creates column headings; useful for a grid-style page. This uses a
423 * different styling than table_head() and is usually used directly
424 * after a table header.
425 *
426 * @access public
427 *
428 * @param array Array of titles to print
429 */
430 function table_column_head($columnarray)
431 {
432 if (is_array($columnarray))
433 {
434 $render = "<tr valign=\"top\" align=\"center\">\n";
435
436 foreach ($columnarray AS $header)
437 {
438 $render .= "\t<td class=\"thead\" align=\"center\">$header</td>\n";
439 }
440
441 $render .= "</tr>\n";
442
443 echo $render;
444 }
445 }
446
447 // ###################################################################
448 /**
449 * Closes a <table> tag
450 *
451 * @access public
452 */
453 function table_end()
454 {
455 echo "\n</table>\n";
456 }
457
458 // -------------------------------------------------------------------
459
460 // ###################################################################
461 /**
462 * Starts a <form> tag and adds the DO hidden input field
463 *
464 * @access public
465 *
466 * @param string Action/name of the file to action to
467 * @param string Value of the DO parameter; used to do-branch
468 * @param bool Enctype attribute; used for mime/multi-part
469 * @param string Name of the form; this only matters for DOM access
470 * @param string Method to action as; POST or GET (default is POST)
471 */
472 function form_start($action, $do, $enctype = false, $name = 'inputform', $submitmethod = 'post')
473 {
474 echo "\n<!-- input form -->\n<form name=\"$name\" action=\"$action\"" . (($enctype) ? " enctype=\"$enctype\"" : '') . " method=\"$submitmethod\">\n";
475
476 if ($do !== null)
477 {
478 $this->form_hidden_field('do', $do);
479 }
480 }
481
482 // ###################################################################
483 /**
484 * Adds a hidden field at the current location
485 *
486 * @access public
487 *
488 * @param string Name of the field
489 * @param string Value of the field
490 */
491 function form_hidden_field($name, $value)
492 {
493 echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />\n";
494 }
495
496 // ###################################################################
497 /**
498 * Closes a <form> tag
499 *
500 * @access public
501 */
502 function form_end()
503 {
504 echo "</form>\n<!-- / input form -->\n";
505 }
506
507 // -------------------------------------------------------------------
508
509 // ###################################################################
510 /**
511 * Creates a table row that spans an entire row; this is used to divide
512 * sections, usually
513 *
514 * @access public
515 *
516 * @param string Text to place in the row
517 * @param string Class name to style with; by default it alternates between alt1 and alt2 (use :swap: to do that)
518 * @param string Alignment of the text in the row
519 * @param integer Colspan attribute
520 */
521 function row_span($text, $class = ':swap:', $align = 'left', $colspan = 2)
522 {
523 if ($class === ':swap:')
524 {
525 $this->registry->modules['functions']->exec_swap_bg();
526 $row_class = $this->registry->modules['functions']->bgcolour;
527 $is_style_element = false;
528 }
529 else
530 {
531 if (preg_match('#:style:(.*?)#i', $class))
532 {
533 $is_style_element = true;
534 $style = str_replace(':style:', '', $class);
535 }
536 else
537 {
538 $row_class = $class;
539 $is_style_element = false;
540 }
541 }
542
543 echo "\n<tr>\n\t<td ". (($is_style_element) ? "style=\"$style\"" : "class=\"$row_class\"") . " colspan=\"$colspan\" align=\"$align\">$text</td>\n</tr>";
544 }
545
546 // ###################################################################
547 /**
548 * Creates a table row that has more than two columns; this is used in
549 * conjunction with table_column_head() usually; it takes an array of
550 * values
551 *
552 * @access public
553 *
554 * @param array Array of values in form value => alignment key (c for center, l for left, and r for right)
555 */
556 function row_multi_item($row_array)
557 {
558 $this->registry->modules['functions']->exec_swap_bg();
559
560 foreach ($row_array AS $item => $align)
561 {
562 $row_data["$align"][] = $item;
563 }
564
565 echo "<tr valign=\"top\">";
566
567 foreach ($row_data AS $align_key => $item_array)
568 {
569 if ($align_key == 'c')
570 {
571 $align = 'center';
572 }
573 else if ($align_key == 'l')
574 {
575 $align = 'left';
576 }
577 else if ($align_key == 'r')
578 {
579 $align = 'right';
580 }
581
582 foreach ($item_array AS $value)
583 {
584 echo "\n\t<td class=\"{$this->registry->modules['functions']->bgcolour}\" align=\"$align\">$value</td>";
585 }
586 }
587
588 echo "\n</tr>\n";
589 }
590
591 // ###################################################################
592 /**
593 * Generic row creation function that has two columns: label and value;
594 * this is used for many other form functions, but can also be used for
595 * non-editable fields
596 *
597 * @access public
598 *
599 * @param string Label text
600 * @param string HTML or text to place in the value column
601 * @param string Vertical align (valign attribute) for the row
602 * @param integer Colspan attribute
603 * @param string Class to style the row with; default is to alternate
604 */
605 function row_text($label, $value = '&nbsp;', $valign = 'top', $colspan = 2, $class = -1)
606 {
607 global $IS_SETTINGS;
608
609 if ($class == -1)
610 {
611 if (!$IS_SETTINGS)
612 {
613 $this->registry->modules['functions']->exec_swap_bg();
614 $row_class = $this->registry->modules['functions']->bgcolour;
615 }
616 else
617 {
618 $row_class = 'alt2';
619 }
620 }
621 else
622 {
623 $row_class = $class;
624 }
625
626 echo "<tr valign=\"$valign\">";
627 echo "\n\t<td class=\"$row_class\">$label</td>";
628 echo "\n\t<td class=\"$row_class\">$value</td>";
629
630 if ($colspan > 2)
631 {
632 echo "\n\t<td class=\"$row_class\" colspan=\"" . $colspan - 2 . "\">&nbsp;</td>";
633 }
634
635 echo "\n</tr>\n";
636 }
637
638 // ###################################################################
639 /**
640 * Creates a table row with an <input> text field as the value column
641 *
642 * @access public
643 *
644 * @param string Label text
645 * @param string Name of the <input> field
646 * @param string Value of the field
647 * @param integer Colspan attribute
648 * @param integer Size of the <input> field
649 * @param integer Length attribute; use FALSE for no length to be specified
650 * @param bool Whether to make this a password field
651 * @param string Vertical align (valign attribute)
652 */
653 function row_input($label, $name, $value = '', $colspan = 2, $size = 35, $length = false, $password = false, $lalign = 'top')
654 {
655 $this->row_text($label, "<input type=\"" . (($password) ? 'password' : 'text') . "\" class=\"input\" name=\"$name\" value=\"$value\" size=\"$size\" " . (($length) ? "maxlength=\"$length\" " : '') . "/>", $lalign, $colspan);
656 }
657
658 // ###################################################################
659 /**
660 * Creates a table row with a <textarea> as the value column
661 *
662 * @access public
663 *
664 * @param string Label text
665 * @param string Name of the <textarea>
666 * @param string Value of the <textarea>
667 * @param integer Colspan attribute
668 * @param integer Number of rows in the <textarea>
669 * @param integer Number of colums in the <textarea>
670 * @param bool Whether or not to use monospacing font
671 * @param string Extra style attributes to apply to the <textarea>
672 */
673 function row_textarea($label, $name, $value = '', $colspan = 2, $rows = 7, $cols = 50, $code = false, $style = '')
674 {
675 $this->row_text($label, "<textarea name=\"$name\" class=\"" . (($code) ? 'code' : 'input') . "\" rows=\"$rows\" cols=\"$cols\"" . (($style) ? ' style="' . $style . '"' : '') . ">$value</textarea>", 'top', $colspan);
676 }
677
678 // ###################################################################
679 /**
680 * Creates a table row with the tfoot class
681 *
682 * @access public
683 *
684 * @param string Extra text or HTML to insert into the row
685 * @param integer Colspan attribute
686 */
687 function row_tfoot($data, $colspan = 2)
688 {
689 echo $this->row_span($data, 'tfoot', 'center', $colspan);
690 }
691
692 // ###################################################################
693 /**
694 * Creates a tfoot table row with submit buttons
695 *
696 * @access public
697 *
698 * @param string Extra HTML to imbed in the row after the buttons
699 * @param string Submit button text (by default it uses pre-translated "Submit" from :save:)
700 * @param string Reset button text (default it uses pre-translated "Reset" from :reset:)
701 * @param integer Colspan attribute
702 */
703 function row_submit($extra = false, $submit = ':save:', $reset = ':reset:', $colspan = 2)
704 {
705 if ($submit === ':save:')
706 {
707 $submit = " " . $this->registry->modules['localize']->string('Submit') . " ";
708 }
709 else
710 {
711 $submit = " $submit ";
712 }
713
714 if ($reset === ':reset:')
715 {
716 $reset = " " . $this->registry->modules['localize']->string('Reset') . " ";
717 }
718 else
719 {
720 $reset = (($reset) ? " $reset " : '');
721 }
722
723 $output = "\n\t\t<input type=\"submit\" class=\"button\" name=\"__submit__\" value=\"$submit\" accesskey=\"s\" />";
724 $output .= ($reset ? "\n\t\t<input type=\"reset\" class=\"button\" name=\"__reset__\" value=\"$reset\" accesskey=\"r\" />" : '');
725 $output .= ($extra ? "\n\t\t$extra" : '');
726 $output .= "\n\t";
727 $this->row_tfoot($output, $colspan);
728 }
729
730 // ###################################################################
731 /**
732 * Creates an upload row; you need to specify some other paramaters in
733 * form_start() for this to work
734 *
735 * @access public
736 *
737 * @param string Label text
738 * @param string Upload name
739 * @param integer Colspan attribute
740 */
741 function row_upload($label, $name, $colspan = 2)
742 {
743 $this->row_text($label, "<input type=\"file\" class=\"button\" name=\"$name\" size=\"35\" />", 'top', $colspan);
744 }
745
746 // ###################################################################
747 /**
748 * Adds a name-value pair to an array that is constructed into a
749 * <select> list
750 *
751 * @access public
752 *
753 * @param string Text displayed for the option
754 * @param string Value of the option
755 * @param bool Whether or not to select this particluar option
756 */
757 function list_item($name, $value, $selected = false)
758 {
759 global $listitem;
760
761 $listitem[] = "\n\t<option value=\"$value\"" . (($selected == true) ? ' selected="selected"' : '') . ">$name</option>";
762 }
763
764 // ###################################################################
765 /**
766 * Assembles a <select> table row from list_item() items
767 *
768 * @access public
769 *
770 * @param string Label text
771 * @param string Name of the <select>
772 * @param bool Automatically submit the form on a change?
773 * @param integer Colspan attribute
774 */
775 function row_list($label, $name, $is_jump = false, $colspan = 2)
776 {
777 global $listitem;
778
779 foreach ($listitem AS $option)
780 {
781 $optionlist .= $option;
782 }
783
784 $listitem = '';
785
786 $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=\" " . $this->registry->modules['localize']->string('Go') . " \" accesskey=\"g\" />" : '') . "\n", $colspan);
787 }
788
789 // ###################################################################
790 /**
791 * Creates a row with two radio buttons: yes and now
792 *
793 * @access public
794 *
795 * @param string Label text
796 * @param string Name of the BOOL flag
797 * @param bool TRUE to select the YES by default; FALSE for NO
798 * @param integer Colspan attribute
799 */
800 function row_yesno($label, $name, $value, $colspan = 2)
801 {
802 $this->row_text($label, "<input type=\"radio\" name=\"$name\" value=\"1\"" . (($value) ? ' checked="checked"' : '') . " /> " . $this->registry->modules['localize']->string('Yes') . " <input type=\"radio\" name=\"$name\" value=\"0\"" . ((!$value) ? ' checked="checked"' : '') . " /> " . $this->registry->modules['localize']->string('No'), $colspan);
803 }
804 }
805
806 /*=====================================================================*\
807 || ###################################################################
808 || # $HeadURL$
809 || # $Id$
810 || ###################################################################
811 \*=====================================================================*/
812 ?>