<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 1.0.0

Copyright (C) 2006-2014  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 interventions en fonction agence */
/*************************************************/

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

function print_plain()
{
    require_once("../lib/gettext/gettext.inc");
    header("Content-type: text/html; charset=UTF-8");
    $interv = get_int($_REQUEST["age"]);
    $result = [];
    foreach ($interv as $row) {
        if (($row["int_code"] <> "") AND ($row["int_code"] <> "-1"))
            $result[] = $row["int_code"] . "=" . $row["int_code"] . " ("._("Client")." : " . $row["int_codecli"] . ")";
        else {
            if ($row["int_code"] == "")
                $result[] = $row["int_code"] . "=" . _("-- Faites votre choix --");
            if ($row["int_code"] == "-1")
                $result[] = $row["int_code"] . "=" . _("Pas de choix possible");
        }
    }
    print implode(";", $result);
}

/**
 * @param $age
 * @return array
 */
function get_int($age)
{

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

    $result = [];
    $tab = [
        "int_code" => "",
        "int_codecli" => ""];
    $result[] = $tab;

    $tab = [
        "int_code" => "-1",
        "int_codecli" => ""];
    $result[] = $tab;
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    // On recupere les enregistrements
    $rech = "SELECT * FROM `" . TBINT . "` WHERE (int_codeage='" . str_majuscules($age) . "'";
    $rech .= " OR int_codeages='" . str_majuscules($age) . "') AND int_datefin='0000-00-00' ORDER BY int_code DESC";
    $rows = $db->get_results($rech, ARRAY_A);
    // On cree le resultat
    $result = [];
    foreach ($rows as $row) {
        $result[] = $row;
    }
    $db->disconnect();
    return $result;
}

?>