<?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/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_pdt.inc.php");
include_once("../inc/fic_zc.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("../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);

$export_ok = TRUE;
$retour_idx = FALSE;

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

// vérification du mode du connecté (pour éviter les hacks)
switch ($td) {
    case "cli" :
    case "scli" :
        if (@$_SESSION["niv_log"] <= 3)
            $export_ok = FALSE;
        break;
    case "art" :
        // Les agences n'ont droit qu'aux exports clients et interventions
        if (@$_SESSION["niv_log"] <= 4)
            $export_ok = FALSE;
        break;
    default :
        $export_ok = FALSE;
        break;
}

$titre = '';
if ($export_ok == TRUE) {
    switch ($td) {
        case "cli" :
            $titre = _("EXPORTATION CLIENTS");
            break;
        case "scli" :
            $titre = _("EXPORTATION SITES CLIENTS");
            break;
        case "art" :
            $titre = _("EXPORTATION ARTICLES");
            break;
        case "int" :
            $titre = _("EXPORTATION INTERVENTIONS");
            break;
        default :
            $export_ok = FALSE;
            break;
    }
}

$lst_id_readonly = '';
$age = '';
$tab_zc = [];
if (@$_SESSION["niv_log"] == 4) {
    $lst_id_readonly = 'age-pays';
    $age = @$_SESSION["code_age"];
    $tab_zc = tab_zcc_age_open_full($_SESSION["code_age"], TRUE);
}
?>
<!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 _("Exportation"); ?></title>

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

    <?PHP
    switch ($td) {
        case "cli" :
    case "scli" :
        ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:cat ; format:liste ; req:Y ; txt:<?PHP echo _("Catégorie"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                <?PHP if (@$_SESSION["niv_log"] <> 4)
                {
                ?>
                "nom:age ; format:liste ; req:Y ; txt:<?PHP echo _("Agence"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:pays ; format:liste ; req:Y ; txt:<?PHP echo _("Pays"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                <?PHP
                }
                else
                {
                ?>
                "nom:age ; format:alphanum ; req:Y ; txt:<?PHP echo _("Agence"); ?> ; stok:frmok ; stbad:frmbad",
                "nom:pays ; format:alphanum ; req:Y ; txt:<?PHP echo _("Pays"); ?> ; stok:frmok ; stbad:frmbad",
                <?PHP
                }
                ?>
                "nom:dep[] ; format:listem ; req:Y ; txt:<?PHP echo _("Départements"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:champs[] ; format:checkbox ; req:Y ; txt:<?PHP echo _("Champs"); ?> ; stok:frmok ; stbad:frmbad"];
            //-->
        </script>
    <?PHP
    break;
    case "art" :
    ?>
        <script language="javascript">
            <!--
            var tab1_champ = ["nom:cat ; format:liste ; req:Y ; txt:<?PHP echo _("Catégorie"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:fam[] ; format:listem ; req:N ; txt:<?PHP echo _("Familles"); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
                "nom:champs[] ; format:checkbox ; req:Y ; txt:<?PHP echo _("Champs"); ?> ; stok:frmok ; stbad:frmbad"];
            //-->
        </script>
        <?PHP
        break;
    }

    if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) { ?>
        <script type="text/javascript">
            <!--
            function init_all() {
                init_Body();
                init_ajax_dep_zc_pays_3();
                init_ajax_dep_zc_pays_4();
                readonly_id('<?PHP echo $lst_id_readonly; ?>',false);
                init_txtfromvalsel('dep', 'deptxt', 'click');
                init_txtfromvalsel('fam', 'famtxt', 'click');
                init_autohref('quit', 'click', 'index.php',false);
                init_valid_exp('exp','exp_maj','<?PHP echo $_SESSION['date_fmtshort']; ?>',tab1_champ,'','');
                Focus_first();
            }

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

</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 ($export_ok == TRUE) {
    ?>
    <!-- Formulaire permettant la sélection l'exportation -->
    <form name="exp_maj" id="exp_maj" method="post" action="export.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="nbutton" type="hidden" id="nbutton" value="">
    <?PHP
    switch ($td) {
        /******************************************************/
        /**                    CLIENTS                       **/
        /******************************************************/

        case "cli" :
            if (nb_cli("")) {
                $url_link = "exp_data.php?td=cli";
                ?>
                <!-- Liste des catégories -->
                <table width="100%" border="0">
                    <tr>
                        <td width="160" class="rubfrm" id="f_cat" align="right"><?PHP echo _("Catégorie"); ?></td>
                        <td>
                            <?PHP
                            // création de la liste des catégories
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "SELECT * FROM `" . TBCCAT . "` ORDER BY cat_desc";
                            $rows = $db->get_results($rech, ARRAY_A);
                            $db->disconnect();
                            $tab_first_options = [
                                '*' => _("-- TOUTES LES CATEGORIES --")
                            ];
                            cre_select_tab('cat', 'cat', $rows, 'cat_code', 'cat_desc', '', $cat, '', _("-- Faites votre choix --"), 1, FALSE, '', '', TRUE, TRUE, FALSE, $tab_first_options);
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_age" align="right"><?PHP echo _("Agence"); ?></td>
                        <td>
                            <?PHP
                            // création de la liste des agences
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "SELECT * FROM `" . TBAGE . "` WHERE `age_ouvert` = '1' ORDER BY age_nom";
                            $rows = $db->get_results($rech, ARRAY_A);
                            $db->disconnect();
                            $tab_first_options = [
                                '*' => _("-- TOUTES LES AGENCES --")
                            ];
                            cre_select_tab('age', 'age', $rows, 'age_code', 'age_nom', '', $age, '', _("-- 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', '', $age, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_dep" align="right"><?PHP echo _("Département/Région"); ?></td>
                        <td>
                            <?PHP
                            // création de la liste des Départements
                            cre_select('dep[]', 'dep', $tab_zc, '', '', _("-- Faites votre choix --"), 5, TRUE);
                            if (@$_SESSION["niv_log"] == 4)
                                echo "(Départements couverts par votre agence)";
                            else
                                echo "(Départements couverts par les agences)";
                            ?>
                            <input name="deptxt" type="hidden" id="deptxt" value="<?PHP echo $dep ?>" size="50" readonly>
                        </td>
                    </tr>
                </table>
                <br />
            <?PHP
            } else
                $retouridx = TRUE;
            break;

        /******************************************************/
        /**                 SITES CLIENTS                    **/
        /******************************************************/

        case "scli" :
            if (nb_scli("", "", TRUE)) {
                $url_link = "exp_data.php?td=scli";
                ?>
                <!-- Liste des catégories -->
                <table width="100%" border="0">
                    <tr>
                        <td width="160" class="rubfrm" id="f_cat" align="right"><?PHP echo _("Catégorie"); ?></td>
                        <td>
                            <?PHP
                            // création de la liste des catégories
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "SELECT * FROM `" . TBCCAT . "` ORDER BY cat_desc";
                            $rows = $db->get_results($rech, ARRAY_A);
                            $db->disconnect();
                            $tab_first_options = [
                                '*' => _("-- TOUTES LES CATEGORIES --")
                            ];
                            cre_select_tab('cat', 'cat', $rows, 'cat_code', 'cat_desc', '', $cat, '', _("-- Faites votre choix --"), 1, FALSE, '', '', TRUE, TRUE, FALSE, $tab_first_options);
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_age" align="right"><?PHP echo _("Agence"); ?></td>
                        <td>
                            <?PHP
                            // création de la liste des agences
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "SELECT * FROM `" . TBAGE . "` WHERE `age_ouvert` = '1' ORDER BY age_nom";
                            $rows = $db->get_results($rech, ARRAY_A);
                            $db->disconnect();
                            $tab_first_options = [
                                '*' => _("-- TOUTES LES AGENCES --")
                            ];
                            cre_select_tab('age', 'age', $rows, 'age_code', 'age_nom', '', $age, '', _("-- 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', '', $cli_codepays, '', _("-- Faites votre choix --"), 1, FALSE, '-1', _("-- Inconnu --"));
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_dep"
                            align="right"><?PHP echo _("Département/Région"); ?></td>
                        <td>
                            <?PHP
                            // création de la liste des Départements
                            cre_select('dep[]', 'dep', $tab_zc, '', '', _("-- Faites votre choix --"), 5, TRUE);
                            if (@$_SESSION["niv_log"] == 4)
                                echo "(Départements couverts par votre agence)";
                            else
                                echo "(Départements couverts par les agences)";
                            ?>
                            <input name="deptxt" type="hidden" id="deptxt" value="<?PHP echo $dep ?>" size="50" readonly>
                        </td>
                    </tr>
                </table>
                <br />
            <?PHP
            } else
                $retouridx = TRUE;
            break;

        /******************************************************/
        /**                    ARTICLES                      **/
        /******************************************************/

        case "art" :
            if (nb_pdt() > 0) {
                $url_link = "exp_data.php?td=art";
                ?>
                <!-- Liste des catégories -->
                <table width="100%" border="0">
                    <tr>
                        <td width="160" class="rubfrm" id="f_cat" align="right"><?PHP echo _("Catégorie"); ?></td>
                        <td>
                            <?PHP
                            $tab_options = [
                                '*' => _("-- TOUTES LES CATEGORIES --"),
                                '1' => _("Vendus"),
                                '0' => _("Non vendus")
                            ];
                            cre_select('cat', 'cat', $tab_options, '', '', _("-- Faites votre choix --"));
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_dep" align="right">Famille(s)</td>
                        <td>
                            <?PHP
                            // création de la liste des familles
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "SELECT * FROM `" . TBFAM . "` ORDER BY fam_desc";
                            $rows = $db->get_results($rech, ARRAY_A);
                            $db->disconnect();
                            cre_select_tab('fam[]', 'fam', $rows, 'fam_code', 'fam_desc', '', $fam, '', _("-- Faites votre choix --"), 5, TRUE);
                            echo ' <input name="famtxt" type="hidden" id="famtxt" value="' . $fam . '" size="50" readonly>';
                            ?>
                        </td>
                    </tr>
                </table>
                <br />
            <?PHP
            } else
                $retouridx = TRUE;
            break;
        default :
            break;
    }

    if ($retouridx == FALSE) {
        ?>
        <table width="100%" border="0">
            <tr>
                <td colspan="5">
                    <div align="center" class="titre_std_gras_jaune_frouge"><?PHP echo _("Champs à exporter"); ?></div>
                </td>
            </tr>
            <?PHP
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
            switch ($td) {
                case "cli" :
                    $rech = "DESCRIBE `" . TBCLI . "`";
                    break;
                case "scli" :
                    $rech = "DESCRIBE `" . TBSCLI . "`";
                    break;
                case "art" :
                    $rech = "DESCRIBE `" . TBPDT . "`";
                    break;
                case "int" :
                    $rech = "DESCRIBE `" . TBINT . "`";
                    break;
            }
            $fields = $db->get_results($rech, ARRAY_A);
            foreach ($fields as $field) {
                echo "<tr>";
                $j = 0;
                while ($j < 5) {
                    $nom = $field['Field'];
                    $aexporter = TRUE;
                    switch ($td) {
                        /******************************************************/
                        /**                    CLIENTS                       **/
                        /******************************************************/

                        case "cli" :
                            if (@$_SESSION["niv_log"] == 5) {
                                if (($nom == "cli_id") OR ($nom == "cli_pass"))
                                    $aexporter = FALSE;
                            }
                            if (@$_SESSION["niv_log"] == 4) {
                                if (($nom == "cli_id") OR ($nom == "cli_prop") OR ($nom == "cli_codedo") OR ($nom == "cli_codecrea") OR ($nom == "cli_codemod") OR ($nom == "cli_pass") OR ($nom == "cli_notaa"))
                                    $aexporter = FALSE;
                            }
                            if ($aexporter == TRUE) {
                                $id_champ = "f_" . $nom;
                                $nom_champ = "cb_" . $nom;
                                echo "<td width=\"20%\" id=\"" . $id_champ . "\">";
                                echo "<input type=\"checkbox\" name=\"champs[]\" value=\"" . $nom . "\" style=\"background : #FFFFFF;\" checked>";
                                echo $nom;
                                if ($nom == "cli_ville")
                                    echo " (CP + Ville)";
                                $j++;
                            }
                            break;
                        /******************************************************/
                        /**                 SITES CLIENTS                    **/
                        /******************************************************/

                        case "scli" :
                            if (@$_SESSION["niv_log"] == 5) {
                                if ($nom == "scli_id")
                                    $aexporter = FALSE;
                            }
                            if (@$_SESSION["niv_log"] == 4) {
                                if ($nom == "scli_id")
                                    $aexporter = FALSE;
                            }
                            if ($aexporter == TRUE) {
                                $id_champ = "f_" . $nom;
                                $nom_champ = "cb_" . $nom;
                                echo "<td width=\"20%\" id=\"" . $id_champ . "\">";
                                echo "<input type=\"checkbox\" name=\"champs[]\" value=\"" . $nom . "\" style=\"background : #FFFFFF;\" checked>";
                                echo $nom;
                                if ($nom == "scli_ville")
                                    echo " (CP + Ville)";
                                $j++;
                            }
                            break;
                        /******************************************************/
                        /**                    ARTICLES                      **/
                        /******************************************************/

                        case "art" :
                            if (@$_SESSION["niv_log"] == 5) {
                                if ($nom == "pdt_id")
                                    $aexporter = FALSE;
                            }
                            if ($aexporter == TRUE) {
                                $id_champ = "f_" . $nom;
                                $nom_champ = "cb_" . $nom;
                                echo "<td width=\"20%\" id=\"" . $id_champ . "\">";
                                echo "<input type=\"checkbox\" name=\"champs[]\" value=\"" . $nom . "\" style=\"background : #FFFFFF;\" checked>";
                                echo $nom;
                                $j++;
                            }
                            break;
                    }
                    $i++;
                    echo "</td>";
                }
                // On ferme la ligne uniquement si dernière colonne atteinte
                if ($j == 5)
                    echo "</tr>";
            }
            $db->disconnect();

            // On fini la ligne si pas sur dernière colonne
            if ($j < 5) {
                for ($k = $j; $k < 5; $k++) {
                    echo "<td width=\"20%\" id=\"\">";
                    echo "&nbsp;";
                    echo "</td>";
                }
                echo "</tr>";
            }
            ?>
        </table>
        <br />
        <table width="100%" border="0">
            <tr>
                <td colspan="2">
                    <div align="center"
                         class="titre_std_gras_jaune_frouge"><?PHP echo _("Paramètres d'exportation"); ?></div>
                </td>
            </tr>
            <tr>
                <td width="30%" class="rubfrm" id="f_ligtitre"
                    align="right"><?PHP echo _("Nom des champs en 1ère ligne"); ?></td>
                <td>
                    <?PHP
                    cre_select('ligtitre', 'ligtitre', $non_oui4, 'O');
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_cadre" align="right"><?PHP echo _("Champs encadrés par"); ?></td>
                <td>
                    <?PHP
                    cre_select('cadre', 'cadre', $tab_typ_encadrement, 'G');
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_sep" align="right"><?PHP echo _("Champs séparés par des"); ?></td>
                <td>
                    <?PHP
                    cre_select('sep', 'sep', $tab_typ_separateur, 'P');
                    ?>
                </td>
            </tr>
            <tr>
                <td width="160" class="rubfrm" id="f_sep" align="right"><?PHP echo _("Lignes terminées par"); ?></td>
                <td>
                    <?PHP
                    cre_select('fl', 'fl', $tab_typ_fligne, 'N');
                    ?>
                </td>
            </tr>
        </table>
        <div align="center" id="boutons" style="display:block">
            <input name="exp" type="button" class="bton_std" id="exp" value="<?PHP echo _("Exporter"); ?>">
            <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo _("Réinitialiser"); ?>">
            <input name="quit" type="button" class="bton_std" id="quit" value="<?PHP echo _("Quitter"); ?>">
        </div>
        <div align="center" id="patience" style="display:none"><?PHP echo _("Merci de patienter..."); ?>
            <br /><?PHP echo _("Cliquez"); ?> <a
                href="<?PHP echo $url_link; ?>"><?PHP echo _("ici"); ?></a> <?PHP echo _("pour recommencer"); ?></div>
        </form>
        <!-- Fin du formulaire -->
    <?PHP
    } else
        pop_ret_auto(_("Pas d'enregistrements"), 'warn', "index.php");
} else
    pop_ret_auto( _("Type d'exportation incorrect"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>