Source

codeigniter-qform-library / Qform.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 *	@package Cyb3rNet
 *	@subpackage CI-Form
 *	@version 1.0.3
 *	@license MIT License
 *
 *	@author Serafim Junior Dos Santos Fagundes <serafim@cyb3r.ca>
 *	@copyright Serafim Junior Dos Santos Fagundes Cyb3r Network
 *
 *	File containing classes for XHTML document generation. 
 */


/**
 *	@name CYB3RNET_CI_QFORM Constant for detection and interaction with other libraries
 */
define( 'CYB3RNET_CI_QFORM', '20100506_1' );


/**
 *	Class Qform includes methods to generate XHTML form fields
 */
class Qform
{
	/**
	 *	@var object Holds the CodeIgniter instance
	 *
	 *	@access private
	 */
	private $_oCI = NULL;
	
	
	/**
	 *	@var object Holds the codeigniter-jsf-library instance
	 *
	 *	@access public
	 */
	public $jsf = NULL;
	
	
	/**
	 *	@var string Holds de last object/field id
	 *
	 *	@access private
	 */
	private $_sLastId = '';
	

	public function __construct()
	{
		if ( ! defined( 'CYB3RNET_CI_XHTML' ) )
		{
			show_error( 'The library codeigniter-xhtml-library is needed and must be loaded previously to codeigniter-form-library.' );
		}
		
		if ( ! defined( 'CYB3RNET_CI_JSF' ) )
		{
			show_error( 'The library codeigniter-jsf-library is needed and must be loaded previously to codeigniter-jsf-library' );
		}
		
		$this->_oCI =& get_instance();
		
		$this->jsf =& $this->_oCI->jsf;
	}
		 
	
	/**
	 *	Creates a form with codeigniter-xhtml-library
	 *
	 *	@param string $sId Id of the form
	 *	@param string $sAction Form action
	 *	@param string $sMethod Method for the form submission
	 *	@param bool $bHasFieldUploads Indicates the enctype to be used
	 *
	 *	@access public
	 *	@return object
	 */
	public function form( $sId, $sAction, $sMethod, $bHasFileUploads = FALSE )
	{
		$xhtml =& $this->_oCI->xhtml;
		
		$form = $xhtml->form();
		
		$form->id( $sId );
		
		$this->jsf->form_id( $sId );
		
		$form->attr( 'action', $sAction );
		$form->attr( 'method', $sMethod );
		
		if ( $bHasUploads == TRUE )
		{
			$form->attr( 'enctype', 'multipart/form-data' );
		}

		$this->_sLastId = $sId;
		
		return $form;
	}
		 
	
	/**
	 *	Creates a text field; input or textarea
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *	@param bool $bMultiLine Indicates if the field is multiline; if true returns a textarea
	 *	@param int $iRows Number of textarea rows
	 *	@param int $iCols Number of textarea cols
	 *
	 *	@access public
	 *	@return object
	 */
	public function text( $sNameId, $sValue = "", $bDisabled = FALSE, $bMultiLine = FALSE, $iRows = 5, $iCols = 10 )
	{
		if ( $bMultiLine )
		{
			$oField = $this->_oCI->xhtml->textarea();
			
			$oField->id( $sNameId );
			
			$oField->attr( 'name', $sNameId );
			$oField->attr( 'rows', $iRows );
			$oField->attr( 'cols', $iRows );
			
			if ( strlen( $sValue ) > 0 )
			{
				$oField->append( $sValue );
			}
			
			if ( $bDisabled )
			{
				$oField->attr( 'disabled', 'disabled' );
			}
		}
		else
		{
			$oField = new Field( $sNameId, "text", $sValue, $bDisabled );
		}
		
		$this->_sLastId = $sNameId;
		
		return $oField;
	}
	
	
	/**
	 *	Creates a password field
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function password( $sNameId, $sValue = "", $bDisabled = FALSE )
	{
		$this->_sLastId = $sNameId;
		
		return new Field( $sNameId, "password", $sValue, $bDisabled );
	}
	
	
	/**
	 *	Creates a checkbox field
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bChecked Indicates if field is checked
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function checkbox( $sNameId, $sValue = "", $bChecked = FALSE, $bDisabled = FALSE )
	{
		$oField = new Field( $sNameId, "checkbox", $sValue = "", $bDisabled = FALSE );
		
		if ( $bChecked )
		{
			$oField->attr( 'checked', 'checked' );
		}
		
		$this->_sLastId = $sNameId;
		
		return $oField;
	}
	
	
	/**
	 *	Creates a radio button check field
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bChecked Indicates if field is checked
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function radio( $sNameId, $sValue = "", $bChecked = FALSE, $bDisabled = FALSE )
	{
		$oField = new Field( $sNameId, "radio", $sValue = "", $bDisabled = FALSE );
		
		if ( $bChecked )
		{
			$oField->attr( 'checked', 'checked' );
		}
		
		$this->_sLastId = $sNameId;
		
		return $oField;
	}
	
	
	/**
	 *	Creates a reset button
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function reset_button( $sNameId, $sValue = "Reset", $bDisabled = FALSE )
	{
		$this->_sLastId = $sNameId;
		
		return new Field( $sNameId, "reset", $sValue, $bDisabled );
	}
	
	
	/**
	 *	Creates a submit button
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function submit_button( $sNameId, $sValue = "Submit", $bDisabled = FALSE )
	{
		$this->_sLastId = $sNameId;
	
		return new Field( $sNameId, "submit", $sValue, $bDisabled );
	}
	
	
	/**
	 *	Creates a file field
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function file( $sNameId, $bDisabled = FALSE )
	{
		$this->_sLastId = $sNameId;
		
		return new Field( $sNameId, "file", "", $bDisabled );
	}
	
	
	/**
	 *	Creates a hidden field
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function hidden( $sNameId, $sValue = "", $bDisabled = FALSE )
	{
		$this->_sLastId = $sNameId;
		
		return new Field( $sNameId, "hidden", $sValue, $bDisabled );
	}
	
	
	/**
	 *	Creates an image field
	 *
	 *	@param string $sURLSource URL of the image
	 *	@param string $sAlt Image description
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function image_button( $sURLSource, $sAlt, $sNameId, $sValue = "", $bDisabled = FALSE )
	{
		$oField = new Field( $sNameId, "image", $sValue, $bDisabled );
		
		$oField->attr( 'src', $sURLSource );
		$oField->attr( 'alt', $sAlt );
		
		$this->_sLastId = $sNameId;
		
		return $oField;
	}
	
	
	/**
	 *	Creates a button field
	 *
	 *	@param string $sNameId Name and id of the field
	 *	@param string $sValue Value of the field
	 *	@param bool $bDisabled Indicates if the field is disabled
	 *
	 *	@access public
	 *	@return object
	 */
	public function button( $sNameId, $sValue = "", $bDisabled = FALSE )
	{
		$this->_sLastId = $sNameId;
		
		return new Field( $sNameId, "button", $sValue, $bDisabled );
	}
}


/**
 *	Creates a field object; extends MLObject from codeigniter-xhtml-library
 *
 *	@uses MLObject
 */
class Field extends MLObject
{
	/**
	 *	@param string $sNameId Value of the name and id attribute of tag input
	 *	@param string $sValue Value of of the field
	 *	@param string $sType Value of tag input attribute type
	 *	@param bool $bDisabled Indicates if field is disabled
	 *
	 *	@access public
	 */
	public function __construct( $sNameId, $sType, $sValue = "", $bDisabled = FALSE )
	{
		parent::__construct( 'input', array(), FALSE );
		
		parent::id( $sNameId );
		parent::attr( 'name', $sNameId );
		parent::attr( 'type', $sType );
		parent::attr( 'value', $sValue );
		
		if ( $bDisabled )
		{
			parent::attr( 'disabled', 'disabled' );
		}
	}
}


?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.