<?php

define("LIBERATIONSWEB", 20091223);

//Librairie de fonctions
session_start();

//Librairie de fonctions
require (__DIR__ . '/vendor/autoload.php');
require_once("lib/libconfig.inc.php");
require_once("lib/libsession.inc.php");
require_once("lib/libfonctions.inc.php");
require_once("lib/libmysql.inc.php");
require_once("lib/libencode.inc.php");
require_once("lib/libcourriel.inc.php");
require_once("lib/libcentrale.inc.php");
require_once("fpdf/fpdf.php");

class PDF extends FPDF {

    var $NomContact;
    var $Dept;
    var $NomEmployeur;
    var $EmployeursAdresse1;
    var $EmployeursVille;
    var $EmployeursCodePostal;
    var $DateDemande;
    var $NumEmploye;
    var $NomEmploye;
    var $EmployesAdresse;
    var $Opr;
    var $VilleEmploye;
    var $configCentrale;

    function WordWrap(&$text, $maxwidth) {
        $text = trim($text);
        if ($text === '')
            return 0;
        $space = $this->GetStringWidth(' ');
        $lines = explode("\n", $text);
        $text = '';
        $count = 0;

        foreach ($lines as $line) {
            $words = preg_split('/ +/', $line);
            $width = 0;

            foreach ($words as $word) {
                $wordwidth = $this->GetStringWidth($word);
                if ($wordwidth > $maxwidth) {
                    // Word is too long, we cut it
                    for ($i = 0; $i < strlen($word); $i++) {
                        $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                        if ($width + $wordwidth <= $maxwidth) {
                            $width += $wordwidth;
                            $text .= substr($word, $i, 1);
                        } else {
                            $width = $wordwidth;
                            $text = rtrim($text) . "\n" . substr($word, $i, 1);
                            $count++;
                        }
                    }
                } elseif ($width + $wordwidth <= $maxwidth) {
                    $width += $wordwidth + $space;
                    $text .= $word . ' ';
                } else {
                    $width = $wordwidth + $space;
                    $text = rtrim($text) . "\n" . $word . ' ';
                    $count++;
                }
            }
            $text = rtrim($text) . "\n";
            $count++;
        }
        $text = rtrim($text);
        return $count;
    }

