Location: PHPKode > projects > Network Configuration Store > NCS-3.0.2/wwwroot/inc/CSForm.class.php
<?php

define('CSF_TEXTBOX',	1);
define('CSF_TEXTAREA',	2);
define('CSF_PASSWORD',	3);
define('CSF_SELECT',	4);
define('CSF_OPTION',	5);
define('CSF_CHECK',	6);
define('CSF_RADIO',	7);
define('CSF_HIDDEN',	8);
define('CSF_LABEL',	9);
define('CSF_TEXT',	10);
define('CSF_SUBMIT',	11);
define('CSF_RESET',	12);
define('CSF_BUTTON',	13);

$CSF_ENTITIES = array(
		CSF_TEXTBOX	=> array( 'tag' =>	'input type="text"',		'close' => FALSE ),
		CSF_TEXTAREA	=> array( 'tag' =>	'textarea',			'close' => TRUE ),
		CSF_PASSWORD	=> array( 'tag' =>	'input type="password"',	'close' => FALSE ),
		CSF_SELECT	=> array( 'tag' => 	'select',			'close' => TRUE ),
		CSF_OPTION	=> array( 'tag' =>	'option',			'close' => TRUE ),
		CSF_CHECK	=> array( 'tag' => 	'input type="checkbox"',	'close' => FALSE ),
		CSF_RADIO	=> array( 'tag' =>	'input type="radio"',		'close' => FALSE ),
		CSF_HIDDEN	=> array( 'tag' =>	'input type="hidden"',		'close' => FALSE ),
		CSF_LABEL	=> array( 'tag' =>	'label',			'close' => TRUE ),
		CSF_TEXT	=> array( 'tag' =>	'text',				'close' => FALSE ),
		CSF_SUBMIT	=> array( 'tag' =>	'input type="submit"',		'close' => FALSE ),
		CSF_RESET	=> array( 'tag' =>	'input type="reset"',		'close' => FALSE ),
		CSF_BUTTON	=> array( 'tag' =>	'input type="button"',		'close' => FALSE ),
);

class CSFormControl
{
	private $type;
	private $args;
	private $children;

	public function __construct($type, $args)
	{
		if (empty($type))
			throw new Exception('CSFormControl: invalid control type');
		if (!is_array($args))
			throw new Exception('CSFormControl: invalid arguments');

		$this->type = $type;
		$this->args = $args;

		$this->children = array();
	}
	public function __destruct() { }

	public function getChildCount() { return count($this->children); }

	public function __get($p)
	{
		if (array_key_exists($p, $this->args))
			return $this->args[$p];
		return null; // Returning FALSE means is_null() becomes FALSE!
	}
	public function __set($p, $v)
	{
		if (is_null($v))
			unset($this->args[$p]);
		else
			$this->args[$p] = $v;
		return true;
	}

	public function AddChild(CSFormControl $c)
	{
		$this->children[] = $c;
		return $c;
	}

	public function __toString()
	{
		global $CSF_ENTITIES;

		$e = $CSF_ENTITIES[$this->type];

		if ($this->type == CSF_TEXT)
		{
			if (!is_null($this->text))
				return (string)$this->text;
			return '';
		}

		$buf = '<'.$e['tag'];

		if (!is_null($this->for))
			$buf .= ' for="'.$this->for.'"';
		if (!is_null($this->name))
			$buf .= ' name="'.$this->name.'"';
		if (!is_null($this->value))
			$buf .= ' value="'.$this->value.'"';
		if (!is_null($this->id))
			$buf .= ' id="'.$this->id.'"';
		if (!is_null($this->size))
			$buf .= ' size="'.$this->size.'"';
		if (!is_null($this->class))
			$buf .= ' class="'.$this->class.'"';
		if (!is_null($this->onclick))
			$buf .= ' onclick="'.$this->onclick.'"';

		// Boolean fields must be TRUE (1), not just defined...
		if ($this->selected)
			$buf .= ' selected="selected"';
		if ($this->checked)
			$buf .= ' checked="checked"';
		if ($this->disabled)
			$buf .= ' disabled="disabled"';

		// If close==FALSE, this is an <input /> style tag

		if ($e['close'] == FALSE)
		{
			// Close the tag
			$buf .= ' />';

			// Add any child controls
			//
			foreach($this->children as $childControl)
				$buf .= $childControl;
		}
		else
		{
			$buf .= '>';

			if (!is_null($this->text))
				$buf .= $this->text;

			// Add any child controls
			//
			foreach ($this->children as $childControl)
				$buf .= $childControl;

			// Close this control off
			$buf .= '</'.$e['tag'].'>';
		}

		return $buf;
	}
}

class CSForm
{
	private $action;
	private $method;
	private $name;

	private $tableWidth;
	private $firstColumnWidth;
	private $secondColumnWidth;
	private $formControlClass;

	private $hidden;
	private $controls;
	private $buttons;

