mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
150 lines
5.1 KiB
PHP
150 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace Chill\DocGeneratorBundle\Controller;
|
|
|
|
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
|
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
|
|
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
|
|
use GuzzleHttp\Exception\TransferException;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
|
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
|
|
use PhpOffice\PhpWord\TemplateProcessor;
|
|
use GuzzleHttp\Client;
|
|
|
|
// TODO à mettre dans services
|
|
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
|
|
|
|
/**
|
|
* Class DocGeneratorTemplateController
|
|
*
|
|
* @package Chill\DocGeneratorBundle\Controller
|
|
*/
|
|
class DocGeneratorTemplateController extends AbstractController
|
|
{
|
|
/**
|
|
* @Route(
|
|
* "{_locale}/doc/gen/templates/for/{entityClassName}",
|
|
* name="chill_docgenerator_templates_for_entity_api"
|
|
* )
|
|
*/
|
|
public function listTemplateApiAction(
|
|
string $entityClassName, DocGeneratorTemplateRepository $templateRepository): Response
|
|
{
|
|
$entities = $templateRepository->findByEntity($entityClassName);
|
|
|
|
$ret = array();
|
|
|
|
foreach ($entities as $entity) {
|
|
$ret[] = array(
|
|
'id' => $entity->getId(),
|
|
'name' => $entity->getName(),
|
|
'description' => $entity->getDescription()
|
|
);
|
|
}
|
|
|
|
return new JsonResponse(["results" => $ret]);
|
|
}
|
|
|
|
/**
|
|
* @Route(
|
|
* "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}",
|
|
* name="chill_docgenerator_generate_from_template"
|
|
* )
|
|
*/
|
|
public function generateDocFromTemplateAction(
|
|
\ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface $tempUrlGenerator,
|
|
DocGeneratorTemplate $template,
|
|
string $entityClassName,
|
|
int $entityId,
|
|
Request $request
|
|
): Response {
|
|
$getUrlGen = $tempUrlGenerator->generate(
|
|
'GET',
|
|
$template->getFile());
|
|
|
|
$tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE');
|
|
file_put_contents($tmpfname, file_get_contents($getUrlGen->{"url"}));
|
|
|
|
$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($tmpfname);
|
|
|
|
foreach ($datas['setValues'] as $setValuesConf) {
|
|
$templateProcessor->setValues($setValuesConf);
|
|
}
|
|
|
|
foreach ($datas['cloneRowAndSetValues'] as $cloneRowAndSetValues) {
|
|
$templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]);
|
|
}
|
|
|
|
$tmpfname2 = tempnam(sys_get_temp_dir(), 'DOC_GENERATED');
|
|
$templateProcessor->saveAs($tmpfname2);
|
|
|
|
unlink($tmpfname);
|
|
|
|
$fileContent = fopen($tmpfname2, 'r'); // the generated file content
|
|
|
|
$genDocName = 'doc_' . sprintf('%010d', rand()) . '.docx';
|
|
|
|
$getUrlGen = $tempUrlGenerator->generate(
|
|
'PUT',
|
|
$genDocName);
|
|
|
|
unlink($tmpfname2);
|
|
|
|
$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' => $genDocName,
|
|
'returnPath' => $request->query->get('returnPath', "/")
|
|
]);
|
|
}
|
|
} catch (TransferException $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|