    //Entête de page
    function Header() {
        $Image41 = 'logo/' . $_SESSION["syndicat"] . "." . $_SESSION['logo'];
        $this->Image($Image41, 139, 25, 47, 22);

        //Employeur
        $x1 = 119;
        $x2 = 159;
        $x3 = 160;

        //Employeur
        $this->SetXY($x1, 54);
        $this->SetFont('Times', 'BU', 11);
        $etiquette64 = "Employeur";
        $this->Cell(52, 6, $etiquette64, 0, 0, 'L');

        $this->SetXY($x1, 60);
        $this->SetFont('Times', '', 11);
        $this->Cell(70, 5, $this->NomContact, 0, 0, 'L');

        $this->SetXY($x1, 65);
        $this->SetFont('Times', '', 11);
        $this->Cell(91, 5, $this->Dept, 0, 0, 'L');

        $this->SetXY($x1, 70);
        $this->SetFont('Times', '', 11);
        $this->Cell(91, 5, $this->NomEmployeur, 0, 0, 'L');

        $this->SetXY($x1, 75);
        $this->SetFont('Times', '', 11);
        $this->Cell(91, 5, $this->EmployeursAdresse1, 0, 0, 'L');

        $this->SetXY($x1, 80);
        $this->SetFont('Times', '', 11);
        $this->Cell(37, 5, $this->EmployeursVille, 0, 0, 'L');

        $this->SetXY($x2, 80);
        $this->SetFont('Times', '', 11);
        $this->Cell(30, 5, $this->EmployeursCodePostal, 0, 0, 'L');

        $this->SetXY($x3, 90);
        $this->SetFont('Times', '', 11);
        $this->Cell(28, 5, $this->DateDemande, 0, 0, 'L');

        //étiquette de détail
        $this->SetXY(119, 90);
        $this->SetFont('Times', 'B', 11);
        $DateDemande_etiquette = "Date de la demande :";
        $this->Cell(28, 5, $DateDemande_etiquette, 0, 0, 'L');

        $this->SetXY(25, 90);
        $this->SetFont('Times', 'B', 11);
        $etiquette46 = mb_convert_encoding("LA(LES) LIBÉRATION(S) SYNDICALE(S)", 'ISO-8859-1', 'UTF-8');
        $this->Cell(67, 6, $etiquette46, 0, 0, 'L');

        $this->SetXY(25, 95);
        $this->SetFont('Times', 'BU', 11);
        $etiquette56 = "DATE(S)";
        $this->Cell(16, 6, $etiquette56, 0, 0, 'L');

        $this->SetXY(64, 95);
        $this->SetFont('Times', 'BU', 11);
        $etiquette57 = "ARTICLE";
        $this->Cell(20, 6, $etiquette57, 0, 0, 'L');

        $this->SetXY(46, 95);
        $this->SetFont('Times', 'BU', 11);
        $etiquette59 = mb_convert_encoding("DURÉE", 'ISO-8859-1', 'UTF-8');
        $this->Cell(16, 6, $etiquette59, 0, 0, 'L');

        $this->SetXY(86, 95);
        $this->SetFont('Times', 'BU', 11);
        $etiquette71 = "LIEU";
        $this->Cell(20, 6, $etiquette71, 0, 0, 'L');

        $this->SetXY(119, 95);
        $this->SetFont('Times', 'BU', 11);
        $etiquette58 = "COMMENTAIRES";
        $this->Cell(29, 6, $etiquette58, 0, 0, 'L');

        //Employé
        $this->SetXY(25, 54);
        $this->SetFont('Times', 'BU', 11);
        $etiquette65 = mb_convert_encoding("Employé", 'ISO-8859-1', 'UTF-8');
        $this->Cell(52, 6, $etiquette65, 0, 0, 'L');

        $this->SetXY(25, 30);
        $this->SetFont('Times', 'B', 20);
        $this->Cell(113, 9, $this->Opr, 0, 0, 'C');

        $this->SetXY(25, 60);
        $this->SetFont('Times', '', 11);
        $this->Cell(60, 5, $this->NomEmploye, 0, 0, 'L');

        $this->SetXY(25, 65);
        $this->SetFont('Times', '', 11);
        $this->Cell(60, 5, $this->EmployesAdresse, 0, 0, 'L');

        $this->SetXY(25, 70);
        $this->SetFont('Times', '', 11);
        $this->Cell(59, 5, $this->VilleEmploye, 0, 0, 'L');

        $this->SetXY(25, 75);
        $this->SetFont('Times', '', 11);
        $etiquette66 = "Matricule :";
        $this->Cell(19, 5, $etiquette66, 0, 0, 'L');

        $this->SetXY(46, 75);
        $this->SetFont('Times', '', 11);
        $this->Cell(19, 5, $this->NumEmploye, 0, 0, 'L');

        $this->SetLineWidth(1);
        $this->Rect(25, 25, 113, 22);

        $this->SetY(100);
    }

