generate document with relatorio: config and driver

This commit is contained in:
2021-12-02 18:02:19 +01:00
parent 2245f83631
commit af6efdd0ba
15 changed files with 286 additions and 363 deletions

View File

@@ -30,12 +30,24 @@ class AdminDocGeneratorTemplateController extends CRUDController
public function new(Request $request): Response
{
if (!$request->query->has('context')) {
return $this->redirectToRoute("chill_docgen_admin_template_pick-context");
return $this->redirectToRoute('chill_docgen_admin_template_pick-context');
}
return parent::new($request);
}
/**
* @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context")
*/
public function pickContext(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [
'contexts' => $this->contextManager->getContexts(),
]);
}
protected function createEntity(string $action, Request $request): object
{
/** @var DocGeneratorTemplate $entity */
@@ -47,19 +59,4 @@ class AdminDocGeneratorTemplateController extends CRUDController
return $entity;
}
/**
* @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context")
* @param Request $request
* @return Response
*/
public function pickContext(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [
'contexts' => $this->contextManager->getContexts()
]);
}
}

View File

@@ -1,76 +0,0 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Controller;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class DocGeneratorController.
*/
class DocGeneratorController extends AbstractController
{
/**
* @Route(
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* )
*/
public function getDoc(Request $request, TempUrlOpenstackGenerator $tempUrlGenerator): Response
{
$p = $tempUrlGenerator->generate(
'GET',
'FORMULAIRE_AEB.docx',
$request->query->has('expires_delay') ? $request->query->getInt('expires_delay', 0) : null
);
$tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE');
file_put_contents($tmpfname, file_get_contents($p->url));
$templateProcessor = new TemplateProcessor($tmpfname);
$templateProcessor->setValues(['firstname' => 'John', 'lastname' => 'Doe']);
$tmpfname2 = tempnam(sys_get_temp_dir(), 'DOC_GENERATED');
$templateProcessor->saveAs($tmpfname2);
unlink($tmpfname);
$fileContent = fopen($tmpfname2, 'rb'); // the generated file content
$response = new Response(fread($fileContent, filesize($tmpfname2)));
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
'foo.docx'
);
$response->headers->set('Content-Disposition', $disposition);
unlink($tmpfname2);
return $response;
}
/**
* @Route(
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* )
*/
public function testAction(): Response
{
return (new Response())->setContent('Test');
}
}

View File

@@ -21,22 +21,22 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use PhpOffice\PhpWord\TemplateProcessor;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// TODO à mettre dans services
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class DocGeneratorTemplateController extends AbstractController
@@ -47,6 +47,8 @@ final class DocGeneratorTemplateController extends AbstractController
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private DriverInterface $driver;
private KernelInterface $kernel;
private LoggerInterface $logger;
@@ -55,8 +57,6 @@ final class DocGeneratorTemplateController extends AbstractController
private TempUrlGeneratorInterface $tempUrlGenerator;
private DriverInterface $driver;
public function __construct(
ContextManager $contextManager,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
@@ -66,7 +66,6 @@ final class DocGeneratorTemplateController extends AbstractController
TempUrlGeneratorInterface $tempUrlGenerator,
KernelInterface $kernel,
HttpClientInterface $client
) {
$this->contextManager = $contextManager;
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
@@ -93,7 +92,7 @@ final class DocGeneratorTemplateController extends AbstractController
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
if (null === $entity) {
throw new NotFoundHttpException("Entity with classname $entityClassName and id $entityId is not found");
throw new NotFoundHttpException("Entity with classname {$entityClassName} and id {$entityId} is not found");
}
try {
@@ -102,6 +101,8 @@ final class DocGeneratorTemplateController extends AbstractController
throw new NotFoundHttpException($e->getMessage(), $e);
}
$contextGenerationData = [];
if ($context->hasPublicForm($template, $entity)) {
$builder = $this->createFormBuilder();
$context->buildPublicForm($builder, $template, $entity);
@@ -115,8 +116,6 @@ final class DocGeneratorTemplateController extends AbstractController
return $this->render($template, $templateOptions);
}
} else {
$contextGenerationData = [];
}
$getUrlGen = $this->tempUrlGenerator->generate(
@@ -137,11 +136,11 @@ final class DocGeneratorTemplateController extends AbstractController
$dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat);
if (false === $dataDecrypted) {
throw new \Exception('Error during Decrypt ', 1);
throw new Exception('Error during Decrypt ', 1);
}
if (false === $templateResource = fopen('php://memory', 'r+')) {
$this->logger->error("Could not write data to memory");
if (false === $templateResource = fopen('php://memory', 'r+b')) {
$this->logger->error('Could not write data to memory');
throw new HttpException(500);
}
@@ -156,7 +155,7 @@ final class DocGeneratorTemplateController extends AbstractController
fclose($templateResource);
$genDocName = 'doc_' . sprintf('%010d', mt_rand()).'odt';
$genDocName = 'doc_' . sprintf('%010d', mt_rand()) . 'odt';
$getUrlGen = $this->tempUrlGenerator->generate(
'PUT',
@@ -181,7 +180,7 @@ final class DocGeneratorTemplateController extends AbstractController
try {
$context->storeGenerated($template, $storedObject, $entity, $contextGenerationData);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error('Could not store the associated document to entity', [
'entityClassName' => $entityClassName,
'entityId' => $entityId,
@@ -202,7 +201,7 @@ final class DocGeneratorTemplateController extends AbstractController
throw $e;
}
throw new \Exception('Unable to generate document.');
throw new Exception('Unable to generate document.');
}
/**