<?php

// --------
// Sécurité
// --------
if (isValidConstant() != 1) {
    fermeSessionUtilisateur();
}

// ----------------------
// Droit de l'utilisateur
// ----------------------
if ($_SESSION['gestion_site'] < 'Oui') {
    exit;
}

// ------------------------------
// Caractéristiques du module
// ------------------------------
unset($module);
$module = array(
    "titre" => "Gestion des employeurs",
    "urldest" => $SCRIPT_NAME . "?page=liste_employeurs",
    "recherche" => array("engin" => false),
    "jeton" => $jeton,
    "opradmin" => "Ajouter un employeur",
    "fenetre" => array(
        "nom" => "liste_employeurs",
        "titre" => "titre",
        "largeur" => "1200",
        "hauteur" => "550",
        "validation" => true,
        "champs" => array("lnoemployeur", "lnom", "lcontact", "ltitre", "ladresse1", "ladresse2", "lville", "lcodepostal", "ltelephone", "ltelecopieur", "lemail")
    ),
    "msgajoutbouton" => "Ajout d'une fiche d'employeur",
    "actionajoutbouton" => "Ajouter la fiche de l'employeur",
    "msgmodifbouton" => "Modification de la fiche d'un employeur",
    "actionmodifbouton" => "Modifier la fiche de l'employeur",
    "nombre_a_afficher" => array(
        0 => array("refid" => 15, "valeur" => 15),
        1 => array("refid" => 30, "valeur" => 30),
        2 => array("refid" => 45, "valeur" => 45),
        3 => array("refid" => 60, "valeur" => 60),
        4 => array("refid" => 120, "valeur" => 120)
    ),
    "lientri" => true,
    "entete" => array(
        1 => array("# Employeur" => "refemployeur", "largeur" => "145px"),
        2 => array("Employeur" => "nomemployeur", "largeur" => "200px"),
        3 => array("Contact" => "nomcontact", "largeur" => "200px"),
        4 => array("Titre/Dept" => "dept", "largeur" => "125px"),
        5 => array("Adresse" => "(adresse1+adresse2+ville+codepostal)", "largeur" => "165px"),
        6 => array("Téléphone" => "numtel", "largeur" => "90px"),
        7 => array("Télécopieur" => "numfax", "largeur" => "90px"),
        8 => array("Adresse @" => "email", "largeur" => "200px"),
        9 => array("Action" => "expression", "largeur" => "45px")
    ),
    "msg" => array(
        "maj_ok" => "Modification effectuée avec succès.",
        "maj_echec" => "L'opération de mise à jour de l'employeur a échouée."
    )
);

// ------------------
// Requête sur le tri
// ------------------
$rtri = antiInjection($module["entete"][$tri][key($module["entete"][$tri])] . " " . $dir);

