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