mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
full generation for accompanying period
This commit is contained in:
@@ -11,8 +11,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Context;
|
||||
|
||||
use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException;
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
|
||||
class ContextManager
|
||||
{
|
||||
/**
|
||||
* @var DocGeneratorContextInterface[]|iterable
|
||||
*/
|
||||
private iterable $contexts;
|
||||
|
||||
public function __construct(iterable $contexts)
|
||||
@@ -20,7 +26,21 @@ class ContextManager
|
||||
$this->contexts = $contexts;
|
||||
}
|
||||
|
||||
public function getContext(): array
|
||||
/**
|
||||
* @throw ContextNotFoundException when the context is not found
|
||||
*/
|
||||
public function getContextByDocGeneratorTemplate(DocGeneratorTemplate $docGeneratorTemplate): DocGeneratorContextInterface
|
||||
{
|
||||
foreach ($this->contexts as $key => $context) {
|
||||
if ($docGeneratorTemplate->getContext() === $key) {
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
||||
throw new ContextNotFoundException($docGeneratorTemplate->getContext());
|
||||
}
|
||||
|
||||
public function getContexts(): array
|
||||
{
|
||||
return iterator_to_array($this->contexts);
|
||||
}
|
||||
|
@@ -11,6 +11,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Context;
|
||||
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
|
||||
/**
|
||||
* Interface for context for for document generation.
|
||||
*/
|
||||
@@ -39,6 +42,8 @@ interface DocGeneratorContextInterface
|
||||
*/
|
||||
public function hasForm(): bool;
|
||||
|
||||
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void;
|
||||
|
||||
/**
|
||||
* True of false which entity supports.
|
||||
*/
|
||||
|
@@ -0,0 +1,22 @@
|
||||
<?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\Context\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class ContextNotFoundException extends RuntimeException
|
||||
{
|
||||
public function __construct($contextName)
|
||||
{
|
||||
parent::__construct(sprintf('the context with name %s is not found', $contextName));
|
||||
}
|
||||
}
|
@@ -11,9 +11,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Context;
|
||||
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
|
||||
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use function count;
|
||||
use function get_class;
|
||||
@@ -23,6 +27,13 @@ use function get_class;
|
||||
*/
|
||||
class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data that will be injected to the generated document.
|
||||
*
|
||||
@@ -107,6 +118,19 @@ class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void
|
||||
{
|
||||
// Only for evaluation
|
||||
if ($entity instanceof AccompanyingPeriodWorkEvaluation) {
|
||||
$doc = new AccompanyingPeriodWorkEvaluationDocument();
|
||||
$doc
|
||||
->setStoredObject($storedObject)
|
||||
->setTemplate($template);
|
||||
$entity->addDocument($doc);
|
||||
$this->em->persist($doc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True of false which entity supports.
|
||||
*/
|
||||
|
@@ -13,25 +13,24 @@ namespace Chill\DocGeneratorBundle\Controller;
|
||||
|
||||
use Base64Url\Base64Url;
|
||||
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface;
|
||||
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
|
||||
use Chill\DocGeneratorBundle\Context\ContextManager;
|
||||
use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException;
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
|
||||
|
||||
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\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
@@ -42,20 +41,32 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
{
|
||||
private HttpClientInterface $client;
|
||||
|
||||
private ContextManager $contextManager;
|
||||
|
||||
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
|
||||
|
||||
private KernelInterface $kernel;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
private PaginatorFactory $paginatorFactory;
|
||||
|
||||
private TempUrlGeneratorInterface $tempUrlGenerator;
|
||||
|
||||
public function __construct(
|
||||
ContextManager $contextManager,
|
||||
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
|
||||
LoggerInterface $logger,
|
||||
PaginatorFactory $paginatorFactory,
|
||||
TempUrlGeneratorInterface $tempUrlGenerator,
|
||||
KernelInterface $kernel,
|
||||
HttpClientInterface $client
|
||||
) {
|
||||
$this->contextManager = $contextManager;
|
||||
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
|
||||
$this->logger = $logger;
|
||||
$this->paginatorFactory = $paginatorFactory;
|
||||
$this->tempUrlGenerator = $tempUrlGenerator;
|
||||
$this->kernel = $kernel;
|
||||
$this->client = $client;
|
||||
}
|
||||
@@ -67,13 +78,12 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
* )
|
||||
*/
|
||||
public function generateDocFromTemplateAction(
|
||||
TempUrlGeneratorInterface $tempUrlGenerator,
|
||||
DocGeneratorTemplate $template,
|
||||
string $entityClassName,
|
||||
int $entityId,
|
||||
Request $request
|
||||
): Response {
|
||||
$getUrlGen = $tempUrlGenerator->generate(
|
||||
$getUrlGen = $this->tempUrlGenerator->generate(
|
||||
'GET',
|
||||
$template->getFile()->getFilename()
|
||||
);
|
||||
@@ -102,25 +112,29 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
exit;
|
||||
}
|
||||
|
||||
if (fwrite($handle, $dataDecrypted) === false) {
|
||||
if (false === $ftemplate = fwrite($handle, $dataDecrypted)) {
|
||||
echo "Cannot write to file ({$tmpfnameDeCrypted})";
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
dump("Success, wrote ({$dataDecrypted}) to file ({$tmpfnameDeCrypted})");
|
||||
dump("Success, wrote (to file ({$tmpfnameDeCrypted})");
|
||||
|
||||
fclose($handle);
|
||||
|
||||
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
|
||||
|
||||
if ($template->getContext() === HouseholdMemberSelectionContext::class) {
|
||||
$context = new HouseholdMemberSelectionContext();
|
||||
$datas = $context->getData($entity);
|
||||
} else {
|
||||
throw new \Exception('Not implemented', 1);
|
||||
try {
|
||||
$context = $this->contextManager->getContextByDocGeneratorTemplate($template);
|
||||
} catch (ContextNotFoundException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage(), $e);
|
||||
}
|
||||
|
||||
$datas = $context->getData($entity);
|
||||
|
||||
dump('process the data', $datas);
|
||||
|
||||
/*
|
||||
$templateProcessor = new TemplateProcessor($tmpfnameDeCrypted);
|
||||
|
||||
foreach ($datas['setValues'] as $setValuesConf) {
|
||||
@@ -130,28 +144,32 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
foreach ($datas['cloneRowAndSetValues'] as $cloneRowAndSetValues) {
|
||||
$templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]);
|
||||
}
|
||||
|
||||
$tmpfnameGenerated = tempnam($this->kernel->getCacheDir(), 'DOC_GENERATED');
|
||||
$templateProcessor->saveAs($tmpfnameGenerated);
|
||||
|
||||
$fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content
|
||||
*/
|
||||
|
||||
$genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx';
|
||||
|
||||
$getUrlGen = $tempUrlGenerator->generate(
|
||||
$getUrlGen = $this->tempUrlGenerator->generate(
|
||||
'PUT',
|
||||
$genDocName
|
||||
);
|
||||
|
||||
unlink($tmpfnameDeCrypted);
|
||||
unlink($tmpfnameGenerated);
|
||||
//unlink($tmpfnameGenerated);
|
||||
|
||||
$client = new Client();
|
||||
|
||||
try {
|
||||
/*
|
||||
$putResponse = $client->request('PUT', $getUrlGen->url, [
|
||||
'body' => $fileContent,
|
||||
]);
|
||||
*/
|
||||
$putResponse = $client->request('PUT', $getUrlGen->url, [
|
||||
'body' => $ftemplate,
|
||||
]);
|
||||
|
||||
if ($putResponse->getStatusCode() === 201) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
@@ -163,14 +181,16 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
|
||||
$em->persist($storedObject);
|
||||
|
||||
// Only for evaluation
|
||||
if ($entity instanceof AccompanyingPeriodWorkEvaluation) {
|
||||
$doc = new AccompanyingPeriodWorkEvaluationDocument();
|
||||
$doc
|
||||
->setStoredObject($storedObject)
|
||||
->setTemplate($template);
|
||||
$entity->addDocument($doc);
|
||||
$em->persist($doc);
|
||||
try {
|
||||
$context->storeGenerated($template, $storedObject, $entity);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Could not store the associated document to entity', [
|
||||
'entityClassName' => $entityClassName,
|
||||
'entityId' => $entityId,
|
||||
'contextKey' => $context->getName(),
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
@@ -184,7 +204,7 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
throw $e;
|
||||
}
|
||||
|
||||
throw new Exception('Unable to generate document.');
|
||||
throw new \Exception('Unable to generate document.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -35,7 +35,7 @@ class DocGeneratorTemplateType extends AbstractType
|
||||
{
|
||||
$contexts = array_flip(array_map(static function (DocGeneratorContextInterface $c) {
|
||||
return $c->getName();
|
||||
}, $this->contextManager->getContext()));
|
||||
}, $this->contextManager->getContexts()));
|
||||
|
||||
$builder
|
||||
->add('name', TranslatableStringFormType::class, [
|
||||
|
Reference in New Issue
Block a user