<?php

session_start();
$dbuser = "root";
$dbpass = "castillejos";
$dbhost = "localhost";
$con = mysqli_connect($dbhost,$dbuser,$dbpass);
if (!$con){
	die('Error N:' . mysqli_errno($con$con) . ": " . mysqli_error($con));
}else{
	echo "CONECTADO A DB...<br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>

</head>
<link rel="stylesheet" href="css/estilo1.css" type="text/css"/>
<body>
<form id="form1" name="form1" method="post" action="">
  <div align="center"><span class="text_mensaje">
    
    GENERA CODIGO CL PARA SIAD </span> <br>
  </div>
  <table  align="center" >
    <tr>
      <td colspan="3"><div align="center">ACCIONES</div></td>
    </tr>
    <tr>
      <td>Base: </td>
      <td><input name="base" type="text" id="base" tabindex="1" maxlength="255" 
  <?php
 
  if (isset($_REQUEST['base'])){
  	echo ' value = "'.$_REQUEST['base'].'"';
  }
  ?>
  /></td>
      <td>Tabla a buscar:
<input name="tabla" type="text" id="tabla" tabindex="1" maxlength="255" 
  <?php
 
  if (isset($_REQUEST['tabla'])){
  	echo ' value = "'.$_REQUEST['tabla'].'"';
  }
  ?>
  /></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center">
        <input name="buscar" type="submit" id="buscar" value="Buscar" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="genCod" type="submit" id="genCod" value="Genera Codigo" />
      </div></td>
    </tr>
  </table>

  <p>
    <label>
 
    </label>
  </p>
</form>
<?php

if(isset($_POST['buscar'])){
	$BD=$_POST['base'];
	$nomTab=$_POST['tabla'];
	$rs=mysqli_db_query($BD,"SELECT * from $nomTab limit 1");
	
	echo '<table name="desTab"  align="center">';
		echo '<tr>
			  <th>'.'Field Name'.'</th>
			  <th>'.'Field Name Trans'.'</th>
		     <tr>';
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		echo '<tr>
			  <td>'.mysqli_field_name($rs,$i).'</td>
			   <td>'.transCad(mysqli_field_name($rs,$i)).'</td>
		      <tr>';
	}
	echo '</table>';
	
}

if(isset($_POST['genCod'])){
	$BD=$_POST['base'];
	$nomTab=$_POST['tabla'];
	$rs=mysqli_db_query($BD,"SELECT * from $nomTab limit 1");
	echo '<div align="center"><textarea name="textfield" cols="150" rows="100">';
	echo 'class '.transCad(substr($nomTab,2,(strlen($nomTab)-2)))."{\n";
	
	createClassVar($rs);
	echo "\n";
	createFunCons($nomTab);
	echo "\n";
	createFunResDat($rs);
	echo "\n";
	createFunAgre($rs);
	echo "\n";
	createFunMod($rs);
	echo "\n";
	createFunBus($rs);
	echo "\n";
	createFunEli($rs);
	echo "\n";
	createFunUtils();
	echo "\n";
	echo '}';
	echo '</textarea></div>';
	echo '<div align="center"><textarea name="sta_asi" cols="150" rows="100">';	
	createStaAsi($rs);
	echo '</textarea></div>';
}

function createClassVar($rs){
	echo "\tvar \$tabla;\n";
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		echo "\tvar \$".mysqli_field_name($rs,$i).";\n";
	}
	echo "\tvar \$lastInsId;\n";	
}

function createStaAsi($rs){
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		echo "\$data['".transCad(mysqli_field_name($rs,$i))."']=\$_POST[".mysqli_field_name($rs,$i)."];\n";
	}
}

function createFunCons($nomTab){
	echo "\tfunction ".transCad(substr($nomTab,2,(strlen($nomTab)-2)))."(\$data){\n";
	echo "\t\t\$this->tabla='$nomTab';\n";	
	echo "\t\t\$this->resetData(\$data);\n";
	echo "\t}\n";
}

