mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Generation du doc àpd du template
This commit is contained in:
parent
7e64ec0a45
commit
e6f5ef91ec
@ -6,7 +6,15 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
|
|||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
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
|
* Class DocGeneratorTemplateController
|
||||||
@ -38,4 +46,53 @@ class DocGeneratorTemplateController extends AbstractController
|
|||||||
|
|
||||||
return new JsonResponse(["results" => $ret]);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user