Commit b65b1438 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Exporteur PDF : nouvelles méthodes set(Footer|Header)ScriptToDefault() et...

Exporteur PDF : nouvelles méthodes set(Footer|Header)ScriptToDefault() et set(Footer|Header)ScriptToNone().
parent c40d83f9
Loading
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
CHANGELOG
=========

5.1.0
-----
- Exporteur PDF : nouvelles méthodes set(Footer|Header)ScriptToDefault() et set(Footer|Header)ScriptToNone().

5.0.1
-----
- [FIX] FormControlGroup : prise en compte de la classe CSS des labels de checkboxes. 
+90 −32
Original line number Diff line number Diff line
<?php
namespace UnicaenApp\Exporter;

use Laminas\View\Resolver\TemplatePathStack;
use Mpdf\Mpdf as mPDF;
use UnicaenApp\Exception\LogicException;
use UnicaenApp\Exception\RuntimeException;
@@ -129,6 +130,13 @@ class Pdf implements ExporterInterface
     */
    private $logo;

    /**
     * Chemin absolu du répertoire contenant les scripts de vue par défaut.
     *
     * @var string
     */
    protected $defaultScriptsDirPath = __DIR__ . '/scripts';

    /**
     * Constructeur.
     *
@@ -151,6 +159,9 @@ class Pdf implements ExporterInterface
        $this->orientationPaysage = $orientationPaysage;
        $this->defaultFontSize = $defaultFontSize;

        $this->setHeaderScriptToDefault();
        $this->setFooterScriptToDefault();

        $this->setLogo(file_get_contents(__DIR__ . "/../../../public/unicaen/app/img/logo-unicaen.png"));
    }
    
@@ -179,6 +190,32 @@ class Pdf implements ExporterInterface
        return $this;
    }

    /**
     * Spécifie qu'aucun script ne soit utilisé pour les entêtes de pages.
     * Autrement dit, les pages du document PDF n'auront pas d'entête !
     *
     * @return self
     */
    public function setHeaderScriptToNone()
    {
        $this->headerScripts = array();

        return $this;
    }

    /**
     * Restaure le script de vue par défaut pour les entêtes de pages (pages paires et impaires).
     *
     * @return self
     */
    public function setHeaderScriptToDefault()
    {
        $this->headerScripts['O'] = 'header-odd.phtml';
        $this->headerScripts['E'] = 'header-even.phtml';

        return $this;
    }

    /**
     * Ajoute un script de vue à inclure dans le rendu du corps du document PDF.
     *
@@ -243,6 +280,32 @@ class Pdf implements ExporterInterface
        return $this;
    }

    /**
     * Restaure le script de vue par défaut pour les pieds de pages (pages paires et impaires).
     *
     * @return self
     */
    public function setFooterScriptToDefault()
    {
        $this->footerScripts['O'] = 'footer-odd.phtml';
        $this->footerScripts['E'] = 'footer-even.phtml';

        return $this;
    }

    /**
     * Spécifie qu'aucun script ne soit utilisé pour les pieds de pages.
     * Autrement dit, les pages du document PDF n'auront pas de pied de page !
     *
     * @return self
     */
    public function setFooterScriptToNone()
    {
        $this->footerScripts = array();

        return $this;
    }
    
    /**
     * Génère le document PDF et l'envoie éventuellement au navigateur.
     *
@@ -366,7 +429,7 @@ class Pdf implements ExporterInterface
        $parts = array();
        
        // styles de base fournis par la librairie Unicaen
        if (file_exists(($filepath = $this->getDefaultScriptsPath() . '/pdf.css'))) {
        if (file_exists(($filepath = $this->defaultScriptsDirPath . '/pdf.css'))) {
            $css = file_get_contents($filepath);
            $this->getMpdf()->WriteHTML($css, 1);
            $parts[] = $css;
@@ -409,20 +472,20 @@ class Pdf implements ExporterInterface
        if (isset($this->headerScripts['O'])) {
            $headerOdd = $this->getRenderer()->render($this->headerScripts['O'], $scriptVars);
        }
        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/header-odd.phtml')) {
            ob_start();
            include $filepath;
            $headerOdd = ob_get_clean();
        }
//        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/header-odd.phtml')) {
//            ob_start();
//            include $filepath;
//            $headerOdd = ob_get_clean();
//        }
        
        if (isset($this->headerScripts['E'])) {
            $headerEven = $this->getRenderer()->render($this->headerScripts['E'], $scriptVars);
        }
        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/header-even.phtml')) {
            ob_start();
            include $filepath;
            $headerEven = ob_get_clean();
        }
//        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/header-even.phtml')) {
//            ob_start();
//            include $filepath;
//            $headerEven = ob_get_clean();
//        }
        
        if ($headerOdd) {
            $this->getMpdf()->SetHTMLHeader($headerOdd, 'O');
@@ -498,20 +561,20 @@ class Pdf implements ExporterInterface
        if (isset($this->footerScripts['O'])) {
            $footerOdd = $this->getRenderer()->render($this->footerScripts['O'], $scriptVars);
        }
        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/footer-odd.phtml')) {
            ob_start();
            include $filepath;
            $footerOdd = ob_get_clean();
        }
//        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/footer-odd.phtml')) {
//            ob_start();
//            include $filepath;
//            $footerOdd = ob_get_clean();
//        }
        
        if (isset($this->footerScripts['E'])) {
            $footerEven = $this->getRenderer()->render($this->footerScripts['E'], $scriptVars);
        }
        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/footer-even.phtml')) {
            ob_start();
            include $filepath;
            $footerEven = ob_get_clean();
        }
//        elseif (file_exists($filepath = $this->getDefaultScriptsPath() . '/footer-even.phtml')) {
//            ob_start();
//            include $filepath;
//            $footerEven = ob_get_clean();
//        }
        
        if ($footerOdd) {
            $this->getMpdf()->SetHTMLFooter($footerOdd, 'O');
@@ -532,6 +595,11 @@ class Pdf implements ExporterInterface
    public function setRenderer(PhpRenderer $renderer)
    {
        $this->renderer = $renderer;

        $this->renderer->resolver()->attach(new TemplatePathStack(['script_paths' => [
            $this->defaultScriptsDirPath,
        ]]));

        return $this;
    }
    
@@ -595,16 +663,6 @@ class Pdf implements ExporterInterface
        return $this->mpdf;
    }
    
    /**
     * Retourne le chemin absolu du répertoire contenant les scripts de vue par défaut.
     * 
     * @return string
     */
    public function getDefaultScriptsPath()
    {
        return __DIR__ . '/scripts';
    }
    
    /**
     * Spécifie le chemin absolu dans lequel enregistrer le document PDF généré
     * avec le paramètre DESTINATION_FILE.