<?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 fichiers opérateurs */
/* et fichiers rattachés                            */
/****************************************************/

/**
 * Vérification du nombre d'opérateurs
 *
 * @param bool $open
 * @return null
 */
function nb_ope($open = TRUE)
{
    global
    $db;

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

    return $nbexist;
}

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

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

    return $row2;
}

/**
 * @param string $except : sauf un opérateur (si vide, on en tient pas compte)
 * @param bool $open
 *
 * @return array
 */

function give_tab_ope($except = '', $open = TRUE)
{
    global
    $db;

    $tab_res = [];

    // Création de la liste des opérateurs présents
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = "SELECT ope_code,ope_nom,ope_prenom FROM `" . TBOPE . "` WHERE `ope_out` = '0'";

    if ($except <> '')
        $rech.= " AND `ope_code` <> '".$except."'";
    $rech.= " ORDER BY ope_nom";

    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $tab_res[$ope_code] = $ope_nom. ' '.$ope_prenom. " (" . $ope_code . ")";
        }
    }

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

    return $tab_res;
}
?>