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