    //Pied de page
    function Footer() {
        //Numéro de page
        //Positionnement à 1,5 cm du bas
        //Police Arial italique 8
        $this->SetY(-12);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');

        $etiquette52 = mb_convert_encoding($this->configCentrale["presidence_nom"] . ", " .
                $this->configCentrale["presidence_poste"], 'ISO-8859-1', 'UTF-8');
        $etiquette43 = mb_convert_encoding($this->configCentrale["syndicat_nom"], 'ISO-8859-1', 'UTF-8');
        $etiquette45 = mb_convert_encoding($this->configCentrale["adresse"] . ", " . $this->configCentrale["ville"] .
                " (" . $this->configCentrale["province"] . ") " . $this->configCentrale["codepostal"] .
                " Téléphone : " . str_replace(array("(", ")"), "", $this->configCentrale["telephone"]) .
                " Télécopieur : " . str_replace(array("(", ")"), "",
                        $this->configCentrale["telecopieur"]), 'ISO-8859-1', 'UTF-8');
        $etiquette71 = mb_convert_encoding("À compléter par l'employeur et retourner par courriel à " .
                $this->configCentrale["courriel"], 'ISO-8859-1', 'UTF-8');

        // Ligne de signature
        $this->SetLineWidth(0);
        $this->Line(125, 218, 184, 218);

        // Nom de la personne à la présidence
        $this->SetXY(125, 220);
        $this->SetFont('Times', 'B', 9);
        $this->WordWrap($etiquette52, 57);
        $this->MultiCell(60, 4, $etiquette52);

        //Nom du syndicat
        $this->SetXY(25, 259);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(160, 4, $etiquette43, 0, 0, 'C');

        // Adresse
        $this->SetXY(25, 264);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(160, 4, $etiquette45, 0, 0, 'C');

        // Mention
        $this->SetXY(130, 232);
        $this->SetFont('Times', '', 9);
        $this->WordWrap($etiquette71, 38);
        $this->MultiCell(38, 3, $etiquette71);

        $this->SetLineWidth(1);
        $this->Line(25, 264, 184, 264);

        // Rectangle employeur
        $this->SetLineWidth(0);
        $this->Rect(29, 229, 145, 23);

        // Section employeur
        $this->SetXY(32, 227);
        $this->SetFont('Times', 'B', 9);
        $etiquette69 = "Section Employeur";
        $this->SetFillColor(255, 255, 255);
        $this->Cell(37, 4, $etiquette69, 0, 0, 'L', true);
        $this->SetXY(32, 227);
        $this->SetFillColor(0, 0, 0);
        $this->Cell(37, 4, $etiquette69, 0, 0, 'L', false);

        $this->SetLineWidth(0);
        $this->Rect(33, 234, 4, 4);

        // Case à cocher Acceptée
        $this->SetXY(41, 236);
        $this->SetFont('Times', 'B', 9);
        $etiquette73 = mb_convert_encoding("Acceptée", 'ISO-8859-1', 'UTF-8');
        $this->Cell(21, 4, $etiquette73, 0, 0, 'L');

        $this->SetLineWidth(0);
        $this->Rect(83, 234, 4, 4);

        // Case à cocher Refusée
        $this->SetXY(92, 235);
        $this->SetFont('Times', 'B', 9);
        $etiquette76 = mb_convert_encoding("Refusée", 'ISO-8859-1', 'UTF-8');
        $this->Cell(21, 4, $etiquette76, 0, 0, 'L');

        $this->SetLineWidth(0);
        $this->Line(34, 246, 66, 246);

        // Signature employeur
        $this->SetXY(38, 247);
        $this->SetFont('Times', 'B', 9);
        $etiquette78 = "Signature";
        $this->Cell(24, 4, $etiquette78, 0, 0, 'L');

        $this->SetLineWidth(0);
        $this->Line(75, 246, 107, 246);

        $this->SetXY(77, 247);
        $this->SetFont('Times', 'B', 9);
        $etiquette80 = "Date";
        $this->Cell(24, 4, $etiquette80, 0, 0, 'L');
    }
}

// ********************
// Traitement principal
// ********************
//Traitement des paramètres
$rlreference = antiInjection(filter_input(INPUT_GET, "lreference"));
$rltypeopr = antiInjection(filter_input(INPUT_GET, "ltypeopr"));
$rldatecreation = antiInjection(filter_input(INPUT_GET, "ldatecreation"));
$rdatelib = antiInjection(filter_input(INPUT_GET, "datelib"));
$rlarticle = antiInjection(filter_input(INPUT_GET, "larticle"));
$rduree = antiInjection(filter_input(INPUT_GET, "lduree"));
$rcommentaire = antiInjection(filter_input(INPUT_GET, "commentaire"));
$rlieu = antiInjection(filter_input(INPUT_GET, "lieu"));
$rlassumerpar = antiInjection(filter_input(INPUT_GET, "lassumerpar"));

unset($sqlparam);
$sqlparam["table"][] = "employes";
$sqlparam["table"][] = "employeurs";
$sqlparam["join"][] = "employes.refemployeur=employeurs.refemployeur";
$sqlparam["champs"][] = "concat(IF(employes.presentation=-1,'Madame','Monsieur'),\" \","
        . "employes.prenom,\" \",employes.nomfamille) as nomemploye";
$sqlparam["champs"][] = "CONCAT(employes.adresse1, if(isnull(employes.adresse2),' ',"
        . "CONCAT(' ',employes.adresse2))) AS adresse";
$sqlparam["champs"][] = "CONCAT(employes.ville,\", \",LEFT(employes.codepostal,3),\" \","
        . "RIGHT(employes.codepostal,3)) as ville";
$sqlparam["champs"][] = "employes.numtel";
$sqlparam["champs"][] = "employes.numemploye";
$sqlparam["champs"][] = "employeurs.nomemployeur";
$sqlparam["champs"][] = "employeurs.nomcontact";
$sqlparam["champs"][] = "employeurs.dept";
$sqlparam["champs"][] = "employeurs.adresse1 as adresseemployeur";
$sqlparam["champs"][] = "employes.ville as villeemployeur";
$sqlparam["champs"][] = "CONCAT(LEFT(employes.codepostal,3),\" \",RIGHT(employes.codepostal,3)) as codepost2";
$sqlparam["ordre"][] = "CONCAT(employes.nomfamille, ' ', employes.prenom) ASC";
$sqlparam["where"][] = sprintf("refemploye=%d", $rlreference);

