contextManager = $contextManager; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; $this->driver = $driver; $this->logger = $logger; $this->paginatorFactory = $paginatorFactory; $this->tempUrlGenerator = $tempUrlGenerator; $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( DocGeneratorTemplate $template, string $entityClassName, int $entityId, Request $request ): Response { $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); if (null === $entity) { throw new NotFoundHttpException("Entity with classname {$entityClassName} and id {$entityId} is not found"); } try { $context = $this->contextManager->getContextByDocGeneratorTemplate($template); } catch (ContextNotFoundException $e) { throw new NotFoundHttpException($e->getMessage(), $e); } $contextGenerationData = []; if ($context instanceof DocGeneratorContextWithPublicFormInterface && $context->hasPublicForm($template, $entity)) { $builder = $this->createFormBuilder(); $context->buildPublicForm($builder, $template, $entity); $form = $builder->getForm()->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $contextGenerationData = $form->getData(); } elseif (!$form->isSubmitted() || ($form->isSubmitted() && !$form->isValid())) { $template = '@ChillDocGenerator/Generator/basic_form.html.twig'; $templateOptions = ['entity' => $entity, 'form' => $form->createView(), 'template' => $template]; return $this->render($template, $templateOptions); } } $getUrlGen = $this->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 (false === $dataDecrypted) { throw new Exception('Error during Decrypt ', 1); } if (false === $templateResource = fopen('php://memory', 'r+b')) { $this->logger->error('Could not write data to memory'); throw new HttpException(500); } fwrite($templateResource, $dataDecrypted); rewind($templateResource); $datas = $context->getData($template, $entity, $contextGenerationData); dump('datas compiled', $datas); $generatedResource = $this->driver->generateFromResource($templateResource, $template->getFile()->getType(), $datas, $template->getFile()->getFilename()); fclose($templateResource); $genDocName = 'doc_' . sprintf('%010d', mt_rand()) . 'odt'; $getUrlGen = $this->tempUrlGenerator->generate( 'PUT', $genDocName ); $client = new Client(); try { $putResponse = $client->request('PUT', $getUrlGen->url, [ 'body' => $generatedResource, ]); if ($putResponse->getStatusCode() === 201) { $em = $this->getDoctrine()->getManager(); $storedObject = new StoredObject(); $storedObject ->setType($template->getFile()->getType()) ->setFilename($genDocName); $em->persist($storedObject); try { $context->storeGenerated($template, $storedObject, $entity, $contextGenerationData); } catch (Exception $e) { $this->logger->error('Could not store the associated document to entity', [ 'entityClassName' => $entityClassName, 'entityId' => $entityId, 'contextKey' => $context->getName(), ]); throw $e; } $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']] ); } }