Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/lib/html.php
<?php

/*

	HTML Components library, (c) 1998-2001 Leo West

	provides an object-oriented view of HTML widgets components, mainly form widgets
	Html class is an utility providing static methods, see source code below

	@Author	Leo West <west_leo AT yahoo DOT com>
	@Version 1.10
	@Last modified	march 2002

	@Changes (latest only)
	1.9
		integrate smart data to page bindings ( bindObject and bindPage methods )
	1.10 
		modify Html::Stylesheet behavior
*/

// shortcuts for widget related CSS classes & styles
	$tbZ = 'style="width:40px;height:18px"';
	$tbS = 'style="width:80px;height:18px"';
	$tbM = 'style="width:160px;height:18px"';
	$tbL = 'style="width:400px;height:18px"';
	$taSS = 'style="width:100px;height:40px"';
	$taMS = 'style="width:160px;height:40px"';
	$taLS = 'style="width:400px;height:40px"';
	$taLM = 'style="width:400px;height:120px"';
	$btZ = 'style="width:40px"';
	$btS = 'style="width:80px"';
	$btM = 'style="width:100px"';
	$btL = 'style="width:180px"';
	$sbZ = 'style="width:40px;height:18px"';
	$sbS = 'style="width:80px;height:18px"';
	$sbM = 'style="width:160px;height:18px"';
	$sbL = 'style="width:240px;height:18px"';
	$cbS = 'style="border:1px;margin:0"';


class Html 
{
	
	// shortway to display hidden fields
	function Form( $action=NULL, $method='post', $params=NULL )
	{
		if( $action == NULL ) 
			$action = @$GLOBALS['PHP_SELF'];
		$params || $params = 'name="frm"';
		if( $method == "" ) $method = "post";
		print '<form method="' . $method . '" action="' . $action . "\" $params>\n";
	}

	// shortway to display hidden fields
	function Hidden( $name, $value )
	{
		print '<input type="hidden" name="' . $name . '" value="' . htmlspecialchars($value) . "\">\n";			
	}

	/* include a CSS stylesheet dedicated to forms 
	 also code required by active labels stuff
	*/
	function styleSheet( $csspath )
	{
		global $HTTP_SERVER_VARS;
		echo '<style type="text/css"><!--';
		include $csspath;
		echo "-->\n</style>\n";

	}

	function styleLink( $csspath )
	{
		echo '<link rel="stylesheet" href="' . $csspath . "\">\n";
	}

	function Link( $url, $text, $extra= NULL, $target="_self" )
	{
		return sprintf( '<a href="%s" %s target="%s">%s</a>', $url, $extra, $target, $text );
	}

	function Section( $title = NULL, $extra=NULL )
	{
		global $HTMLSECTION_IN, $HTMLSECTION_TABLE;

		if( ! $HTMLSECTION_TABLE ) {
			echo "\n\n<!-- SectionStart -->\n	<table class=params>\n";
			$HTMLSECTION_TABLE = true;
		}
		
		if( $HTMLSECTION_IN ) {
			echo "		</table></td></tr>\n";
		}
		
		if( $title ) {
			echo "		<!-- Section:caption --><tr><td class=\"caption\">$title</td></tr>\n";
		}
		echo "		<tr><td><table width=\"100%\">\n";
		$HTMLSECTION_IN = true;
			
	}

	function SectionEnd()
	{
		global $HTMLSECTION_IN, $HTMLSECTION_TABLE;
		if( $HTMLSECTION_IN )
			echo "		</table></td></tr>\n<!-- SectionEnd-->\n";
		$HTMLSECTION_IN = false;
		$HTMLSECTION_TABLE = false;
	}

	function buttonBar()
	{
		
		echo "	<!--buttonbar-->\n	<tr><td style=\"padding-top:16px;text-align:right;\">\n";
		
		$cnt = func_num_args();
		for( $i=0; $i< $cnt; $i++ ) {
			$arg =& func_get_arg( $i );
			echo $arg->toString();
		}
		echo "	</td></tr><!-- /buttonbar-->\n";
		
	}

	function errorMessage( $message="", $icon = "/lib/img/warning.gif" )
	{
		if( $message == "" )
			return;
		echo "<font color=red><b>";
		if( ! empty($icon) )
			echo "<img src=\"$icon\" hspace=\"8\">";
		if( is_array($message) )
			echo implode( "<br>", $message);
		else
			echo $message;
		echo "</b></font><br>";
	}

	function statusMessage( $message="", $icon = "/img/info.gif" )
	{
		if( $message == "" )
			return;
		echo "<font color=red><b>";
		if( ! empty($icon) )
			echo "<img src=\"$icon\" hspace=\"8\">";
		if( is_array($message) )
			echo implode( "<br>", $message);
		else
			echo $message;
		echo "</b></font><br>";
	}


} // Html class


class Textinput
{
	
	function Textinput( $name, $value="", $options="" )
	{
		$this->name = $name;
		$this->value = $value;
		$this->options = $options;
		$this->widgetClass = "text";
		$this->widgetFamily = "text";
	}

	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{		
		return sprintf( '<input type="%s" name="%s" value="%s" class="textbox" %s>',
			$this->widgetClass, $this->name, htmlspecialchars($this->value), $this->options );
	}
	
}


