Location: PHPKode > projects > WB News > includes/lib/HTMLForm.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 22nd Dec 2006                           #||
||#     Filename: HTMLForm.php                           #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: HTMLForm.php,v 1.1.2.2 2008/06/25 10:29:42 pmcilwaine Exp $
 */

class HTMLForm
{

	var $_names = array();
	var $_ids = array();
	var $_msgs = array();

	var $width = 10;
	
	var $date_picker_width = 10;
	var $date_picker_img = "img/date.png";
	var $date_picker_format = "%d/%m/%Y";

	var $msgformat = "<br/><span class=\"field_error\">%s</span>";

	/**
	 * Nothing to do here, move along
	 */
	function HTMLForm() { }

	/**
	 * Creates an HTML Input text field
	 * 
	 * @param string $name
	 * @param string $value
	 * @param mixed $width
	 * @param array $attrs
	 * 
	 * @return string
	 */
	function InputTag( $name, $value, $width = NULL, $attrs = array() )
	{
		// check if the field name already exists
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		if ( NULL == $width )
		{
			$width = $this->width;
		}

		$id = $this->SetElementID( $name );
		$index = $this->GetTabIndex();
		$name = htmlspecialchars($name);

		$this->_names[] = $name;
		
		if ( $attrs )
		{
			$non_valid_keys = array( "name", "type", "id", "value", "width" );
			$tmp_attrs = $attrs;
			foreach ( $tmp_attrs as $attr )
			{
				if ( in_array($attr, $non_valid_keys) )
				{
					unset( $attrs[$attr] );
				}
			}
		}

		$field = "<input tabindex=\"$index\" type=\"text\" name=\"$name\" id=\"$id\" value=\"$value\" size=\"$width\"";
		if ( $attrs )
		{
		}

		$field .= " />";
		return $field;
	}

	/**
	 *
	 *	@param string $name
	 *	@param string $value
	 *	@param array $attrs
	 */
	function PasswordTag( $name, $value = NULL, $width = NULL, $attrs = array() )
	{
		// check if the field name already exists
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		$id = $this->SetElementID( $name );
		$name = htmlspecialchars($name);

		if ( NULL == $width )
		{
			$width = $this->width;
		}

		$this->_names[] = $name;
		
		if ( $attrs )
		{
			$non_valid_keys = array( "name", "type", "id", "value", "width" );
			$tmp_attrs = $attrs;
			foreach ( $tmp_attrs as $attr )
			{
				if ( in_array($attr, $non_valid_keys) )
				{
					unset( $attrs[$attr] );
				}
			}
		}

		$field = "<input type=\"password\" name=\"$name\" id=\"$id\" value=\"$value\" size=\"$width\"";
		if ( $attrs )
		{
		}

		$field .= " />";
		return $field;
	}

	/**
	 *
	 */
	function TextareaTag( $name, $value, $rows = 10, $cols = 50, $attrs = array() )
	{
		// check if the field name already exists
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		$id = $this->SetElementID( $name );
		$name = htmlspecialchars($name);

		$this->_names[] = $name;
		
		if ( $attrs )
		{
			$non_valid_keys = array( "name", "type", "id", "value", "width" );
			$tmp_attrs = $attrs;
			foreach ( $tmp_attrs as $attr )
			{
				if ( in_array($attr, $non_valid_keys) )
				{
					unset( $attrs[$attr] );
				}
			}
		}

		$field = "<textarea name=\"$name\" id=\"$id\" cols=\"$cols\" rows=\"$rows\"";
		if ( $attrs )
		{
		}

		$field .= ">$value</textarea>";
		return $field;
	}

	/**
	 *
	 */
	function ImageTag( $image, $attrs=array() )
	{
		$image = "<img src=\"$image\"";

		foreach ( $attrs as $attr => $value )
		{
			$image .= " $attr=\"$value\"";
		}

		$image .= "/>";
		return $image;
	}

	/**
	 *
	 *	@param String $name the name of the form
	 *	@param String $value the value to be added
	 *	@param int $height the height the editor is meant to be
	 *	@param String $path the path to the editor
	 */
	function RichTextEditor( $name, $value=NULL, $height=NULL, $path=NULL )
	{
		require_once( LIBDIR . "/HTML/editor/fckeditor.php" );
		
		$editor = new FCKeditor( $name );
		$editor->BasePath = str_replace( $_SERVER["DOCUMENT_ROOT"], "", $path ? $path : LIBDIR . "/HTML/editor/" );
		$editor->Value = $value;
		$editor->ToolbarSet = "WBNews";
		$editor->Height = $height ? intval($height) : 300;
		
		return $editor->CreateHTML();
	}

