<?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
 ****************************************************************************************/

/*************************************************************/
/* Récupération département zone chalandise en fonction pays */
/*************************************************************/

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

/** Récupération des départements couverts par les agences dans un pays donné **/

function print_plain()
{
    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");

    header("Content-type: text/html; charset=UTF-8");
    $dep = get_depzc($_REQUEST["pays"]);
    $result = [];
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    foreach ($dep as $row) {
        // On recupere les enregistrements
        $rech2 = "SELECT dep_nom FROM `" . TBDEP . "` WHERE dep_code='" . $row["zcc_codedep"] . "' AND dep_codepays='" . $row["zcc_codepays"] . "' AND dep_lng='" . str_majuscules($_SESSION["lng"]) . "'";
        $row2 = $db->get_row($rech2, ARRAY_A);
        if ($row2)
            $result[] = $row["zcc_codedep"] . "=" . $row["zcc_codedep"] . " (" . $row2["dep_nom"] . ")";
    }
    $db->disconnect();
    if (count($result) == 0)
        $result[] = "-1="._("-- Inconnu --");
    print implode(";", $result);
}

/**
 * @param $pays
 * @return array
 */
function get_depzc($pays)
{

    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");

    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    // On recupere les enregistrements
    $rech = "SELECT DISTINCT zcc_codedep,zcc_codepays FROM `" . TBZCC . "` WHERE `zcc_codepays`='" . $pays . "' ORDER BY zcc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    // On cree le resultat
    $result = [];
    foreach ($rows as $row) {
        $result[] = $row;
    }
    $db->disconnect();
    return $result;
}

?>