Location: PHPKode > projects > GnomePHP > peec-GnomePHP-b5a360b/gnomephp/form/Form.php
<?php
namespace gnomephp\form;

class Form{
	
	protected $id;
	protected $action;
	protected $method;
	protected $listener;
	protected $cls = '';
	/**
	 * 
	 * @var gnomephp\Url
	 */
	protected $url;
	
	protected $input;
	
	protected $postVals = array();
	

	public function __construct($id, $url, $action, $method='post', $listener = null, $input=null){
		$this->id = $id;
		$this->action = $action;
		$this->method = $method;
		$this->url = $url;
		$this->listener = $listener;
		$this->input = $input;
		if ($input->post){
			if ($listener !== null && is_callable($listener)){
				if ($input->post->get('form_identity') == $id){
					$listener($input);
				}
			}
		}
	}
	
	/**
	 * Do you want to remember the post variables if a post has been made ? 
	 * @return gnomephp\form\Form
	 */
	public function rememberPostValues(){
		if ($this->input->post && $data = $this->input->post->get()){
			$this->postVals = $data;
		}
		return $this;
	}
	
	/**
	 * Creates a <input /> element.
	 * @param string $type Type of the input element, example: text, password, submit etc.
	 * @param string $name Name of the element.
	 * @param string $value Value of the element.
	 * @param array $props Other properties in array (key => val ) eg. array('class' => 'passwordField', 'id' => 'password1')
	 */
	public function input($type, $name, $value = null, $props=array()){
		return new Input($this->id, $this->url, $name, ($value !== null ? $value : (isset($this->postVals[$name]) ? $this->postVals[$name] : null)), $props, $type);
	}
	
	/**
	 * Creates a <textarea /> element.
	 * @param string $name Name of the element.
	 * @param string $value Value of the element.
	 * @param array $props Other properties in array (key => val ) eg. array('class' => 'passwordField', 'id' => 'password1')
	 */
	public function textarea($name, $value = null, $props=array()){
		return new Textarea($this->id, $this->url, $name, ($value !== null ? $value : (isset($this->postVals[$name]) ? $this->postVals[$name] : null)), $props);
	}
	
	/**
	 * Return Captcha class wich can return image and input element for the image. 
	 * @param string $name Name of the element.
	 * @param string $value The alt attribute for the image. Default is Human verification.
	 * @param array $props Other properties in array (key => val ) eg. array('class' => 'borderedImage')
	 */
	public function captcha($name=null, $value='Human verification', $imgProps=array()){
		if ($name===null){
			$name = \gnomephp\captcha\Captcha::CAPTCHA_POST_NAME;
		}
		return new Captcha($this->id, $this->url, $name, $value, $imgProps);
	}
	
	
	/**
	 * Closes the form.
	 */
	public function close(){
		return '</form>';
	}
	
	/**
	 * Adds a class css selector to the form.
	 * @param string $cls The class name to add.
	 */
	public function addClass($cls){
		$this->cls .= $cls . ' ';
		return $this;
	}
	
	/**
	 * Sets the ID of the form.
	 * @param string $id Id of the form.
	 */
	public function id($id){
		$this->id =$id;
		return $this;
	}
	
	/**
	 * Prints the form element start tag.
	 * Also adds a form_identity hidden element wich contains the id of this form.
	 */
	public function __toString(){
		return '<form id="'.$this->id.'" action="'.$this->action.'" method="'.$this->method.'" '.($this->cls ? 'class="'.$this->cls.'"' : '').'>' . 
		$this->input('hidden', 'form_identity', $this->id)
		;
	}
	
	
	
	
}
Return current item: GnomePHP