<?PHP


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


/*************************************/
/*   Zones couvertes (Ss-Traitants)  */
/*************************************/

/**
 * Vérifie si un département fait partie d'une zone couverte dans un pays donné
 *
 * @param      $dep
 * @param      $pays
 * @param bool $open
 *
 * @return bool
 */
function dep_in_zoc_pays_open($dep, $pays, $open = TRUE)
{
    global
    $db;
    $est_ok = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rdep = "SELECT COUNT(*) FROM `" . TBZOC . "` WHERE `zoc_codepays`='" . $db->escape($pays) . "' AND `zoc_codedep`='" . $db->escape($dep) . "'";
    $nbres = $db->get_var($rdep);
    if ($nbres > 0)
        $est_ok = TRUE;
    if ($open == TRUE)
        $db->disconnect();
    return $est_ok;
}

/**
 * Vérifie si un département fait partie d'une zone couverte d'un sous-traitant
 *
 * @param      $dep
 * @param      $pays
 * @param      $st
 * @param bool $open
 *
 * @return bool
 */
function dep_in_zoc_st_open($dep, $pays, $st, $open = TRUE)
{
    global
    $db;
    $est_ok = FALSE;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rdep = "SELECT COUNT(*) FROM `" . TBZOC . "` WHERE `zoc_codepays`='" . $db->escape($pays) . "' AND `zoc_codedep`='" . $db->escape($dep) . "' AND `zoc_code`='" . $db->escape($st) . "'";
    $nbres = $db->get_var($rdep);
    if ($nbres > 0)
        $est_ok = TRUE;
    if ($open == TRUE)
        $db->disconnect();
    return $est_ok;
}

/**
 * Crée le texte des zones couvertes d'un sous-traitant
 *
 * @param      $st
 * @param bool $open
 *
 * @return string
 */
function str_zoc_st_open($st, $open = TRUE)
{
    global
    $db;
    $str_zoc = "";

    if ($st <> '') {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT DISTINCT zoc_codedep FROM `" . TBZOC . "` WHERE `zoc_code`='" . $st . "' ORDER BY zoc_codedep";
        $rows = $db->get_results($rech, ARRAY_A);
        if ($rows) {
            foreach ($rows as $row) {
                $row = encode_str($row);
                $str_zoc .= $row["zoc_codedep"] . "_";
            }
        }
        if ($open == TRUE)
            $db->disconnect();
        if ($str_zoc != "")
            $str_zoc = substr($str_zoc, 0, strlen($str_zoc) - 1);
    }
    return $str_zoc;
}

/**
 * Crée un tableau des départements couverts par un sous-traitant
 *
 * @param      $st
 * @param bool $open
 *
 * @return array
 */
function tab_zoc_st_open($st, $open = TRUE)
{
    global
    $db;
    $tab_zoc = [];;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT DISTINCT zoc_codedep FROM `" . TBZOC . "` WHERE `zoc_code`='" . $st . "' ORDER BY zoc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            if (!exist_in_array(trim($row["zoc_codedep"]), $tab_zoc))
                $tab_zoc[] = $row["zoc_codedep"];
        }
    }
    if ($open == TRUE)
        $db->disconnect();
    return $tab_zoc;
}

/**
 * Crée un tableau des départements couverts sur un sous-traitant
 *
 * @param      $pays
 * @param bool $open
 *
 * @return array
 */
function tab_zoc_pays_open($pays, $open = TRUE)
{
    global
    $db;
    $tab_zoc = [];;

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT DISTINCT zoc_codedep FROM `" . TBZOC . "` WHERE `zoc_codepays`='" . $pays . "' ORDER BY zoc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            if (!exist_in_array(trim($row["zoc_codedep"]), $tab_zoc))
                $tab_zoc[] = $row["zoc_codedep"];
        }
    }
    if ($open == TRUE)
        $db->disconnect();
    return $tab_zoc;
}
