Location: PHPKode > scripts > WebWidgets > webwidgets/FormControlSets.lib.php
<?php
/************************************************************
 *	Part of WebWidgets library
 *	Contain classes for making sets of input fields.
 * **********************************************************
 *	Version 0.8  09.10.02
 * **********************************************************
 * License: GPL - http://www.fsf.org/copyleft/gpl.txt
 * **********************************************************
 * Contain classes:
 *
 * FormControlSet	- abstract class
 * RadioSet		- class for making groups of radio buttons
 * CheckBoxSet		- class for making groups of checkboxes
 * DateField		- set of fields to select date
 * **********************************************************
 * Author: Dmitry Levashov (hide@address.com , hide@address.com)
 * *********************************************************/


/************************************************************
 * Base class for create groups of form fields. 
 * You can manipulate with group as with one field.
 * For example set of radio buttons.
 * If Your need any specific set of form elements, derive class from this one.
 ************************************************************/
class FormControlSet extends FormControl
{
	/**
	 * Error message
	 * @param	string
	 */
	var $ErrMsg = 'Select something';
	/**
	 * Array of childs objects
	 * @param	array
	 */
	var $Childs = array();

	/**
	 * Constructor
	 * @param	string	tag name
	 * @param	string	name of form field
	 * @param	string	value of field
	 * @param	string	label of field
	 * @param	array		tags attributes
	 * @access public
	 */
	function FormControlSet($tag, $name, $value='', $label='', $attr=null)
	{	
		parent::FormControl($tag, $name, '', $label, $attr);
		$this->Value = $value;
	} // end func constructor

	/**
	 * Set default value
	 * @param	mix
	 * @access	public
	 */
	function setValue($val) 
	{ 
		$this->Value = $val; 
	} // end func setValue

	/**
	 * Add object to $Childs array
	 * @param	object
	 * @acces	public
	 * @return	int
	 */
	function add(&$Widget)
	{
		if (!is_subclass_of($Widget, 'webwidget'))
			return false;

		$this->Childs[] = &$Widget;
		return $Widget->Id;
	} // end func add

	/**
	 * Collect values from all childs and return.
	 * @param	boolean
	 * @access	public
	 * @return array
	 */
	function getValue($struct=true)
	{
		$data = array();
		foreach ($this->Childs as $i=>$Child) {
			if ( ($child_data = $this->Childs[$i]->getValue($struct)) !== false )
				$data = array_merge_recursive($data, $child_data);
		}
		return $data;
	} // end func getValue

	/**
	 * Call _complite method for all childs
	 * @param	array		_GET or _POST by reference 
	 * @param	array		array of error messages	
	 * @access	private
	 * @return void
	 */
	function _complite(&$source, &$err) 
	{
		foreach ($this->Childs as $i=>$Child) {
			$this->Childs[$i]->_complite($source, $err);
			
		}

		if (!$this->_valid())
			$err[($this->Label) ? $this->Label : $this->_getFullName()] = $this->ErrMsg;
	} // end func _complite

	/**
	 * Build html output
	 * @access	private
	 * @return void
	 */
	function _createView()
	{
		if ($this->Tag)
			$this->View .= '<' . $this->Tag . $this->_attr2str() . ">\n";

		foreach ($this->Childs as $i=>$Child) {
			$this->View .= $this->Childs[$i]->getView();
			
		}
		
		if ($this->Tag)
			$this->View .= '</' . $this->Tag . ">\n";
	} // end func _createView

} // end class FormControlSet


/********************************************
 * produce set of radio buttons
 *******************************************/
class RadioSet extends FormControlSet
{
	/**
	 * Add object to $Childs array
	 * If object is a radio button, add label and new line
	 * @param	object
	 * @acces	public
	 * @return	int
	 */
	function add(&$Widget)
	{
		if (!is_subclass_of($Widget, 'webwidget'))
			return false;

		$this->Childs[] = &$Widget;
		
		if ('radio' == get_class($Widget)) {
			$Widget->setAttr('name', $this->_getFullName());

			if ($this->Value == $Widget->Attr['value'])
				$Widget->Attr['checked'] = 'on';
			$this->Childs[] = new CData($Widget->Label);
			if (!$this->Nl) $this->Nl = new ETag('br');
			$this->Childs[] = &$this->Nl;

		}
		return $Widget->Id;
	} // end func add

	/**
	 * Wrapper for "add" method. 
	 * Create and add radio button(s).
	 *	@param	mix		radio button value
	 *	@param	string	radio button label
	 *	@param	bool		if $value-array, use array keys as buttons values
	 *	@return mix
	 */
	function addOption($value, $label='', $use_keys=false)
	{
		if (!is_array($value)) {
			return $this->add(new Radio('', $value, $label));
		} else {
			foreach ($value as $val=>$label) {
				$id[] = $this->add(new Radio('', ($use_keys)?$val:$label, $label));
			}
		}
	} // end func addOption


	function setRange($min=0)
	{
		$this->Range['min'] = ($min>0) ? 1 : 0;
	}

