Location: PHPKode > projects > FxFramework > fxFrm/fxForm.php
<?php
include_once('fxControls.php');
include_once('fxValidators.php');
include_once('fxDataBase.php');
include_once('fxCsvParser.php');
include_once('fxErrors.php');

class FxForm
{
	private $_target = '';
	private $_postBack = '';
	private $_arrValidators = Array();
	private $_arrControls = Array();
	private $_arrFileDirs = Array(); //For fxFileUploadBox
	private $_arrFileNames = Array(); //For fxFileUploadBox
	private $_uId;
	private $_dataBaseConnections;
	private $_isMultipart;
	private $_hasCalendar = false;
	
	public static function &fxInit()
	{
		if(!isset($_GET['uID'])){
			$uId = $_POST['____uID'];
		}
		else
		{
			$uId = base64_decode($_GET['uID']);
		}

		if(($uId != '') && (isset($_SESSION['forms'][$uId])))
		{
			$form = &$_SESSION['forms'][$uId];
		}
		else
		{
			$uId = uniqid();
			$form = new FxForm;
			$_SESSION['forms'][$uId] = $form;
		}
		foreach($form->_arrControls as $key => $value)
		{
			if($_POST){
				if($form->_arrControls[$key] == 'fxFile')
				{
					$target_path = $form->_arrFileDirs[$key]."/";
					$target_path = $target_path.basename( $_FILES[$key]['name']); 
					if(@!move_uploaded_file($_FILES[$key]['tmp_name'], $target_path)) {
					  	FxError::fxCritical("Ther been an error while uploading the file to '".$target_path."'");  
					} 
					$form->_arrFileNames[$key] = $target_path;
				}
				else 
				{
					$form->_arrControls[$key] = $_POST[$key];
				}
			}
		}

		$form->_uId = $uId;

		return $form;
	}

	public static function fxDestroy()
	{
		if(!isset($_GET['uID'])){
			$uId = $_POST['____uID'];
		}
		else
		{
			$uId = base64_decode($_GET['uID']);
		}

		if($uId != '')
		{
			unset($_SESSION['forms'][$uId]);
			print "All gone";
		}
	}
	
	function fxFormBegin($postBack = '', $isMultipart = false, $method = "post")
	{
		$this->_isMultipart = $isMultipart;
		$enctype = $isMultipart ? "enctype='multipart/form-data'" : "";
		$postBack = $postBack == '' ? '' : "action='".$postBack."'";
		print "<form ".$enctype." ".$postBack." method='".$method."' onSubmit='return validateForm();'>\n";
		print "<input type='hidden' name='____uID' id='____uID' value='".$this->_uId."'/>\n";
	}

	function fxFormEnd()
	{
		$jsScript = "";
		$valFunction = "true";
		$valVars = "";
		$cont = 0;
		if(count($this->_arrValidators))
		{
			foreach($this->_arrValidators as $key => $validator)
			{
				foreach($validator as $validatorScript)
				{
					$varNom = "____".$key."_".($cont++)."____";
					$var = "var ".$varNom." = ".$validatorScript['name'].";\n";
					$valVars .= $var;
					$valFunction .= " && ".$varNom;
					$jsScript .= $validatorScript['code']."\n";
				}
			}
		}
		print "
		<script laguange='JavaScript'>
		".$jsScript."
		function validateForm()
		{
			".$valVars."
			return (".$valFunction.");
		}
		</script>
		</form>";
	}

	//THE VALIDATORS
	function fxNotNullValidator($assocControl, $errMsg)
	{
		$fnName = '';
		$this->_arrValidators[$assocControl]['notNull']['code'] = FxNotNullValidator::validatorScript($assocControl, $errMsg, $fnName);
		$this->_arrValidators[$assocControl]['notNull']['name'] = $fnName;
		print FxNotNullValidator::validator($assocControl);
	}

	function fxRegExpValidator($assocControl, $regExp, $errMsg)
	{
		$fnName = '';
		$this->_arrValidators[$assocControl]['regExp']['code'] = FxRegExpValidator::validatorScript($assocControl, $regExp, $errMsg, $fnName);
		$this->_arrValidators[$assocControl]['regExp']['name'] = $fnName;
		print FxRegExpValidator::validator($assocControl);
	}


	//LOS CONTROLES
	function fxTextBox($name, $id, $style, $classname, $value = "")
	{
		if(!isset($this->_arrControls[$name])){
			$this->_arrControls[$name] = $value;
		}
		print FxTextBox::textBox($name, $id, $style, $classname, $this->_arrControls[$name]);
	}
	
	function fxPasswordBox($name, $id, $style, $classname, $value = "")
	{
		if(!isset($this->_arrControls[$name])){
			$this->_arrControls[$name] = $value;
		}
		print FxPasswordBox::passwordBox($name, $id, $style, $classname, $this->_arrControls[$name]);
	}
	
