<?php

define("LIBERATIONSWEB", 20091223);

session_start();

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

//Librairie de fonctions
require_once(__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/libliberations.inc.php");
require_once("lib/libcentrale.inc.php");
require_once('fpdf/fpdf.php');

class PDF extends FPDF {

    var $NomEmploye;
    var $NumEmploye;
    var $Adresse1;
    var $Adresse2;
    var $Ville;
    var $CodePostal;
    var $NumeroTel;
    var $Texte17;
    var $Texte18;
    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++;
            $champs["numtel"] = str_replace("-", "", $champs["numtel"]);
        }
        $text = rtrim($text);
        return $count;
    }

    //Entête de page 
    function Header() {
        $this->SetXY(26, 50);
        $this->SetFont('Times', 'BI', 11);
        $NomEmploye_etiquette = mb_convert_encoding("Employé", 'ISO-8859-1', 'UTF-8');
        
        $this->Cell(44, 5, $NomEmploye_etiquette, 0, 0, 'L');

        $this->SetXY(73, 49);
        $this->SetFont('Times', 'BI', 11);
        $NumEmploye_etiquette = mb_convert_encoding("Employé #", 'ISO-8859-1', 'UTF-8');
        
        $this->Cell(21, 5, $NumEmploye_etiquette, 0, 0, 'L');

        $this->SetXY(96, 49);
        $this->SetFont('Times', 'BI', 11);
        $Adresse1_etiquette = "Adresse";
        $this->Cell(70, 5, $Adresse1_etiquette, 0, 0, 'L');

        $this->SetXY(168, 49);
        $this->SetFont('Times', 'BI', 11);
        $Ville_etiquette = "Ville";
        $this->Cell(26, 5, $Ville_etiquette, 0, 0, 'L');

        $this->SetXY(206, 49);
        $this->SetFont('Times', 'BI', 11);
        $CodePostal_etiquette = "Code postal";
        $this->Cell(18, 5, $CodePostal_etiquette, 0, 0, 'L');

        $this->SetXY(226, 49);
        $this->SetFont('Times', 'BI', 11);
        $NumeroTel_etiquette = mb_convert_encoding("Téléphone", 'ISO-8859-1', 'UTF-8');
        $this->Cell(27, 5, $NumeroTel_etiquette, 0, 0, 'L');

        $this->SetLineWidth(1);
        $this->Line(25, 56, 243, 56);

        $this->SetXY(25, 26);
        $this->SetFont('Times', 'BI', 20);
        $etiquette16 = mb_convert_encoding("Liste des employés par ordre croissant", 'ISO-8859-1', 'UTF-8');
        $this->Cell(119, 8, $etiquette16, 0, 0, 'L');

        $Image21 = 'logo/' . $_SESSION["syndicat"] . "." . $_SESSION['logo'];
        $this->Image($Image21, 197, 25, 47, 22);

        $this->SetY(58);
    }

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

        $this->SetLineWidth(0);
        $this->Line(26, 205, 244, 205);

        $etiquette43 = $this->configCentrale["syndicat_nom"];
        $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');

        $this->SetXY(25, 201);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(219, 4, $etiquette43, 0, 0, 'C');

        $this->SetXY(25, 205);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(219, 4, $etiquette45, 0, 0, 'C');
    }
}

$i = 0;
unset($sqlparam);
$sqlparam["table"][] = "employes";
$sqlparam["table"][] = "employeurs";
$sqlparam["join"][] = "employes.refemployeur=employeurs.refemployeur";
$sqlparam["champs"][] = "CONCAT(employes.nomfamille,' ',employes.prenom) as nomemploye";
$sqlparam["champs"][] = "CONCAT(employes.adresse1, if(isnull(employes.adresse2),' ',"
        . "CONCAT(' ',employes.adresse2))) AS adresse";
$sqlparam["champs"][] = "employes.ville";
$sqlparam["champs"][] = "CONCAT(LEFT(employes.codepostal,3),\" \",RIGHT(employes.codepostal,3)) as codepost";
$sqlparam["champs"][] = "employes.numtel";
$sqlparam["champs"][] = "employes.numemploye";
$sqlparam["champs"][] = "employeurs.nomemployeur";
$sqlparam["ordre"][] = "CONCAT(employes.nomfamille, ' ', employes.prenom) ASC";

$result = executerRequeteSql($sqlparam);

$pdf = new PDF('L', 'mm', 'Letter');

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

foreach ($result as $clef => $champs) {

    $pdf->NomEmploye = $champs["nomemploye"]; //0
    $pdf->NumEmploye = $champs["numemploye"]; //1
    $pdf->Adresse = $champs["adresse"]; //2
    $pdf->Ville = substr($champs["ville"], 0, 20); //3
    $pdf->CodePostal = $champs["codepost"];
    $pdf->NumeroTel = $champs["numtel"];

    $pdf->SetX(26);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(44, 4, mb_convert_encoding($pdf->NomEmploye, 'ISO-8859-1', 'UTF-8'), 0, 0, 'L');

    $pdf->SetX(72);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(21, 4, $pdf->NumEmploye, 0, 0, 'L');

    $pdf->SetX(95);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(52, 4, mb_convert_encoding($pdf->Adresse, 'ISO-8859-1', 'UTF-8'), 0, 0, 'L');

    $pdf->SetX(167);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(26, 4, mb_convert_encoding($pdf->Ville, 'ISO-8859-1', 'UTF-8'), 0, 0, 'L');

    $pdf->SetX(205);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(18, 4, $pdf->CodePostal, 0, 0, 'L');

    $pdf->SetX(225);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(27, 4, formatPhone($pdf->NumeroTel, 0, 0, 'L'));

    $pdf->Ln();
}

$pdf->Output();
