<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  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/config.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_repl.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
fix_magic_quotes();
$_GET = clean_str($_GET);

$repli_ok = TRUE;
$tab_dep = [];

$td = @$_GET['td']; // On récupère le type en ligne de commande

if ($td == "")
    $td = @$_POST['td']; // On récupère le type en ligne de commande

if (isset($_GET['id'])) {
    $id = intval(@$_GET['id']);
    $row = lit_enr_replm($id);
    if (is_array($row)) {
        extract($row);
        $tab_dep = explode('_', $replm_dep);
    } else
        $repli_ok = FALSE;
} else {
    if ($td <> "add")
        $repli_ok = FALSE;
}

$titre = '';
switch ($td) {
    case "add" :
        $titre = _("CREATION REPLICATION");
        $replm_typ = -1;
        $replm_mode = -1;
        $replm_sctact = -1;
        $replm_ssites = -1;
        break;
    case "edit" :
        $titre = _("MISE A JOUR REPLICATION");
        break;
    case "del" :
        $titre = _("SUPPRESSION REPLICATION");
        break;
    default :
        $repli_ok = FALSE;
        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 _("Réplication"); ?></title>

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

    <script language="javascript">
        <!--
        var tab1_champ = ["nom:typ ; format:liste ; req:Y ; txt:<?PHP echo _("Type de réplication"); ?>; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:nom ; format:txt ; req:Y ; txt:<?PHP echo _("Nom du Modèle"); ?>; stok:frmok ; stbad:frmbad",
            "nom:mysql_dbname ; format:liste ; req:Y ; txt:<?PHP echo _("Nom base de données"); ?>; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:cat ; format:liste ; req:Y ; txt:<?PHP echo _("Catégorie"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:pays ; format:liste ; req:Y ; txt:<?PHP echo _("Pays"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:dep[] ; format:listem ; req:Y ; txt:<?PHP echo _("Départements"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:mrepli ; format:liste ; req:Y ; txt:<?PHP echo _("Mode réplication"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:sctact ; format:liste ; req:Y ; txt:<?PHP echo _("Synchroniser les contacts"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:ssites ; format:liste ; req:Y ; txt:<?PHP echo _("Synchroniser les sites secondaires"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <script type="text/javascript">
        <!--
        $(function () {
            $("#nom").Setcase({caseValue: 'upper', changeonFocusout: true});
        });

        function init_all() {
            init_Body();
            init_txtfromvalsel('dep', 'deptxt');
            <?php
             switch ($td)
             {
                case "add" :
                case "edit" :
                    echo "init_valid_form('enr','chxtbl','".$_SESSION['date_fmtshort']."',tab1_champ,'','');";
                    break;
                case "view" :
                    echo "readonly_all();";
                    break;
                case "del" :
                    echo "readonly_all();";
                    echo "warn_del('');";
                    break;
             }
             ?>
            init_ajax_dep2();
            init_back('annul', 'click');
            Focus_first();
        }

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

</head>

<body>
<?php
include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<p>&nbsp;</p>
<?PHP
if ($repli_ok == TRUE) {
    cre_ent_form($titre, 'repl_lst.php', $_SERVER['HTTP_REFERER'], '');
    ?>
    <!-- Formulaire permettant la sélection de la table à répliquer -->
    <form name="chxtbl" id="chxtbl" method="post" action="repl_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="id" type="hidden" id="id" value="<?PHP echo $replm_id; ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">

        <table width="100%" border="0">
            <tr>
                <td class="rubfrm" id="f_typ" align="right"><?PHP echo _("Type de réplication"); ?></td>
                <td>
                    <?PHP
                    cre_select('typ', 'typ', $typ_repli, $replm_typ, '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td width="50%" align="right" class="rubfrm"><?PHP echo _("Nom du modèle"); ?></td>
                <td>
                    <input name="nom" type="text" id="nom" value="<?PHP echo $replm_nom ?>" size="50">
                    <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                </td>
            </tr>
            <tr>
                <td width="50%" align="right"
                    class="rubfrm"><?PHP echo _("Nom de la base de données secondaire"); ?></td>
                <td>
                    <?PHP
                    echo '<select name="mysql_dbname" id="mysql_dbname">';
                    echo '<option value="">-- Choisissez une table (base) --</option>';
                    // Connexion à la base principale
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    foreach ($db->get_col("SHOW DATABASES", 0) as $database) {
                        if (($database <> DBNAME) AND (trim($database) <> "")) {
                            $db->select($database);
                            foreach ($db->get_col("SHOW TABLES LIKE '" . TBCLI . "'", 0) as $table_name) {
                                echo '<option value="' . $table_name . '"';
                                if ($replm_dbname == $table_name)
                                    echo " selected";
                                echo '>' . $table_name . ' (' . $database . ')</option>';
                            }
                        }
                    }
                    $db->disconnect();
                    echo '</select>';
                    echo ' <img src="../img/oblig.png" align="absmiddle" alt="' . _("Champ obligatoire") . '">';
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_cat" align="right"><?PHP echo _("Catégorie de client"); ?></td>
                <td>
                    <?PHP
                    $tab_first_options = [
                        "*" => _("-- TOUTES LES CATEGORIES --")
                    ];
                    // Connexion à la base principale
                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBCCAT . "` ORDER BY cat_desc";
                    $rows = $db->get_results($rech, ARRAY_A);
                    $db->disconnect();
                    cre_select_tab('cat', 'cat', $rows, 'cat_code', 'cat_desc', '', $replm_ccat, '', _("-- Faites votre choix --"), 1, FALSE, '', '', TRUE, TRUE, FALSE, $tab_first_options);
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_pays" align="right"><?PHP echo _("Pays"); ?></td>
                <td>
                    <?PHP
                    // Création de la liste des pays
                    $rows = give_rows_pays();
                    cre_select_tab('pays', 'pays', $rows, 'pays_code', 'pays_nom', '', $replm_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
                    $rows = give_rows_dep($replm_codepays);
                    cre_select_tab('dep[]', 'dep', $rows, 'dep_code', 'dep_code', 'dep_nom', $tab_dep, '', _("-- Faites votre choix --"), 5, TRUE);
                    echo '<input name="deptxt" type="hidden" id="deptxt" value="' . $dep . '" size="50" readonly>';
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_mrepli" align="right"><?PHP echo _("Mode réplication"); ?></td>
                <td>
                    <?PHP
                    cre_select('mpreli', 'mrepli', $mode_repli, $replm_mode, '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_sctact" align="right"><?PHP echo _("Synchroniser les contacts ?"); ?></td>
                <td>
                    <?PHP
                    cre_select('sctact', 'sctact', $non_oui, $replm_sctact, '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
            <tr>
                <td class="rubfrm" id="f_sctact"
                    align="right"><?PHP echo _("Synchroniser les sites secondaires ?"); ?></td>
                <td>
                    <?PHP
                    cre_select('ssites', 'ssites', $non_oui, $replm_ssites, '', _("-- Faites votre choix --"));
                    ?>
                </td>
            </tr>
        </table>
        <p align="center">
            <?PHP
            switch ($td)
            {
                case "add" :
                case "edit" :
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
                    echo '<input name="submit" type="submit" class="bton_std" id="submit" value="'._("Enregistrer").'">';
                    break;
                case "del" :
                    echo '<input name="del" type="submit" class="bton_std" id="del" value="'._("Supprimer").'">';
                    echo '<input name="annul" type="button" class="bton_std" id="annul" value="'._("Annuler").'">';
                    break;
            }
            ?>
        </p>
    </form>
    <!-- Fin du formulaire -->
<?PHP
} else
    pop_ret_auto(_("Type de réplication incorrect"), 'warn', "index.php");

include_once("pied.php");
?>
</body>
</html>