<?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 $Adresse1;
    var $Ville;
    var $CodePostal;
    var $NumeroTel;
    var $NomEmployeur;
    var $NomEmploye;
    var $Texte23;
    var $Texte24;
    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() {
        $this->SetXY(25, 25);
        $this->SetFont('Times', '', 24);
        $etiquette22 = "Liste des membres du conseil d'administration";
        $this->Cell(160, 10, $etiquette22, 0, 0, 'L');

        $Image29 = 'logo/' . $_SESSION["syndicat"] . "." . $_SESSION['logo'];
        $this->Image($Image29, 190, 25, 51, 18);

        $this->SetXY(26, 47);
        $this->SetFont('Times', 'B', 10);
        $NomFamille_etiquette = "Membre";
        $this->Cell(38, 5, $NomFamille_etiquette, 0, 0, 'L');

        $this->SetXY(68, 47);
        $this->SetFont('Times', 'B', 10);
        $Adresse1_etiquette = "Adresse";
        $this->Cell(38, 5, $Adresse1_etiquette, 0, 0, 'L');

        $this->SetXY(110, 47);
        $this->SetFont('Times', 'B', 10);
        $Ville_etiquette = "Ville";
        $this->Cell(34, 5, $Ville_etiquette, 0, 0, 'L');

        $this->SetXY(149, 47);
        $this->SetFont('Times', 'B', 10);
        $CodePostal_etiquette = "Code p.";
        $this->Cell(13, 5, $CodePostal_etiquette, 0, 0, 'L');

        $this->SetXY(164, 47);
        $this->SetFont('Times', 'B', 10);
        $NumeroTel_etiquette = mb_convert_encoding("Téléphone", 'ISO-8859-1', 'UTF-8');
        $this->Cell(20, 5, $NumeroTel_etiquette, 0, 0, 'L');

        $this->SetXY(187, 47);
        $this->SetFont('Times', 'B', 10);
        $NomEmployeur_etiquette = "Employeur";
        $this->Cell(23, 5, $NomEmployeur_etiquette, 0, 0, 'L');

        $this->SetLineWidth(1);
        $this->Line(26, 53, 244, 53);

        $this->SetY(55);
    }

    //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 = $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"]);

        $this->SetXY(25, 201);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(219, 4, mb_convert_encoding($etiquette43, 'ISO-8859-1', 'UTF-8'), 0, 0, 'C');

        $this->SetXY(25, 205);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(219, 4, mb_convert_encoding($etiquette45, 'ISO-8859-1', 'UTF-8'), 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"][] = "employeurs.nomemployeur";
$sqlparam["ordre"][] = "CONCAT(employes.nomfamille, ' ', employes.prenom) ASC";
$sqlparam["where"][] = "employes.membreca=-1";
$result = executerRequeteSql($sqlparam);

//Instanciation de la classe dérivée
$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 = mb_convert_encoding($champs["nomemploye"], 'ISO-8859-1', 'UTF-8');
    $pdf->Adresse = mb_convert_encoding($champs["adresse"], 'ISO-8859-1', 'UTF-8');
    $pdf->Ville = mb_convert_encoding($champs["ville"], 'ISO-8859-1', 'UTF-8');
    $pdf->CodePostal = mb_convert_encoding($champs["codepost"], 'ISO-8859-1', 'UTF-8');

    $removeChars = array("(", ")", "-", " ");
    $champs["numtel"] = str_replace($removeChars, "", $champs["numtel"]);
    $pdf->NumeroTel = formatPhone($champs["numtel"]);

    $pdf->NomEmployeur = mb_convert_encoding($champs["nomemployeur"], 'ISO-8859-1', 'UTF-8');

    $pdf->SetX(25);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(38, 5, $pdf->NomEmploye, 0, 0, 'L');

    $pdf->SetX(68);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(38, 4, $pdf->Adresse, 0, 0, 'L');

    $pdf->SetX(110);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(34, 4, $pdf->Ville, 0, 0, 'L');

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

    $pdf->SetX(164);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(20, 4, $pdf->NumeroTel, 0, 0, 'L');

    $pdf->SetX(187);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(56, 4, $pdf->NomEmployeur, 0, 0, 'L');

    $pdf->Ln();
}
$pdf->Output();