	/**
	 * If $Range[min] is set, one button must be selected
	 * @return bool
	 */
	function _valid()
	{
		if ($this->Range['min']) {

			foreach ($this->Childs as $i=>$Child) {
				if ('radio' == get_class($Child) && $Child->Attr['checked'])
					$count++;
			}
			if ($count < 1)	return false;
		}

		return true;
	} // end func _valid

} // end class RadioSet

/*****************************************************
 * produce set of checkboxes
 *****************************************************/
class CheckBoxSet extends FormControlSet
{
	/**
	 * Add object to $Childs array
	 * If object is a checkbox, add label and new line.
	 * @param	object
	 * @access	public
	 * @return	int
	 */
	function add(&$Widget)
	{
		if (!is_subclass_of($Widget, 'webwidget'))
			return false;

		$this->Childs[] = &$Widget;
		
		if ('checkbox' == get_class($Widget)) {
			$Widget->setAttr('name', $this->_getFullName().'['.$Widget->Attr['name'].']');
			if ((!is_array($this->Value) && $this->Value == $Widget->Attr['value'])
			|| is_array($this->Value) && in_array($Widget->Attr['value'], $this->Value))
				$Widget->Attr['checked'] = 'on';

			$this->Childs[] = new CData($Widget->Label);
			if (!$this->Nl) $this->Nl = new ETag('br');
			$this->Childs[] = &$this->Nl;

		}
		return $Widget->Id;
	} // end func add

	/**
	 * Wrapper for "add" method. 
	 * Create and add checkbox(s).
	 *	@param	mix		checkbox value
	 *	@param	string	checkbox label
	 *	@param	bool		if $value-array, use array keys as checkboxes values
	 *	@return 	mixed
	 */
	function addOption($value, $label='', $use_keys=false)
	{
		if (!is_array($value)) {
			return $this->add(new CheckBox('', $value, $label));
		} else {
			foreach ($value as $val=>$label) {
				$id[] = $this->add(new CheckBox('', ($use_keys)?$val:$label, $label));
			}
		}
	} // end func addOption

	/**
	 * Set min & max numbers of fields, that must be checked
	 * @param	int
	 * @param	int
	 */
	function setRange($min=0, $max=0)
	{
		if ($min > 0) $this->Range['min'] = (int)$min;
		if ($max > 0) $this->Range['max'] = (int)$max;
	} // end func setRange

	/**
	 * Validate number of checked fields
	 * @return bool
	 */
	function _valid()
	{
		if ($this->Range['min'] || $this->Range['max']) {

			foreach ($this->Childs as $i=>$Child) {
				if ('checkbox' == get_class($Child) && $Child->Attr['checked'])
					$count++;
			}
			if ($this->Range['min'] && $count < $this->Range['min'])	return false;
			if ($this->Range['max'] && $count > $this->Range['max'])	return false;
		}
		return true;
	} // end func _valid

} // end class CheckBoxSet


/****************************************************************************
 * Produce set of 3 form fields (day, month, year) to get a date from user.
 * Your can set default date value and output date format (as in date func)
 ****************************************************************************/
class DateField extends FormControlSet
{
	/**
	 * Error message
	 * @param	string
	 */
	var $ErrMsg = 'Incorrect data';
	/**
	 * Output date format (see PHP manual, date func )
	 * @param	string
	 */
	var $Format = 'd.m.Y';

	/**
	 * default date  
	 * @param	array
	 */
	var $Value = array();
	
	/**
	 * Constructor. Default value sets in "mm/dd/yyyy" format
	 * @param	string	tag name
	 * @param	string	name of form field
	 * @param	string	value of field
	 * @param	string	label of field
	 * @param	array		tags attributes
	 * @param	string	output date format
	 * @access public
	 */
	function DateField($tag, $name, $value='', $label='', $attr=null, $format='') 
	{
		parent::FormControlSet($tag, $name, '', $label, $attr);
		
		if ($value)
			$this->Value = explode('/', $value);
		if ($format) $this->Format = $format;
		
		$Day = new Select('_day_', $this->Value[1]);
		$Day->addOption(range(1, 31)); //print_r($Day);
		$this->add($Day); 
		$Month = new Select('_month_', $this->Value[0]);
		while(++$i <= 12) {
			$Month->addOption($i, date('F', mktime(0,0,0,$i,1,2002)));
		}
		$this->add($Month);
		$this->add(new TextField('_year_', $this->Value[2],'', array('size'=>5)));
	} // end func constructor

	/**
	 * Set output date format
	 * @param	string	date format
	 * @access	public
	 */
	function setFormat($format) { $this->Format = $format; } // end func setFormat
	
	/**
	 * Return selected date.
	 * @param	boolean
	 * @access	public
	 * @return array
	 */
	function getValue($struct=true)
	{
		$data = parent::getValue(false);
		$res = date($this->Format, mktime(0,0,0,$data['_month_'],$data['_day_'],$data['_year_']));
		return $this->_getStruct($res, $struct);
	} // end func getValue

	/**
	 * Valid date
	 * @access	private
	 * @return boolean
	 */
	function _valid()
	{
		$data = parent::getValue(false); 
		if (!checkdate($data['_month_'], $data['_day_'], $data['_year_']))	
			return false;
		return true;
	} // end func _valid

} // end class DateField


?>
Return current item: WebWidgets