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

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

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

    return $nbexist;
}

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

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

    return $row2;
}

/**
 * Génération du code HTML pour l'affichage d'une icône statut
 *
 * @param $code
 * @param string $url
 * @param bool $open
 * @return string
 */
function str_div_ico_statut($code, $url = '', $open = TRUE)
{
    global
        $db;

    $str_div = '<span class="annot_std_it">'.$code.'</span>';

    if (trim($code)<>'')
    {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech2 = "SELECT statut_id,statut_img,statut_desc FROM `" . TBSTA . "` WHERE statut_code = '" . $db->escape($code) . "'";
        $row2 = $db->get_row($rech2, ARRAY_A);
        if ($row2) {
            $row2 = encode_str($row2);
            extract($row2);

            if ($statut_img <> "")
            {
                $str_div = '<img src="' . $statut_img . '" title="' . $statut_desc . '" border="0" align="absmiddle"';
                if (trim($url)<>'')
                    $str_div .= " onclick=\"init_new_window('".$url."','statut_det_".$statut_id."',600,300,'yes')\")";
                $str_div.= '>';
            }
        }
        if ($open == TRUE)
            $db->disconnect();
    }

    return $str_div;
}


/**
 * @param string $except
 * @param bool $open
 * @return array
 */
function give_rows_statut($except = '', $open = TRUE)
{
    global
        $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBSTA . "`";
    if ($except <> '')
        $rech .= " WHERE `statut_code` <> '".$except."'";
    $rech .=" ORDER BY statut_desc";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $rows;
}
