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