	public function __construct($action, $method="post", $name=null)
	{
		$this->action = $action;
		$this->method = $method;
		$this->name = $name;

		// Set some style defaults.
		//
		$this->tableWidth = 100;
		$this->firstColumnWidth = null;
		$this->secondColumnWidth = null;
		$this->formControlclass = null;

		$this->hidden = array();
		$this->controls = array();
		$this->buttons = array();
	}
	public function __destruct() { }

	public function getHiddenCount() { return count($this->hidden); }
	public function getControlCount() { return count($this->controls); }
	public function getButtonCount() { return count($this->buttons); }

	public function SetTableWidth($percent=null)
	{
		if (is_int($percent) && $percent >= 1 && $percent <= 100)
			$this->tableWidth = $percent;
		else
			$this->tableWidth = null;
	}

	public function SetFirstColumnWidth($px=null)
	{
		if (is_int($px) && $px > 1)
			$this->firstColumnWidth = $px;
		else
			$this->firstColumnWidth = null;
	}

	public function SetSecondColumnWidth($px=null)
	{
		if (is_int($px) && $px > 1)
			$this->secondColumnWidth = $px;
		else
			$this->secondColumnWidth = null;
	}

	public function setFormControlClass($class=null)
	{
		if (is_string($class) && strlen($class) > 0)
			$this->formControlClass = $class;
		else
			$this->formControlClass = null;
	}

	// AddHidden()
	//	Add a hidden parameter to a form in the format name=value
	//
	public function AddHidden($name, $value)
	{
		$this->hidden[$name] = $value;
	}

	// AddControl()
	//	Add a CSFormControl object to this form, with the specified label.
	//	Can also specify an array of controls.
	//
	public function AddControl($label, $c)
	{
		if (!is_a($c, 'CSFormControl'))
		{
			if (!is_array($c))
				throw new Exception('CSForm::AddControl() called with invalid control object');

			foreach($c as $cItem)
			{
				if (!is_a($cItem, 'CSFormControl'))
					throw new Exception('CSForm::AddControl() called with invalid control array item');

				$cItem->label = $label;
			}
		}
		else {
			$c->label = $label;
		}

		$this->controls[] = $c;
		return $c;
	}

	// AddButton()
	//	Add a button CSFormControl object, which are inserted at the end of a form.
	//
	public function AddButton(CSFormControl $c)
	{
		$this->buttons[] = $c;
	}

	// InsertHtml()
	//	Inserts the client-side HTML when called.
	//
	public function InsertHtml()
	{
		$buf = array();

		// Only add form elements if we have input buttons!
		//
		if (count($this->buttons) > 0)
		{
			if (is_string($this->name))
				$buf[] = '<form action="'.$this->action.'" method="'.$this->method.'" name="'.$this->name.'">';
			else
				$buf[] = '<form action="'.$this->action.'" method="'.$this->method.'">';

			// Insert any hidden fields
			if (count($this->hidden) > 0)
			{
				$buf[] = '<div>';

				foreach($this->hidden as $hiddenName => $hiddenValue)
					$buf[] = '<input type="hidden" name="'.$hiddenName.'" value="'.$hiddenValue.'" />';

				$buf[] = '</div>';
			}
		}

		// Now add the main controls in a nice pretty table!

		$i = 0;

		if (is_int($this->tableWidth))
			$buf[] = '<table width="'.$this->tableWidth.'%">';
		else
			$buf[] = '<table>';
		
		foreach($this->controls as $controlItem)
		{
			$i++;
			$controlItemLabel = '';
			$controlItemBuf = '';

			if (is_array($controlItem))
			{
				foreach($controlItem as $childItem)
				{
					$controlItemLabel = $childItem->label; // Last item wins this, although they should be
										// identical if CSForm::AddControl() has been used.
					$controlItemBuf .= $childItem.'&nbsp;';
				}
			}
			else {
				$controlItemLabel = $controlItem->label;
				$controlItemBuf = $controlItem;
			}

			if ($i % 2) {
				$buf[] = '<tr class="Odd">';
			} else {
				$buf[] = '<tr class="Even">';
			}

			if (is_int($this->firstColumnWidth)) {
				$buf[] = '<td width="'.$this->firstColumnWidth.'">'.$controlItemLabel.'</td>';
			} else {
				$buf[] = '<td>'.$controlItemLabel.'</td>';
			}

			if (is_int($this->secondColumnWidth))  {
				$buf[] = '<td width="'.$this->secondColumnWidth.'">'.$controlItemBuf.'</td>';
			} else {
				$buf[] = '<td>'.$controlItemBuf.'</td>';
			}

			$buf[] = '</tr>';
		}

		$buf[] = '</table>';

		// If we have any buttons, put them on and close the form.
		if (count($this->buttons) > 0)
		{
			if (is_string($this->formControlClass)) {
				$buf[] = '<div id="'.$this->formControlClass.'">';
			} else {
				$buf[] = '<div id="FormControlArea">';
			}

			foreach($this->buttons as $control)
				$buf[] = $control;

			$buf[] = '</div>';

			$buf[] = '</form>';
		}

		// Write it to the client
		foreach($buf as $formLine)
			echo "$formLine\n";
	}
}

?>
Return current item: Network Configuration Store