Technology Temerity

Class – Forms

<?php 

class class_forms {

	/*
	Utility
	Damon Vaughn Caskey
	2013_01_21
	
	Miscellaneous form input functions.
	*/
		
	/* Constants */
	const FORMS_ID_USE_NAME						= NULL;		//Use name to generate ID.
	const FORMS_ITEM_ADDITIONS_NONE				= NULL;		//No manual additions to generated item list.
	const FORMS_LABEL_NONE						= NULL;		//No label for fieldset item.
	const FORMS_LABEL_BLANK						= 1;		//Blank label for fieldset item.
	const FORMS_LABEL_USE_ITEM_KEY				= 2;		//Use the item key of a field for its fieldset label or visible selection.
	const FORMS_LABEL_USE_ITEM_NAME				= 3;		//Use the item name of a field for its fieldset label or visible selection.
	const FORMS_LABEL_USE_ITEM_VALUE			= 4;		//Use the items value of a field for its fieldset label or visible selection.
	const FORMS_LABEL_TYPE_FIELDSET				= 0;		//Fieldset label.
	const FORMS_LABEL_TYPE_INLINE				= 1;		//Label text only; no formatting.
	const FORMS_LEGEND_NONE 					= NULL;		//No legend for fieldset.
	const FORMS_QUERY_PARAMETERS_NONE			= NULL;		//No query parameters.
	const FORMS_TYPE_RADIO						= 0;		//Radio type list.
	const FORMS_TYPE_SELECT						= 1;		//Select type list.
	const FORMS_VALUE_CURRENT_NONE				= NULL;		//Current (last selected) value.
	const FORMS_VALUE_DEFAULT_NONE				= NULL;		//Default selected value.
	const FORMS_VALUE_DEFAULT_NOW				= -1;		//Default to current time for specific field.
	const FORMS_EVENTS_NONE						= NULL;		//No events attached to fieldset item.
	const FORMS_READ_ONLY_OFF					= FALSE;
	const FORMS_READ_ONLY_ON					= TRUE;
		
	public $cItemsList 							= NULL;		//Array of items list for select/radio/etc.
	public $cFormElement						= NULL;		//Arracy to store completed element markup that will be placed in a fieldset.
	public $cFormElementActions					= NULL;		//Array of actions tied to form element (onchange, onclick, etc.).
	public $cFieldset							= NULL;		//Array of completed fieldset markups ready to echo into page.
	public $cFieldsetAddsA						= NULL;		//Array of additional instructions, links, etc. that may be added to fieldset above items.
				
	function __construct($oDep)
	{
		/*
		Constructor
		Damon Vaughn Caskey
		2013-01-21
		
		Class constructor.
		*/
		
		/* Import object dependencies. */
		//$this->oUtl = $oDep['Utl'];
		$this->oDB	= $oDep['DB'];
		//$this->Err	= $oDep['Err'];
				
		/* Verify object dependencies. */
		//if(!$this->oUtl)	trigger_error("Missing object dependency: Utility.", E_USER_ERROR);		
		if(!$this->oDB)		trigger_error("Missing object dependency: Database.", E_USER_ERROR);
		//if(!$this->Err)		trigger_error("Missing object dependency: Errors.", E_USER_ERROR);
	}	
	
	private function forms_events_markup($cEvents=self::FORMS_EVENTS_NONE)
	{
		/*
		forms_events_markup
		Damon Vaughn Caskey
		2013-03-26
		
		Prepare HTML markup string for fieldset form item events (onchange, onclick, etc.).
		*/
		
		if(isset($cEvents))
		{		
			foreach ($cEvents as $cKey => $cValue)
			{
				
				$cEvent .= $cKey.'="'.$cValue.'"';
			}
		}
		return $cEvent;
	}
	
	public function test()
	{
		echo self::FORMS_LABEL_USE_ITEM_VALUE;
	}
	
