<?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 objets possibles en fonction type objet */
/********************************************************/

session_start();
sleep(1);
if (array_key_exists("id", $_REQUEST)) {
    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]?id=id&to=codeto";
    }
} else {
    print "Usage : $_SERVER[PHP_SELF]?id=id&to=codeto";
}

function print_plain()
{
    header("Content-type: text/html; charset=UTF-8");
    unset($_SESSION["obj_idnomenc_to_be_enfants"]);
    $objid = intval($_REQUEST["id"]);
    $objto = $_REQUEST["to"];
    $result = get_objachoisir($objid,$objto);
    print implode(";", $result);
}

/**
 * @param $id
 * @param $to
 * @return array
 */
function get_objachoisir($id,$to)
{
    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 = [];

    if ((intval($id)>0) AND ($to <> ''))
    {
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        // On récupère les id des objets qui ne sont ni parents, ni enfants de l'id sur lequel on travaille
        // et qui peuvent être composants
        $rech = "SELECT robj_id,robj_desc FROM `" . TBROBJ . "`, `" . TBRTO . "` WHERE `robj_id` <> '".$id."' AND `robj_actif` = 1 AND `robj_coderto` = '".$to."'";

        if ((count($_SESSION["obj_idnomenc_enfants"])>0) OR (count($_SESSION["obj_idnomenc_parents"])>0))
        {
            $rech .=" AND (";
            if (count($_SESSION["obj_idnomenc_enfants"])>0)
            {
                foreach($_SESSION["obj_idnomenc_enfants"] as $value)
                {
                    $rech .= " `robj_id` <> '".$value."' AND";
                }
            }

            if (count($_SESSION["obj_idnomenc_parents"])>0)
            {
                foreach($_SESSION["obj_idnomenc_parents"] as $value)
                {
                    $rech .= " `robj_id` <> '".$value."' AND";
                }
            }

            // Suppression du dernier 'AND'
            $rech = substr($rech, 0, strlen($rech) - 4);

            $rech.= ")";
        }

        $rech .= " AND " . TBROBJ . ".robj_coderto = " . TBRTO . ".rto_code";
        $rech .= " AND " . TBRTO . ".rto_comp = 1";

        $rows = $db->get_results($rech, ARRAY_A);

        // On récupère les enregistrements
        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                extract($row);

                $_SESSION["obj_idnomenc_to_be_enfants"][] = intval($robj_id);
                $result[] = $robj_id . '=' . $robj_desc;
            }
        }

        $db->disconnect();
    }

    /*
    // On se connecte a la base
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    foreach ($tab as $value) {
        // On recupere les enregistrements
        $rech2 = "SELECT robj_id, robj_desc FROM `" . TBROBJ . "` WHERE  `robj_id` = '" . intval($value) . "'";
        $row2 = $db->get_row($rech2, ARRAY_A);
        if ($row2) {
            $row2 = encode_str($row2);
            extract($row2);
            $tab_id[] = $robj_id;
            $result[] = $robj_id . '=' . $robj_desc;
        }
    }
    $_SESSION["obj_idnomenc_enfants"] = $tab_id;
    */
    return $result;
}

?>