Render for generic doc

This commit is contained in:
2023-05-24 21:57:20 +02:00
parent 8dbe2d6ec2
commit e550817ded
14 changed files with 299 additions and 66 deletions

View File

@@ -11,12 +11,16 @@ declare(strict_types=1);
namespace Chill\DocStoreBundle\GenericDoc;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
class GenericDocDTO
{
public function __construct(
public readonly string $key,
public readonly array $identifiers,
public readonly \DateTimeImmutable $docDate
public readonly \DateTimeImmutable $docDate,
public AccompanyingPeriod|Person $linked,
) {
}
}

View File

@@ -13,7 +13,7 @@ namespace Chill\DocStoreBundle\GenericDoc;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
interface ProviderForAccompanyingPeriodInterface
interface GenericDocForAccompanyingPeriodProviderInterface
{
public function buildFetchQueryForAccompanyingPeriod(
AccompanyingPeriod $accompanyingPeriod,

View File

@@ -23,7 +23,7 @@ class Manager
public function __construct(
/**
* @var iterable<ProviderForAccompanyingPeriodInterface>
* @var iterable<GenericDocForAccompanyingPeriodProviderInterface>
*/
private readonly iterable $providersForAccompanyingPeriod,
private readonly Connection $connection,
@@ -81,7 +81,8 @@ class Manager
yield new GenericDocDTO(
$row['key'],
json_decode($row['identifiers'], true, JSON_THROW_ON_ERROR),
new \DateTimeImmutable($row['doc_date'])
new \DateTimeImmutable($row['doc_date']),
$accompanyingPeriod,
);
}
}

View File

@@ -14,15 +14,17 @@ namespace Chill\DocStoreBundle\GenericDoc\Providers;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\GenericDoc\FetchQuery;
use Chill\DocStoreBundle\GenericDoc\FetchQueryInterface;
use Chill\DocStoreBundle\GenericDoc\ProviderForAccompanyingPeriodInterface;
use Chill\DocStoreBundle\GenericDoc\GenericDocForAccompanyingPeriodProviderInterface;
use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
final readonly class AccompanyingCourseDocumentProvider implements ProviderForAccompanyingPeriodInterface
final readonly class AccompanyingProviderCourseDocumentGenericDoc implements GenericDocForAccompanyingPeriodProviderInterface
{
public const KEY = 'accompanying_course_document';
public function __construct(
private Security $security,
private EntityManagerInterface $entityManager,
@@ -34,7 +36,7 @@ final readonly class AccompanyingCourseDocumentProvider implements ProviderForAc
$classMetadata = $this->entityManager->getClassMetadata(AccompanyingCourseDocument::class);
$query = new FetchQuery(
'accompanying_course_document',
self::KEY,
sprintf('jsonb_build_object(\'id\', %s)', $classMetadata->getIdentifierColumnNames()[0]),
sprintf($classMetadata->getColumnName('date')),
$classMetadata->getSchemaName() . '.' . $classMetadata->getTableName()

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\DocStoreBundle\GenericDoc\Renderer;
use Chill\DocStoreBundle\GenericDoc\GenericDocDTO;
use Chill\DocStoreBundle\GenericDoc\Twig\GenericDocRendererInterface;
use Chill\DocStoreBundle\GenericDoc\Providers\AccompanyingProviderCourseDocumentGenericDoc;
use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository;
final readonly class AccompanyingCourseDocumentGenericDocRenderer implements GenericDocRendererInterface
{
public function __construct(
private AccompanyingCourseDocumentRepository $accompanyingCourseDocumentRepository,
) {
}
public function supports(GenericDocDTO $genericDocDTO, $options = []): bool
{
return $genericDocDTO->key === AccompanyingProviderCourseDocumentGenericDoc::KEY;
}
public function getTemplate(GenericDocDTO $genericDocDTO, $options = []): string
{
return '@ChillDocStore/List/list_item.html.twig';
}
public function getTemplateData(GenericDocDTO $genericDocDTO, $options = []): array
{
return [
'document' => $this->accompanyingCourseDocumentRepository->find($genericDocDTO->identifiers['id']),
'accompanyingCourse' => $genericDocDTO->linked,
'options' => $options,
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\DocStoreBundle\GenericDoc\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
final class GenericDocExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('chill_generic_doc_render', [GenericDocExtensionRuntime::class, 'renderGenericDoc'], [
'needs_environment' => true,
'is_safe' => ['html'],
])
];
}
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\DocStoreBundle\GenericDoc\Twig;
use Chill\DocStoreBundle\GenericDoc\GenericDocDTO;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Extension\RuntimeExtensionInterface;
final readonly class GenericDocExtensionRuntime implements RuntimeExtensionInterface
{
public function __construct(
/**
* @var list<GenericDocRendererInterface>
*/
private iterable $renderers,
) {
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function renderGenericDoc(Environment $twig, GenericDocDTO $genericDocDTO, array $options = []): string
{
foreach ($this->renderers as $renderer) {
if ($renderer->supports($genericDocDTO)) {
return $twig->render(
$renderer->getTemplate($genericDocDTO, $options),
$renderer->getTemplateData($genericDocDTO, $options),
);
}
}
throw new \LogicException("no renderer found");
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\DocStoreBundle\GenericDoc\Twig;
use Chill\DocStoreBundle\GenericDoc\GenericDocDTO;
interface GenericDocRendererInterface
{
public function supports(GenericDocDTO $genericDocDTO, $options = []): bool;
public function getTemplate(GenericDocDTO $genericDocDTO, $options = []): string;
public function getTemplateData(GenericDocDTO $genericDocDTO, $options = []): array;
}