Location: PHPKode > projects > chillyCMS > chillyCMS/core/formelement.class.php
<?php
//#################################################################################################
//	Class FormElement and derivate classes
//#################################################################################################
//	chillyCMS - Content Management System
//	Copyright (C) 2008
//	Stefanie Wiegand <hide@address.com> & Johannes Cox <hide@address.com>
//	
//	This program is licensed under the GPL 3.0 license. For more information see LICENSE.txt.
//#################################################################################################
defined('DOIT') or die('Restricted access');

require_once('recaptchalib.php');

abstract class FormElement {
	//Class variables//////////////////////////////////////////////////////////////////////////
	public $id;		//css/js-id
	protected $name;	//html name
	protected $text;	//label name
	protected $compulsory;	//true/false
	protected $value;	//(predefined) value
	protected $title;
	protected $class;	//css class
	protected $errorhint;	//error hint
	protected $javascript;	//any javascript like onclick=...
	protected $valid;	//true/false
	protected $check;	//check[0]=regular expression, check[1]=description for info
	public $error;		//a compulsory field with empty or missing value
	//Functions////////////////////////////////////////////////////////////////////////////////
	public function __construct($name,$text=false,$value=false,$compulsory=false,$check=false,$id=false,$class=false,$title=false,$errorhint=false,$javascript=false) {
		$this->name = $name;
		$this->text = $text;
		$this->value = escape_html($value);
		$this->compulsory = $compulsory;
		$this->check = $check;
		$this->id = $id;
		$this->class = $class;
		$this->title = $title;
		$this->errorhint = $errorhint;
		$this->javascript = $javascript;
		
		$this->error = false;
		if (isset($this->name) && $this->name && $this->name!='') {
			$this->valid = true;
		} else {
			$this->valid = false;
		}
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Input fields											 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormInput extends FormElement {
	private $type;
	private $size;
	private $maxlength;
	private $cols;
	private $rows;

	public function __construct($type,$name,$text=false,$value=false,$compulsory=false,$check=false,$size=false,$maxlength=false,$cols=false,$rows=false,$id=false,$class=false,$title=false,$errorhint=false,$javascript=false) {
		parent::__construct($name,$text,$value,$compulsory,$check,$id,$class,$title,$errorhint,$javascript);
		
		$this->type = false;
		$allowed = array('password','text','textarea','hidden');
		if (in_array($type,$allowed)) {
			$this->type = $type;
		} else {
			$this->valid = false;
		}
		$this->size = $size;
		$this->maxlength = $maxlength;
		$this->cols = $cols;
		$this->rows = $rows;
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}

	public function render($style='without_id') {
		$string = '';

		//errorhint above
		if ($this->error && $this->errorhint && $this->errorhint[1]=='above') {
			$string .= '<span class="errorhint">'.$this->errorhint[0].'</span>';
		}
		if ($this->type=='textarea') {
			$string .= '<textarea name="'.$this->name.'" ';
			if ($this->id && $style=='with_id') { $string .= 'id="'.$this->id.'" '; }
			if ($this->title) { $string .= 'title="'.$this->title.'" '; }
			if ($this->class) { $string .= 'class="'.$this->class.'" '; }
			if ($this->cols) { $string .= 'cols="'.intval($this->cols).'" '; }
			if ($this->rows) { $string .= 'rows="'.intval($this->rows).'" '; }
			$string .= '>'.$this->value.'</textarea>';
		} else {
			$string .= '<input type="'.$this->type.'" name="'.$this->name.'" value="'.$this->value.'" ';
			if ($style=='with_id') {
				if ($this->id && $this->id !="") {
					$string .= 'id="'.$this->id.'" ';
				} else {
					$string .= 'id="'.$this->name.'" ';
				}
			}
			if ($this->size) { $string .= 'size="'.intval($this->size).'" '; }
			if ($this->maxlength) { $string .= 'maxlength="'.intval($this->maxlength).'" '; }
			if ($this->title && $this->title !="") { $string .= 'title="'.$this->title.'" '; }
			if ($this->class && $this->class !="") { $string .= 'class="'.$this->class.'" '; }
			$string .= ' />';
		}
		//errorhint below
		if ($this->error && $this->errorhint && $this->errorhint[1]=='below') {
			$string .= '<span class="errorhint">'.$this->errorhint[0].'</span>';
		}
		return $string;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Choice fields										 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormChoice extends FormElement {
	private $type;		//checkbox,radio
	private $options;	//associative array! key=name/description, value=value
	private $selected;	//array of selected items
	
	public function __construct($type,$name,$text,$options,$compulsory=false,$selected=false,$id=false,$class=false,$title=false,$errorhint=false,$javascript=false) {
		parent::__construct($name,$text,false,$compulsory,false,$id,$class,$title,$errorhint,$javascript);
		
		$this->type = $this->options = false;
		$allowed = array('checkbox','radio');
		if (in_array($type,$allowed)) {
			$this->type = $type;
		} else {
			$this->valid = false;
		}
		if (is_array($options) && !empty($options)) {
			$this->options = $options;
		} else {
			$this->valid = false;
		}
		if ($selected!==false) {
			$this->select($selected);
		}
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}
	//Select
	public function select($selected) {
		if (is_array($selected) && !empty($selected)) {
			foreach ($selected as $s) {
				//check if each option to select is available in the options
				if (isset($this->options[$s])) {
					//if yes, add them to the selected options
					$this->selected[] = $s;
				}
			}
		} elseif ($selected!==false && $selected!=='') {
			$this->selected[] = $selected;
		}
	}

	//Render
	public function render($style='without_id') {
		$string = '';
		//walk through options array
		if (is_array($this->options) && !empty($this->options)) {
			foreach ($this->options as $key=>$value) {
				//checkbox
				if ($this->type=='checkbox') {
					$string .= "\t\t\t".'<input type="checkbox" name="'.str_replace(' ','_',$this->name);
					//if there were more than one option, make an array
					if (sizeof($this->options)>1) {
						$string .= '[]';
					}
					$string .= '" '.
					'value="'.$key.'" ';
					if ($this->id && $style=='with_id') { $string .= 'id="'.$this->id.'" '; }
					if ($this->title) { $string .= 'title="'.$this->title.'" '; }
					if ($this->class) { $string .= 'class="'.$this->class.'" '; }
					if (is_array($this->selected) && in_array($key,$this->selected)) {
						$string .= 'checked="checked"';
					}
					$string .= ' />'.$value;
					if (sizeof($this->options)>1) {
						$string .= '<br />';
					}
					$string .= "\n";
				//radiobutton
				} elseif ($this->type == 'radio') {
					$string .= "\t\t\t".'<input type="radio" name="'.str_replace(' ','_',$this->name);
					//if there were more than one option, make an array
					if (sizeof($this->options)>1) {
						$string .= '[]';
					}
					$string .= '" '.
					'value="'.$key.'" ';
					if ($this->id && $style=='with_id') { $string .= 'id="'.$this->id.'" '; }
					if ($this->title) { $string .= 'title="'.$this->title.'" '; }
					if ($this->class) { $string .= 'class="'.$this->class.'" '; }
					if (is_array($this->selected) && in_array($key,$this->selected)) {
						
						$string .= 'checked="checked" ';
					}
					$string .= '/>'.$value;
					if (sizeof($this->options)>1) {
						$string .= '<br />';
					}
					$string .= "\n";
				}
			}
		}
		return $string;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Select fields										 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormSelect extends FormElement {
	private $options;	//associative array! key=name/description, value=value
	private $selected;	//the value(s) of the options array that are selected
	private $size;		//number of options visible simultanely
	private $multiple;	//multiple choice?
	
	public function __construct($name,$text,$options,$compulsory=false,$selected=false,$size=1,$multiple=false,$id=false,$class=false,$title=false,$errorhint=false,$javascript=false) {
		parent::__construct($name,$text,false,$compulsory,false,$id,$class,$title,$errorhint,$javascript);

		$this->size = intval($size);
		if ($this->size < 1) { $this->size = 1; }
		$this->options = false;
		if (is_array($options) && !empty($options)) {
			$this->options = $options;
		} else {
			$this->valid = false;
		}
		$this->multiple = (boolean)$multiple;
		if ($selected!==false) {
			$this->select($selected);
		}
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; echo 'setting '.$this->$name; }
		else { return false; }	
	}
	//select
	public function select($selected) {
		if (is_array($selected) && !empty($selected)) {
			foreach ($selected as $s) {
				//check if each option to select is available in the options
				if (isset($this->options[$s])) {
					//if yes, add them to the selected options
					$this->selected[] = $s;
				}
			}
		} elseif ($selected!==false && $selected!=='') {
			$this->selected[] = $selected;
		}
	}
	
	public function render($style='without_id') {
		$string = "\t\t\t".'<select size="'.$this->size.'" name="'.str_replace(' ','_',$this->name);
		if ($this->multiple) { $string .= '[]'; }
		$string .= '" ';
		if ($this->id && $style=='with_id') { $string .= 'id="'.$this->id.'" '; }
		if ($this->title) { $string .= 'title="'.$this->title.'" '; }
		if ($this->class) { $string .= 'class="'.$this->class.'" '; }
		if ($this->multiple) { $string .= 'multiple="multiple"'; }
		$string .= ">\n";
		//walk through options array
		if (is_array($this->options) && !empty($this->options))
		foreach ($this->options as $key=>$value) {
			$string .= "\t\t\t\t".'<option value="'.$key.'"';
			//mark selected options
			if (is_array($this->selected) && in_array($key,$this->selected)) {
				$string .= ' selected="selected"';
			}
			$string .= '>'.$value."</option>\n";
		}			
		$string .= "\t\t\t</select>\n";
		return $string;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Buttons											 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormButton extends FormElement {
	private $type;
	private $link;

	public function __construct($type,$name,$text=false,$value=false,$link=false,$id=false,$class=false,$title=false,$javascript=false) {
		parent::__construct($name,$text,$value,false,false,$id,$class,$title,false,$javascript);
		
		$this->type = false;
		$allowed = array('submit','reset','reload','back','jsback','jsprint','link');
		if (in_array($type,$allowed)) {
			$this->type = $type;
		} else {
			$this->valid = false;
		}
		$this->link = $link;
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}

	public function render($style='with_id') {
		$string = '';
		
		//if there is already a class append error class
		if($this->class && sizeof($this->class)>0) {
			$this->class = 'button '.$this->class;
		//else make class error class
		} else {
			$this->class = 'button';
		}
		
		if ($this->type=='submit' or $this->type=='reset') {
			$string .= "\t\t\t".'<input type="'.$this->type.'" name="'.$this->name.'" value="'.$this->value.'" ';
			if ($style=='with_id') {
				if ($this->id && $this->id !="") {
					$string .= 'id="'.$this->id.'" ';
				} else {
					$string .= 'id="'.$this->name.'" ';
				}
			}
			if ($this->title && $this->title !="") { $string .= 'title="'.$this->title.'" '; }
			if ($this->class && $this->class !="") { $string .= 'class="'.$this->class.'" '; }
			if ($this->javascript) {
				$string .= ' '.$this->javascript;
			}
			$string .= ' />';
		} elseif ($this->type=='reload' or $this->type=='back' or $this->type=='jsback' or $this->type=='jsprint' or $this->type=='link') {
			$string .= "\t\t\t<a";
			if ($this->id && $style='with_id') { $string .= ' id="'.$this->id.'"'; }
			if ($this->title) { $string .= ' title="'.$this->title.'"'; }
			if ($this->class) { $string .= ' class="'.$this->class.'"'; }
			if ($this->type=='back') {
				if (isset($_SERVER['HTTP_REFERER'])) {
					$string .= ' href="'.$_SERVER['HTTP_REFERER'].'"';
				}
			} elseif ($this->type=='reload') {
				$string .= ' href="'.$_SERVER['REQUEST_URI'].'"';
			} elseif ($this->link) {
				$string .= ' href="'.$this->link.'"';
			}
			if ($this->type=='jsback') {
				$string .= ' onclick="window.history.back();return false;" ';
			} elseif ($this->type=='jsprint') {
				$string .= ' onclick="window.print();return false;" ';
			} else {
				if ($this->javascript) {
					$string .= ' '.$this->javascript;
				}
			}
			
			$string .= '>';
			if ($this->text && $this->text !='') {
				$string .= $this->text;
			} elseif ($this->value && $this->value !='') {
				$string .= $this->value;
			} else {
				$string .= $this->name;
			}
			$string .= '</a>';
		}
		return $string;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Fileupload fields										 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormFile extends FormElement {
	private $size;
	private $accept;

	public function __construct($name,$text=false,$compulsory=false,$check=false,$size=false,$accept=false,$id=false,$class=false,$title=false,$errorhint=false,$javascript=false) {
		parent::__construct($name,$text,false,$compulsory,$check,$id,$class,$title,$errorhint,$javascript);

		$this->size = $size;
		$this->accept = $accept;
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}

	public function render($style='without_id') {
		$string = '';
		//errorhint above
		if ($this->error && $this->errorhint && $this->errorhint[1]=='above') {
			$string .= '<span class="errorhint">'.$this->errorhint[0].'</span>';
		}
		$string .= '<input name="'.$this->name.'" type="file"';
		if ($this->id && $style='with_id') { $string .= ' id="'.$this->id.'"'; }
		if ($this->class) { $string .= ' class="'.$this->class.'"'; }
		if ($this->title) { $string .= ' title="'.$this->title.'"'; }
		if ($this->size) { $string .= ' size="'.$this->size.'"'; }
		if ($this->accept) { $string .= ' accept="'.$this->accept.'"'; }
		if ($this->javascript) { $string .= ' '.$this->javascript; }
		$string .= '>';
		//errorhint below
		if ($this->error && $this->errorhint && $this->errorhint[1]=='below') {
			$string .= '<span class="errorhint">'.$this->errorhint[0].'</span>';
		}
		return $string;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Calendar fields										 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormCalendar extends FormElement {}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Textrows without something to submit								 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormTextrow extends FormElement {
	private $longtext;	//the text to display
	private $size;		//1 or 2 columns; if 2, then there is no "text" field

	public function __construct($name,$text=false,$longtext=false,$size=1,$id=false,$class=false,$title=false,$javascript=false) {
		parent::__construct($name,$text,false,false,false,$id,$class,$title,false,$javascript);
		
		$this->longtext = $longtext;
		$this->size = $size;
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}

	public function render($style='without_id') {
		$output = '<div';
		if ($this->id && $style=='with_id') { $output .= ' id="'.$this->id.'"'; }
		if ($this->class) { $output .= ' class="'.$this->class.'"'; }
		if ($this->title) { $output .= ' title="'.$this->title.'"'; }
		if ($this->javascript) { $output .= ' '.$this->javascript; }
		$output .= '>'.$this->longtext.'</div>';
		return $output;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// A ReCaptcha											 //
///////////////////////////////////////////////////////////////////////////////////////////////////
class FormCaptcha extends FormElement {
	public $resp;	//the text to display
	public $captchaerror;
	private $publickey;
	private $privatekey;

	public function __construct($name,$text=false,$id=false,$class=false,$title=false,$errorhint=false) {
		parent::__construct($name,$text,false,true,false,$id,$class,$title,$errorhint,false);
		
		$this->resp = null;
		$this->captchaerror = null;
		$this->publickey = "6LfNhgsAAAAAANax57hhn42m7g2KG2dYPP8SisHT";
		$this->privatekey = "6LfNhgsAAAAAALgsI_77rXv7joaIdQVyqpqPnIcK";
	}
	//Getter
	public function __get($name) {
		if (isset($name, $this->$name)) { return $this->$name; }
		else { return false; }
	}
	//Setter
	public function __set($name,$value) {
		if (isset($name, $this->$name)) { $this->$name=$value; }
		else { return false; }	
	}

	public function render() {
		return recaptcha_get_html($this->publickey, $this->captchaerror);
	}
}

?>
Return current item: chillyCMS