// -----------------------
// Opération de sauvegarde
// -----------------------
if (isset($action)) {

    $msg = "";
    $action = stripslashes(antiInjection($action));

    if ($action == "Modifier la fiche de l'employeur" || $action == "Ajouter la fiche de l'employeur") {

        $lnoemployeur = antiInjection($lnoemployeur);
        $lnom = antiInjection($lnom);
        $lcontact = antiInjection($lcontact);
        $ltitre = antiInjection(htmlspecialchars($ltitre, ENT_NOQUOTES, "UTF-8"));
        $ladresse1 = antiInjection($ladresse1);
        $ladresse2 = antiInjection($ladresse2);
        $lville = antiInjection($lville);
        $lcodepostal = antiInjection($lcodepostal);
        $ltelephone = antiInjection($ltelephone);
        $ltelecopieur = antiInjection($ltelecopieur);
        $lemail = antiInjection($lemail);

        if ($action == "Modifier la fiche de l'employeur") {
            $sqlparam["type"] = "UPDATE";
            $sqlparam["where"][] = "refemployeur = " . sprintf("%d", $lnoemployeur);
        } else {
            $sqlparam["type"] = "INSERT";
            $sqlparam["champs"]["refemployeur"] = sprintf("%d", $lnoemployeur);
        }

        $sqlparam["table"][] = "employeurs";
        $sqlparam["champs"]["nomemployeur"] = "'" . sprintf("%s", $lnom) . "'";
        $sqlparam["champs"]["nomcontact"] = "'" . sprintf("%s", $lcontact) . "'";
        $sqlparam["champs"]["dept"] = "'" . sprintf("%s", $ltitre) . "'";
        $sqlparam["champs"]["adresse1"] = "'" . sprintf("%s", $ladresse1) . "'";
        $sqlparam["champs"]["adresse2"] = "'" . sprintf("%s", $ladresse2) . "'";
        $sqlparam["champs"]["ville"] = "'" . sprintf("%s", $lville) . "'";
        $sqlparam["champs"]["codepostal"] = "'" . sprintf("%s", $lcodepostal) . "'";
        $sqlparam["champs"]["numtel"] = "'" . sprintf("%s", $ltelephone) . "'";
        $sqlparam["champs"]["numfax"] = "'" . sprintf("%s", $ltelecopieur) . "'";
        $sqlparam["champs"]["email"] = "'" . sprintf("%s", $lemail) . "'";
        // TODO - Mettre une valeur pour le champ «TitreContact» même s'il n'est pas utilisé.
        // TODO - Champ possiblement à retirer.
        $sqlparam["champs"]["titrecontact"] = "'vide'";
        
        $result = executerRequeteSql($sqlparam);
        
        if (isset($result)) {
            unset($lnoemployeur);
            unset($lnom);
            unset($lcontact);
            unset($ltitre);
            unset($ladresse1);
            unset($ladresse2);
            unset($lville);
            unset($lcodepostal);
            unset($ltelephone);
            unset($ltelecopieur);
            unset($lnoemployeur);
            unset($lemail);
            $lnoemployeur = -1;
            $lnom = '';
            $lcontact = '';
            $ltitre = '';
            $ladresse1 = '';
            $ladresse2 = '';
            $lville = '';
            $lcodepostal = '';
            $ltelephone = '';
            $ltelecopieur = '';
            $lemail = '';
            $module["resultatSQL"] = true;
            $module["message"] = $module["msg"]["maj_ok"];
        } else {
            $module["resultatSQL"] = false;
            $module["message"] = $module["msg"]["maj_echec"];
        }
    }
} else {
    unset($lnom);
    unset($lcontact);
    unset($ltitre);
    unset($ladresse1);
    unset($ladresse2);
    unset($lville);
    unset($lcodepostal);
    unset($ltelephone);
    unset($ltelecopieur);
    unset($lnoemployeur);
    unset($lemail);
    $lnom = '';
    $lcontact = '';
    $ltitre = '';
    $ladresse1 = '';
    $ladresse2 = '';
    $lville = '';
    $lcodepostal = '';
    $ltelephone = '';
    $ltelecopieur = '';
    $lnoemployeur = '';
    $lemail = '';
}

$i = 0;
unset($sqlparam);

$sqlparam["table"][] = "employeurs";
$sqlparam["champs"][] = "employeurs.refemployeur";
$sqlparam["champs"][] = "employeurs.nomemployeur";
$sqlparam["champs"][] = "employeurs.nomcontact";
$sqlparam["champs"][] = "employeurs.dept";
$sqlparam["champs"][] = "employeurs.adresse1";
$sqlparam["champs"][] = "employeurs.adresse2";
$sqlparam["champs"][] = "employeurs.ville";
$sqlparam["champs"][] = "employeurs.codepostal";
$sqlparam["champs"][] = "employeurs.numtel";
$sqlparam["champs"][] = "employeurs.numfax";
$sqlparam["champs"][] = "employeurs.email";
$sqlparam["champs"][] = "(SELECT count(employeurs.refemployeur) FROM employeurs) as compte";
$sqlparam["limite"] = ($nombreaffiche * ($nopage - 1)) . "," . $nombreaffiche + 1;
$sqlparam["ordre"][] = $rtri;

$result = executerRequeteSql($sqlparam);
if (isset($result) && is_array($result) && count($result) > 0) {
    $module["compte"] = $result[0]["compte"];
} else {
    $module["compte"] = 0;
}

print preparerModule($module);

