Commit ca87ce71 authored by Jean-Baptiste Oellers's avatar Jean-Baptiste Oellers
Browse files

Ajout enregistrement pdf

parent d057f67c
Loading
Loading
Loading
Loading
Loading
+125 −10
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use BjyAuthorize\Exception\UnAuthorizedException;
use Laminas\Http\Response;
use Laminas\View\Model\JsonModel;
use Oscar\Entity\CheminDecisionnelFormulaire;
use Oscar\Exception\OscarException;
use Oscar\Provider\Privileges;
use Oscar\Service\ProjectGrantService;
use Oscar\Traits\UseLoggerService;
@@ -85,6 +86,10 @@ class CheminDecisionnelController extends AbstractOscarController implements Use

        if ($this->getRequest()->isPost()) {
            if ($this->params()->fromPost('action') == "creer_convention") {
                if ($formulaire == NULL) {
                    throw new OscarException("Formulaire NULL");
                }

                $newActivity = new \Oscar\Entity\Activity();

                if ($this->params()->fromPost('dateStart')) {
@@ -111,6 +116,112 @@ class CheminDecisionnelController extends AbstractOscarController implements Use
                    $this->getLoggerService()->error($e->getMessage());
                }





                $nextId = $this->getEntityManager()->getRepository(\Oscar\Entity\ContractDocument::class)->getLastDocumentId();
                $originalFilename = "convention.pdf";
                $renameOriginal = "$nextId-" . $originalFilename;
                $slugify = new \Cocur\Slugify\Slugify();
                $renamed = sprintf(
                    "oscar-%s-%s-%s-%s",
                    $newActivity->getId(),
                    1,
                    $nextId,
                    $slugify->slugify($originalFilename)
                ) . ".pdf";




                $html_tmp = '/tmp' . DIRECTORY_SEPARATOR . uniqid('htmltmp_') . '.html';
                $pdf_path = $this->getOscarConfigurationService()->getDocumentDropLocation() . DIRECTORY_SEPARATOR . $renamed;
        

                $html = $this->getConventionHtml($formulaire->getTexteConvention(), $this->params()->fromPost('dateStart'), $this->params()->fromPost('dateEnd'), $this->params()->fromPost('amount'));

                if (!file_put_contents($html_tmp, $html)) {
                    throw new OscarException("Impossible de créer le fichier temporaire '$html_tmp'");
                }
            
                $cmd = sprintf(
                    'export QT_QPA_FONTDIR=%s && export QT_QPA_PLATFORM=offscreen && wkhtmltopdf -O portrait %s %s',
                    '',
                    $html_tmp,
                    $pdf_path
                );
                $done = false;
                if (shell_exec($cmd) === false) {
                    unlink($html_tmp);
                    throw new OscarException("Impossible de créer le fichier PDF");
                }
                unlink($html_tmp);
                
                $fileSize = filesize($pdf_path);










                $mimes = $this->getOscarConfigurationService()->getDocumentExtensions();
                $type = "pdf";



                $tabDocuments = $this->getEntityManager()->getRepository(\Oscar\Entity\TabDocument::class)->findBy([], ['label' => 'ASC']);
                $tabDocument = NULL;
                foreach ($tabDocuments as $t) {
                    if ($t->isDefault()) {
                        $tabDocument = $t;
                        break;
                    }
                }
                if ($tabDocument == NULL && count($tabDocuments) > 0) {
                    $tabDocument = $tabDocuments[0];
                }

                $typesDocumentsDatas = $this->getEntityManager()->getRepository(\Oscar\Entity\TypeDocument::class)->getTypes();
                $typeDocument = NULL;
                foreach ($typesDocumentsDatas as $t) {
                    if ($t->isDefault()) {
                        $typeDocument = $t;
                        break;
                    }
                }
                if ($typeDocument == NULL && count($typesDocumentsDatas) > 0) {
                    $typeDocument = $typesDocumentsDatas[0];
                }

                 // Création du document
                $document = new \Oscar\Entity\ContractDocument();
                $this->getEntityManager()->persist($document);

                $document
                    ->setVersion(1)
                    ->setDateUpdoad(new \DateTime())
                    ->setFileName($renameOriginal)
                    ->setPath($renamed)
                    ->setLocation(\Oscar\Entity\ContractDocument::LOCATION_LOCAL_FILE)
                    ->setFileTypeMime("application/pdf")
                    ->setFileSize($fileSize)
                    //->setPerson($sender)
                    ->setTabDocument($tabDocument)
                    ->setTypeDocument($typeDocument)
                    ->setGrant($newActivity);
                    //->setInformation($description)
                    //->setDateDeposit($dateDeposit)
                    //->setDateSend($dateSend);

                
                $this->getEntityManager()->flush();

                
                $this->redirect()->toRoute('chemins-decisionnels/demarrer/terminer', ['id'=>$cheminDecisionnel->getId()], ['query' => 'activity_id=' . $newActivity->getId()]);
                return;
            }
@@ -152,12 +263,21 @@ class CheminDecisionnelController extends AbstractOscarController implements Use
            throw new \Exception("Le formulaire n'a pas été trouvé.");
        }

        $texteConvention = $cheminDecisionnelFormulaire->getTexteConvention();
        $html = $this->getConventionHtml($cheminDecisionnelFormulaire->getTexteConvention(), $this->params()->fromQuery('dateStart'), $this->params()->fromQuery('dateEnd'), $this->params()->fromQuery('amount'));

        $output = new \Oscar\Formatter\Output\OutputWkhtmltopdfStrategy();
        $output->output(
            $html,
            "convention.html"
        );
    }

    private function getConventionHtml($texteConvention, $dateStart, $dateEnd, $amount): string {
        $texteConvention = str_replace(["\r\n", "\r", "\n"], "<br/>", $texteConvention);

        $dateStart = $this->params()->fromQuery('dateStart') ? $this->params()->fromQuery('dateStart') : '';
        $dateEnd = $this->params()->fromQuery('dateEnd') ? $this->params()->fromQuery('dateEnd') : '';
        $amount = $this->params()->fromQuery('amount') ? $this->params()->fromQuery('amount') : '';
        $dateStart = $dateStart ? $dateStart : '';
        $dateEnd = $dateEnd ? $dateEnd : '';
        $amount = $amount ? $amount : '';

        $allActivityForm = $this->getOscarConfigurationService()->getActivityFormConfigKeying();
        foreach (CheminDecisionnelFormulaire::CHAMPS_VALIDES as $v) {
@@ -187,11 +307,6 @@ class CheminDecisionnelController extends AbstractOscarController implements Use
        $html .= "</body>
</html>
";

        $output = new \Oscar\Formatter\Output\OutputWkhtmltopdfStrategy();
        $output->output(
            $html,
            "convention.html"
        );
        return $html;
    }
}