	/**
	 *
	 *	@param String $name
	 *	@param String $value
	 *	@param String $label
	 *	@param Array $attrs
	 *	@return String
	 */
	function CheckboxTag( $name, $value, $label, $attrs = array() )
	{
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		$id = $this->SetElementID( $name );
		$name = htmlspecialchars($name);
		$label = htmlspecialchars($label);

		$this->_names[] = $name;
		
		$content = "<input type=\"checkbox\" id=\"$id\" name=\"$name\" value=\"On\"";
		if ( $value )
		{
			$content .= " checked=\"checked\"";
		}

		$content .= "/><label for=\"$id\">$label</label>";
		return $content;
	}
	
	/**
	 *
	 *  @param String $name
	 *  @param String $value
	 *  @param int $width
	 *  @return mixed
	 */
	function DateTag( $name, $value, $width = 0 )
	{
		$name = trim($name);
		$value = trim($value);
	
		$id = $this->SetElementID( $name );
		$string = "<input type=\"text\" name=\"$name\" id=\"$id\"";
	
		if ( $width )
		{
			$width = (int)$width; // should strip of any crap
			$string .= " size=\"$width\"";
		}
		else
		{
			$string .= " size=\"" . $this->date_picker_width . "\"";
		}
		
		$string .= " value=\"$value\"/>";
	
		$date_format = $this->date_picker_format;
		$string .= "<img alt=\"\" src=\"" . $this->date_picker_img . "\" id=\"date_$id\" />";
		$string .= "\n<script type=\"text/javascript\">  Calendar.setup({ inputField : '$id', ifFormat : '$date_format', showsTime : false, button : 'date_$id', singleClick : true, step: 1 });</script>";
	
		if ( !isset($_SESSION["HTMLForm"]) )
		{
			$_SESSION["HTMLForm"] = array(
				"css" => array(),
				"js" => array()
				);
		}

		$_SESSION["HTMLForm"]["js"]["date"] = LIBDIR . "/HTML/calendar/calendar.js.php";
		$_SESSION["HTMLForm"]["css"]["date"] = LIBDIR . "/HTML/calendar/calendar.css.php";
		
		return $string;
	}
	/**
	 *
	 *	@param Array $name
	 *	@param String $value
	 *	@param Array $labels
	 *	@param Array $attrs
	 *	@param String $separator
	 *	@return String
	 */
	function CheckboxGroup( $name, $options=array(), $selected=array(), $attrs = array(), $separator=NULL )
	{
		if ( NULL == $separator )
		{
			$separator = "<br/>";
		}
		
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		if ( count($options) == 0 )
		{
			return FALSE;
		}
		
		$name = htmlspecialchars($name);
		$i = 0;
		$checkboxes = array();
		foreach ( $options as $value => $label )
		{
			$is_selected = in_array( $value, $selected ) && TRUE == $selected[$value];
			$value = htmlspecialchars( $value );
			$tabindex = $this->GetTabIndex();

			$n_name = str_replace( "[]", "_$i", $name );
			$this->names[] = $n_name;
			$id = $this->SetElementID( $n_name, "checkbox" );
			
			$checkbox = "<input type=\"checkbox\" name=\"$name\" value=\"$value\"";
			if ( $is_selected )
			{
				$checkbox .= " checked=\"checked\"";
			}
			
			$checkbox .= " id=\"$id\" tabindex=\"$tabindex\" /><label for=\"$id\">$label</label>";

			$checkboxes[] = $checkbox;
			$i++;
		}

		return join( $separator, $checkboxes );
	}

	/**
	 *
	 */
	function SelectTag( $name, $options, $value, $attrs=array(), $id=NULL )
	{
		// check if the field name already exists
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		if ( NULL == $id )
		{
			$id = $this->SetElementID( $name );
		}
		
		$name = htmlspecialchars($name);

		$this->_names[] = $name;

		$field = "<select name=\"$name\" id=\"$id\"";
		if ( $attrs )
		{
		}

		$field .= ">";

		foreach ( $options as $key => $label )
		{
			if ( $key == $value )
			{
				$field .= "<option selected=\"selected\" value=\"$key\">$label</option>";
				continue;
			}
			$field .= "<option value=\"$key\">$label</option>";
		}

		$field .= "</select>";
		return $field;
	}

	/**
	 *
	 */
	function RadioTag( $name, $value, $label, $selected, $attrs = array() )
	{
		$id = $this->SetElementID( $name );
		$name = htmlspecialchars( $name );
		$label = htmlspecialchars( $label );

		$this->_names[] = $name;

		$checked = "";
		if ( $selected == $value )
		{
			$checked = " checked=\"checked\"";
		}

		$field = "<input type=\"radio\" name=\"$name\" id=\"$id\" value=\"$value\"$checked";

		if ( is_array($attrs) && count($attrs) != 0 )
		{
			foreach ( $attrs as $k => $v )
			{
				if ( is_array($k) || is_array($v) )
				{
					continue;
				}
				
				$k = htmlspecialchars( $k );
				$v = htmlspecialchars( $v );
				$field .= " $k=\"$v\"";
			}
		}

		$field .= "/><label for=\"$id\">$label</label>";
		return $field;
	}

