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