<?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 ville en fonction département */
/**********************************************/

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

function print_plain()
{
    header("Content-type: text/html; charset=UTF-8");
    $dep = $_REQUEST["dep"];
    $tab_dep = explode("_", $dep);
    $ville = get_ville($tab_dep[0], $tab_dep[1]);
    $result = [];
    foreach ($ville as $row) {
        $result[] = $row["villes_id"] . "=" . str_majuscules($row["villes_nom"]) . " (" . $row["villes_cp"] . ")";
    }
    /*
    if (count($result)==0)
        $result[]="-1=-- Inconnu --";
    */
    print implode(";", $result);
}

/**
 * @param $dep
 * @param $pays
 * @return array
 */
function get_ville($dep, $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 vérifie s'il y a des départements, régions ou états dans ce pays
    // Il y en a, on filtre donc par département, sur le pays
    // Cela limite le temps de réponse car les départements, régions, ou états sont souvent présents dans les grands pays
    //  donc, dans des pays avec de nombreuses communes
    if ($dep <> "") {
        if ($dep <> "-1") {
            $filtre = $dep;
            if (($filtre == "2A") OR ($filtre == "2B"))
                $filtre = "20";
            $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_dep` = '" . str_majuscules($filtre) . "' AND `villes_codepays`='" . str_majuscules($pays) . "' AND villes_lng='" . str_majuscules($_SESSION["lng"]) . "' ORDER BY villes_nom";
        } else {
            $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_codepays`='" . str_majuscules($pays) . "' AND villes_lng='" . str_majuscules($_SESSION["lng"]) . "' ORDER BY villes_nom";
        }
    } // Il n'y en a pas, on prend toutes les villes du pays
    else {
        $rech = "SELECT * FROM `" . TBVILLES . "` WHERE `villes_codepays`='" . str_majuscules($pays) . "' AND villes_lng='" . str_majuscules($_SESSION["lng"]) . "' ORDER BY villes_nom";
    }
    $result = [];
    if ($rech <> "") {
        $rows = $db->get_results($rech, ARRAY_A);
        // On cree le resultat
        $result = [];
        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                $result[] = $row;
            }
        }
    }
    $db->disconnect();
    return $result;
}

?>