Generation du doc àpd du template

This commit is contained in:
Marc Ducobu 2021-08-19 15:46:27 +02:00
parent 7e64ec0a45
commit e6f5ef91ec

View File

@ -6,7 +6,15 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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;
// TODO à mettre dans services
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
/**
* Class DocGeneratorTemplateController
@ -38,4 +46,53 @@ class DocGeneratorTemplateController extends AbstractController
return new JsonResponse(["results" => $ret]);
}
/**
* @Route(
* "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}",
* name="chill_docgenerator_generate_from_template"
* )
*/
public function generateDocFromTemplateAction(
TempUrlOpenstackGenerator $tempUrlGenerator,
DocGeneratorTemplate $template, string $entityClassName, int $entityId): Response
{
$p = $tempUrlGenerator->generate(
'GET',
$template->getFile());
$tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE');
file_put_contents($tmpfname, file_get_contents($p->{"url"}));
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
if ($template->getContext() == HouseholdMemberSelectionContext::class) {
$context = new HouseholdMemberSelectionContext();
// $datas = $context->getData($entity);
$datas = [];
} else {
throw new \Exception("Not implemented", 1);
}
$templateProcessor = new TemplateProcessor($tmpfname);
$templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe'));
$tmpfname2 = tempnam(sys_get_temp_dir(), 'DOC_GENERATED');
$templateProcessor->saveAs($tmpfname2);
unlink($tmpfname);
$fileContent = fopen($tmpfname2, 'r'); // 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;
}
}