<?php
/*=====================================================================*\
|| ###################################################################
|| # Blue Static ISSO Framework
|| # Copyright ©2002-[#]year[#] Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version [#]gpl[#] of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/
/**
* Printer
* printer.php
*
* @package ISSO
*/
/**
* Printer
*
* This framework generates standard HTML through various functions. The purpose
* is generally so that things like the admin system can be created without templates.
*
* Constants:
* ISSO_PRINTER_DONE_HEADER - An internal constant that is used to check to see
* if the page header has already been printed
* ISSO_PRINTER_HIDE_SETUP - Will stop the page footer data (copyright and debug
* box) from being printed
* ISSO_PRINTER_NO_NAVIGATION - Do not show the navigation frame from ISSO.Printer.Navigation
*
* @author Blue Static
* @copyright Copyright ©2002 - [#]year[#], Blue Static
* @version $Revision$
* @package ISSO
*
*/
class Printer
{
/**
* Framework registry object
* @var object
* @access private
*/
var $registry = null;
/**
* Realm that we are operating in (displayed in the <title>)
* @var string
* @access private
*/
var $realm = '[UNDEFINED REALM]';
/**
* CSS to place in the page
* @var string
* @access private
*/
var $css = '';
/**
* Extra code to place
* @var sring
* @access private
*/
var $code = '';
/**
* Page-start hooko
* @var string
* @access private
*/
var $page_start_hook = ':=NO METHOD=:';
/**
* Language information array: ('langcode' =>, 'direction' =>, 'charset' =>)
* @var array
* @access private
*/
var $language = array('langcode' => 'en_US', 'direction' => 'ltr', 'charset' => 'utf-8');
// ###################################################################
/**
* Constructor
*/
function __construct(&$registry)
{
$this->registry =& $registry;
}
// ###################################################################
/**
* (PHP 4) Constructor
*/
function Printer(&$registry)
{
$this->__construct($registry);
}
// ###################################################################
/**
* Sets the realm
*
* @access public
*
* @param string Realm
*/
function setRealm($realm)
{
$this->realm = $realm;
}
// ###################################################################
/**
* Gets the realm
*
* @access public
*
* @return string Realm
*/
function getRealm()
{
return $this->realm;
}
// ###################################################################
/**
* Gets the language array information
*
* @access public
*
* @return array Language array
*/
function getLanguageInformation()
{
return $this->language;
}
// ###################################################################
/**
* Sets the language array information
*
* @access public
*
* @param array Language array
*/
function setLanguageInformation($lang)
{
$this->language = $lang;
}
// ###################################################################
/**
* Creates a redirect to another page; constructs the header and footer
* (therefore execution stops)
*
* @access public
*
* @param string Location to redirect to
* @param string Redirect message to be shown
* @param array An aray of POST variables to send through on the redirect
*/
function redirect($location, $message = null, $postvars = array())
{
$timeout = 10 * 200;
if ($postvars)
{
$js = <<<JS
<script type="text/javascript">
<!--
var timeout = $timeout;
if (timeout > 0)
{
setTimeout("redirect()", $timeout);
}
else
{
redirect();
}
function redirect()
{
document.forms.postvars.submit();
}
//-->
</script>
JS;
}
else
{
$js = <<<JS
<script type="text/javascript">
<!--
var timeout = $timeout;
if (timeout > 0)
{
setTimeout("redirect()", $timeout);
}
else
{
redirect();
}
function redirect()
{
window.location = "$location";
}
//-->
</script>
JS;
}
if (!defined('ISSO_PRINTER_NO_NAVIGATION'))
{
define('ISSO_PRINTER_NO_NAVIGATION', 1);
}
$this->page_start(T('Redirect'));
if ($postvars)
{
$this->form_start($location, null, false, 'postvars');
foreach ($postvars AS $key => $value)
{
$this->form_hidden_field($key, $value);
}
$this->form_end();
}
$redir = sprintf(T('Please wait to be redirected. If you are not redirected in a few seconds, click <a href="%1$s">here</a>.'), $location);
$override = false;
if ($message == null)
{
$showmessage = $redir;
}
else
{
$showmessage = '<blockquote>' . $message . '</blockquote>';
$showmessage .= "\n<p>" . $redir . "</p>";
$override = true;
}
$this->page_message(T('Redirect'), $showmessage, $override);
$this->page_code($js);
$this->page_end();
}
// ###################################################################
/**
* Throws a fatal error; constructs the header and footer
*
* @access public
*
* @param string Error messsage text
*/
function error($message)
{
if (!defined('ISSO_PRINTER_NO_NAVIGATION'))
{
define('ISSO_PRINTER_NO_NAVIGATION', 1);
}
$this->page_start(T('Error'));
$this->page_message(T('Error'), $message);
$this->page_end();
exit;
}
// ###################################################################
/**
* Outputs the header of the page: doctype, <html>, <head>, <title>,
* <body> and imbeds the style information
*
* @access public
*
* @param string Title of the page
* @param string Class of the page to be applied to the body
* @param string Extra HTML to imbed in the <head> tag
* @param string <body> onLoad action to imbed
*/
function page_start($actiontitle, $pageclass = null, $extra = '', $onload = false)
{
if ($this->registry->debug AND isset($_GET['query']))
{
ob_start();
}
if (defined('ISSO_PRINTER_DONE_HEADER') AND constant('ISSO_PRINTER_DONE_HEADER'))
{
return;
}
$title = sprintf(T('%1$s - %2$s - %3$s'), $this->registry->getApplication(), $this->realm, $actiontitle);
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
echo "<html xml:lang=\"" . $this->language['langcode'] . "\" lang=\"" . $this->language['langcode'] . "\" dir=\"" . $this->language['direction'] . "\">\n<head>";
echo "\n\t<title>$title</title>";
echo "\n\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" . $this->language['charset'] . "\" />";
echo $this->css;
echo $this->code;
echo ($extra ? "\n$extra" : '');
echo "\n</head>\n<body" . ($pageclass != null ? " class=\"$pageclass\"" : '') . ($onload ? " onload=\"$onload\"" : '') . ">\n";
if ($this->registry->is_loaded('printer_navigation') AND (!defined('ISSO_PRINTER_NO_NAVIGATION') OR (defined('ISSO_PRINTER_NO_NAVIGATION') AND constant('ISSO_PRINTER_NO_NAVIGATION') != true)))
{
echo $this->registry->modules['printer_navigation']->generate_header_html();
}
if (!defined('ISSO_PRINTER_DONE_HEADER'))
{
define('ISSO_PRINTER_DONE_HEADER', 1);
}
}
// ###################################################################
/**
* Links CSS to the page from a given relative path
*
* @access public
*
* @param string Relative path to the CSS file
*/
function css_link($path)
{
$this->css .= "\n\t<link rel=\"stylesheet\" href=\"$path\" />";
}
// ###################################################################
/**
* Imbeds actual CSS information into the page in <style> tags
*
* @access public
*
* @param string Path of the CSS file to be imbeded
*/
function css_imbed($path)
{
$data = require_once($path);
$css = preg_replace('#/\*(.*?)\*/(\r|\n)*#s', '', $css);
$css = trim($css);
$this->css .= "\n\t<style type=\"text/css\">\n\t<!--\n" . $css . "\n\t//-->\n\t</style>";
}
// ###################################################################
/**
* Places raw HTML code directly into the documet at the current
* position
*
* @access public
*
* @param string HTML code
*/
function page_code($code)
{
if (defined('ISSO_PRINTER_DONE_HEADER'))
{
echo "\n\n$code\n\n";
}
else
{
$this->code .= "\n\n$code\n\n";
}
}
// ###################################################################
/**
* A block function that produces a table with a message in it. This
* does not print the header and footer.
*
* @access public
*
* @param string The title of the message (appears at the top of the block)
* @param string Content of the message
* @param bool Override the message: control the entire output (no <blockquote>)?
*/
function page_message($title, $message, $overridemessage = false)
{
$this->table_start(true, '75%');
$this->table_head($title, 1);
$this->row_span(($overridemessage ? $message : "<blockquote>$message</blockquote>"), ':swap:', 'left', 1);
$this->table_end();
}
// ###################################################################
/**
* Produces an entire page layout that asks the user whether or not
* they want to perform X action and provides a link to the YES and NO
* action
*
* @access public
*
* @param string Message that asks if they want to do X
* @param string Location to go for YES
* @param string DO action to pass
* @param array Hidden parameters to pass to the next page
*/
function page_confirm($message, $location, $action, $params)
{
if (!defined('ISSO_PRINTER_NO_NAVIGATION'))
{
define('ISSO_PRINTER_NO_NAVIGATION', 1);
}
$this->page_start(T('Confirm'));
$this->form_start($location, $action);
foreach ($params AS $key => $value)
{
$this->form_hidden_field($key, $value);
}
$this->table_start(true, '75%');
$this->table_head(T('Confirm'), 1);
$this->row_span("<blockquote>$message</blockquote>", ':swap:', 'left', 1);
$this->row_submit('<input type="button" class="button" name="no" value=" ' . T('No') . ' " onclick="history.back(1); return false;" />', T('Yes'), '');
$this->table_end();
$this->form_end();
$this->page_end();
}
// ###################################################################
/**
* Closes the HTML document structure an adds the copyright; this also
* stops execution of the page
*
* @access public
*/
function page_end()
{
if ($this->registry->debug AND isset($_GET['query']))
{
ob_clean();
ob_end_clean();
if (is_array($this->registry->modules[ISSO_DB_LAYER]->history))
{
foreach ($this->registry->modules[ISSO_DB_LAYER]->history AS $query)
{
echo $this->registry->modules[ISSO_DB_LAYER]->construct_query_debug($query);
}
}
exit;
}
if ($this->registry->is_loaded('printer_navigation') AND (!defined('ISSO_PRINTER_NO_NAVIGATION') OR (defined('ISSO_PRINTER_NO_NAVIGATION') AND constant('ISSO_PRINTER_NO_NAVIGATION') != true)))
{
echo $this->registry->modules['printer_navigation']->generate_footer_html();
}
$copyright = "\n<br />\n<p align=\"center\" class=\"copyright\">\n\t<a href=\"http://www.bluestatic.org\" target=\"_blank\">" . $this->registry->getApplication() . ' ' . $this->registry->getAppVersion() . ", ©2002 - " . date('Y') . " Blue Static</a>\n</p>";
if (!defined('ISSO_PRINTER_HIDE_SETUP'))
{
echo "\n$copyright";
echo $this->registry->construct_debug_block(false);
}
echo "\n\n</body>\n</html>";
exit;
}
// -------------------------------------------------------------------
// ###################################################################
/**
* Opens a <table> tag with styling
*
* @access public
*
* @param bool Whether to add a <br /> before the table
* @param string Value of the width attribute
*/
function table_start($break = true, $width = '90%')
{
if ($break)
{
echo '<br />';
}
echo "\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" align=\"center\" width=\"$width\" class=\"tborder\">\n";
}
// ###################################################################
/**
* Adds a table row that is sued to head the entire table
*
* @access public
*
* @param string Title string
* @param integer Colspan attribute value
* @param bool Whether to bold the title
*/
function table_head($title, $colspan = 2, $strong = true)
{
echo "<tr>\n\t<td class=\"tcat\" align=\"center\" colspan=\"$colspan\">" . (($strong) ? "<strong>$title</strong>" : $title) . "</td>\n</tr>\n";
}
// ###################################################################
/**
* Creates column headings; useful for a grid-style page. This uses a
* different styling than table_head() and is usually used directly
* after a table header.
*
* @access public
*
* @param array Array of titles to print
*/
function table_column_head($columnarray)
{
if (is_array($columnarray))
{
$render = "<tr valign=\"top\" align=\"center\">\n";
foreach ($columnarray AS $header)
{
$render .= "\t<td class=\"thead\" align=\"center\">$header</td>\n";
}
$render .= "</tr>\n";
echo $render;
}
}
// ###################################################################
/**
* Closes a <table> tag
*
* @access public
*/
function table_end()
{
echo "\n</table>\n";
}
// -------------------------------------------------------------------
// ###################################################################
/**
* Starts a <form> tag and adds the DO hidden input field
*
* @access public
*
* @param string Action/name of the file to action to
* @param string Value of the DO parameter; used to do-branch
* @param bool Enctype attribute; used for mime/multi-part
* @param string Name of the form; this only matters for DOM access
* @param string Method to action as; POST or GET (default is POST)
*/
function form_start($action, $do, $enctype = false, $name = 'inputform', $submitmethod = 'post')
{
echo "\n<!-- input form -->\n<form name=\"$name\" action=\"$action\"" . (($enctype) ? " enctype=\"$enctype\"" : '') . " method=\"$submitmethod\">\n";
if ($do !== null)
{
$this->form_hidden_field('do', $do);
}
}
// ###################################################################
/**
* Adds a hidden field at the current location
*
* @access public
*
* @param string Name of the field
* @param string Value of the field
*/
function form_hidden_field($name, $value)
{
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />\n";
}
// ###################################################################
/**
* Closes a <form> tag
*
* @access public
*/
function form_end()
{
echo "</form>\n<!-- / input form -->\n";
}
// -------------------------------------------------------------------
// ###################################################################
/**
* Creates a table row that spans an entire row; this is used to divide
* sections, usually
*
* @access public
*
* @param string Text to place in the row
* @param string Class name to style with; by default it alternates between alt1 and alt2 (use :swap: to do that)
* @param string Alignment of the text in the row
* @param integer Colspan attribute
*/
function row_span($text, $class = ':swap:', $align = 'left', $colspan = 2)
{
if ($class === ':swap:')
{
$this->registry->modules['functions']->exec_swap_bg();
$row_class = $this->registry->modules['functions']->bgcolour;
$is_style_element = false;
}
else
{
if (preg_match('#:style:(.*?)#i', $class))
{
$is_style_element = true;
$style = str_replace(':style:', '', $class);
}
else
{
$row_class = $class;
$is_style_element = false;
}
}
echo "\n<tr>\n\t<td ". (($is_style_element) ? "style=\"$style\"" : "class=\"$row_class\"") . " colspan=\"$colspan\" align=\"$align\">$text</td>\n</tr>";
}
// ###################################################################
/**
* Creates a table row that has more than two columns; this is used in
* conjunction with table_column_head() usually; it takes an array of
* values
*
* @access public
*
* @param array Array of values in form value => alignment key (c for center, l for left, and r for right)
*/
function row_multi_item($row_array)
{
$this->registry->modules['functions']->exec_swap_bg();
foreach ($row_array AS $item => $align)
{
$row_data["$align"][] = $item;
}
echo "<tr valign=\"top\">";
foreach ($row_data AS $align_key => $item_array)
{
if ($align_key == 'c')
{
$align = 'center';
}
else if ($align_key == 'l')
{
$align = 'left';
}
else if ($align_key == 'r')
{
$align = 'right';
}
foreach ($item_array AS $value)
{
echo "\n\t<td class=\"{$this->registry->modules['functions']->bgcolour}\" align=\"$align\">$value</td>";
}
}
echo "\n</tr>\n";
}
// ###################################################################
/**
* Generic row creation function that has two columns: label and value;
* this is used for many other form functions, but can also be used for
* non-editable fields
*
* @access public
*
* @param string Label text
* @param string HTML or text to place in the value column
* @param string Vertical align (valign attribute) for the row
* @param integer Colspan attribute
* @param string Class to style the row with; default is to alternate
*/
function row_text($label, $value = ' ', $valign = 'top', $colspan = 2, $class = -1)
{
global $IS_SETTINGS;
if ($class == -1)
{
if (!$IS_SETTINGS)
{
$this->registry->modules['functions']->exec_swap_bg();
$row_class = $this->registry->modules['functions']->bgcolour;
}
else
{
$row_class = 'alt2';
}
}
else
{
$row_class = $class;
}
echo "<tr valign=\"$valign\">";
echo "\n\t<td class=\"$row_class\">$label</td>";
echo "\n\t<td class=\"$row_class\" colspan=\"" . ($colspan - 1) . "\">$value</td>";
echo "\n</tr>\n";
}
// ###################################################################
/**
* Creates a table row with an <input> text field as the value column
*
* @access public
*
* @param string Label text
* @param string Name of the <input> field
* @param string Value of the field
* @param integer Colspan attribute
* @param integer Size of the <input> field
* @param integer Length attribute; use FALSE for no length to be specified
* @param bool Whether to make this a password field
* @param string Vertical align (valign attribute)
*/
function row_input($label, $name, $value = '', $colspan = 2, $size = 35, $length = false, $password = false, $lalign = 'top')
{
$this->row_text($label, "<input type=\"" . (($password) ? 'password' : 'text') . "\" class=\"input\" name=\"$name\" value=\"$value\" size=\"$size\" " . (($length) ? "maxlength=\"$length\" " : '') . "/>", $lalign, $colspan);
}
// ###################################################################
/**
* Creates a table row with a <textarea> as the value column
*
* @access public
*
* @param string Label text
* @param string Name of the <textarea>
* @param string Value of the <textarea>
* @param integer Colspan attribute
* @param integer Number of rows in the <textarea>
* @param integer Number of colums in the <textarea>
* @param bool Whether or not to use monospacing font
* @param string Extra style attributes to apply to the <textarea>
*/
function row_textarea($label, $name, $value = '', $colspan = 2, $rows = 7, $cols = 50, $code = false, $style = '')
{
$this->row_text($label, "<textarea name=\"$name\" class=\"" . (($code) ? 'code' : 'input') . "\" rows=\"$rows\" cols=\"$cols\"" . (($style) ? ' style="' . $style . '"' : '') . ">$value</textarea>", 'top', $colspan);
}
// ###################################################################
/**
* Creates a table row with the tfoot class
*
* @access public
*
* @param string Extra text or HTML to insert into the row
* @param integer Colspan attribute
*/
function row_tfoot($data, $colspan = 2)
{
echo $this->row_span($data, 'tfoot', 'center', $colspan);
}
// ###################################################################
/**
* Creates a tfoot table row with submit buttons
*
* @access public
*
* @param string Extra HTML to imbed in the row after the buttons
* @param string Submit button text (by default it uses pre-translated "Submit" from :save:)
* @param string Reset button text (default it uses pre-translated "Reset" from :reset:)
* @param integer Colspan attribute
*/
function row_submit($extra = false, $submit = ':save:', $reset = ':reset:', $colspan = 2)
{
if ($submit === ':save:')
{
$submit = " " . T('Submit') . " ";
}
else
{
$submit = " $submit ";
}
if ($reset === ':reset:')
{
$reset = " " . T('Reset') . " ";
}
else
{
$reset = (($reset) ? " $reset " : '');
}
$output = "\n\t\t<input type=\"submit\" class=\"button\" name=\"__submit__\" value=\"$submit\" accesskey=\"s\" />";
$output .= ($reset ? "\n\t\t<input type=\"reset\" class=\"button\" name=\"__reset__\" value=\"$reset\" accesskey=\"r\" />" : '');
$output .= ($extra ? "\n\t\t$extra" : '');
$output .= "\n\t";
$this->row_tfoot($output, $colspan);
}
// ###################################################################
/**
* Creates an upload row; you need to specify some other paramaters in
* form_start() for this to work
*
* @access public
*
* @param string Label text
* @param string Upload name
* @param integer Colspan attribute
*/
function row_upload($label, $name, $colspan = 2)
{
$this->row_text($label, "<input type=\"file\" class=\"button\" name=\"$name\" size=\"35\" />", 'top', $colspan);
}
// ###################################################################
/**
* Adds a name-value pair to an array that is constructed into a
* <select> list
*
* @access public
*
* @param string Text displayed for the option
* @param string Value of the option
* @param bool Whether or not to select this particluar option
*/
function list_item($name, $value, $selected = false)
{
global $listitem;
$listitem["$value"] = array('name' => $name, 'selected' => $selected);
}
// ###################################################################
/**
* Assembles a <select> table row from list_item() items
*
* @access public
*
* @param string Label text
* @param string Name of the <select>
* @param bool Automatically submit the form on a change?
* @param integer Colspan attribute
*/
function row_list($label, $name, $is_jump = false, $colspan = 2)
{
global $listitem;
foreach ($listitem AS $value => $option)
{
$optionlist .= "\n\t<option value=\"$value\"" . ($option['selected'] == true ? ' selected="selected"' : '') . ">$option[name]</option>";
}
$listitem = array();
$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=\" " . T('Go') . " \" accesskey=\"g\" />" : '') . "\n", $colspan);
}
// ###################################################################
/**
* Assembles a list of checkboxes from list_item() items
*
* @access public
*
* @param string Label text
* @param string Name of the <input>s
* @param integer Colspan attribute
*/
function row_checkbox($label, $name, $colspan = 2)
{
global $listitem;
foreach ($listitem AS $value => $box)
{
$optionlist[] = "\n\t<input type=\"checkbox\" class=\"button\" name=\"{$name}[]\" value=\"$value\"" . ($box['selected'] == true ? ' checked="checked"' : '') . " /> $box[name]";
}
$listitem = array();
$this->row_text($label, "\n" . implode('<br />', $optionlist) . "\n", $colspan);
}
// ###################################################################
/**
* Creates a row with two radio buttons: yes and now
*
* @access public
*
* @param string Label text
* @param string Name of the BOOL flag
* @param bool TRUE to select the YES by default; FALSE for NO
* @param integer Colspan attribute
*/
function row_yesno($label, $name, $value, $colspan = 2)
{
$this->row_text($label, "<input type=\"radio\" name=\"$name\" value=\"1\"" . (($value) ? ' checked="checked"' : '') . " /> " . T('Yes') . " <input type=\"radio\" name=\"$name\" value=\"0\"" . ((!$value) ? ' checked="checked"' : '') . " /> " . T('No'), $colspan);
}
}
/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>