function createFunResDat($rs){
	echo "\tfunction resetData(\$data){\n";
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		echo "\t\t\$this->".mysqli_field_name($rs,$i)."=\$this->checkData(\$data['".transCad(mysqli_field_name($rs,$i))."'],'');\n";
	}
	echo "\t}\n";
}

function createFunAgre($rs){
	echo "\tfunction agregar(){\n";
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		if(mysqli_field_flags($rs,$i)!="not_null primary_key auto_increment")
			$arrFields[]=mysqli_field_name($rs,$i)."='\$this->".mysqli_field_name($rs,$i)."'";
	}
	echo "\t\t\$sq_create=\"INSERT INTO \$this->tabla SET ".implode(", ",$arrFields)."\";\n";
	echo "		
		\$rs=mysqli_query($con ,\$sq_create);
		if(\$rs){
			\$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;\n";
	echo "\t}\n";
}

function createFunMod($rs){
	echo "\tfunction modificar(){\n";
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		if(mysqli_field_flags($rs,$i)!="not_null primary_key auto_increment")
			$arrFields[]=mysqli_field_name($rs,$i)."='\$this->".mysqli_field_name($rs,$i)."'";
		else	
			$pkMod=mysqli_field_name($rs,$i)."='\$this->".mysqli_field_name($rs,$i)."'";
	}
	echo "\t\t\$sq_update=\"UPDATE \$this->tabla SET ".implode(", ",$arrFields)." WHERE $pkMod\";\n";
	echo "		
		\$rs=mysqli_query($con ,\$sq_update);
		if(\$rs)
			return 1;
		else
			return 0;\n";
	echo "\t}\n";
}

function createFunEli($rs){
	echo "\tfunction eliminar(){\n";
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		if(mysqli_field_flags($rs,$i)=="not_null primary_key auto_increment"){
			$pkMod=mysqli_field_name($rs,$i)."='\$this->".mysqli_field_name($rs,$i)."'";
			break;
		}
	}
	echo "\t\t\$sq_delete=\"DELETE FROM \$this->tabla WHERE $pkMod\";\n";
	echo "		
		\$rs=mysqli_query($con ,\$sq_delete);
		if(\$rs)
			return 1;
		else
			return 0;\n";
	echo "\t}\n";
}

function createFunBus($rs){
	echo "\tfunction busReg(){\n";
	for($i=0;$i<mysqli_num_fields($rs);$i++){
		if(mysqli_field_flags($rs,$i)=="not_null primary_key auto_increment"){
			$pkMod=mysqli_field_name($rs,$i)."='\$this->".mysqli_field_name($rs,$i)."'";
			break;
		}
	}
	echo "\t\t\$sq_retrieve=\"SELECT * FROM \$this->tabla WHERE $pkMod\";\n";
	echo "		
		\$rs=mysqli_query($con ,\$sq_retrieve);
		if(\$rs)
			return \$rs;
		else
			return 0;\n";
	echo "\t}\n";
}

function createFunUtils(){
	echo "	
	function checkData(\$data,\$default){
		if(isset(\$data)) 
			\$auxData=\$data;
		else 
			\$auxData=\$default;
		if(get_magic_quotes_gpc()) 
			\$auxData = stripslashes(\$auxData);
		\$auxData=htmlentities(\$auxData);
		return mysqli_real_escape_string($con,\$auxData);
	}\n
	function getLastInsId(){
		return \$this->lastInsId;
	}\n
	function getMySQLError(){
		return 'Error N:' . mysqli_errno($con) . \": \" . mysqli_error($con); 
	}\n";
}

function transCad($cadena){
	$lonStr=strlen($cadena);
	$fl=false;
	$newStr='';
	$k=0;
	for($i=0;$i<$lonStr;$i++){
		if(($k>0 && ($k%3)==0) || $cadena[$i]=='_'){
			if($cadena[$i]!='_'){
				$newStr.=strtoupper($cadena[$i]);
			}
			else{
				$k=2;
			}
		}
		else{
			$newStr.=strtolower($cadena[$i]);
		}
		$k++;
	}
	return $newStr;
}
?>
</body>
</html>