	private function forms_label_markup($iStyle=self::FORMS_LABEL_NONE, $iType=self::FORMS_LABEL_TYPE_FIELDSET, $cName=NULL, $cID=NULL, $cKey=NULL, $cValue=NULL)
	{
		/*
		forms_label_markup
		Damon Vaughn Caskey
		2013-03-26
		
		Prepare HTML markup string for fieldset form item labels.
		
		Preconditions:
			$cName: 	Name of item.
			$cID:		ID of item.
			$cKey:		Array key of item.
			$cValue: 	Array value of item.
			$iStyle: 	How to arrange label markup.
				self::FORMS_LABEL_USE_ITEM_KEY = The item's Key will be used as a label.
				self::FORMS_LABEL_USE_ITEM_VALUE = The item's Value will be used as label.
				self::FORMS_LABEL_BLANK = The label will be left blank.
				self::FORMS_LABEL_NONE = No label markuop at all.
		
		Postconditions:
			Return finished label markup string.		
		*/
		
		$cClosing = NULL;
		$cLabel = NULL;
		
		if($iType == self::FORMS_LABEL_TYPE_FIELDSET)
		{
			/* Prepare opening label markup. */
			$cLabel = '<div class="'.$cClass['Container_Label'].'"><label for="'.$cID.'">';
			$cClosing = '</label></div>';	
		}							
			
		switch($iStyle)
		{
			case self::FORMS_LABEL_USE_ITEM_KEY:				
				
				$cLabel .= $cKey.$cClosing;									
				break;
		
			case self::FORMS_LABEL_USE_ITEM_NAME:			
				
				$cLabel .= $cName.$cClosing;									
				break;
			
			case self::FORMS_LABEL_USE_ITEM_VALUE:
				
				$cLabel .= $cValue.$cClosing;											
				break;
			
			case self::FORMS_LABEL_BLANK:
			
				$cLabel .= $cClosing;
				break;				
			
			case self::FORMS_LABEL_NONE:
			
				$cLabel = NULL;
				break;
				
			default:
				
				$cLabel .= $iStyle.$cClosing;							
		}
		
		/* Return label markup string. */
		return $cLabel;
	}
	
	public function forms_list_array_from_query($cQuery=NULL, $cParams=array(self::FORMS_QUERY_PARAMETERS_NONE), $cAddsT=self::FORMS_ITEM_ADDITIONS_NONE, $cAddsB=self::FORMS_ITEM_ADDITIONS_NONE){		
		
		/*
		forms_list_array_from_query
		Damon Vaughn Caskey
		2012-12-19
		
		Create list array directly from query results.
		
		$iType:		Select, options, etc.		
		$cQuery:	SQL query string.
		$cParams:	Parameter array.
		$cDefault:	Default selection.
		$cCurrent:	Current selection.
		$bKey:		Use key as item label?
		$cAddsT:	Additional items to place at top of generated list.
		$cAddsB:	Additional items to place at bottom of generated list.
		*/
	
		$i		=	NULL;		//Counter.
		$cList	=	NULL;		//Output string.
		$cKey	=	NULL;
		
		$cList	=	array();
		
		/* Query for list items. */
		$this->oDB->db_basic_select($cQuery, $cParams);
				
		/* Populate list variable. */
		while($this->oDB->db_line(SQLSRV_FETCH_NUMERIC))
		{	
			/* Get key (used for items visible to user). */
			$cKey = $this->oDB->cDBLine[1];
			
			/* If fields exist beyond first key, append to a single key value. */
			if($this->oDB->iDBFCount > 2)
			{	
				/* Start with field #2 (third field) and append to key string until last field is reached. */
				for($i = 2; $i <= ($this->oDB->iDBFCount-1); $i++)
				{
					$cKey .= "; ". $this->oDB->cDBLine[$i];	
				}
			}
			
			/* Populate list array using visible portion of list values as key. */			 
			$cList[$cKey] = $this->oDB->cDBLine[0];									
		}
		
		/* Merge Additions. (As of 5.4 array_merge() will always reorder indexes). */
		if($cAddsT && is_array($cAddsT))
		{	
			$cList = $cAddsT + $cList;	
		}
		
		if($cAddsB && is_array($cAddsB))
		{	
			$cList += $cAddsB;	
		}	
		
		$this->cItemsList = $cList;
		
		/* Output final list value. */
		return $this->cItemsList;
	}
	
