Location: PHPKode > projects > Mocovie web framework > webs/common/controls/dbinput.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
 */

ControlFactory::load('inline');

/**
 * Generates a generic input field used for database fields.
 *
 * @package mocovi
 * @subpackage controls
 */
class dbinput_control extends input_control
{
	public $db;

	protected $presets = array
		( 'VARCHAR'			=> array
			( 'type'		=> 'text'
			, 'pattern'		=> '/^[\w._- ]*$/'
			)
		, 'INT'			=> array
			( 'type'		=> 'text'
			, 'pattern'		=> '/^[0-9]*$/'
			)
		);

	/**
	 * @override
	 */
	public function program()
	{
		$this->renameNode('input');
		$split = explode('.',$this->getOption('connect'));
		$field = $this->getFieldInfo($split[0], $split[1]);
		if(!$this->options['maxlength'])
			$this->options['maxlength'] = $field->maxlength;
		if(!$this->options['preset'])
			$this->options['preset'] = $field->type;
		if(!$this->options['name'])
			$this->options['name'] = $this->getOption('connect');
		parent::program();
	}

	protected function getFieldInfo($table, $field)
	{
		$field = mysqli_query
			( $this->db->getConnection()
			, $this->db
				->select()
				->from
					( $table
					, array($field)
					)
				->limit(1,1)
			)
			->fetch_field();
		return (object) array
			( 'name'		=> $field->name
			, 'table'		=> $field->table
			, 'type'		=> $this->getFieldType($field->type)
			, 'maxlength'	=> $field->length
			);
	}

	protected function getFieldType($number)
	{
		switch($number)
		{
			case MYSQLI_TYPE_DECIMAL:
				return 'DECIMAL';
			case MYSQLI_TYPE_NEWDECIMAL:
				return 'NUMERIC';
			case MYSQLI_TYPE_BIT:
				return 'BIT';
			case MYSQLI_TYPE_TINY:
				return 'TINYINT';
			case MYSQLI_TYPE_SHORT:
				return 'SMALLINT';
			case MYSQLI_TYPE_LONG:
				return 'INT';
			case MYSQLI_TYPE_FLOAT:
				return 'FLOAT';
			case MYSQLI_TYPE_DOUBLE:
				return 'DOUBLE';
			case MYSQLI_TYPE_NULL:
				return 'DEFAULT NULL';
			case MYSQLI_TYPE_TIMESTAMP:
				return 'TIMESTAMP';
			case MYSQLI_TYPE_LONGLONG:
				return 'BIGINT';
			case MYSQLI_TYPE_INT24:
				return 'MEDIUMINT';
			case MYSQLI_TYPE_TIME:
				return 'TIME';
			case MYSQLI_TYPE_DATETIME:
				return 'DATETIME';
			case MYSQLI_TYPE_YEAR:
				return 'YEAR';
			case MYSQLI_TYPE_DATE:
			case MYSQLI_TYPE_NEWDATE:
				return 'DATE';
			case MYSQLI_TYPE_INTERVAL:
				return 'INTERVAL';
			case MYSQLI_TYPE_ENUM:
				return 'ENUM';
			case MYSQLI_TYPE_SET:
				return 'SET';
			case MYSQLI_TYPE_TINY_BLOB:
				return 'TINYBLOB';
			case MYSQLI_TYPE_MEDIUM_BLOB:
				return 'MEDIUMBLOB';
			case MYSQLI_TYPE_LONG_BLOB:
				return 'LONGBLOB';
			case MYSQLI_TYPE_BLOB:
				return 'BLOB';
			case MYSQLI_TYPE_VAR_STRING:
				return 'VARCHAR';
			case MYSQLI_TYPE_STRING:
				return 'STRING';
			case MYSQLI_TYPE_CHAR:
				return 'CHAR';
			case MYSQLI_TYPE_GEOMETRY:
				return 'GEOMETRY';
			default:
				return null;
		}
	}
}
Return current item: Mocovie web framework