Commit 34e5b825 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Tâche #26198

Modifier le système d'états de sortie pour pouvoir générer des CSV avec un post-traitement PHP
parent e4ed440a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
<?php
+35 −0
Original line number Diff line number Diff line
@@ -40,6 +40,11 @@ class EtatSortie
     */
    protected $pdfTraitement;

    /**
     * @var string
     */
    protected $csvTraitement;

    /**
     * @var bool
     */
@@ -370,6 +375,35 @@ class EtatSortie



    /**
     * @return string
     */
    public function getCsvTraitement()
    {
        return $this->csvTraitement;
    }



    /**
     * @param string $csvTraitement
     *
     * @return EtatSortie
     */
    public function setCsvTraitement($csvTraitement): EtatSortie
    {
        $fichierGenerique = getcwd() . '/' . $this->csvTraitement;
        if (file_exists($fichierGenerique)) {
            $this->csvTraitement = substr(file_get_contents($fichierGenerique), 5);
        }

        $this->csvTraitement = $csvTraitement;

        return $this;
    }



    /**
     * @return bool
     */
@@ -426,6 +460,7 @@ class EtatSortie
            return true;
        } else {
            $fichierGenerique = getcwd() . '/data/Etats de sortie/' . $this->getCode() . '.odt';

            return file_exists($fichierGenerique);
        }
    }
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
        <field name="cle" type="string" column="CLE" nullable="true"/>
        <field name="csvParams" type="string" column="CSV_PARAMS" nullable="true"/>
        <field name="pdfTraitement" type="string" column="PDF_TRAITEMENT" nullable="true"/>
        <field name="csvTraitement" type="string" column="CSV_TRAITEMENT" nullable="true"/>
        <field name="autoBreak" type="boolean" column="AUTO_BREAK" nullable="false"/>
        <field name="fichier" type="blob" column="FICHIER" nullable="false"/>
        <field name="requete" type="string" column="REQUETE" nullable="false"/>
+14 −1
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace Application\Form;

use Application\Entity\Db\EtatSortie;
use Application\Form\AbstractForm;
use Application\Service\Traits\StatutIntervenantServiceAwareTrait;
use Application\Service\Traits\StructureServiceAwareTrait;
use Zend\Stdlib\Hydrator\HydratorInterface;
@@ -76,6 +75,18 @@ class EtatSortieForm extends AbstractForm
            ],
        ]);

        $this->add([
            'type'       => 'TextArea',
            'name'       => 'csv-traitement',
            'options'    => [
                'label' => "Traitement des données",
            ],
            'attributes' => [
                'id'   => 'csv-traitement',
                'rows' => '25',
            ],
        ]);

        $this->add([
            'type'    => 'Checkbox',
            'name'    => 'auto-break',
@@ -210,6 +221,7 @@ class EtatSortieHydrator implements HydratorInterface
        $object->setCle($data['cle']);
        $object->setCsvParams($data['csv-params']);
        $object->setPdfTraitement($data['pdf-traitement']);
        $object->setCsvTraitement($data['csv-traitement']);
        $object->setAutoBreak($data['auto-break'] === 'true');
        $object->setRequete($data['requete']);
        if (isset($data['fichier']['tmp_name']) && $data['fichier']['tmp_name']) {
@@ -249,6 +261,7 @@ class EtatSortieHydrator implements HydratorInterface
            'cle'            => $object->getCle(),
            'csv-params'     => $object->getCsvParams(),
            'pdf-traitement' => $object->getPdfTraitement(),
            'csv-traitement' => $object->getCsvTraitement(),
            'auto-break'     => $object->isAutoBreak() ? 'true' : 'false',
            'requete'        => $object->getRequete(),
        ];
+30 −11
Original line number Diff line number Diff line
@@ -122,14 +122,35 @@ class EtatSortieService extends AbstractEntityService
    /**
     * @param EtatSortie $etatSortie
     * @param array      $filtres
     * @param array      $options
     *
     * @return CsvModel
     * @throws \Exception
     */
    public function genererCsv(EtatSortie $etatSortie, array $filtres): CsvModel
    public function genererCsv(EtatSortie $etatSortie, array $filtres, array $options = []): CsvModel
    {
        $params = $etatSortie->getCsvParamsArray();
        $csv = new CsvModel();

        $data = $this->generateData($etatSortie, $filtres);
        $role = $this->getServiceContext()->getSelectedIdentityRole(); // à fournir à l'évaluateur...


        if (trim($etatSortie->getCsvTraitement())) {
            $__PHP__CODE__TRAITEMENT__ = $etatSortie->getCsvTraitement();
            // Isolation de traitement pour éviter tout débordement...
            $traitement = function () use ($csv, $etatSortie, $data, $filtres, $role, $options, $__PHP__CODE__TRAITEMENT__) {
                eval($__PHP__CODE__TRAITEMENT__);

                return $data;
            };
            $data       = $traitement();
        }

        if (!$csv->getFilename()) {
            $csv->setFilename($etatSortie->getLibelle() . '.csv');
        }
        if (empty($csv->getHeader()) && empty($csv->getData())) {
            $params = $etatSortie->getCsvParamsArray();

            $blocs = $etatSortie->getBlocs();
            $bkey  = null;
@@ -138,8 +159,6 @@ class EtatSortieService extends AbstractEntityService
                break;
            }

        $csvModel = new CsvModel();

            foreach ($data as $k => $d) {

                /* On récupère les sous-données éventuelles */
@@ -159,25 +178,25 @@ class EtatSortieService extends AbstractEntityService
                /* Si il y a des sous-données */
                if ($bdata) {
                    foreach ($bdata as $bd) {
                    $csvModel->addLine($this->filterData($d + $bd, $params));
                        $csv->addLine($this->filterData($d + $bd, $params));
                    }
                } else {
                $csvModel->addLine($this->filterData($d, $params));
                    $csv->addLine($this->filterData($d, $params));
                }
            }

        $csvModel->setFilename($etatSortie->getLibelle() . '.csv');
        if (isset($csvModel->getData()[0])) {
            $head = array_keys($csvModel->getData()[0]);
            if (isset($csv->getData()[0])) {
                $head = array_keys($csv->getData()[0]);
                foreach ($head as $k => $v) {
                    if (isset($params[$v]['libelle']) && $params[$v]['libelle']) {
                        $head[$k] = $params[$v]['libelle'];
                    }
                }
            $csvModel->setHeader($head);
                $csv->setHeader($head);
            }
        }

        return $csvModel;
        return $csv;
    }


Loading