<?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
 ****************************************************************************************/


// Vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

/*************Faites*******************************/
/* Fonctions pour les accès aux Patrimoines */
/* et fichiers rattachés                    */
/********************************************/

// Types Acquisitions

/**
 * Lecture d'un enregistrement Type Acquisition
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_rta($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBRTA . "` WHERE `rta_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

// Types Objets

/**
 * Lecture d'un enregistrement Type Objet
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_rto($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBRTO . "` WHERE `rto_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

// Objets Mobiles

/**
 * Lecture d'un enregistrement Objet Mobile
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_rmob($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBRMOB . "` WHERE `rmob_idrobj` = '" . $id . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

// Objets Immobiles

/**
 * Lecture d'un enregistrement Objet Imobile
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_rimmob($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBRIMMOB . "` WHERE `rimmob_idrobj` = '" . $id . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

// Objets Assurés

/**
 * Lecture d'un enregistrement Objet Assuré
 *
 * @param $id
 * @param bool $open
 * @return array|null
 */
function lit_enr_rassur($id, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBRASSUR . "` WHERE `rassur_idrobj` = '" . $id . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

// Objets

/**
 * Lecture d'un enregistrement Type Objet
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_robj($code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBROBJ . "` WHERE `robj_code` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}


// Objets

/**
 * Vérifie si un objet est enfant d'un autre objet
 *
 * @param $id
 * @param bool $open
 * @return bool
 */
function is_child($id, $open = TRUE)
{
    global
    $db;

    $is_child = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT COUNT(*) FROM `" . TBRNOMENC . "` WHERE `rnomenc_idrobj_enfant` = '" . intval($id) . "'";
    $nb_res = $db->get_var($rech2);
    if ($nb_res > 0)
        $is_child = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $is_child;
}

/**
 * Crée un tableau qui contient la liste des ID des objets enfants d'un id objet
 *
 * @param $id
 * @param bool $open
 */
function cree_tab_idobj_enfants($id, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    if (intval($id)>0)
    {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        // On récupère les codes des objets contenus dans l'id "id"
        $rech = "SELECT rnomenc_idrobj_enfant FROM `" . TBRNOMENC . "` WHERE `rnomenc_idrobj_parent` = '" . intval($id) . "'";
        $rows = $db->get_results($rech, ARRAY_A);

        if ($rows) {
            foreach ($rows as $row) {
                extract($row);
                $_SESSION["obj_idnomenc_enfants"][] = intval($rnomenc_idrobj_enfant);
                cree_tab_idobj_enfants($rnomenc_idrobj_enfant, TRUE);
            }
        }

        if ($open == TRUE)
            $db->disconnect();
    }
}

/**
 * Crée un tableau qui contient la liste détaillée (avec descriptifs) des produits enfants d'un id objet
 *
 * @param $id
 * @param bool $open
 * @return array
 */
function cree_tab_obj_enfants($id, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    // Construction du tableau des produits enfants
    $tab_nomenc = [];
    $tab_tmp = [];

    if (intval($id)>0)
    {
        cree_tab_idobj_enfants($id, $open);

        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        if (count($_SESSION["obj_idnomenc_enfants"]) >0)
        {
            foreach($_SESSION["obj_idnomenc_enfants"] as $value)
            {
                $rech2 = "SELECT 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_tmp[0] = intval($value);
                $tab_tmp[1] = $robj_desc;
                $tab_tmp[2] = 1;

                $tab_nomenc[] = $tab_tmp;
            }
        }

        if ($open == TRUE)
            $db->disconnect();
    }

    return $tab_nomenc;
}

/**
 * Crée un tableau qui contient la liste des ID des objets parents d'un id objet
 *
 * @param $id
 * @param bool $open
 */
function cree_tab_idobj_parents($id, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    if (intval($id)>0)
    {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        // On récupère les codes des objets contenus dans l'id "id"
        $rech = "SELECT rnomenc_idrobj_parent FROM `" . TBRNOMENC . "` WHERE `rnomenc_idrobj_enfant` = '" . intval($id) . "'";
        $rows = $db->get_results($rech, ARRAY_A);

        if ($rows) {
            foreach ($rows as $row) {
                extract($row);
                $_SESSION["obj_idnomenc_parents"][] = intval($rnomenc_idrobj_parent);
                cree_tab_idobj_parents($rnomenc_idrobj_parent, TRUE);
            }
        }

        if ($open == TRUE)
            $db->disconnect();
    }
}

/**
 * Crée un tableau qui contient la liste détaillée (avec descriptifs) des produits parents d'un id objet
 *
 * @param $id
 * @param bool $open
 * @return array
 */
function cree_tab_obj_parents($id, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    // Construction du tableau des produits parents
    $tab_nomenc = [];
    $tab_tmp = [];

    if (intval($id)>0)
    {
        cree_tab_idobj_parents($id, $open);

        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        if (count($_SESSION["obj_idnomenc_parents"]) >0)
        {
            foreach($_SESSION["obj_idnomenc_parents"] as $value)
            {
                $rech2 = "SELECT 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_tmp[0] = intval($value);
                $tab_tmp[1] = $robj_desc;
                $tab_tmp[2] = 1;

                $tab_nomenc[] = $tab_tmp;
            }
        }

        if ($open == TRUE)
            $db->disconnect();
    }

    return $tab_nomenc;
}

/**
 * Crée un tableau qui contient la liste des id possibles en tant qu'enfants
 *
 * @param $id
 * @param bool $open
 */
function cree_tab_idobj_to_be_enfants($id, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    // Construction du tableau des produits possibles
    if (intval($id)>0)
    {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        // On récupère les codes 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 FROM `" . TBROBJ . "`, `" . TBRTO . "` WHERE `robj_id` <> '".$id."' AND `robj_actif` = 1";

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

        if ($open == TRUE)
            $db->disconnect();
    }
}

/**
 * Crée un tableau qui contient la liste détaillée (avec descriptifs) des produits potentiellement enfants d'un id objet
 *
 * @param $id
 * @param bool $open
 * @return array
 */
function cree_tab_obj_to_be_enfants($id, $open = TRUE)
{
    global
    $_SESSION,
    $db;

    // Construction du tableau des produits parents
    $tab_nomenc = [];
    $tab_tmp = [];

    if (intval($id)>0)
    {
        cree_tab_idobj_to_be_enfants($id, $open);

        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

        if (count($_SESSION["obj_idnomenc_to_be_enfants"]) >0)
        {
            foreach($_SESSION["obj_idnomenc_to_be_enfants"] as $value)
            {
                $rech2 = "SELECT 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_tmp[0] = intval($value);
                $tab_tmp[1] = $robj_desc;
                $tab_tmp[2] = 1;

                $tab_nomenc[] = $tab_tmp;
            }
        }

        if ($open == TRUE)
            $db->disconnect();
    }

    return $tab_nomenc;
}
?>