<?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");

/*******************************************************/
/* Fonctions pour les accès aux types marques/éditeurs */
/*******************************************************/

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

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBMQUET . "`";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

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

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

    return $row2;
}

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

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

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

    // On récupère déjà les codes corerspondants au type demandé
    $rech = "SELECT mquet_code FROM `" . TBMQUET . "`";
    if (trim($type) <> "") {
        switch ($type) {
            case "env" :
                $rech .= " WHERE mquet_dist = '1'";
                break;
            case "rpdt" :
                $rech .= " WHERE mquet_rpdt = '1'";
                break;
            case "rlic" :
                $rech .= " WHERE mquet_rlic = '1'";
                break;
            case "assu" :
                $rech .= " WHERE mquet_assu = '1'";
                break;
            case "oc" :
                $rech .= " WHERE mquet_oc = '1'";
                break;
        }
    }
    $rows = $db->get_results($rech, ARRAY_A);

    // On récupère les marques
    $rexist = "SELECT COUNT(*) FROM `" . TBMQUE . "`";
    if ($rows) {
        $rexist .= " WHERE (";
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $rexist .= " mque_codemquet = '" . $mquet_code . "' OR";
        }
        // Suppression du dernier 'OR'
        $rexist = substr($rexist, 0, strlen($rexist) - 3);
        $rexist .= ")";
    }

    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Retourne les codes marques pour un type donné
 *
 * @param $type
 * @param bool $open
 * @return array
 */
function give_tab_marques($type, $open = TRUE)
{
    global
    $db;

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

    // On récupère déjà les codes corerspondants au type demandé
    $rech = "SELECT mquet_code FROM `" . TBMQUET . "`";
    if (trim($type) <> "") {
        switch ($type) {
            case "env" :
                $rech .= " WHERE mquet_dist = '1'";
                break;
            case "rpdt" :
                $rech .= " WHERE mquet_rpdt = '1'";
                break;
            case "rlic" :
                $rech .= " WHERE mquet_rlic = '1'";
                break;
            case "assu" :
                $rech .= " WHERE mquet_assu = '1'";
                break;
            case "oc" :
                $rech .= " WHERE mquet_oc = '1'";
                break;
        }
    }
    $rows = $db->get_results($rech, ARRAY_A);

    // On récupère les marques
    $rexist = "SELECT * FROM `" . TBMQUE . "`";
    if ($rows) {
        $rexist .= " WHERE (";
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $rexist .= " mque_codemquet = '" . $mquet_code . "' OR";
        }
        // Suppression du dernier 'OR'
        $rexist = substr($rexist, 0, strlen($rexist) - 3);
        $rexist .= ")";
    }
    $rexist .= " ORDER BY mque_ste";

    $rows2 = $db->get_results($rexist, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $rows2;
}

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

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

    return $row2;
}

/*************************/
/* Notes privées marques */
/*************************/

/**
 * Lecture d'un texte privé marque
 *
 * @param $code
 * @param $codeu
 * @param bool $open
 * @return array|null
 */
function lit_enr_MqueNPriv($code, $codeu, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBMQUEN . "` WHERE `mquen_codemque` = '" . $db->escape($code) . "' AND `mquen_codeu` = '" . $db->escape($codeu) . "'";
    $row2 = $db->get_row($rexist, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/********************/
/* Contacts marques */
/********************/

/**
 * Vérification du nombre de contacts dans une fiche marque
 * On tient compte du niveau utilisateur
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_cmque_in_mque($code, $open = TRUE)
{
    global
    $db,
    $_SESSION;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBCMQUE . "` WHERE `cmque_codemque` = '" . $db->escape($code) . "'";
    switch ($_SESSION["niv_log"]) {
        case 3 :
        case 4 :
            $rexist .= " AND (`cmque_prop` = '*' OR `cmque_prop` = '" . $_SESSION["code_age"] . "')";
            break;
        default :
            break;
    }
    $nbexist = $db->get_var($rexist);;
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Lecture d'un contact marque
 *
 * @param $id
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_cmque_id_in_mque($id, $code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBCMQUE . "` WHERE `cmque_id` = '$id' AND `cmque_codemque` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rexist, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**********************************/
/* Notes privées contacts marques */
/**********************************/


/**
 * Lecture d'un texte privé contact marque
 *
 * @param $id
 * @param $codeu
 * @param bool $open
 * @return array|null
 */
function lit_enr_CMqueNPriv($id, $codeu, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBCMQUEN . "` WHERE `cmquen_idcmque` = '$id' AND `cmquen_codeu` = '" . $db->escape($codeu) . "'";
    $row2 = $db->get_row($rexist, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Retourne la liste des contacts marque
 *
 * @param $code
 * @param bool $open
 * @return array
 */
function give_tab_cmque($code, $open = TRUE)
{
    global
    $db;

    $tab_cmque = [];

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

    // Filtre sur marque
    $rech = "SELECT cmque_id,cmque_nom,cmque_prenom FROM `" . TBCMQUE . "` WHERE `cmque_codemque` = '" . $db->escape($code) . "' ORDER BY cmque_nom";
    $rows = $db->get_results($rech, ARRAY_A);
    if (($rows) AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            extract($row);

            $txt_cmque = $cmque_nom . " " . $cmque_prenom;
            $row2 = lit_enr_fonc($cmque_codefonc, FALSE);
            if (is_array($row2)) {
                $row2 = encode_str($row2);
                extract($row2);
                $txt_fonc = $fonc_desc;
            } else
                $txt_fonc = $cmque_codefonc;
            $txt_cmque .= " (" . $txt_fonc . ")";
            $tab_cmque[$cmque_id] = $txt_cmque;
        }
    }

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

    return $tab_cmque;
}


/*****************/
/* Liens marques */
/****************/


/**
 * Vérification du nombre de liens dans une fiche marque
 *
 * @param $code
 * @param bool $open
 * @return null
 */
function nb_wmque_in_mque($code, $open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

/**
 * Lecture d'un lien marque
 *
 * @param $id
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_wmque_id_in_mque($id, $code, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBWMQUE . "` WHERE `wmque_id` = '$id' AND `wmque_codemque` = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rexist, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/*******************************/
/* Notes privées liens marques */
/*******************************/

/**
 * Lecture d'un texte privé lien marque
 *
 * @param $code
 * @param $codeu
 * @param bool $open
 * @return array|null
 */
function lit_enr_WMqueNPriv($code, $codeu, $open = TRUE)
{
    global
    $db;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT * FROM `" . TBWMQUEN . "` WHERE `wmquen_idwmque` = '" . $db->escape($code) . "' AND `wmquen_codeu` = '" . $db->escape($codeu) . "'";
    $row2 = $db->get_row($rexist, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}
?>