Merge branch 'docgen3' into 'master'

Docgen3

See merge request Chill-Projet/chill-bundles!132
This commit is contained in:
Marc Ducobu 2021-08-19 16:53:28 +00:00
commit 364346d0aa
4 changed files with 77 additions and 0 deletions

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;
use GuzzleHttp\Client;
// TODO à mettre dans services
use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
/**
* Class DocGeneratorTemplateController
@ -38,4 +46,73 @@ 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
{
$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);
$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
$genDocName = 'doc_'.sprintf( '%010d', rand()).'.docx';
$getUrlGen = $tempUrlGenerator->generate(
'PUT',
$genDocName);
unlink($tmpfname2);
$client = new Client();
$putResponse = $client->request('PUT', $getUrlGen->{'url'}, [
'body' => $fileContent
]);
if ($putResponse->getStatusCode() == 201) {
return new JsonResponse(
array(
"msg" => "Document créé",
"id" => $genDocName,
"response" => array(
"reasonPhrase" => $putResponse->getReasonPhrase(),
"statusCode" => $putResponse->getStatusCode())));
}
return new JsonResponse(
array(
"msg" => "PBM",
"response" => array(
"reasonPhrase" => $putResponse->getReasonPhrase(),
"statusCode" => $putResponse->getStatusCode())));
}
}