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

/// Civilités

/**
 * Vérification du nombre de civilités
 *
 * @param bool $open
 * @return null
 */
function nb_civ($open = TRUE)
{
    global
    $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBCIV . "`";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Lecture d'un enregistrement civilité
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_civ($code, $open = TRUE)
{
    global
    $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBCIV . "` WHERE civ_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

// Fonctions

/**
 * Vérification du nombre de fonctions
 *
 * @param bool $open
 * @return null
 */
function nb_fonc($open = TRUE)
{
    global
    $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT COUNT(*) FROM `" . TBFONC . "`";
    $nbexist = $db->get_var($rexist);
    if ($open == TRUE)
        $db->disconnect();

    return $nbexist;
}

/**
 * Lecture d'un enregistrement fonction
 *
 * @param $code
 * @param bool $open
 * @return array|null
 */
function lit_enr_fonc($code, $open = TRUE)
{
    global
    $db;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech2 = "SELECT * FROM `" . TBFONC . "` WHERE fonc_code = '" . $db->escape($code) . "'";
    $row2 = $db->get_row($rech2, ARRAY_A);
    if ($open == TRUE)
        $db->disconnect();

    return $row2;
}

/**
 * Retourne le prénom, le nom, et le niveau utilisateur d'un compte à partir de son code
 * Pour  les opérateurs téléphoniques, les techniciens agences, les techniciens donneurs d'ordres, les techniciens sous-traitants
 * Pour les clients, les agences, les donneurs d'ordres et les sous-traitants, seul le nom de société est retourné
 * Pour le centre de gestion, le mot "centre de gestion" est retourné
 * Pour l'administrateur, le mot "administrateur" est retourné
 *
 * @param $code
 * @param bool $open
 * @return array
 */
function prenom_nom_compte($code, $open = TRUE)
{
    global
    $db;
    $tab_pn = [];
    $tab_pn["prenom"] = "";
    $tab_pn["nom"] = "";
    $tab_pn["level"] = 0;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rexist = "SELECT pw_level FROM `" . TBPW . "` WHERE `pw_code` = '$code'";
    $row = $db->get_row($rexist, ARRAY_A);
    if ($row) {
        extract($row);
        switch (intval($pw_level)) {
            case 1 :
                $rexist2 = "SELECT cli_type,cli_ste,cli_nom,cli_prenom FROM `" . TBCLI . "` WHERE `cli_code` = '" . $db->escape($code) . "'";
                $row2 = $db->get_row($rexist2, ARRAY_A);
                if ($row2) {
                    extract($row2);
                    if ($cli_type == 1) {
                        $tab_pn["prenom"] = $cli_prenom;
                        $tab_pn["nom"] = $cli_nom;
                    } else {
                        $tab_pn["nom"] = $cli_ste;
                    }
                    $tab_pn["level"] = $pw_level;
                }
                break;
            case 2 :
                $rexist2 = "SELECT ope_prenom,ope_nom FROM `" . TBOPE . "` WHERE `ope_code` = '" . $db->escape($code) . "'";
                $row2 = $db->get_row($rexist2, ARRAY_A);
                if ($row2) {
                    extract($row2);
                    $tab_pn["prenom"] = $ope_prenom;
                    $tab_pn["nom"] = $ope_nom;
                    $tab_pn["level"] = $pw_level;
                }
                break;
            case 3 :
            case 7 :
            case 11 :
                $rexist2 = "SELECT tec_prenom,tec_nom FROM `" . TBTEC . "` WHERE `tec_code` = '" . $db->escape($code) . "'";
                $row2 = $db->get_row($rexist2, ARRAY_A);
                if ($row2) {
                    extract($row2);
                    $tab_pn["prenom"] = $tec_prenom;
                    $tab_pn["nom"] = $tec_nom;
                    $tab_pn["level"] = $pw_level;
                }
                break;
            case 4 :
                $rexist2 = "SELECT age_nom FROM `" . TBAGE . "` WHERE `age_code` = '" . $db->escape($code) . "'";
                $row2 = $db->get_row($rexist2, ARRAY_A);
                if ($row2) {
                    extract($row2);
                    $tab_pn["nom"] = $age_nom;
                    $tab_pn["level"] = $pw_level;
                }
                break;
            case 5 :
                $tab_pn["nom"] = _("Centre de gestion");
                $tab_pn["level"] = $pw_level;
                break;
            case 6 :
                $rexist2 = "SELECT do_nom FROM `" . TBDO . "` WHERE `do_code` = '" . $db->escape($code) . "'";
                $row2 = $db->get_row($rexist2, ARRAY_A);
                if ($row2) {
                    extract($row2);
                    $tab_pn["nom"] = $do_nom;
                    $tab_pn["level"] = $pw_level;
                }
                break;
            case 9 :
                $tab_pn["nom"] = _("Administrateur");
                $tab_pn["level"] = $pw_level;
                break;
            case 10 :
                $rexist2 = "SELECT st_nom FROM `" . TBST . "` WHERE `st_code` = '" . $db->escape($code) . "'";
                $row2 = $db->get_row($rexist2, ARRAY_A);
                if ($row2) {
                    extract($row2);
                    $tab_pn["nom"] = $st_nom;
                    $tab_pn["level"] = $pw_level;
                }
                break;
        }
    }
    if ($open == TRUE)
        $db->disconnect();

    return $tab_pn;
}
