Commit 391b137a authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

[FIX] La même page de validation était appliquée sur tous les rapports...

[FIX] La même page de validation était appliquée sur tous les rapports d'activité téléchargés au format zip.
parent 7744d3c3
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -3,9 +3,18 @@
namespace Application\Exporter\Pdf;

use InvalidArgumentException;
use Laminas\View\Renderer\PhpRenderer;
use Laminas\View\Resolver\AggregateResolver;
use Laminas\View\Resolver\TemplatePathStack;
use UnicaenApp\Exporter\Pdf;
use UnicaenApp\Exporter\Pdf as PdfExporter;

/**
 * !!!!!! Attention !!!!!!
 * En l'état actuel, la classe d'export PDF {@see \UnicaenApp\Exporter\Pdf} ne sait pas faire correctement de
 * génération PDF par lot donc mieux vaut cloner l'instance pour chaque génération/export.
 *
 */
class ApplicationPdfExporter extends PdfExporter
{
    /**
@@ -23,6 +32,20 @@ class ApplicationPdfExporter extends PdfExporter
     */
    protected array $vars = [];

    /**
     * @inheritDoc
     */
    public function __construct(PhpRenderer $renderer = null, $format = 'A4', $orientationPaysage = false, $defaultFontSize = 10)
    {
        // création d'un renderer tout neuf nécessaire pour éviter la collision de noms de templates dans le resolver
        if ($renderer === null) {
            $renderer = new PhpRenderer();
            $renderer->setResolver(new AggregateResolver());
        }

        parent::__construct($renderer, $format, $orientationPaysage, $defaultFontSize);
    }

