<?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 des objets à intégrer en nomenclatures en fonction type objets (Patrimoines) */
/*********************************************************************************************/

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

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

    header('Content-type: text/html; charset=UTF-8');
    $tab_rto = explode('_', $_REQUEST['to']);
    list($codeto,$codeobj) = $tab_rto;
    /*
    $codeto = $tab_rto[0];
    $codeobj = $tab_rto[1];
    */
    $result = [];

    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    // On recupere les enregistrements qui sont dans le type objet recherché
    $rech = "SELECT robj_code,$robj_desc FROM `" . TBROBJ . "` WHERE `robj_coderto` = '" . $db->escape($codeto) . "'";
    $rech .= " AND `robj_code` <> '" . $db->escape($codeobj) . "'";
    $rows = $db->get_results($rech, ARRAY_A);
    close_database();
    if ($rows) {
        // On cree le resultat
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            // On élimine les objets qui sont déjà enfants ou pères de l'objet sur lequel on travaille
            $rech2 = 'SELECT COUNT(*) FROM `' . TBRNOMENC . '` WHERE ';
            $rech2 .= "((`rnomenc_cont` = '" . $db->escape($codeobj) . "' AND `rnomenc_obj` = '" . $db->escape($robj_code) . "') OR ";
            $rech2 .= "(`rnomenc_cont` = '" . $db->escape($robj_code) . "' AND `rnomenc_obj` = '" . $db->escape($codeobj) . "'))";
            // On se connecte a la base
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $nb_res = $db->get_var($rech2);
            close_database();
            if ($nb_res == 0) {
                $result[] = $robj_code . '=' . $robj_desc;
            }
        }
    }

    print implode(';', $result);
}
