<?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 de chalandise   */
/***************************/


/**
 * Vérifie si un département fait partie d'une zone de chalandise dans un pays donné
 *
 * @param      $dep
 * @param      $pays
 * @param bool $open
 *
 * @return bool
 */
function dep_in_zcc_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 `" . TBZCC . "` WHERE `zcc_codepays`='" . $db->escape($pays) . "' AND `zcc_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 de chalandise d'une agence
 *
 * @param      $dep
 * @param      $pays
 * @param      $age
 * @param bool $open
 *
 * @return bool
 */
function dep_in_zcc_age_open($dep, $pays, $age, $open = TRUE)
{
    global
    $db;
    $est_ok = FALSE;
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rdep = "SELECT COUNT(*) FROM `" . TBZCC . "` WHERE `zcc_codepays`='" . $db->escape($pays) . "' AND `zcc_codedep`='" . $db->escape($dep) . "' AND `zcc_code`='" . $db->escape($age) . "'";
    $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 de chalandise d'une agence
 *
 * @param      $age
 * @param bool $open
 *
 * @return string
 */
function str_zcc_age_open($age, $open = TRUE)
{
    global
    $db;
    $str_zcc = "";
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT zcc_codedep, zcc_codepays FROM `" . TBZCC . "` WHERE `zcc_code`='" . $db->escape($age) . "' ORDER BY zcc_codepays, zcc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $str_zcc .= $zcc_codedep . "_" . $zcc_codepays . '%';
        }
    }
    if ($open == TRUE)
        $db->disconnect();
    if ($str_zcc != "")
        $str_zcc = substr($str_zcc, 0, strlen($str_zcc) - 1);
    return $str_zcc;
}

/**
 * Crée un tableau des départements couverts par une agence
 *
 * @param      $age
 * @param bool $open
 *
 * @return array
 */
function tab_zcc_age_open($age, $open = TRUE)
{
    global
    $db;
    $tab_zcc = [];
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT DISTINCT zcc_codepays,zcc_codedep FROM `" . TBZCC . "` WHERE `zcc_code`='" . $db->escape($age) . "' ORDER BY zcc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            if (!exist_in_array(trim($zcc_codedep), $tab_zcc))
                $tab_zcc[] = $zcc_codedep . "_" . $zcc_codepays;
        }
    }
    if ($open == TRUE)
        $db->disconnect();
    return $tab_zcc;
}

/**
 * Crée un tableau avec code_pays associé au libellé correspondant,
 * pour les départements couverts par une agence
 *
 * @param      $age
 * @param bool $open
 *
 * @return array
 */
function tab_zcc_age_open_full($age, $open = TRUE)
{
    global
    $db;
    $tab_zcc = [];
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT DISTINCT zcc_codepays,zcc_codedep FROM `" . TBZCC . "` WHERE `zcc_code`='" . $db->escape($age) . "' ORDER BY zcc_codepays, zcc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            $rech2 = "SELECT dep_nom FROM `" . TBDEP . "` WHERE dep_code='" . $zcc_codedep . "' AND dep_codepays='" . $zcc_codepays . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if (($row2) AND (count($row2) > 0)) {
                extract($row2);
                $rech3 = "SELECT pays_nom FROM `" . TBPAYS . "` WHERE pays_code='" . $zcc_codepays . "'";
                $row3 = $db->get_row($rech3, ARRAY_A);
                if (($row3) AND (count($row3) > 0))
                    extract($row3);
                $chaine_dep = $pays_nom  . " - " . $dep_nom . " (" . $zcc_codedep . ")";
                $tab_zcc[$zcc_codedep . "_" . $zcc_codepays] = $chaine_dep;
            }
        }
    }
    if ($open == TRUE)
        $db->disconnect();
    return $tab_zcc;
}

/**
 * Crée un tableau des départements couverts sur un pays
 *
 * @param      $pays
 * @param bool $open
 *
 * @return array
 */
function tab_zcc_pays_open($pays, $open = TRUE)
{
    global
    $db;
    $tab_zcc = [];
    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT DISTINCT zcc_codepays, zcc_codedep FROM `" . TBZCC . "` WHERE `zcc_codepays`='" . $db->escape($pays) . "' ORDER BY zcc_codepays, zcc_codedep";
    $rows = $db->get_results($rech, ARRAY_A);
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);
            if (!exist_in_array(trim($zcc_codedep), $tab_zcc))
                $tab_zcc[] = $zcc_codedep . "_" . $zcc_codepays;
        }
    }
    if ($open == TRUE)
        $db->disconnect();
    return $tab_zcc;
}
