chill-bundles/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php
2021-12-06 17:27:57 +01:00

88 lines
2.8 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
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 generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = [])
{
switch ($action) {
case 'new':
$context = $this->contextManager->getContextByKey($request->get('context'));
// no break
case 'edit':
$context = $this->contextManager->getContextByDocGeneratorTemplate($entity);
return array_merge(
$defaultTemplateParameters,
['context' => $context]
);
case 'index':
return array_merge(
$defaultTemplateParameters,
['contextManager' => $this->contextManager]
);
default:
return parent::generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters); // TODO: Change the autogenerated stub
}
}
public function new(Request $request): Response
{
if (!$request->query->has('context')) {
return $this->redirectToRoute('chill_docgen_admin_template_pick-context');
}
return parent::new($request);
}
/**
* @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context")
*/
public function pickContext(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [
'contexts' => $this->contextManager->getContexts(),
]);
}
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;
}
}