//Liste principale
if (isset($result)) {
    $i = ($nopage - 1) * $nombreaffiche + 1;
    unset($style);
    $style = "border:1px solid #cfcfcf; font-family:arial, helvetica, sans-serif; font-size:9pt; ";
    $style .= "margin-left:5px; margin-right:5px; padding-right:10px;overflow-x:hidden; white-space:nowrap;";

    //Boucle de lecture
    foreach ($result as $clef => $champs) {
        $adresse1 = ($champs["adresse2"] != "") ? ", " . $champs["adresse2"] . ", " : ", ";
        $adresse2 = ($champs["adresse2"] != "") ? ",\" " . $champs["adresse2"] . "\", " : ", ";
        $telephone = ($champs["numtel"] != "") ? $champs["numtel"] : " ";
        $telecopieur = ($champs["numfax"] != "") ? $champs["numfax"] : " ";
        $email = (empty($champs["email"]) ? "&nbsp;" : $champs["email"]);

        print "<tr>\n";
        print "<td style='$style'>" . $i . "</td>";
        print "<td class='detail_liste' style='width:" . $module["entete"][1]["largeur"] . "px;'title=\"" . $champs["refemployeur"] . "\">" . $champs["refemployeur"] . "</td>";
        print "<td class='detail_liste' style='width:" . $module["entete"][2]["largeur"] . "px;' title=\"" . htmlentities($champs["nomemployeur"]) . "\">";
        print "<div style='overflow:hidden; white-space:nowrap; width:200px;'>" . htmlentities($champs["nomemployeur"], ENT_SUBSTITUTE, "UTF-8") . "</div></td>";
        print "<td class='detail_liste' style='width:" . $module["entete"][3]["largeur"] . "px;' title=\"" . $champs["nomcontact"] . "\">";
        print "<div style='overflow:hidden; white-space:nowrap; width:200px;'>" . htmlentities($champs["nomcontact"], ENT_SUBSTITUTE, "UTF-8") . "</div></td>";
        print "<td class='detail_liste' style=' width:" . $module["entete"][4]["largeur"] . "px;' title=\"" . $champs["dept"] . "\">";
        print "<div style='overflow:hidden; white-space:nowrap; width:125px;'>" . htmlentities($champs["dept"], ENT_SUBSTITUTE, "UTF-8") . "</div></td>";
        print "<td class='detail_liste' style='width:" . $module["entete"][5]["largeur"] . "px;' title=\"" . htmlentities($champs["adresse1"] . $adresse1 . $champs["ville"] . " " . $champs["codepostal"], ENT_SUBSTITUTE, "UTF-8") . "\">";
        print "<div style='overflow:hidden; white-space:nowrap; width:165px;'>" . htmlentities($champs["adresse1"] . $adresse1 . $champs["ville"] . " " . $champs["codepostal"], ENT_SUBSTITUTE, "UTF-8") . "</div></td>\n";
        print "<td class='detail_liste' style='width:" . $module["entete"][6]["largeur"] . "px;' title=\"" . $telephone . "\">";
        print htmlentities($telephone, ENT_SUBSTITUTE, "UTF-8") . "</td>\n";
        print "<td class='detail_liste' style='width:" . $module["entete"][7]["largeur"] . "px;' title=\"" . $telecopieur . "\">";
        print htmlentities($telecopieur, ENT_SUBSTITUTE, "UTF-8") . "</td>\n";
        print "<td class='detail_liste' style='width:" . $module["entete"][8]["largeur"] . "px;' title=\"" . $email . "\">";
        print "<div style='overflow:hidden; white-space:nowrap; width:200px;'>" . htmlentities($email, ENT_SUBSTITUTE, "UTF-8") . "</div></td>\n";
        print "<td class='detail_liste' style='width:30x;'>\n";
        print "<form action='' name='sous_formulaire$i'>";
        print prepareBoutonModifier(array($champs["refemployeur"], $champs["nomemployeur"], $champs["nomcontact"],
            $champs["dept"], $champs["adresse1"], $champs["adresse2"], $champs["ville"], $champs["codepostal"], $champs["numtel"],
            $champs["numfax"], $champs["email"]));
        print "</form>";
        print "</td>\n";
        print "</tr>\n";
        $i++;
    }
    print"</table>";
    ?>
    <script type='text/javascript'>
        function ValidationFormulaire()
        {
            var msgNom = "Nom de l'employeur requis.";
            var msgPersonne = "Nom de la personne contact requis.";
            var msgTitre = "Titre de la personne contact requis.";
            var msgAdresse = "Adresse requise.";
            var msgVille = "Nom de la ville requise.";
            var msgCodePostal1 = "Code postal requis.";
            var msgTel1 = "Numéro de téléphone requis.";
            var msgTel2 = "Numéro de télécopieur requis.";
            var msgEmail = "Adresse de courriel valide requise";

            var frm = document.frmEmp;

            if ($("#lnom").val() === "") {
                jAlert(msgNom);
                return false;
            } else if (frm.lcontact.value === "") {
                jAlert(msgPersonne);
                return false;
            } else if (frm.ltitre.value === "") {
                jAlert(msgTitre);
                return false;
            } else if (frm.ladresse1.value === "") {
                jAlert(msgAdresse);
                return false;
            } else if (frm.lville.value === "") {
                jAlert(msgVille);
                return false;
            } else if (!isPostCode(frm.lcodepostal.value)) {
                jAlert(msgCodePostal1);
                return false;
            } else if (!isTelephone(frm.ltelephone.value)) {
                jAlert(msgTel1);
                frm.ltelephone.focus();
                return false;
            } else if (!isTelephone(frm.ltelecopieur.value)) {
                jAlert(msgTel2);
                frm.ltelecopieur.focus();
                return false;
            } else if (!isCourrielValide(frm.lemail.value)) {
                jAlert(msgEmail);
                frm.lemail.focus();
                return false;
            }

            closeRemote();
            return true;
        }

        function modifpres() {
            var frm = document.frmModif;
            var valeurTexte = remove(frm.lcontact.value, "Madame ");
            valeurTexte = remove(valeurTexte, "Monsieur ");

            if (frm.lpresentation.selectedIndex > 0) {
                frm.lcontact.value = frm.lpresentation.value + " " + valeurTexte;
            }
        }

        // Mise en place des masques de saisie
        $(document).ready(function () {
            $("#lcodepostal").mask("a9a 9a9");
            $("#ltelephone").mask("(999) 999-9999");
            $("#ltelecopieur").mask("(999) 999-9999");
            $("#frmModif").validate();
        });

    </script>
    <?php

    // Pagination
    print "<table style='width:968px; margin:auto;'>";
    print "<tr><td style='vertical-align:bottom;'>";
    print $seqpage;
    print "</td>";
    print "<td style='width:320px; text-align:right;'>";
    //Haut de page
    if ($nombreaffiche > 15 && $module["compte"] > $nombreaffiche) {
        print "<a href='#hautpage' title='Aller en haut de la page'>Haut de page</a>";
        print "<a name='baspage'>";
    }
    print "</td></tr>";
    print "</table>";
    print fenetreModaleDebut($module["fenetre"], $SCRIPT_NAME, $jeton);
    print "<tr>";
    print "<td># Employeur : </td>";

    print "<td><INPUT type='text' id='lnoemployeur' name='lnoemployeur' ";
    print "style='border:0px; ' tabindex='0' value='" . $lnoemployeur . "' class='required'></td>";
    print "</tr>";
    print "<tr>";
    print "<td>Nom : </td>";
    print "<td>";
    print "<INPUT TYPE='text' id='lnom' NAME='lnom' Value='" . $lnom . "' SIZE='50' MAXLENGTH='50'";
    print "class='required '>";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Personne contact : </td>";
    print "<td>";
    print "<select name='lpresentation' onblur=\"javascript:modifpres()\" onclick=\"javascript:modifpres()\">";
    print "<option>-----</option>";
    print "<option>Madame</option>";
    print "<option>Monsieur</option>";
    print "</select>";
    print "<INPUT TYPE='text' NAME='lcontact' id='lcontact' Value='$lcontact' SIZE='50' MAXLENGTH='50' >";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Titre / Département : </td>";
    print "<td>";
    print "<INPUT TYPE='text' NAME='ltitre' id='ltitre' Value='$ltitre' SIZE='50' MAXLENGTH='50' >";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Adresse 1: </td>";
    print "<td>";
    print "<INPUT TYPE='text' NAME='ladresse1' id='ladresse1' Value='$ladresse1' SIZE='50' MAXLENGTH='50' >";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Adresse 2: </td>";
    print "<td>";
    print "<INPUT TYPE='text' NAME='ladresse2' id='ladresse2' Value='$ladresse2' SIZE='50' MAXLENGTH='50'><br>";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Ville : </td>";
    print "<td>";
    print "<INPUT TYPE='text' NAME='lville' id='lville' Value='$lville' SIZE='30' MAXLENGTH='30' ><br>";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Code postal : </td>";
    print "<td>";
    print "<INPUT TYPE='text' id='lcodepostal' NAME='lcodepostal' Value='$lcodepostal' SIZE='7' MAXLENGTH='7' >";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Téléphone : </td>";
    print "<td>";
    print "<INPUT class='mask_phone' TYPE='text'NAME='ltelephone' Value='$ltelephone' id='ltelephone' SIZE='14' MAXLENGTH='14' >";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Télécopieur : </td>";
    print "<td>";
    print "<INPUT class='mask_phone' TYPE='text' NAME='ltelecopieur' Value='$ltelecopieur' id='ltelecopieur' SIZE='14' MAXLENGTH='14' >";
    print "</td>";
    print "</tr>";
    print "<tr>";
    print "<td>Adresse de courriel : </td>";
    print "<td>";
    print "<INPUT TYPE='text' NAME='lemail' id='lemail' Value='$lemail' ";
    print "SIZE='50' MAXLENGTH='50' class='required email'><br>";
    print "</td >";
    print "</tr>";
    print fenetreModaleFin($module["actionmodifbouton"]);
}