	function forms_list_numeric($iType=NULL, $cQuery=NULL, $cParams=NULL, $cDefault=NULL, $cCurrent=NULL, $bKey=self::FORMS_LABEL_USE_ITEM_KEY, $iStart=0, $cAddsT=NULL, $cAddsB=NULL)
	{		
		/*
		forms_list_numeric
		Damon V. Caskey
		2012-12-19		
		
		Generate a simple numeric list with maximum value based number of rows returned by query.
		
		$iType: 	
		$cQuery:	SQL query string.
		$cParams:	Parameter array.
		$cDefault:	Default selection.
		$cCurrent:	Current selection.
		$bKey:		Use key as item label?
		$iStart:	First numeric value of generated list.
		$cAddsT:	Additional items to place at top of generated list.
		$cAddsB:	Additional items to place at bottom of generated list.
		*/
		
		$i		= NULL;	//Counter.
		$cList	= NULL;	//Item list array.					
		
		$cList	= array();
				
		$this->oDB->db_basic_select($cQuery, $cParams);	
								
		for($i = $iStart; $i <= $this->oDB->iDBRowCount; $i++)
		{		
			$cList[$i] = $i;
		}				
		
		/* Merge Additions. (As of 5.4 array_merge() will always reorder indexes). */
		if($cAddsT && is_array($cAddsT))
		{	
			$cList = $cAddsT + $cList;	
		}
		
		if($cAddsB && is_array($cAddsB))
		{	
			$cList += $cAddsB;	
		}
		
		switch ($iType)
		{
			case self::FORMS_TYPE_RADIO:
				//$this->cDLListCode = $this->forms_select_options($cList, $cDefault, $cCurrent, $bKey);
			default:
				$this->cDLListCode = $this->forms_select_options($cList, $cDefault, $cCurrent, $bKey);
		}
		
		return $this->cDLListCode;
	}
		                 
	public function forms_phone($cName=NULL, $cID=self::FORMS_ID_USE_NAME, $cLabel=self::FORMS_LABEL_NONE, $cDefault=array("cc" => "1", "ac" => "859", "lc" => NULL, "pn" => NULL), $cCurrent=self::FORMS_VALUE_CURRENT_NONE, $cClass=NULL, $cEvents=NULL)
	{	
		/*
		forms_phone
		Damon Vaughn Caskey
		2013-01-21
		
		Output form phone input markup.
		*/
	
		$cMarkup 	=	NULL;	//Final markup to echo.
		$cEvent		=	NULL;	//Event string.	
		
		$cMarkup .='<div class="'.$cClass['Container_All'].'">';
				
		/* Set ID to name? */
		$cID = $cID != self::FORMS_ID_USE_NAME ? $cID : $cName;
		
		/* If current value empty or NULL, set "No current" cosntant */
		$cCurrent = $cCurrent ? $cCurrent : self::FORMS_VALUE_CURRENT_NONE;
		
		$cDefault = $cDefault == self::FORMS_VALUE_DEFAULT_NONE ? NULL : $cDefault; 
		
		/* If no current value is available, use default. */
		if(!$cCurrent || $cCurrent == self::FORMS_VALUE_CURRENT_NONE)
		{
			$cCurrent = $cDefault;
		}
				
		/* Parse event actions. */
		$cEvent = $this->forms_events_markup($cEvents);			
		
		/* Prepare label markup. */
		$cMarkup .= $this->forms_label_markup($cLabel, self::FORMS_LABEL_TYPE_FIELDSET, $cName, $cID, $cKey, $cValue);		
		
		$cMarkup .= '<div class="'.$cClass['Container_Item'].'"><input type="text" name="'.$cName.'_cc" id="'.$cID.'_cc" class="'.$cClass.'" value="'.$cDefault['cc'].'" size="1" maxlength="1" />
						  (
							<input type="text" name="'.$cName.'_ac" id="'.$cID.'_ac" class="'.$cClass.'" value="'.$cDefault['ac'].'" size="3" maxlength="3" />
							) 
							<input type="text" name="'.$cName.'_lc" id="'.$cID.'_lc" class="'.$cClass.'" value="'.$cDefault['lc'].'" size="3" maxlength="3" /> 
						  - 
						  <input type="text" name="'.$cName.'_pn" id="'.$cID.'_pn" class="'.$cClass.'" value="'.$cDefault['pn'].'" size="4" maxlength="4" />				  
					</div></div>';		
		/*
		Return end result.
		*/
		return $cMarkup;
	}	
					
