Location: PHPKode > projects > Mocovie web framework > webs/common/controls/input.php
<?php
/**
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Kai Dorschner <the-hide@address.com>
 * @copyright Copyright 2008, Kai Dorschner
 * @license http://www.gnu.org/licenses/lgpl.html LGPLv3
 * @package mocovi
 * @subpackage controls
 */

class_exists('Validator') or require $GLOBALS['library'].'Validator.php';

/**
 * Generates a generic input field used in forms forms.
 *
 * @package mocovi
 * @subpackage controls
 */
class input_control extends Control
{
	protected $types = array
		( 'text'
		, 'password'
		, 'textarea'
		, 'radio'
		, 'checkbox'
		, 'button'
		, 'submit'
		, 'file'
		, 'image'
		, 'pdf'
		);

	protected $presets = array
		( 'name'			=> array
			( 'type'		=> 'text'
			, 'name'		=> 'name'
			, 'pattern'		=> '/^[\w._-]{2,49}$/'
			, 'required'	=> true
			)
		, 'firstname'		=> array
			( 'type'		=> 'text'
			, 'name'		=> 'firstname'
			, 'pattern'		=> '/^[\w._-]{3,49}$/'
			, 'required'	=> true
			)
		, 'lastname'		=> array
			( 'type'		=> 'text'
			, 'name'		=> 'lastname'
			, 'pattern'		=> '/^[\w._-]{3,49}$/'
			, 'required'	=> true
			)
		, 'username'		=> array
			( 'type'		=> 'text'
			, 'name'		=> 'username'
			, 'pattern'		=> '/^[\w\d._-]{3,49}$/'
			, 'required'	=> true
			)
		, 'email'			=> array
			( 'type'		=> 'text'
			, 'name'		=> 'email'
			, 'pattern'		=> '/^[a-zA-Z0-9._%+-]+\@[a-zA-Z0-9.-]{2,}\.(?:[a-zA-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/'
			, 'required'	=> true
			)
		, 'url'				=> array
			( 'type'		=> 'text'
			, 'name'		=> 'email'
			, 'value'		=> 'http://'
			, 'pattern'		=> '/^https?:\/\/[a-zA-Z0-9.-]{2,}\.(?:[a-zA-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/' // needs to be checked!
			, 'required'	=> true
			)
		, 'submit'			=> array
			( 'type'		=> 'submit'
			, 'value'		=> 'Send'
			)
		);

	protected $defaultOptions = array
		( 'type'		=> ''
		, 'name'		=> ''
		, 'value'		=> ''
		, 'preset'		=> '' // Field preset
		, 'pattern'		=> '' // Regular expression match
		, 'minlength'	=> 0
		, 'maxlength'	=> ''
		, 'required'	=> false // Field is required or not
		, 'placeholder'	=> ''
		, 'highlight'	=> false
		);

	/**
	 * @override
	 */
	protected function program()
	{
		$this->setPresets();
		$this->verifyPattern();
		if(strlen($this->getOption('type')) == 0)
			$this->options['type'] = 'text';
		if(!is_bool($this->options['required']))
			$this->options['required'] = (bool)$this->options['required'];
		if($this->getOption('showData'))
			$this->showData();
		$this->adoptAllAttributes();
		$this->setAttribute
				( 'pattern'
				, substr($this->getOption('pattern'), 1, strlen($this->getOption('pattern')) - 2)
				);
	}

	public function receiveData($httpMethod) /* throws Exception */
	{
		$name = $this->getOption('name');
		$dataCandidate = null;
		if($httpMethod == 'post' && isset($_POST[$name]))
			$dataCandidate = $_POST[$name]; // @todo $_FILE has to be considered here!
		if($httpMethod == 'get' && isset($_GET[$name]))
			$dataCandidate = $_GET[$name];
		return $this->valid($dataCandidate);
	}

	public function highlight()
	{
		$this->options['highlight'] = true;
		$this->adoptAttribute('highlight');
	}

	public function setPlaceholder($text)
	{
		$this->options['placeholder'] = $text;
		$this->adoptAttribute('placeholder');
	}

	public function setValue($value)
	{
		$this->options['value'] = $value;
		$this->adoptAttribute('value', $value);
	}

	protected function verifyPattern()
	{
		if(strlen($pattern = $this->getOption('pattern')) > 0)
		{
			$first = str_replace('/', '\/', $pattern[0]);
			if(!preg_match('/^'.$first.'[^'.$first.']+'.$first.'[a-z]*$/', $pattern))
				throw new MvcException('Pattern "'.$pattern.'" is not a perl-compliant expression. Please add delimiters.');
		}
	}

	protected function setPresets()
	{
		if(array_key_exists($this->getOption('preset'), $this->presets))
			foreach($this->presets[$this->getOption('preset')] as $preset => $value)
				if(strlen($this->options[$preset]) <= 0)
					$this->options[$preset] = $value;
		return $this;
	}

	protected function valid($data) /* throws Exception */
	{
		try
		{
			if($this->getOption('required'))
				Validator::required
					( $data
					);
			if($pattern = $this->getOption('pattern'))
				Validator::validateByRegex
					( $data
					, $pattern
					);
			if($this->getOption('minlength'))
				Validator::minLength
					( $data
					, $this->getOption('minlength')
					);
			if($this->getOption('maxlength'))
				Validator::maxLength
					( $data
					, $this->getOption('maxlength')
					);
		}
		catch(Exception $e)
		{
			$exception = get_class($e);
			throw new $exception($this->getOption('name').' '.$e->getMessage());
		}
		return $data;
	}
}
Return current item: Mocovie web framework