	function fxFileInputBox($name, $id, $style, $classname, $fileDir)
	{
		if(!$this->_isMultipart)
		{
			FxError::fxCritical("You are adding a fxFileInputBox but the form enctype is not 'multipart/form-data'.");
		}
		if(!isset($this->_arrControls[$name])){
			$this->_arrControls[$name] = 'fxFile';
		}
		$this->_arrFileDirs[$name] = $fileDir;
		print FxFileUpload::fileUpload($name, $id, $style, $classname);
	}
	
	function fxGetUploadedFileName($control)
	{
		if(isset($this->_arrFileNames[$control]))
		{
			return $this->_arrFileNames[$control];
		}
		else 
		{
			return false;
		}
	}
	
	function fxSubmitButton($name, $id, $style, $classname, $value = "Send")
	{
		print FxSubmit::submitButton($name, $id, $style, $classname, $value);
	}

    function fxPostBackLink($postBackURL, $value)
    {
    	print FxPostBackLink::postBackLink($postBackURL, $value, $this->_uId);
    }
    
    function fxDbComboBox($connId, $sql, $valueField, $labelField, $name, $id, $style = "", $classname = "", $value = "")
    {
    	if(!is_a($this->_dataBaseConnections[$connId], "FxDataBase"))
    	{
    		FxError::fxCritical("The specified database connection does not exist.");
    	}
    	
    	if(!isset($this->_arrControls[$name])){
			$this->_arrControls[$name] = $value;
		}
    	print FxDbComboBox::dbComboBox($this->_dataBaseConnections[$connId], $sql, $valueField, $labelField, $name, $id, $style, $classname, $value);
    }
    
    function fxDateInput($name, $id, $style, $classname, $value = "", $lang='en')
    {
    	print FxDateInput::dateInput(!$this->_hasCalendar, $name, $id, $style, $classname, $value, $lang);
    	$this->_hasCalendar = true;
    }
    
    //DATA BASE SUPPORT
    /**
     * Adds a new database conection for later use
     *
     * @param string $host Name or IP of data base server
     * @param string $user User name
     * @param string $password
     * @param string $dataBase
     * @param string[optional] $driver Database driver
     * 
     * @return Return a dabase unique id for use with database functions
     */
    function fxDbAddDataBase($host, $user, $password, $dataBase, $driver = 'mysql')
    {
    	$dbId = uniqid("fxDb");
    	$this->_dataBaseConnections[$dbId] = new FxDataBase($host, $user, $password, $dataBase, $driver);
    	return $dbId;
    }

    function fxDbDestroy($dbId)
    {
    	unset($this->_dataBaseConnections[$dbId]);
    }
    
    function fxDbQuery($dbId, $query, &$recordSet = "")
    {
    	if(!is_a($this->_dataBaseConnections[$dbId], "FxDataBase"))
    	{
    		FxError::fxCritical("The specified database connection does not exist.");
    	}
    	
    	if(is_array($query))
    	{	
    		if($this->_dataBaseConnections[$dbId]->fxExecuteTransaction($query))
	    	{
	    		return true;
	    	}
	    	else
	    	{
	    		return false;
	    	}
    	}
    	else 
    	{
	    	if($this->_dataBaseConnections[$dbId]->fxDbQuery($query, &$recordSet))
	    	{
	    		return true;
	    	}
	    	else
	    	{
	    		return false;
	    	}
    	}
    }

    function fxDbExists($dbId, $table, $condition)
    {
    	if(!is_a($this->_dataBaseConnections[$dbId], "FxDataBase"))
    	{
    		FxError::fxCritical("The specified database connection does not exist.");
    	}
    	
    	return $this->_dataBaseConnections[$dbId]->fxDbExists($table, $condition);
    }
    
    
    //CSV PARSING SUPPORT
    function fxCsvParseFile($fileName, $separator = ';', $makeAssoc = false, $header = '')
    {
    	@$fCont = file_get_contents($fileName);
    	if($fCont === false)
    	{
    		FxError::fxCritical("Cannot open file '".$fileName."' for Csv Parser.");
    	}
    	
    	return FxCsvParser::fxCsvParse($fCont, $separator, $makeAssoc, $header);
    }
      //DEBUGG
      function fxShowStatus()
      {
      	foreach($_SESSION['forms'] as $key => $value)
      	{
      		print $key."<br>";
      		$form = $_SESSION['forms'][$key];
	      	foreach($form->_arrControls as $key => $value)
	   		{
				print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$key." -> ".$form->_arrControls[$key]."<br>";
	   		}
	   	}
      }
}

?>
Return current item: FxFramework