<?php
/*
**	$Id$
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2003 by the ITools Authors.
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of either the GNU General Public License
**	or the GNU Lesser General Public License, as published by the Free
**	Software Foundation. See http://www.gnu.org/licenses/ for details.
**
**	class it_html_form - Generic HTML Form
*/


/**
 * Functions to facilitate creation of HTML forms
 */
class it_html_form
{
	var	$name;		/* Name of form */

/**
 * Constructor: set action, name and method (echo <FORM> tag)
 * @param $action the action to be performed by this form or empty for link to self
 * @param $name form name (optional)
 * @param $method the method used for the action (defaults to 'post')
 * @param $target Target frame of the form (optional)
 */
function it_html_form($action='*', $name='', $method='post', $target='')
{
	if ($action == '*')
		$action = $_SERVER['PHP_SELF'];

	$this->name = $name;

        echo '<form method="'.$method.'" action="'.$action.'"';

	if ($target)
		echo " target=\"$target\"";

	if ($name)
		echo " name=\"$name\"";

	echo '>';
}


/**
 * Close the form (echo </FORM> tag)
 */
function close()
{
        echo "</form>";
}
     

/**
 * Add a parameter (as a hidden field)
 * @param $name parameter name
 * @param $value default (?) value
 * @param $return_output if true, output will be returned instaed of echoed
 */
function hidden($name, $value="", $return_output=false)
{
	$output = "<input type=\"hidden\" name=\"$name\" value=\"" . htmlspecialchars($value) . "\">";
	if ($return_output)
		return $output;
	else
		echo $output;
}


/**
 * Create a text field or a textarea if size is width:height
 * @param $name text field name
 * @param $size textfield width[:height] in chars
 * @param $maxlength maximum length of input
 * @param $value text field value
 */
function text($name, $size=8, $maxlength=255, $value="")
{
	if (strchr($size, ":"))
	{
		list($cols, $rows) = explode(":", $size);
		echo "<textarea name=\"$name\" rows=\"$rows\" cols=\"$cols\">".htmlspecialchars($value)."</textarea>";
	}
	else
		echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$maxlength\" value=\"".htmlspecialchars($value)."\">";
}


/**
 * Create a password field (masked characters)
 * @param $name text field name
 * @param $size textfield width[:height] in chars
 * @param $maxlength maximum length of input
 * @param $value text field value
 */
function password($name, $size=8, $maxlength=255, $value="")
{
	echo "<input type=\"password\" name=\"$name\" size=\"$size\" maxlength=\"$maxlength\" value=\"".htmlspecialchars($value)."\">";
}


/**
 * Create a dropdown menu object
 * @param $name dropdown field name
 * @param $options array (value => text) of available options or
 *                 string key:val{,key:val} where key will be rawurldecoded so it may contain %2C as comma
 * @param $selected currently selected value
 */
function select($name, $options, $selected='')
{
	if (!is_array($options))
	{
		$opts = explode(',', $options);
		$options = array();
		foreach($opts as $opt)
		{
			list($key, $value) = explode(':', $opt);
			$options[rawurldecode($key)] = $value;
		}
	}

	echo '<select name="'.$name.'">';
	foreach($options as $value => $text)
	{
		$sel = ($value == $selected) ? ' selected' : '';
		echo '<option value="'.htmlspecialchars($value).'"'.$sel.'>'.$text.'</option>';
	}
	echo '</select>';
}

 
/**
 * Create a filename field with browse button
 * @param $name file field name
 * @param $label label to be echoed before the field
 * @param $size file field width in chars
 * @param $maxlength maximum length of input
 * @param $value text field value
 */
function file($name, $label="", $size=16, $maxlength=255, $value="filename")
{
	echo $label."<input type=\"file\" name=\"$name\" size=\"$size\" maxlength=\"$maxlength\" value=\"".htmlspecialchars($value)."\">";
}

/**
 * Create a Submit button
 * @param $label button label
 * @param $name button name
 */
function submit($label="", $name="")
{
	if (!$label)
		$label = T('Button-Save');

	echo "<input type=\"submit\" value=\"$label\"";
	if ($name) echo " name=\"$name\"";
	echo ">";
}

} /* End class html_form */
?>