#!/usr/bin/perl

# Ze PHP generator à partir d'un Mysql Dump (du SQL, donc)
# Passez lui un fichier à partir d'un pipe, il génèrera un fichier php.
# ex : "makeform.pl < clx_dump.sql"

###############################################################################
# Copyright (C) 2000  Gaétan RYCEKBOER
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
###############################################################################

$l=0;

print "<form action=\"#\">\n";
foreach (<STDIN>) {
    if (/^CREATE TABLE ([\w\d]+) \(/) {
	if (0!=$l) { 
	    print "</table>\n"; 
	    print "<?php # \$result = mysql_query(\"SELECT $select FROM $table\");\n";
	    print "      # \$result = mysql_query(\"UPDATE $table SET $update WHERE $pk='\$$pk'\");\n";
            print "      #  if ((!\$result) || (\$result == 0) ) {\n";
	    print "      #    echo \"ERR: Select sur $table pas passé.\";\n";
	    print "      #    echo mysql_errno(). \": \".mysql_error(). \"<br>\";\n";
	    print "      #  } else {\n";
	    print "      #    list($liste) = mysql_fetch_row(\$result);\n";
	    print "      #  }\n";
	    print "      #  En cas de besoin, remontez ces ligne en haut de la table, et décommentez les. ?>\n";
	}

	$liste = "";
	$select = "";
	$update = "";
	print "<h3>Table : $1</h3>\n<table>\n";
	$table=$1;
	$l=1;
    }
    if ((/^[ \t]*([\w]+)[\t ]+([\w]+)[ \t]*\(?([^\(\)]*)\)?([ \t]+DEFAULT[ \t]+'[ \w\d]+')?([ \t]+NOT NULL)?[ \t]*,.*/) 
	&& (0!=$l)) 
    {
	# REGEXP capable de gérer des ligne stelles que 
	#  type enum('morale','physique') DEFAULT 'physique' NOT NULL,
	# ou 
	#  pole varchar(20),
	# les séquences de [ \t]* sont des séparateurs
	$name=$1;
	$type=$2;
	$long=$3;
	$default=$4;
	$notnull=$5;
	unless ($name eq 'KEY') {

		if (!( $liste eq '')) { 
		    $liste .= ", "; 
		    $select .= ", "; 
		    $update .= ", ";
		}
		$liste .= "\$".$name;
		$select .= $name;
		$update .= "$name='\$$name'";
		if ($default =~ /DEFAULT[ \t]+'([\d\w]*)'/) 
		{ 
		    $default="<?php if (''==\$$name) \$$name=\"$1\"; ?>";
		    $defvar=$1;
		} else { $default=''; }
		if ($default =~ /NOT NULL/) 
		{ 
		    $default="";
		    $notnull="NOT NULL"; 
		}
		if (!($notnull eq '')) {
		    $notnull=''; # Un bout de JS pour vérifier la validité du formulaire à la fin
		}

 	    $_ = $type;
 	    if (/text/) {
 		$type="textarea";
 	    }
 	    if (/varchar/) {
 		$type="text";
 	    }
 	    if (/int/) {
 		$type="text";
 	    }
 	    if (/date/) {
 		$type="text";
 		$size="8";
 	    }
 	    if (/enum/) {
 		$type="select";
 	    }
    
 	    print "<tr>\n  <td><b>".ucfirst($name)."</b></td>\n  <td>";
 	    $_ = $type;
 	    if (/^text$/ | /^password$/) {
 		print $default;
 		print "<input type=\"$type\" name=\"$name\" value=\"<?php echo \$$name ?>\"";
 		if (0!=$long) { print " size=\"$long\" maxlength=\"$long\""; }
 		print ">";
 	    }
 	    if (/^textarea$/) {
 		print $default;
 		print "<textarea name=\"$name\" cols=\"60\" rows=\"3\">";
 		print "<?php echo \$$name ?>";
 		print "</textarea>";
 	    }
 	    if (/^select/) {
 		print $default;
 		print "<select name=\"$name\">\n";
 		foreach $line (split (",",$long)) {
 		    if ($line =~ /^'(.*)'$/) {
 			$line=$1;
 		    }
 		    print "    <option <?php if (\$$name==\"$line\") echo \"selected\"; ?>>$line</option>\n";
 		}
 		print "</select>\n  ";
 	    }
 	    print "</td>\n</tr>\n";
    	}
    }
}

if (0!=$l) { 

	    print "</table>\n"; 
	    print "<?php # \$result = mysql_query(\"SELECT $select FROM $table\");\n";
	    print "      # \$result = mysql_query(\"UPDATE $table SET $update WHERE $pk='\$$pk'\");\n";
            print "      #  if ((!\$result) || (\$result == 0) ) {\n";
	    print "      #    echo \"ERR: Select sur $table pas passé.\";\n";
	    print "      #    echo mysql_errno(). \": \".mysql_error(). \"<br>\";\n";
	    print "      #  } else {\n";
	    print "      #    list($liste) = mysql_fetch_row(\$result);\n";
	    print "      #  }\n";
	    print "      #  En cas de besoin, remontez ces ligne en haut de la table, et décommentez les. ?>\n";

    print '<input type="submit" name="submit" value="Valider">';
    print "\n</form>\n";
}