	/**
	 *
	 *	@param String $name
	 *	@param Array $options
	 *	@param String $selected
	 *	@param Array $attrs
	 *	@param String $separator
	 *	@return string
	 */
	function RadioGroup( $name, $options = array(), $selected, $attrs = array(), $separator = "" )
	{
		$separator = NULL == $separator ? "<br/>" : $separator;
		
		$content = array();
		foreach ( $options as $value => $label )
		{
			$content[] = $this->RadioTag( $name, $value, $label, $selected, $attrs );
		}
		
		return join( $separator, $content );
	}

	/**
	 *
	 *	@param String $name
	 *	@param Array $info
	 *	@return string
	 */
	function FileUploadTag( $name, $info = array() )
	{
		// check if the field name already exists
		if ( in_array($name, $this->_names ) )
		{
			return FALSE;
		}

		$id = $this->SetElementID( $name );
		$name = htmlspecialchars($name);

		$this->_names[] = $name;
		$tabindex = $this->GetTabIndex();
		
		return "<input type=\"file\" name=\"$name\" tabindex=\"$tabindex\"/>";
	}
	
	/**
	 *
	 *	@param Array $fields
	 *	@return String
	 */
	function HiddenTags( $fields = array(), $allow_same=FALSE )
	{
		$content = array();
		foreach ( $fields as $name => $value )
		{
			// check if the field name already exists
			if ( in_array($name, $this->_names ) && !$allow_same )
			{
				return FALSE;
			}
			$name = htmlspecialchars($name);
			$this->_names[] = $name;

			$content[] = "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>";
		}

		if ( !$content )
		{
			return NULL;
		}

		return join("\n", $content );
	}

	/**
	 *	Builds a button
	 *
	 *	@param string $buttons
	 *	@return string
	 */
	function SubmitButtons( $buttons, $attrs = array() )
	{
		$content = "";
		$buttons = split( ",",$buttons );

		$html_attrs = NULL;
		if ( count($attrs) != 0 )
		{
			$html_attrs = array();
			foreach ( $attrs as $key => $value )
			{
				$key = htmlspecialchars( $key );
				$value = htmlspecialchars( $value );
				$html_attrs[] = "$key=\"$value\"";
			}
			
			$html_attrs = " " . join( " ", $html_attrs );
		}
			
		foreach ( $buttons as $button )
		{
			$content .= "<input type=\"submit\" name=\"submit_$button\" value=\"$button\"$html_attrs/>";
		}

		return $content;
	}

	/**
	 *
	 *	@param String $element
	 *	@return String
	 */
	function SetElementID( $element, $type="text" )
	{
		$id = NULL;
		
		switch ( $type )
		{
		case "text":
			
			if ( !isset($this->_ids[$type]) )
			{
				$id = $type . "_1";
			}
			else
			{
				$last_id = substr( array_pop($this->_ids[$type]), strlen($type) + 1 );
				$id = $type . "_" . ++$last_id;
			}
			
			break;
		case "checkbox":
			$label = "chk";
			if ( !isset($this->_ids[$type]) )
			{
				$id = $label . "_1";
			}
			else
			{
				$last_id = substr( array_pop($this->_ids[$type]), strlen($label) + 1 );
				$id = $label . "_" . ++$last_id;
			}
			
			break;
		}

		$this->_ids[$type][$element] = $id;
		return $id;
	}

	/**
	 *
	 *	@param String $element
	 *	@return String
	 */
	function GetElementID( $element )
	{
		if ( array_key_exists( $element, $this->_ids ) )
		{
			return $this->_ids[$lement];
		}

		return NULL;
	}

	function GetTabIndex()
	{
		static $tab_index = 1;
		
		$mytab = $tab_index;
		$tab_index++;
		return $mytab;
	}

	/**
	 *
	 *	@param String $name
	 */
	function Message( $name="" )
	{
		if ( NULL != $name && array_key_exists( $name, $this->_msgs ) )
		{
			$msg = $this->_msgs[$name];
			unset( $this->_msgs[$name] );
			return sprintf( $this->msgformat, $msg );
		}

		if ( NULL == $name )
		{
			$content = "";
			foreach ( $this->_msgs as $label => $text )
			{
				$content .= "$label : $text";
			}

			return $content;
		}

		return;
	}
	
}

return;

?>
Return current item: WB News