chill-bundles/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php

214 lines
7.2 KiB
PHP

<?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\TempUrlGeneratorInterface;
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
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 Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\HeaderUtils;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
use Jose\Component\Core\JWK;
use Base64Url\Base64Url;
use Symfony\Component\HttpKernel\KernelInterface;
// TODO à mettre dans services
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class DocGeneratorTemplateController extends AbstractController
{
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private PaginatorFactory $paginatorFactory;
private KernelInterface $kernel;
private HttpClientInterface $client;
public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
PaginatorFactory $paginatorFactory,
KernelInterface $kernel,
HttpClientInterface $client
) {
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->paginatorFactory = $paginatorFactory;
$this->kernel = $kernel;
$this->client = $client;
}
/**
* @Route(
* "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}",
* name="chill_docgenerator_generate_from_template"
* )
*/
public function generateDocFromTemplateAction(
TempUrlGeneratorInterface $tempUrlGenerator,
DocGeneratorTemplate $template,
string $entityClassName,
int $entityId,
Request $request
): Response {
$getUrlGen = $tempUrlGenerator->generate(
'GET',
$template->getFile()->getFilename());
$data = $this->client->request('GET', $getUrlGen->url);
$iv = $template->getFile()->getIv(); // iv as an Array
$ivGoodFormat = pack('C*', ...$iv); // iv as a String (ok for openssl_decrypt)
$method = 'AES-256-CBC';
$key = $template->getFile()->getKeyInfos()['k'];
$keyGoodFormat = Base64Url::decode($key);
$dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat);
if ($dataDecrypted === FALSE) {
throw new \Exception("Error during Decrypt ", 1);
}
$tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins
if (!$handle = fopen($tmpfnameDeCrypted, 'a')) {
echo "Cannot open file ($tmpfnameDeCrypted)";
exit;
}
if (fwrite($handle, $dataDecrypted) === FALSE) {
echo "Cannot write to file ($tmpfnameDeCrypted)";
exit;
}
dump("Success, wrote ($dataDecrypted) 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);
}
$templateProcessor = new TemplateProcessor($tmpfnameDeCrypted);
foreach ($datas['setValues'] as $setValuesConf) {
$templateProcessor->setValues($setValuesConf);
}
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(
'PUT',
$genDocName
);
unlink($tmpfnameDeCrypted);
unlink($tmpfnameGenerated);
$client = new Client();
try {
$putResponse = $client->request('PUT', $getUrlGen->url, [
'body' => $fileContent,
]);
if ($putResponse->getStatusCode() === 201) {
$em = $this->getDoctrine()->getManager();
$storedObject = new StoredObject();
$storedObject
// currently, only docx is supported
->setType('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
->setFilename($genDocName);
$em->persist($storedObject);
// Only for evaluation
if ($entity instanceof AccompanyingPeriodWorkEvaluation) {
$doc = new AccompanyingPeriodWorkEvaluationDocument();
$doc
->setStoredObject($storedObject)
->setTemplate($template);
$entity->addDocument($doc);
$em->persist($doc);
}
$em->flush();
return $this->redirectToRoute('chill_wopi_file_edit', [
'fileId' => $storedObject->getUuid(),
'returnPath' => $request->query->get('returnPath', '/'),
]);
}
} catch (TransferException $e) {
throw $e;
}
throw new Exception('Unable to generate document.');
}
/**
* @Route(
* "/api/1.0/docgen/templates/by-entity/{entityClassName}",
* name="chill_docgenerator_templates_for_entity_api"
* )
*/
public function listTemplateApiAction(string $entityClassName): Response
{
$nb = $this->docGeneratorTemplateRepository->countByEntity($entityClassName);
$paginator = $this->paginatorFactory->create($nb);
$entities = $this->docGeneratorTemplateRepository->findByEntity(
$entityClassName,
$paginator->getCurrentPageFirstItemNumber(),
$paginator->getItemsPerPage()
);
return $this->json(
new Collection($entities, $paginator),
Response::HTTP_OK,
[],
[AbstractNormalizer::GROUPS => ['read']]
);
}
}