<?php require_once("../classes/DbConnection.php");
/* This version of dtf.php (DynamicTableForm) assumes that :
 * The db table has a column called id (INT AUTO_INCREMENT) which you should not specify when writing the field names array
 * NOTE: You must edit the dtf.js as follows:
 * 1. Change the 'ajax({ url:' parameter to the correct location
 * 2. Change the 'ajax({ data: (dtf:' parameter to the correct table name
 * 3. Change the table IDs used to the correct strings which should be 'DbName+TblName'
 * 4. Change the database name in the db connction script to use the correct database
 */
class dtf {
	private $nOcols=0;
	private $dbConn=NULL;
	public $db;
	private $dbName=NULL;
	private $dbTableName=NULL;
	private $dbFieldNamesArr=NULL;
	private $dbFieldTypesArr=NULL;
	
	private $fieldWidthsArr=NULL;
	private $fieldAlignmentsArr=NULL;
	
	private $fieldNamesArr=NULL;
	private $fieldUIArr=NULL;
	private $fieldToolTipsArr=NULL;
	private $fieldDataSrcArr=NULL;
	
    function __construct(){
		$this->db=new DbConnection();
	}
	
	public function setDBName($val){ $this->dbName=$val; }
	public function setDBTableName($val){ $this->dbTableName=$val; }
	public function setDBFieldNamesArr($val){ $this->dbFieldNamesArr=$val; $this->nOcols=sizeof($val); }
	public function setDBFieldTypesArr($val){ $this->dbFieldTypesArr=$val; }
	
	public function setFieldWidthsArr($val){ $this->fieldWidthsArr=$val; }
	public function setFieldAlignmentsArr($val){ $this->fieldAlignmentsArr=$val; }
	
	public function setFieldNamesArr($val){ $this->fieldNamesArr=$val; }
	public function setFieldUIArr($val){ $this->fieldUIArr=$val; }
	public function setFieldToolTipsArr($val){ $this->fieldToolTipsArr=$val; }
	public function setFieldDataSrcArr($val){ $this->fieldDataSrcArr=$val; }
	
