<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  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");

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

/**
 * Vérification du nombre de demandes
 *
 * @param bool $open
 * @return null
 */
function nb_dde($open = TRUE)
{
    global
    $db;

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

    $rexist = "SELECT COUNT(*) FROM `" . TBDDE . "`";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

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

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

    return $row2;
}


// Catégories


/**
 * Vérification de la présence d'une catégorie de demandeur dans une demande
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_ddec_in_dde($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBDDE . "` WHERE `dde_codeddec` = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rexist);
    if ($nbexist > 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}

/**
 * Lecture d'un enregistrement catégorie de demandeur
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_ddec($code, $open = TRUE)
{
    global
    $db;

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

    return $row2;
}

// Destinataires

/**
 * Vérification de la présence d'un destinataire de demande dans une demande
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_dded_in_dde($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBDDE . "` WHERE `dde_codedded` = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rexist);
    if ($nbexist > 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}

/**
 * Lecture d'un enregistrement destinataire de demande
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_dded($code, $open = TRUE)
{
    global
    $db;

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

    return $row2;
}

// Types

/**
 * Vérification de la présence d'un type de demande dans une demande
 *
 * @param $code
 * @param bool $open
 * @return bool
 */
function exist_ddet_in_dde($code, $open = TRUE)
{
    global
    $db;
    $exist = FALSE;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBDDE . "` WHERE `dde_codeddet` = '" . $db->escape($code) . "'";
    $nbexist = $db->get_var($rexist);
    if ($nbexist > 0)
        $exist = TRUE;
    if ($open == TRUE)
        $db->disconnect();

    return $exist;
}

/**
 * Lecture d'un enregistrement type de demande
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_ddet($code, $open = TRUE)
{
    global
    $db;

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

    return $row2;
}
