add form to doc generation and custom form to admin template configuration

This commit is contained in:
2021-12-01 23:00:02 +01:00
parent 7719d2b073
commit 475b40e896
15 changed files with 484 additions and 108 deletions

View File

@@ -11,8 +11,55 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Controller;
use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\MainBundle\CRUD\Controller\CRUDController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AdminDocGeneratorTemplateController extends CRUDController
{
private ContextManager $contextManager;
public function __construct(ContextManager $contextManager)
{
$this->contextManager = $contextManager;
}
public function new(Request $request): Response
{
if (!$request->query->has('context')) {
return $this->redirectToRoute("chill_docgen_admin_template_pick-context");
}
return parent::new($request);
}
protected function createEntity(string $action, Request $request): object
{
/** @var DocGeneratorTemplate $entity */
$entity = parent::createEntity($action, $request);
$key = $request->query->get('context');
$context = $this->contextManager->getContextByKey($key);
$entity->setContext($key)->setEntity($context->getEntityClass());
return $entity;
}
/**
* @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context")
* @param Request $request
* @return Response
*/
public function pickContext(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [
'contexts' => $this->contextManager->getContexts()
]);
}
}

View File

@@ -30,6 +30,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// TODO à mettre dans services
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
@@ -83,6 +84,35 @@ final class DocGeneratorTemplateController extends AbstractController
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);
}
if ($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);
}
} else {
$contextGenerationData = [];
}
$getUrlGen = $this->tempUrlGenerator->generate(
'GET',
$template->getFile()->getFilename()
@@ -107,30 +137,22 @@ final class DocGeneratorTemplateController extends AbstractController
$tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins
if (!$handle = fopen($tmpfnameDeCrypted, 'ab')) {
echo "Cannot open file ({$tmpfnameDeCrypted})";
$this->logger->error("Cannot open file ({$tmpfnameDeCrypted})");
exit;
throw new HttpException(500);
}
if (false === $ftemplate = fwrite($handle, $dataDecrypted)) {
echo "Cannot write to file ({$tmpfnameDeCrypted})";
$this->logger->error("Cannot write to file ({$tmpfnameDeCrypted})");
exit;
throw new HttpException(500);
}
dump("Success, wrote (to file ({$tmpfnameDeCrypted})");
fclose($handle);
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
try {
$context = $this->contextManager->getContextByDocGeneratorTemplate($template);
} catch (ContextNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
}
$datas = $context->getData($entity);
$datas = $context->getData($template, $entity, $contextGenerationData);
dump('process the data', $datas);
@@ -182,7 +204,7 @@ final class DocGeneratorTemplateController extends AbstractController
$em->persist($storedObject);
try {
$context->storeGenerated($template, $storedObject, $entity);
$context->storeGenerated($template, $storedObject, $entity, $contextGenerationData);
} catch (\Exception $e) {
$this->logger->error('Could not store the associated document to entity', [
'entityClassName' => $entityClassName,