class Button
{

	function Button( $caption, $action, $name="", $options="" )
	{
		$this->name = $name;
		$this->action = $action;
		$this->value = $caption;
		$this->options = $options;

		switch( strtolower( $action ) ) {
		
		case "submit":
			$this->widgetClass = "submit";
			break;
		
		case "reset":
			$this->widgetClass = "reset";
			break;
		
		default:
			$this->widgetClass = "button";
			$this->options .= " onClick=\"$action\"";
		}
	}
	
	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{
		return sprintf( '<input type="%s" name="%s" value="%s" class="button" %s>',
			$this->widgetClass, $this->name, $this->value, $this->options );
	}
	
}

class Popup
{

	function Popup( $name, $value="", $setOfValues ="", $options="" )
	{
		$this->name = $name;
		$this->value = (string) $value;
		$this->options = $options;
		$this->setValues( $setOfValues );
		$this->useHash = true;
	}

	function setValues( $setOfValues )
	{
		if( ! is_array( $setOfValues ) )
			return false;		
		$this->_values = $setOfValues;
	}
	
	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	/*
		whether to consider the values array as a hash (true, default)
		or a simple array ( in this case values are used both as key and labels of the popup
		this hack turn around the famous "array-hash PHP ambiguity"
	*/
	function useHash( $boolean ) 
	{
		$this->useHash = $boolean; 
	}	

	function toString()
	{
		
		$out = '<select name="' . $this->name . '" class="popup" ' . $this->options . ">\n";
		
		if( is_array( $this->_values ) ) {
			if( $this->useHash ) {
				while( list($k,$v) = each( $this->_values ) ) {
					$eq = ( $this->value == $k );
//					echo " $this->value == $k : $eq | types=" . gettype($this->value) . "," . gettype($k) . "<br>";
					if( $eq )
						$out .= "<option class=wg value=\"" . htmlspecialchars($k) . "\" selected>$v</option>\n";
					else
						$out .= "<option value=\"". htmlspecialchars($k) ."\">$v</option>\n";
				}
				
			} else {
				foreach( $this->_values as $v ) {
					if( $this->value == $v )
						$out .= "<option class=wg value=\"" . htmlspecialchars($v) . "\" selected>$v</option>\n";
					else
						$out .= "<option value=\"". htmlspecialchars($v) ."\">$v</option>\n";
				}
			}
		}
		
		$out .= "</select>\n";
		return $out;
	}

}




class Password
{

	function Password( $name, $value="", $options="" )
	{
		$this->name = $name;
		$this->value = $value;
		$this->options = $options;
		$this->widgetClass = "password";
		$this->widgetFamily = "text";
	}
	
	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{
		return sprintf( '<input type="%s" name="%s" value="%s" class="textbox" %s>', 
			$this->widgetClass, $this->name, htmlspecialchars($this->value), $this->options );
	}

}



class Checkbox
{

	function Checkbox( $name, $value="", $status = false, $options="" )
	{
		$this->name = $name;
		$this->value = $value;
		$this->options = $options;
		$this->widgetClass = "checkbox";
		$this->widgetFamily = "box";		
		$this->Checked( $status );
	}
	
	function Checked( $status )
	{
		if( $status )
			$this->checked = "checked";
		else
			$this->checked = "";
	}

	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
	//	echo "$this->name::addOption( $htmlcode )<br>";
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{
		$this->fmt = '<input type="%s" name="%s" value="%s" %s %s>';
		return sprintf( $this->fmt, $this->widgetClass, $this->name, $this->value, $this->checked, $this->options );
	}	
		
}


class CheckboxGroup
{
	var
		$_values,
		$sep = " &nbsp;"; // separation used between radios
	
	function CheckboxGroup( $name, $currentValues="" , $setOfValues = "", $options="" )
	{
		$this->name= $name;
		$this->value = $value;
		if( is_array($currentValues) ) {
			$this->currentValues = $currentValues;
		} else {
			$this->currentValues = array();
			if( $currentValues != "" )
				$this->currentValues[] =  $currentValues;
		}
		$this->options = $options;
		$this->setValues( $setOfValues );
		$this->useHash( true );
	}

	function setValues( $setOfValues, $useHash=true )
	{
		if( ! is_array( $setOfValues ) )
			return false;		
		$this->_values = $setOfValues;
		$this->useHash( $useHash );
	}

	/*
		whether to consider the values array as a hash (true, default)
		or a simple array ( in this case values are used both as key and labels of the popup
		this hack turn around the famous "array-hash PHP ambiguity"
	*/
	function useHash( $boolean ) 
	{
		$this->useHash = $boolean;
	}
	

	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
	//	echo "$this->name::addOption( $htmlcode )<br>";
		$this->options .= " " . $htmlcode;
	}
		
	function toString()
	{		
		// radio separator, eg <br> or whitespaces
		if( ! $this->sep )
			$this->sep = " &nbsp; &nbsp; &nbsp;";
		$ret = "";
		
		if( ! is_array( $this->_values ) )
			return $ret;
		
		while( list($k,$v) = each( $this->_values ) ) {

			if( $this->useHash == false )
				$k = $v;
			
			if( in_array( $k, $this->currentValues ) )
				$sel = "checked";
			else
				$sel = "";
			$ret .= "<input type=\"checkbox\" name=\"$this->name\" value=\"$k\" $sel $this->options> $v $this->sep";
		}
		return $ret;
	}
}