	public function forms_radio($cName=NULL, $cID=self::FORMS_ID_USE_NAME, $iLabelStyle=self::FORMS_LABEL_USE_ITEM_KEY, $cList=array(NULL), $cDefault=NULL, $cCurrent=self::FORMS_VALUE_CURRENT_NONE, $cClass=array("Item" => "Standard"), $cEvents=NULL)
	{
		/*
		forms_radio
		Damon Vaughn Caskey
		2013-03-24	
		*/
								
		$cKey		=	NULL;	//Array key.
		$cValue		=	NULL;	//Array value.
		$cChecked	= 	NULL;	//Checked (default/current)?
		$cMarkup 	=	NULL;	//Final markup to echo.
		$cLabel		=	NULL;	//Item label markup.
		$cIDFinal	= 	NULL;	//Final ID inserted into markup.
		$cEvent		=	NULL;	
		
		$cMarkup .='<div class="'.$cClass['Container_All'].'">';
		
		/* Set ID to name? */
		$cID = $cID != self::FORMS_ID_USE_NAME ? $cID : $cName;
		
		/* If current value empty or NULL, set "No current" cosntant */
		$cCurrent = $cCurrent ? $cCurrent : self::FORMS_VALUE_CURRENT_NONE;
		
		/* Parse event actions. */
		$cEvent = $this->forms_events_markup($cEvents);		
		
		foreach ($cList as $cKey => $cValue)
		{				
			/* If $cValue matches $cCurrent, or $cCurrent not provided but $cValue matches $cDefault, add 'checked' to make this the default list item selected. */	
			if ($cCurrent == $cValue || ($cCurrent == self::FORMS_VALUE_CURRENT_NONE && $cValue == $cDefault))
			{
				$cChecked = 'checked="checked"';
			}
			else
			{
				$cChecked = NULL;
			}		
					
			/* IDs must be unique, so we'll combine ID with value. */
			$cIDFinal = $cID."_".$cValue;			
			
			/* Prepare label markup. */
			$cMarkup .= $this->forms_label_markup($iLabelStyle, self::FORMS_LABEL_TYPE_FIELDSET, $cName, $cIDFinal, $cKey, $cValue);			
			
			$cMarkup .= '<div class="'.$cClass['Container_Item'].'"><input type="radio" name="'.$cName.'" id="'.$cIDFinal.'" value="'.$cValue.'" '.$cChecked.' '.$cEvent.' /></div>';			
		}					
		
		$cMarkup .= '</div>';
		
		/*
		Return end result.
		*/
		return $cMarkup;
	
	}
	
