<?php
/**
 * @file generer_requete_insert.php
 * @brief Ce script va générer du code SQL d'insertion à partir de données
 *
 * @author hughes monget
 * @see http://monget.com/
 */
$str_table = '';
$str_texte = '';
$arr_str_code = array();
if (isset($_REQUEST['texte']) && is_string($_REQUEST['texte']) && strlen($_REQUEST['texte'])
    && isset($_REQUEST['table']) && is_string($_REQUEST['table']) && strlen($_REQUEST['table'])
    && isset($_REQUEST['bouton']) && is_string($_REQUEST['bouton']))
{
    $str_table = $_REQUEST['table'];
    $str_texte = $_REQUEST['texte'];
    if (get_magic_quotes_gpc())
    {
        $str_texte = stripslashes($str_texte);
    }
    $arr_str_code = explode("\n", $str_texte);
    $arr_str_code = array_map('trim', $arr_str_code);
    $arr_str_code = array_filter($arr_str_code);
    //natcasesort($arr_str_code);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Générer une requête sql INSERT</title>
        <style type="text/css">
        <!--
        body *
        {
            font-family: monospace;
            font-size: 8pt;
        }
        textarea
        {
            width: 600px;
        }
        form
        {
            margin: 0;
        }
        div
        {
            margin: 0 0 5px 0;
        }
        -->
        </style>
        <script type="text/javascript">
        <!--;
            function on_load()
            {
                if (document && document.getElementById)
                {
                    var obj_input = document.getElementById('texte');
                    if (obj_input && obj_input.focus)
                    {
                        obj_input.focus();
                    }
                }
            }
        //-->
        </script>
    </head>
    <body onload="on_load();">
        <form method="post" action="">
            <div>
                <label for="table">Table:</label> <input id="table" name="table" value="<?php echo htmlspecialchars($str_table, ENT_QUOTES); ?>" />
            </div>
            <div>
                <textarea id="texte" name="texte" rows="20" cols="80"><?php echo htmlspecialchars($str_texte, ENT_QUOTES); ?></textarea>
            </div>
            <div>
                <input type="submit" class="submit" name="bouton" value="Générer" />
            </div>
        </form>
<?php
if ($arr_str_code)
{
    ob_start();
    $ii = 0;
    $int_id_filiere = 0;
    echo "truncate `{$str_table}`;\n";
    foreach ($arr_str_code as $str_code)
    {
        if (preg_match('/^#(\d)$/', $str_code, $arr_match))
        {
            $int_id_filiere = intval($arr_match[1]);
        }
        else
        {
            $ii++;
            list($str_code, $id_departement) = explode(';', $str_code);
            $str_libelle = '"'. mysql_escape_string($str_code) . '"';
            //echo "insert `{$str_table}` (`id_{$str_table}`, `libelle_{$str_table}`) values ($ii, $str_libelle);\n";
            //echo "insert `{$str_table}` (`id_{$str_table}`, `libelle_{$str_table}`, `id_filiere`) values ($ii, $str_libelle, {$int_id_filiere});\n";
            echo "insert `{$str_table}` (`id_{$str_table}`, `libelle_{$str_table}`, `id_departement`) values ($ii, $str_libelle, {$id_departement});\n";
        }
    }
    $str_code = ob_get_clean();
    $int_ligne = substr_count($str_code, "\n") + 1;
    echo '<div><textarea rows="',$int_ligne,'" cols="80" onclick="this.select();">',htmlspecialchars($str_code, ENT_QUOTES),'</textarea></div>';
}
echo '<hr />';
highlight_file(__FILE__);
?>
    </body>
</html>