<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  Sylvain FATOME
 * 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.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/

// vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_ret.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$td = $_GET['td'];
$nvil = intval($_GET['nvil']);
$tx = $_GET['tx']; // Source de l'appel (tx="f" -> depuis une fiche)

$pbm_ville = FALSE;

if ($nvil > 0) {
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_id`='" . $nvil . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        $row = encode_str($row);
        extract($row);
    } else
        $pbm_ville = TRUE;
    $db->disconnect();
} else {
    if ($td <> "add") $pbm_ville = TRUE;
}

switch ($td) {
    case "add" :
        $titre = _("CREATION VILLE");
        $villes_lng = str_majuscules($_SESSION["lng"]);
        break;
    case "edit" :
        $titre = _("MODIFICATION VILLE");
        break;
    case "del" :
        $titre = _("SUPPRESSION VILLE");
        break;
    default :
        $pbm_ville = TRUE;
        break;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Fiche Ville"); ?></title>

    <?PHP include_once("../inc/header.inc.php"); ?>

    <script language="javascript">
        <!--
        var tab_champ = ["nom:cp ; format:txt ; req:Y ; txt:<?PHP echo _("Code Postal"); ?> ; lmx:10; stok:frmok ; stbad:frmbad",
            "nom:nom ; format:txt ; req:Y ; txt:<?PHP echo _("Nom"); ?> ; lmx:40; stok:frmok ; stbad:frmbad",
            "nom:pays; format:liste ; req:Y ; txt:<?PHP echo _("Pays"); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:dep; format:liste ; req:Y ; txt:<?PHP echo _("Département"); ?> ; vmn:1; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_ajax_dep();
            <?PHP
            switch ($td)
            {
                case "add" :
                case "edit" :
                    echo "init_valid_form('enr','villes_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                    break;
                case "del" :
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
            }
            ?>
            add_func_click('quit', 'sclose', 'fscomp');
            Focus_first();
        }

        $(document).ready(function () {
            init_all();
        });
        //-->
    </script>
</head>

<body>
<?php
if ($tx <> "f")
    include_once("../inc/entete.inc.php");
if ($pbm_ville == FALSE) {
    cre_ent_form($titre, 'villes_lst.php', $_SERVER['HTTP_REFERER'], '');
    ?>
    <form name="villes_maj" id="villes_maj" method="post" action="villes_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
        <input name="tx" type="hidden" id="tx" value="<?PHP echo $tx ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <?PHP if (($td == "edit") OR ($td == "del")) { ?>
            <input name="nville" type="hidden" id="nville" value="<?PHP echo $villes_id; ?>">
        <?PHP } ?>
        <table width="100%" border="0">
            <tr>
                <td width="160" class="rubfrm" id="f_codepays" align="right"><?PHP echo _("Pays"); ?></td>
                <td>
                    <?PHP
                    // Création de la liste des pays
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBPAYS . "`";
                    if ($td == "edit")
                        $rech .= " WHERE pays_lng='" . $villes_lng . "'";
                    $rech .= " ORDER BY pays_nom";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('pays', 'pays', $rows, 'pays_code', 'pays_nom', '', $villes_codepays, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_dep" align="right"><?PHP echo _("Département"); ?></td>
                <td>
                    <?PHP
                    // Création de la liste des départements
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBDEP . "` WHERE `dep_codepays`='" . $villes_codepays . "'";
                    if ($td == "edit")
                        $rech .= " AND dep_lng='" . $villes_lng . "'";
                    $rech .= " ORDER BY dep_code";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('dep', 'dep', $rows, 'dep_code', 'dep_code', 'dep_nom', $villes_dep, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="10%" class="rubfrm" id="f_code" align="right"><?PHP echo _("Code Postal"); ?></td>
                <td width="43%">
                    <?PHP if ($td == "add") { ?>
                        <input name="cp" type="text" id="cp" value="<?PHP echo $villes_cp ?>" size="30">
                        <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                    <?PHP } else { ?>
                        <input name="cp" type="text" id="cp" value="<?PHP echo $villes_cp ?>" size="30" readonly>
                    <?PHP } ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_nom" align="right"><?PHP echo _("Nom"); ?></td>
                <td><input name="nom" type="text" id="nom" value="<?PHP echo $villes_nom ?>" size="50">
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>"></td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_lng" align="right"><?PHP echo _("Langue"); ?></td>
                <td>
                    <?PHP
                    if ($td == "add")
                        cre_select('lng', 'lng', $tab_lngdisp, $villes_lng);
                    else // Edition ou suppression = zone non modifiable (affichage seul)
                    {
                        ?>
                        <input name="lng" type="hidden" id="lng" value="<?PHP echo $villes_lng ?>">
                        <input name="nlng" type="text" id="nlng"
                               value="<?PHP echo $tab_lngdisp[strtolower($villes_lng)] ?>" size="50" maxlength="50"
                               readonly>
                    <?PHP
                    }
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <?PHP
            switch ($td) {
                case "add" :
                case "edit" :
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _("Enregistrer") . '">';
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _("Réinitialiser") . '">';
                    break;
                case "del" :
                    echo '<input name="del" type="submit" class="bton_std" id="del" value="' . _("Supprimer") . '">';
                    break;
            }
            if ($tx == "f")
                echo '<input name="quit" type="button" class="bton_std" id="quit" value="' . _("Quitter") . '">';
            ?>
        </p>
    </form>
<?PHP
} else {
    if ($tx == "r")
        close_fen_auto('', 'fscomp');
    else
        pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
}
if ($tx <> "f")
    include_once("pied.php");
?>
</body>
</html>