	public function forms_select($cName=NULL, $cID=self::FORMS_ID_USE_NAME, $iLabelStyle=self::FORMS_LABEL_USE_ITEM_KEY, $iKeyStyle=self::FORMS_LABEL_USE_ITEM_KEY, $cList=array(NULL => NULL), $cDefault=NULL, $cCurrent=self::FORMS_VALUE_CURRENT_NONE, $cClass=array("Item" => "Standard", "Container_All" => "Show Me"), $cEvents=NULL)
	{
		/*
		forms_radio
		Damon Vaughn Caskey
		2013-03-24	
		*/
								
		$cKey		=	NULL;	//Array key.
		$cValue		=	NULL;	//Array value.
		$cChecked	= 	NULL;	//Checked (default/current)?
		$cMarkup 	=	NULL;	//Final markup to echo.
		$cLabel		=	NULL;	//Item label markup.
		$cEvent		=	NULL;	
		$cItems		=	NULL;	//Select options.
		
		$cMarkup .='<div class="'.$cClass['Container_All'].'">';
		
		/* Set ID to name? */
		$cID = $cID != self::FORMS_ID_USE_NAME ? $cID : $cName;		
		
		/* Parse event actions. */
		$cEvent = $this->forms_events_markup($cEvents);		
		
		if (!is_array($cList))
		{
			$cList = array(NULL => NULL);
		}
		
		foreach ($cList as $cKey => $cValue)
		{				
			/* If $cValue matches $cCurrent, or $cCurrent not provided but $cValue matches $cDefault, add 'checked' to make this the default list item selected. */	
			if ($cCurrent == $cValue || ($cCurrent == self::FORMS_VALUE_CURRENT_NONE && $cValue == $cDefault))
			{
				$cChecked = 'selected';
			}
			else
			{
				$cChecked = NULL;
			}		
					
			/* Prepare label markup. */
			$cLabel = $this->forms_label_markup(self::FORMS_LABEL_USE_ITEM_KEY, self::FORMS_LABEL_TYPE_INLINE, $cName, $cID, $cKey, $cValue);	
			
			$cItems.='<option value="'.$cValue.'" '.$cChecked.'>' . $cLabel . '</option>';			
		}				
		
		
		/* Prepare label markup. */
		$cLabel = $this->forms_label_markup($iLabelStyle, self::FORMS_LABEL_TYPE_FIELDSET, $cName, $cID, $cKey, $cValue);	

		$cMarkup .= $cLabel.'<div class="'.$cClass['Container_Item'].'"><select type="select" name="'.$cName.'" id="'.$cID.'" class="'.$cClass['Item'].'" '.$cEvent.'>'.$cItems.'</select></div></div>';
		
		/*
		Return end result.
		*/
		return $cMarkup;
	
	}
					
	public function forms_text($cName=NULL, $cID=self::FORMS_ID_USE_NAME, $cLabel=self::FORMS_LABEL_NONE, $cDefault=self::FORMS_VALUE_DEFAULT_NONE, $cCurrent=self::FORMS_VALUE_CURRENT_NONE, $cClass=array("Item" => "Standard"), $cEvents=NULL)
	{	
		/*
		forms_text
		Damon Vaughn Caskey
		2013-01-21
		
		Output form text input markup.				
		*/
	
		$cMarkup 	=	NULL;	//Final markup to echo.
		$cEvent		=	NULL;	//Event string.	
		
		$cMarkup .='<div class="'.$cClass['Container_All'].'">';
				
		/* Set ID to name? */
		$cID = $cID != self::FORMS_ID_USE_NAME ? $cID : $cName;
		
		/* If current value empty or NULL, set "No current" cosntant */
		$cCurrent = $cCurrent ? $cCurrent : self::FORMS_VALUE_CURRENT_NONE;
		
		$cDefault = $cDefault == self::FORMS_VALUE_DEFAULT_NONE ? NULL : $cDefault; 
		
		/* If no current value is available, use default. */
		if(!$cCurrent || $cCurrent == self::FORMS_VALUE_CURRENT_NONE)
		{
			$cCurrent = $cDefault;
		}
				
		/* Parse event actions. */
		$cEvent = $this->forms_events_markup($cEvents);			
		
		/* Prepare label markup. */
		$cMarkup .= $this->forms_label_markup($cLabel, self::FORMS_LABEL_TYPE_FIELDSET, $cName, $cID, $cKey, $cValue);
					
		$cMarkup .= '<div class="'.$cClass['Container_Item'].'"><input type="text" name="'.$cName.'" id="'.$cID.'" class="'.$cClass['Item'].'" value="'.$cCurrent.'" '.$cEvent.' /></div></div>';
		
		/*
		Return end result.
		*/
		return $cMarkup;
	}
	
