<?php 
require "commonincludefiles.php";
	
		
		$db_host="localhost";   
		$db_login="root";       
		$db_pass="";     
		$db_base=$_REQUEST['db_name'];    
		
		$connection = mysql_connect($db_host, $db_login, $db_pass) or die ("Unable to connect!" . "<br /> Error :"  . mysqlDie());
		mysql_select_db($db_base) or die ("Unable to select database!" . "<br /> Error :"  . mysqlDie());
		
		
		
		$sql = "SELECT * FROM ".$_REQUEST['table_name']."";
		$query = dbQuery($sql);
		$columns = dbFields($query);
		$fileContent="";
		$fileContent="<?php \n";
		$fileContent.=" class    ".$_REQUEST['table_name']."Mgmt\n";
		$fileContent.="{ \n";
		
		for($i=0;$i<$columns;$i++){
			$fileContent.="public  $".mysql_field_name($query,$i).";\n";
		} 
		$fileContent.="\n\n function getAllRecords(".'$limit=0'.",".'$start=0'.",".'$condition=""'.",".'$order_field=""'.",".'$order_type=""'.")\n";
    	$fileContent.="{\n";
		$fileContent.='$qry="'."SELECT * FROM ".$_REQUEST['table_name'].'";';
		$fileContent.="\n\n"	;
		$fileContent.='if($condition != "" )'; 
		$fileContent.="{\n";
        $fileContent.='$qry.= $condition;'."\n";
        $fileContent.="} \n";
		
		$fileContent.='if ($order_field !="" && $order_type !="")';
		$fileContent.="{\n";
        $fileContent.='$qry.= " order by $order_field $order_type";';
        $fileContent.="\n}\n\n";
		
		
		$fileContent.='if(intval($limit) > 0 && intval($start) >=0) ';
		$fileContent.="{\n";
        $fileContent.='$qry.= " Limit $start , $limit";';
        $fileContent.="\n}\n\n";
		
		$fileContent.='$out=dbQuery($qry);'."\n";
		$fileContent.='if (dbRows($out)>0){'."\n";
        $fileContent.='return $out;'."\n";							
        $fileContent.='}'."\n";
		$fileContent.='return;'."\n}\n\n\n";
		
		$fileContent.='function add(){'."\n".'$Qry1 = "";'."\n";
		
		for($i=0;$i<$columns;$i++){
			$fileContent.='$Qry1 .= "'.mysql_field_name($query,$i)."   ='".'"  '.'.Escape($this->'.mysql_field_name($query,$i).")".'."'."'".",".'"'.";\n"; 
		} 	
		
		$fileContent.='if($Qry1 != ""){'."\n";
        $fileContent.='$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);'."\n";
        $fileContent.='$qry = "insert into '.$_REQUEST['table_name'].' set " . $Qry1 . " ";'."\n";
        $fileContent.='dbQuery($qry);'."\n";
        $fileContent.='$this->'.mysql_field_name($query,0).'=dbInsertedId();'."\n";
		$fileContent.='} '."\n\n}\n\n";
		
		
		$fileContent.='function edit(){'."\n".' $Qry1 = "";'."\n";
		
		for($i=0;$i<$columns;$i++){
			$fileContent.='$Qry1 .= "'.mysql_field_name($query,$i)."   ='".'"  '.'.Escape($this->'.mysql_field_name($query,$i).")".'."'."'".",".'"'.";\n"; 
		} 	
		
		$fileContent.='if($Qry1 != ""){ '."\n";
        $fileContent.='$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);'."\n";
        $fileContent.='$qry = "update '.$_REQUEST['table_name'].' set " . $Qry1 . " where  '.mysql_field_name($query,0).'=".$this->'.mysql_field_name($query,0).';'."\n";
        $fileContent.='dbQuery($qry);'."\n";
        $fileContent.='}'."\n\n } \n\n\n";
		
		
		$fileContent.='function delete() '."\n";
    	$fileContent.='{ '."\n";	
		$fileContent.='	$qry = "delete from '.$_REQUEST['table_name'].' where '.mysql_field_name($query,0).' in (".$this->'.mysql_field_name($query,0).'.") ";'."\n";
        $fileContent.='	dbQuery($qry);'."\n";
   		$fileContent.='} '."\n\n\n\n\n\n}";
			
		$fileContent.='/* Code For EXTJS START \n';	
		
		for($i=0;$i<$columns;$i++){
			$fileContent.="{\n xtype: 'textfield',\n";
			$fileContent.="name: '".mysql_field_name($query,$i)."',\n";
			$fileContent.="id:'".mysql_field_name($query,$i)."',\n";
			$fileContent.="allowBlank:false,\n";
			$fileContent.="tabIndex:".($i+1).",\n";
			$fileContent.="labelSeparator:'',\n";	
			$fileContent.="fieldLabel :'".mysql_field_name($query,$i).":',\n";
			$fileContent.="width:100\n";
			$fileContent.="},\n";
		}
		$fileContent.="Code For EXTJS END */\n\n\n";	
		
		
		$fileContent.="\n\n/* Code For Save File START \n\n\n";	
		
		$fileContent.='$obj'.$_REQUEST['table_name']."Mgmt= new " .$_REQUEST['table_name']."Mgmt();\n";
		
		for($i=0;$i<$columns;$i++){
			$fileContent.='$obj'.$_REQUEST['table_name']."Mgmt->".mysql_field_name($query,$i)."=".'$_POST['."'".mysql_field_name($query,$i)."'];\n";
		}
		
		$fileContent.='if('.'$obj'.$_REQUEST['table_name']."Mgmt->".mysql_field_name($query,0).' == "0" ||  '.'$obj'.$_REQUEST['table_name']."Mgmt->".mysql_field_name($query,0).'== ""){ '."\n";
		$fileContent.='$obj'.$_REQUEST['table_name']."Mgmt->".'add(); '."\n";
		$fileContent.='} '."\n";
		$fileContent.='else{'."\n";		
		$fileContent.='if($_REQUEST['.'act'.'] == "del")'."\n {";	
		$fileContent.='$obj'.$_REQUEST['table_name']."Mgmt->"."delete();\n }";	
		$fileContent.='else'."\n {";
		$fileContent.='$obj'.$_REQUEST['table_name']."Mgmt->"."edit();\n";	
		$fileContent.='}'."\n";	
		$fileContent.='echo'."'{".'"success"'.":".'"true"'."}'; \n }";
		$fileContent.="\n\nCode For Save File END */\n\n  ?>";	
		
		ob_start();
		echo $fileContent;
		$HtmlCode= ob_get_contents();
		ob_end_flush();
		
		$fso = fopen("inc/temp.php", "w");
		fwrite($fso,$HtmlCode);	
		fclose($fso); 	
	
?>