<?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 contacts ss-traitant en fonction code ss-traitant */
/******************************************************************/

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

function print_plain()
{
    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/fic_hum.inc.php");
    header("Content-type: text/html; charset=UTF-8");
    $cst = get_cst($_REQUEST["st"]);
    $result = [];
    foreach ($cst as $row) {
        $txt = $row["cst_nom"] . " " . $row["cst_prenom"];
        $row2 = lit_enr_fonc($row["cst_codefonc"], TRUE);
        if (is_array($row2)) {
            $row2 = encode_str($row2);
            extract($row2);
            $txt_fonc = $fonc_desc;
        } else
            $txt_fonc = $row["cst_codefonc"];
        $txt .= " (" . $txt_fonc . ")";
        $result[] = $row["cst_id"] . "=" . $txt;
    }
    print implode(";", $result);
}

/**
 * @param $st
 * @return array
 */
function get_cst($st)
{

    require_once("../lib/gettext/gettext.inc");
    include_once("../inc/config.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");
    include_once("../inc/func_txt.inc.php");

    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $result = [];
    // On recupere les contacts sur le sous-traitant
    $rech = "SELECT * FROM `" . TBCST . "` WHERE cst_codest='" . $st . "' ORDER BY cst_nom";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows)
    {
        foreach ($rows as $row) {
            $row = encode_str($row);
            $result[] = $row;
        }
    }
    $db->disconnect();
    return $result;
}

?>