	public function forms_time($cName=NULL, $cID=self::FORMS_ID_USE_NAME, $cLabel=self::FORMS_LABEL_NONE, $cDefault=NULL, $cCurrent=NULL, $cOptions="{dateFormat: 'yy-mm-dd', timeFormat: 'HH:mm:ss', controlType: 'select', changeYear: true, constrainInput: true}", $cFunction="datetimepicker", $bReadOnly=self::FORMS_READ_ONLY_ON, $cClass=NULL, $cEvents=NULL)
	{	
		/*
		forms_time
		Damon Vaughn Caskey
		2013-01-21
		
		Output form date/time input markup.
		*/
	
		$cMarkup 	=	NULL;	//Final markup to echo.
		$cEvent		=	NULL;	//Event string.	
				
		$cMarkup .='<div class="'.$cClass['Container_All'].'">';
				
		/* Set ID to name? */
		$cID = $cID != self::FORMS_ID_USE_NAME ? $cID : $cName;
		
		/* If current value empty or NULL, set "No current" cosntant */
		$cCurrent = $cCurrent ? $cCurrent : self::FORMS_VALUE_CURRENT_NONE;
		
		$cDefault = $cDefault == self::FORMS_VALUE_DEFAULT_NONE ? NULL : $cDefault; 
		
		/* If no current value is available, use default. */
		if(!$cCurrent || $cCurrent == self::FORMS_VALUE_CURRENT_NONE)
		{
			$cCurrent = $cDefault;
		}
		 						
		/* Parse event actions. */
		$cEvent = $this->forms_events_markup($cEvents);			
		
		/* Prepare label markup. */
		$cMarkup .= $this->forms_label_markup($cLabel, self::FORMS_LABEL_TYPE_FIELDSET, $cName, $cID, $cKey, $cValue);		
		
		$cMarkup .= "<script>$(function(){
						$( '#".$cID."' ).".$cFunction."(".$cOptions.");
					});</script>";

		$cMarkup .= "<input type='text' name='".$cName."' id='".$cID."' value='".$cCurrent."'";
		
		if ($bReadOnly==TRUE)
		{
			$cMarkup .= " readonly='readonly'";
		}
		
		$cMarkup .= " /></div>";
		
		/*	Return end result. */
		return $cMarkup;
	}
	
	public function forms_fieldset($cID=NULL, $cLegend=self::FORMS_LEGEND_NONE, $cElements=array(NULL), $cAddOns=array(NULL), $cClass=array("Div"=>"table_row_0", "Fieldset"=>"Standard"))
	{
		/*
		forms_fieldset
		Damon Vaughn Caskey
		2013-03-25
		
		Arrage form input items into a fieldset.
		
		$cID: 		Fieldset ID.
		$cLegend: 	Legend label, if any.
		$cElements:	Markup of field list items (select lists, radio groups, text boxes, etc).
		$cClass:	Style classes.
		*/
		
		$cElementKey	= NULL;	//Single element key.
		$cElementVal	= NULL;	//Single element value.
		
		$cMarkup = '<div class="'.$cClass['Div'].'"><fieldset id="'.$cID.'" class="'.$cClass['Fieldset'].'">';
		
		
		if($cLegend != self::FORMS_LEGEND_NONE)
		{
			$cMarkup .= '<legend>'.$cLegend.'</legend>';	 
		}				
		
		foreach($cAddOns as $cElementKey => $cElementVal)
		{
			$cMarkup .= '<div class="'.$cElementKey.'">'.$cElementVal.'</div>';
		}	
		
		foreach($cElements as $cElementKey => $cElementVal)
		{
			$cMarkup .= '<div class="'.$cElementKey.'">'.$cElementVal.'</div>';
		}		
		
		$cMarkup .= '</fieldset></div>';
		
		$this->cFieldset[$cID] = $cMarkup;
		
		return $this->cFieldset[$cID];
	}
		
}
?>

Author: Damon Caskey

Hello all, Damon Caskey here - the esteemed owner of this little slice of cyberspace. Welcome!

Leave a Reply