<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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
 ****************************************************************************************/

/*****************************************************************************/
/* Récupération département en fonction chaine existante et code département */
/*****************************************************************************/

session_start();
sleep(1);
if (array_key_exists('txt', $_REQUEST)) {
    $tomorrow = 0; //60*60*24; // en nb de secondes
    header("Cache-Control: max-age=$tomorrow");
    print_plain();
} else {
    print "Usage : $_SERVER[PHP_SELF]?txt=dep_pays";
}

function print_plain()
{
    header('Content-type: text/html; charset=UTF-8');
    $result = [];

    $tab_req = explode('_', $_REQUEST['txt']);
    $taille_tab_req = count($tab_req);
    if ($taille_tab_req > 0) {
        $tab_to_add = [];

        // On reprend ce qui a déjà été choisi
        if ($_SESSION['txtzc'] != '') {
            $tab_str = explode('%', $_SESSION['txtzc']);
            if (count($tab_str) > 0) {
                foreach ($tab_str as $value) {
                    $tab_to_add[] = $value;
                }
            }
        }

        // On rajoute ce que l'on vient de choisir
        $pays = $tab_req[$taille_tab_req - 1];
        if (count($tab_req) > 0) {
            foreach ($tab_req as $key => $value) {
                if ($key <> $taille_tab_req - 1) {
                    $tab_to_add[] = $value . '_' . $pays;
                }
            }
        }
        $result = get_depzc($tab_to_add);
    }
    print implode(';', $result);
}

/**
 * @param $tab
 * @return array
 */
function get_depzc($tab)
{
    require_once('../lib/gettext/gettext.inc');
    include_once('../inc/config.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');

    $new_tab = [];
    $result = [];
    $_SESSION['txtzc'] = '';
    if (count($tab) > 0) {
        foreach ($tab as $value) {
            $tab_enr = explode('_', $value);
            // On se connecte a la base
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            // On recupere les enregistrements
            $rech2 = 'SELECT dep_nom FROM `' . TBDEP . "` WHERE dep_code='" . $tab_enr[0] . "' AND dep_codepays='" . $tab_enr[1] . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                extract($row2);
                $rech3 = 'SELECT pays_nom FROM `' . TBPAYS . "` WHERE pays_code='" . $tab_enr[1] . "'";
                $row3 = $db->get_row($rech3, ARRAY_A);
                if ($row3) {
                    $row3 = encode_str($row3);
                    extract($row3);
                }
                $chaine_dep = $pays_nom . ' - ' . $dep_nom . ' (' . $tab_enr[0] . ')';
                $result[] = $tab_enr[0] . '_' . $tab_enr[1] . '=' . $chaine_dep;
                $new_tab[] = $tab_enr[0] . '_' . $tab_enr[1];
            }
            close_database();
        }
    }
    if (count($new_tab) > 0) {
        $_SESSION['txtzc'] = implode('%', $new_tab);
    }
    //print_r($result);
    asort($result);
    return $result;
}