    /**
     * Spécifie le chemin absolu du template PHTML à transformer en PDF.
     *
@@ -98,6 +121,6 @@ class ApplicationPdfExporter extends PdfExporter
    {
        $this->prepare();

        return PdfExporter::export(basename($filename), $destination, $memoryLimit);
        PdfExporter::export(basename($filename), $destination, $memoryLimit);
    }
}
 No newline at end of file
+10 −8
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter

    public function deleteFile(string $dirPath, string $fileName)
    {
        $filePath = implode($this->pathSeparator, [$dirPath, $fileName]);
        $filePath = $this->assemblePath($dirPath, $fileName);

        if (! file_exists($filePath)) {
            throw (new StorageAdapterException("Le fichier suivant est introuvable : " . $filePath))
@@ -96,8 +96,9 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
        $filePath = $this->assemblePath($dirPath, $fileName);

        if (! is_readable($filePath)) {
            throw new StorageAdapterException(
                "Le fichier suivant n'existe pas ou n'est pas accessible sur le serveur : " . $filePath);
            throw (new StorageAdapterException("Le fichier suivant n'existe pas ou n'est pas accessible sur le serveur : " . $filePath))
                ->setDirPath($dirPath)
                ->setFileName($fileName);
        }

        return file_get_contents($filePath);
@@ -112,11 +113,12 @@ class FilesystemStorageAdapter extends AbstractStorageAdapter
     */
    public function saveToFilesystem(string $fromDirPath, string $fromFileName, string $toFilesystemPath)
    {
        $filePath = implode($this->pathSeparator, [$fromDirPath, $fromFileName]);
        $filePath = $this->assemblePath($fromDirPath, $fromFileName);

        if (! is_readable($filePath)) {
            throw new StorageAdapterException(
                "Le fichier suivant n'existe pas ou n'est pas accessible sur le serveur : " . $filePath);
            throw (new StorageAdapterException("Le fichier suivant n'existe pas ou n'est pas accessible sur le serveur : " . $filePath))
                ->setDirPath($fromDirPath)
                ->setFileName($fromFileName);
        }

        copy($filePath, $toFilesystemPath);
+2 −2
Original line number Diff line number Diff line
@@ -56,8 +56,8 @@ return [

    // Options concernant les rapports d'activité
    'rapport-activite' => [
        // Page de couverture des rapports d'activité déposés
        'page_de_couverture' => [
        // Page de validation des rapports d'activité déposés
        'page_de_validation' => [
            'template' => [
                // template .phtml
                'phtml_file_path' => __DIR__ . '/../view/rapport-activite/page-de-validation/template.phtml',
+2 −7
Original line number Diff line number Diff line
@@ -12,19 +12,14 @@ class PageValidationPdfExporterFactory
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): PageValidationPdfExporter
    {
        /** @var \Laminas\View\Renderer\PhpRenderer $renderer */
        $renderer = $container->get('ViewRenderer');

//        return new PageValidationPdfExporter($renderer, 'A4');

        $config = $container->get('Config');
        $pageSupplConfig = $config['rapport-activite']['page_de_couverture'];
        $pageSupplConfig = $config['rapport-activite']['page_de_validation'] ?: $config['rapport-activite']['page_de_couverture'];

        $templateConfig = $pageSupplConfig['template'];
        $templateFilePath = $templateConfig['phtml_file_path'];
        $cssFilePath = $templateConfig['css_file_path'];

        $exporter = new PageValidationPdfExporter($renderer, 'A4');
        $exporter = new PageValidationPdfExporter(null, 'A4');
        $exporter
            ->setTemplateFilePath($templateFilePath)
            ->setCssFilePath($cssFilePath);
+17 −14
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace RapportActivite\Service\Fichier;

use Application\Command\Exception\TimedOutCommandException;
use Application\Command\ShellCommandRunnerTrait;
use Fichier\Command\Pdf\PdfMergeShellCommandQpdf;
use Application\Command\ShellCommandRunnerTrait;
use Fichier\Service\Fichier\FichierServiceAwareTrait;
@@ -11,7 +12,6 @@ use Fichier\Service\Storage\Adapter\Exception\StorageAdapterException;
use RapportActivite\Entity\Db\RapportActivite;
use RapportActivite\Service\Fichier\Exporter\PageValidationExportData;
use RapportActivite\Service\Fichier\Exporter\PageValidationPdfExporterTrait;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Exporter\Pdf;

class RapportActiviteFichierService
@@ -31,12 +31,11 @@ class RapportActiviteFichierService
     */
    public function createFileWithPageValidation(RapportActivite $rapport, PageValidationExportData $data): string
    {
        // generation de la page de couverture
        $pdcFilePath = tempnam(sys_get_temp_dir(), 'sygal_rapport_pdc_') . '.pdf';
        $this->generatePageValidation($rapport, $data, $pdcFilePath);
        // generation de la page de validation
        $pdvFilePath = $this->generatePageValidation($rapport, $data);

        $outputFilePath = tempnam(sys_get_temp_dir(), 'sygal_fusion_rapport_pdc_') . '.pdf';
        $command = $this->createCommandForAjoutPageValidation($rapport, $pdcFilePath, $outputFilePath);
        $outputFilePath = sys_get_temp_dir() . '/' . uniqid('sygal_fusion_rapport_pdv_') . '.pdf';
        $command = $this->createCommandForAjoutPageValidation($rapport, $pdvFilePath, $outputFilePath);
        try {
            $this->runShellCommand($command);
        } catch (TimedOutCommandException $e) {
@@ -48,13 +47,12 @@ class RapportActiviteFichierService

    /**
     * @param \RapportActivite\Entity\Db\RapportActivite $rapport
     * @param string $pdcFilePath
     * @param string $pdvFilePath
     * @param string $outputFilePath
     * @return PdfMergeShellCommandQpdf
     */
    private function createCommandForAjoutPageValidation(RapportActivite $rapport, string $pdcFilePath, string $outputFilePath): PdfMergeShellCommandQpdf
    private function createCommandForAjoutPageValidation(RapportActivite $rapport, string $pdvFilePath, string $outputFilePath): PdfMergeShellCommandQpdf
    {
//        $rapportFilePath = $this->fichierService->computeFilePathForFichier($rapport->getFichier());
        try {
            $rapportFilePath = $this->fichierStorageService->getFileForFichier($rapport->getFichier());
        } catch (StorageAdapterException $e) {
@@ -65,7 +63,7 @@ class RapportActiviteFichierService
        $command = new PdfMergeShellCommandQpdf();
        $command->setInputFilesPaths([
            0 => $rapportFilePath,
            1 => $pdcFilePath,
            1 => $pdvFilePath,
        ]);
        $command->setOutputFilePath($outputFilePath);
        $command->generateCommandLine();
@@ -76,12 +74,17 @@ class RapportActiviteFichierService
    /**
     * @param \RapportActivite\Entity\Db\RapportActivite $rapport
     * @param PageValidationExportData $data
     * @param string $filepath
     * @return string
     */
    public function generatePageValidation(RapportActivite $rapport, PageValidationExportData $data, string $filepath)
    public function generatePageValidation(RapportActivite $rapport, PageValidationExportData $data): string
    {
        $this->pageValidationPdfExporter->setVars(['rapport' => $rapport, 'data' => $data]);
        $this->pageValidationPdfExporter->export($filepath, Pdf::DESTINATION_FILE);
        $outputFilepath = sys_get_temp_dir() . '/' . uniqid('sygal_rapport_pdv_') . '.pdf';

        $exporter = clone $this->pageValidationPdfExporter; // clonage indispensable
        $exporter->setVars(['rapport' => $rapport, 'data' => $data]);
        $exporter->export($outputFilepath, Pdf::DESTINATION_FILE);

        return $outputFilepath;
    }

}
 No newline at end of file
Loading