	public function generate(){
		$dbTb_ID="{$this->dbName}{$this->dbTableName}";
		if($this->nOcols<50){
			echo "<div class='dtfcontainer'>";
			echo "<div align='center' class='dtfinfobar' id='dtfinfobar{$dbTb_ID}'></div>";
			echo "<table border='0' cellpadding='0' cellspacing='0' class='dtf' id='{$dbTb_ID}'>";
			
			//Create the query, table headers, and field arrays used to create the JSONs for the fields in one loop
			echo "<tr>";
			$qry="select ";
			$fnArr="new Array('{$this->dbTableName}'";
			$fgArr="new Array('{$this->dbTableName}'";//the field GUI array
			$fdSrcArr="[['0']";//the field data source array
			
			for($j=0; $j<$this->nOcols; $j++){
				$tt=$this->fieldDataSrcArr[$j];
				if($tt==NULL){
					$ttArr="['0']";
				}else{
					$ttArr="['0'";
					for($k=0; $k<sizeof($tt); $k++){
						$ttArr=$ttArr.",'{$tt[$k]}'";
					}
					$ttArr=$ttArr."]";
				}
				$fdSrcArr=$fdSrcArr.",{$ttArr}";
				
				$qry=$qry.$this->dbFieldNamesArr[$j].",";
				echo "<th id='{$this->dbFieldNamesArr[$j]}'";
					if($this->fieldWidthsArr!=NULL) echo " width='{$this->fieldWidthsArr[$j]}'";
					if($this->fieldAlignmentsArr!=NULL) echo " align='{$this->fieldAlignmentsArr[$j]}'";
					if($this->fieldToolTipsArr!=NULL) echo " title='{$this->fieldToolTipsArr[$j]}'";
				echo ">{$this->fieldNamesArr[$j]}</th>";
				$fnArr=$fnArr.",'{$this->dbFieldNamesArr[$j]}'";
				$fgArr=$fgArr.",'{$this->fieldUIArr[$j]}'";
			}
			
			$qry=$qry."id from {$this->dbTableName} order by id limit 50";
			$fnArr=$fnArr.")";
			$fgArr=$fgArr.")";
			$fdSrcArr=$fdSrcArr."]";
			echo "</tr>";
			
			//The table body
			$d=$this->db->executeQuery($qry);
			while($rows=mysql_fetch_array($d[2])){
				echo "<tr id='{$rows[id]}'>";
				for($j=0; $j<=$this->nOcols; $j++){
					if($j==$this->nOcols){ //it's the last column which displays the controls. dtfDeleteRow's last par identifies an infoBar
						echo "<td class='last'><div class='dtfDelBut' title='Delete Row' onclick=\"javascript:dtfDeleteRow('{$rows['id']}','{$this->dbTableName}','{$dbTb_ID}') \"></div></td>";
					}else{
						$fN=$this->dbFieldNamesArr[$j];
						echo "<td id='{$j}'";
							if($this->fieldAlignmentsArr!=NULL) echo " align='{$this->fieldAlignmentsArr[$j]}'";
							if($this->fieldToolTipsArr!=NULL) echo " title='{$this->fieldToolTipsArr[$j]}'";
						echo ">";
						//insert the data considering the GUI container to use
						switch($this->fieldUIArr[$j]){
							case "select":
							$fSrc=$this->fieldDataSrcArr[$j];
							echo "<select name='{$this->dbFieldNamesArr[$j]}'><option></option>";
							for($n=0; $n<sizeof($fSrc); $n++){
								$nStr=$fSrc[$n];
								if($nStr==$rows[$fN]) $seltracker=" selected";
								else $seltracker="";
								echo "<option{$seltracker}>{$nStr}</option>";
							}
							echo "</select>";
							break;
							
							case "text":
							echo "<input type='text' value='{$rows[$fN]}' name='{$this->dbFieldNamesArr[$j]}'/>";
							break;
						}
						echo "</td>";
					}
				}
				echo "</tr>";
			}
			
			//The insert row for adding new data--------------------------------------------------------------------------------
			
			echo "<tr id='dtfInsertRow'>";
			for($j=0; $j<=$this->nOcols; $j++){
				if($j==$this->nOcols){
					echo "<td class='last'><div class='dtfSaveBut' title='Save Row' onclick=\"javascript:dtfSaveNew({$fnArr},'{$dbTb_ID}',{$fgArr},{$fdSrcArr}, '{$lastRowID}') \" ></div></td>";//[[0],['wee',4]]
				}else{
					echo "<td id='{$j}'";
						if($this->fieldAlignmentsArr!=NULL) echo " align='{$this->fieldAlignmentsArr[$j]}'";
						if($this->fieldToolTipsArr!=NULL) echo " title='{$this->fieldToolTipsArr[$j]}'";
					echo ">";
					//insert the data considering the GUI container to use
					switch($this->fieldUIArr[$j]){
						case "select":
						$fSrc=$this->fieldDataSrcArr[$j];
						echo "<select name='{$this->dbFieldNamesArr[$j]}' id='{$this->dbFieldNamesArr[$j]}'><option></option>";
						for($n=0; $n<sizeof($fSrc); $n++){
							$nStr=$fSrc[$n];
							$rv=$rows['{$this->dbFieldNamesArr[$j]}'];
							if($nStr==$rows['{$rv}']) $seltracker=" selected";
							else $seltracker="";
							echo "<option{$seltracker}>{$nStr}</option>";
						}
						echo "</select>";
						break;
											
						case "text":
						echo "<input type='text' value='' name='{$this->dbFieldNamesArr[$j]}' id='{$this->dbFieldNamesArr[$j]}' />";
						break;
					}
					echo "</td>";
				}
			}
			
			echo "</tr>";
			
			//------------------------------------------------------------------------------------------------------------------
			echo "</table></div>";
		}
	}	
}
?>