class Radio
{
	function Radio( $name, $value=1, $checkedValue=NULL, $options = NULL )
	{
		$this->name= $name;
		$this->value = $value;
		$this->options = $options;
		$this->Selected( $checkedValue );
	}
	
	function Selected( $checkedValue )
	{
		$status = ( $checkedValue == $this->value );
		if( $status ) // TODO: is it right or better value==$checked
			$this->checked = "checked";
		else
			$this->checked = "";
	}

	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{
		$this->fmt = '<input type="radio" name="%s" value="%s" %s %s>';
		return sprintf( $this->fmt, $this->name, $this->value, $this->checked, $this->options );
	}	
}


class RadioGroup
{
	var
		$_values,
		$sep = " &nbsp;"; // separation used between radios
	
	function RadioGroup( $name, $value="" , $setOfValues = "", $options="" )
	{
		$this->name= $name;
		$this->value = $value;
		$this->options = $options;
		$this->widgetClass = "radio";
		$this->widgetClass = "list"; 
		$this->_values = array();
		$this->setValues( $setOfValues );
	}

	function setValues( $setOfValues )
	{
		if( ! is_array( $setOfValues ) )
			return false;		
		$this->_values = $setOfValues;
	}

	function useHash( $boolean ) 
	{
		$this->useHash = $boolean; 
	}	

	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
	//	echo "$this->name::addOption( $htmlcode )<br>";
		$this->options .= " " . $htmlcode;
	}
		
	function toString()
	{		
		// radio separator, eg <br> or whitespaces
		if( ! $this->sep )
			$this->sep = " &nbsp; &nbsp; &nbsp;";
		$ret = "";
		
		if( ! is_array( $this->_values ) )
			return $ret;
		
		while( list($k,$v) = each( $this->_values ) ) {
			( ! $this->useHash ) && $k=$v;
			if( $this->value == $k )
				$sel = "checked";
			else
				$sel = "";
			$ret .= "<input type=\"radio\" name=\"$this->name\" value=\"$k\" $sel $this->options> $v $this->sep";
		}
		return $ret;
	}
}



class Fileinput
{
	function Fileinput( $name, $value="", $options="" )
	{
		$this->name = $name;
		$this->value = $value;
		$this->options = $options;
		$this->widgetClass = "file";
		$this->widgetFamily = "text";
	}
	
	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{
		return sprintf( '<input type="%s" name="%s" value="%s" class="textbox" %s>', $this->widgetClass, $this->name, $this->value, $this->options );		
	}
}




class TextArea
{

	function TextArea( $name, $value="", $options="" )
	{
		$this->name = $name;
		$this->value = $value;
		$this->options = $options;	
		$this->widgetClass = "textarea";
		$this->widgetFamily = "text";
	}

	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{

		return sprintf( '<textarea name="%s" class="notebox" %s>%s</textarea>', 
			$this->name, $this->options, $this->value );
	}

}



class SelectM
{
	
	function SelectM( $name, $value="", $setOfValues="", $options="" )
	{
		$this->name = $name;
		
		// TODO case for value=="" ??
		if( is_array( $value ) )
			$this->value = $value;
		else
			$this->value = array( $value );
		
		$this->options = $options;
		$this->widgetClass = "listbox"; 
		$this->widgetClass = "list";
		$this->setValues( $setOfValues );
	}

	function setValues( $setOfValues )
	{
		if( ! is_array( $setOfValues ) )
			return false;
		$this->items = $setOfValues;
	}
	
	// add a property to the widget ( ex: "size=20" )
	function addOption( $htmlcode )
	{
		$this->options .= " " . $htmlcode;
	}
	
	function toString()
	{
		
		$out = '<select multiple name="' . $this->name . '" ' . $this->options . ">\n";
		
		
		if( is_array( $this->items ) ) {
			$keys = array_keys($this->items);
			// items is a simple array
			if( $keys[0] === 0 ) {
				foreach( $this->items as $v ) {
					if( in_array( $v, $this->value ) )
						$out .= "<option class=wg value=\"$v\" selected>$v</option>\n";
					else
						$out .= "<option value=\"$v\">$v</option>\n";
				}
				
			// items is a hash
			} else {
				while( list($k,$v) = each( $this->items ) ) {
					if( in_array( $k, $this->value ) )
						$out .= "<option class=wg value=\"$k\" selected>$v</option>\n";
					else
						$out .= "<option value=\"$k\">$v</option>\n";
				}
			}
		}
		$out .= "</select>\n";
		return $out;
	}

}


?>
Return current item: LDAP Navigator