$result = executerRequeteSql($sqlparam);

unset($sqlparam);
$sqlparam["table"][] = "articles";
$sqlparam["champs"][] = "refarticle";
$sqlparam["champs"][] = "noarticle";
$sqlparam["ordre"][] = "noarticle ASC";
$resultart = executerRequeteSql($sqlparam);

unset($article_tab);
if (isset($resultart)) {
    foreach ($resultart as $clef => $valeur) {
        $article_tab[$valeur["refarticle"]] = $valeur["noarticle"];
    }
}

unset($sqlparam);
$sqlparam["table"][] = "duree";
$sqlparam["champs"][] = "refduree";
$sqlparam["champs"][] = "duree";
$sqlparam["ordre"][] = "duree asc";
$resultduree = executerRequeteSql($sqlparam);

unset($duree_tab);
if (isset($resultduree)) {
    foreach ($resultduree as $clef => $valeur) {
        $duree_tab[$valeur["refduree"]] = $valeur["duree"];
    }
}
//Si aucune libération, alors erreur
if (!($result && is_array($result) && !empty($result))) {
    die("Erreur B");
}
//Instanciation de la classe dérivée
$pdf = new PDF('P', 'mm', 'Letter');

$champs = $result[0];

//Info de l'employeur
$pdf->NomContact = $champs["nomcontact"];
$pdf->Dept = $champs["dept"];
$pdf->NomEmployeur = $champs["nomemployeur"]; //2
$pdf->EmployeursAdresse1 = stripslashes($champs["adresseemployeur"]); //3
$pdf->EmployeursVille = $champs["villeemployeur"]; //4
$pdf->EmployeursCodePostal = $champs["codepost2"]; //5
//Info de l'employé
$pdf->NumEmploye = mb_convert_encoding($champs["numemploye"], 'ISO-8859-1', 'UTF-8'); //0
$pdf->NomEmploye = mb_convert_encoding($champs["nomemploye"], 'ISO-8859-1', 'UTF-8'); //1
$pdf->EmployesAdresse = mb_convert_encoding($champs["adresse"], 'ISO-8859-1', 'UTF-8');
$pdf->VilleEmploye = mb_convert_encoding($champs["ville"], 'ISO-8859-1', 'UTF-8');
$pdf->Opr = mb_convert_encoding((($rltypeopr == 1) ? "Demande" : "Annulation") .
        " de libération", 'ISO-8859-1', 'UTF-8');
$pdf->DateDemande = mb_convert_encoding($rldatecreation, 'ISO-8859-1', 'UTF-8');

$pdf->SetMargins(25, 25);
$pdf->AliasNbPages();
$pdf->SetFont('Times', '', 9);
$pdf->AddPage();
$pdf->configCentrale = buildConfigCentrale();

$nombreligne = sizeof($rdatelib);

for ($i = 1; $i <= $nombreligne; $i++) {
    $pdf->datelib = mb_convert_encoding(antiInjection($rdatelib[$i - 1]));
    $pdf->SetX(25);
    $pdf->Cell(16, 4, $pdf->datelib, 0, 0, 'L');

    $rduree[$i - 1] = str_replace(",", "", $rduree[$i - 1]);
    $pdf->duree = $duree_tab[$rduree[$i - 1]];
    $pdf->SetX(46);
    $pdf->Cell(16, 4, $pdf->duree, 0, 0, 'L');

    $rlarticle[$i - 1] = str_replace(",", "", $rlarticle[$i - 1]);
    $pdf->article = mb_convert_encoding($article_tab[$rlarticle[$i - 1]]);

    $pdf->SetX(64);
    $pdf->Cell(15, 4, $pdf->article, 0, 0, 'L');

    $pdf->lieu = mb_convert_encoding(stripslashes($rlieu[$i - 1]));
    $pdf->SetX(86);
    $pdf->Cell(20, 4, $pdf->lieu, 0, 0, 'L');

    $pdf->commentaire = mb_convert_encoding(stripslashes($rcommentaire[$i - 1]));
    $pdf->SetX(119);
    $pdf->MultiCell(84, 4, $pdf->commentaire);

    if ($_SESSION["syndicatnom"] == "siiieq") {
        $pdf->ln();
    }
}

$pdf->Output();

