<?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 articles",
    "urldest" => $SCRIPT_NAME . "?page=liste_articles&jeton=" . $jeton,
    "recherche" => array("engin" => false),
    "jeton" => $jeton,
    "fenetre" => array(
        "nom" => "liste_articles",
        "titre" => "titre",
        "largeur" => "400",
        "hauteur" => "280",
        "champs" => array("lnoarticle", "larticle", "lcommentaire", "lexpression")
    ),
    "msgmodifbouton" => "Modification de la fiche de l'article",
    "actionmodifbouton" => "Modifier l'article",
    "taille" => "800px",
    "lientri" => true,
    "entete" => array(
        1 => array("Article" => "refarticle", "largeur" => "85px"),
        2 => array("Commentaire" => "commentaire", "largeur" => "662px"),
        3 => array("Expression" => "expression", "largeur" => "170px"),
        4 => array("Action" => "expression", "largeur" => "45px")
    ),
    "msg" => array(
        "maj_ok" => "Mise à jour de l'article effectuée avec succès.",
        "maj_echec" => "L'opération de mise à jour de l'article a échouée."
    )
);

// ------------------
// Requête sur le tri
// ------------------
//Traitement spécial pour le tri concernant ce module
//Le tri pour la colonne Article est différent de la norme.
if (key($module["entete"][$tri])==="Article") {
    $rtri = antiInjection("length(refarticle) $dir, noarticle $dir");
} else {
    $rtri = antiInjection($module["entete"][$tri][key($module["entete"][$tri])] . " " . $dir);
}

// -----------------------
// Opération de sauvegarde
// -----------------------
if (isset($action)) {
    $msg = "";
    //Cueillette de données du module
    $action = stripslashes(antiInjection($action));
    if ($action == "Modifier l'article") {
        $error = false;

        if ($rlnoarticle === "" || intval($rlnoarticle) < 0) {
            $module["resultatSQL"] = false;
            $module["message"] = "Le numéro d'article de la convention est invalide ou inexistant.";
            $error = true;
        }
        
        if (!$error) {

            unset($sqlparam, $result);
            $lnoemployeur = antiInjection($lnoemployeur);
            $sqlparam["table"][] = "articles";
            $sqlparam["where"][] = sprintf("refarticle = %d", $rlnoarticle);
            $result = executerRequeteSql($sqlparam);

            if (!isset($result)) {
                $module["resultatSQL"] = false;
                $module["message"] = "Le numéro d'article de la convention est invalide ou inexistant.";
            } else {
                unset($sqlparam, $result);

                $rlnoarticle = antiInjection($lnoarticle);
                $rlcommentaire = antiInjection($lcommentaire);
                $rlexpression = antiInjection($lexpression);
                $sqlparam["type"] = "UPDATE";
                $sqlparam["table"][] = "articles";
                $sqlparam["where"][] = "refarticle = " . $rlnoarticle;
                $sqlparam["champs"]["commentaire"] = "'$rlcommentaire'";
                $sqlparam["champs"]["expression"] = "'$rlexpression'";

                $result = executerRequeteSql($sqlparam);
                if (isset($result)) {
                    $module["resultatSQL"] = true;
                    $module["message"] = $module["msg"]["maj_ok"];
                } else {
                    $module["resultatSQL"] = false;
                    $module["message"] = $module["msg"]["maj_echec"];
                }
            }
        }
    }
}

$i = 0;
unset($sqlparam);
$sqlparam["table"][] = "articles";
$sqlparam["champs"][] = "refarticle";
$sqlparam["champs"][] = "noarticle";
$sqlparam["champs"][] = "commentaire";
$sqlparam["champs"][] = "expression";
$sqlparam["champs"][] = "(SELECT count(articles.refarticle) FROM articles) as compte";
$sqlparam["ordre"][] = $rtri;

$result = executerRequeteSql($sqlparam);
$module["compte"] = $result[0]["compte"];
print preparerModule($module);

if (isset($result)) {

    // Style CSS pour les liste
    $i = ($nopage - 1) * $nombreaffiche + 1;
    unset($style);
    $style = "border:1px solid #cfcfcf; font-family:arial, helvetica, sans-serif; ";
    $style .= "font-size:9pt; margin-left:5px; margin-right:5px;";

    //Boucle de lecture
    foreach ($result as $clef => $champs) {
        $champs4 = ($champs["expression"] != "") ? $champs["expression"] : " ";
        print "<tr>\n";
        print "<td style='$style'>" . $i . "</td>\n";
        print "<td style='$style'>" . htmlentities($champs["noarticle"], ENT_SUBSTITUTE, "UTF-8") . "</td>\n";
        print "<td style='$style'>" . htmlentities($champs["commentaire"], ENT_SUBSTITUTE, "UTF-8") . "</td>\n";
        print "<td style='$style'>" . htmlentities($champs4, ENT_SUBSTITUTE, "UTF-8") . "</td>\n";
        print "<td style='$style'>\n";

        print "<form action='' name='sous_formulaire$i'>";
        print prepareBoutonModifier(array($champs["refarticle"], $champs["noarticle"], $champs["commentaire"], $champs4));

        print "</form>";

        print "\n</td>\n";
        print "</tr>\n";
        $i++;
    }
    print"</table>\n";

    // Pagination
    print "<table style='width:968px; margin:auto;'>\n";
    print "<tr><td style='vertical-align:bottom;'>\n";
    print $seqpage;
    print "</td>\n";
    print "<td style='width:320px; text-align:right;'>\n";
    //Haut de page
    if ($nombreaffiche > 15) {
        print "<a href='#hautpage' title='Aller en haut de la page'>Haut de page</a>\n";
        print "<a name='baspage'>\n";
    }
    print "</td></tr>\n";
    print "</table>\n";

    print fenetreModaleDebut($module["fenetre"], $SCRIPT_NAME, $jeton);
    print "     <tr>\n";
    print "                <td>Article : </td>\n";
    print "                <td><input type='text' readonly='readonly' id='larticle' value=''>\n";
    print "                <input type='hidden' id='lnoarticle' name='lnoarticle' value=''>\n";
    print "                </td>\n";
    print "             </tr>\n";
    print "             <tr>\n";
    print "                <td>Commentaire : </td>\n";
    print "                <td><textarea rows='2' cols='24' id='lcommentaire' name='lcommentaire'></textarea></td>\n";
    print "             </tr>\n";
    print "             <tr>\n";
    print "                <td>Expression : </td>\n";
    print "                <td><INPUT TYPE='text' id='lexpression' name='lexpression' Value='' SIZE='20' MAXLENGTH='20'></td>\n";
    print "             </tr>\n";
    print fenetreModaleFin($module["actionmodifbouton"]);
}

// Mise en place des masques de saisie
print "
<script type='text/javascript'>
   $(document).ready(function() {
      $(\"#frmModif\").validate();
   });
</script>";
