docgen: improve listing for templates

This commit is contained in:
Julien Fastré 2021-11-27 01:23:34 +01:00
parent 69e260f0b1
commit 1c18ba20fc
3 changed files with 60 additions and 26 deletions

View File

@ -14,6 +14,8 @@ use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Chill\PersonBundle\Entity\SocialWork\Evaluation;
@ -22,15 +24,25 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use PhpOffice\PhpWord\TemplateProcessor; use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
// TODO à mettre dans services // TODO à mettre dans services
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
class DocGeneratorTemplateController extends AbstractController class DocGeneratorTemplateController extends AbstractController
{ {
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private PaginatorFactory $paginatorFactory;
public function __construct(DocGeneratorTemplateRepository $docGeneratorTemplateRepository, PaginatorFactory $paginatorFactory)
{
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->paginatorFactory = $paginatorFactory;
}
/** /**
* @Route( * @Route(
* "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}", * "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}",
@ -130,26 +142,25 @@ class DocGeneratorTemplateController extends AbstractController
/** /**
* @Route( * @Route(
* "{_locale}/doc/gen/templates/for/{entityClassName}", * "/api/1.0/docgen/templates/by-entity/{entityClassName}",
* name="chill_docgenerator_templates_for_entity_api" * name="chill_docgenerator_templates_for_entity_api"
* ) * )
*/ */
public function listTemplateApiAction( public function listTemplateApiAction(string $entityClassName): Response
string $entityClassName, {
DocGeneratorTemplateRepository $templateRepository $nb = $this->docGeneratorTemplateRepository->countByEntity($entityClassName);
): Response { $paginator = $this->paginatorFactory->create($nb);
$entities = $templateRepository->findByEntity($entityClassName); $entities = $this->docGeneratorTemplateRepository->findByEntity(
$entityClassName,
$paginator->getCurrentPageFirstItemNumber(),
$paginator->getItemsPerPage()
);
$ret = []; return $this->json(
new Collection($entities, $paginator),
foreach ($entities as $entity) { Response::HTTP_OK,
$ret[] = [ [],
'id' => $entity->getId(), [AbstractNormalizer::GROUPS => ['read']]
'name' => $entity->getName(), );
'description' => $entity->getDescription(),
];
}
return new JsonResponse(['results' => $ret]);
} }
} }

View File

@ -15,31 +15,35 @@ use Symfony\Component\Serializer\Annotation as Serializer;
/** /**
* @ORM\Entity * @ORM\Entity
* @ORM\Table(name="chill_docgen_template") * @ORM\Table(name="chill_docgen_template")
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "docgen_template": DocGeneratorTemplate::class
* })
*/ */
class DocGeneratorTemplate class DocGeneratorTemplate
{ {
/** /**
* @ORM\Column(type="string", length=255) * Class name of the context to use.
*
* Class name of the context to use
* *
* so if $context = '' * so if $context = ''
* this template will use '' as context * this template will use '' as context
*
* @ORM\Column(type="string", length=255)
*/ */
private string $context; private string $context;
/** /**
* @ORM\Column(type="text", nullable=true) * @ORM\Column(type="text", nullable=true)
* @Serializer\Groups({"read"})
*/ */
private string $description; private string $description;
/** /**
* @ORM\Column(type="simple_array") * Class name of the entities for which this template can be used.
*
* Class name of the entities for which this template can be used
* *
* so if $entities = ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction'] * so if $entities = ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction']
* this template can be selected for an AccompanyingPeriod or a SocialAction * this template can be selected for an AccompanyingPeriod or a SocialAction
*
* @ORM\Column(type="simple_array")
*/ */
private array $entities = []; private array $entities = [];

View File

@ -25,6 +25,18 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
$this->repository = $entityManager->getRepository(DocGeneratorTemplate::class); $this->repository = $entityManager->getRepository(DocGeneratorTemplate::class);
} }
public function countByEntity(string $entity): int
{
$builder = $this->repository->createQueryBuilder('t');
$builder
->select('count(t)')
->where('t.entities LIKE :entity')
->setParameter('entity', '%' . addslashes($entity) . '%');
return $builder->getQuery()->getSingleScalarResult();
}
public function find($id, $lockMode = null, $lockVersion = null): ?DocGeneratorTemplate public function find($id, $lockMode = null, $lockVersion = null): ?DocGeneratorTemplate
{ {
return $this->repository->find($id, $lockMode, $lockVersion); return $this->repository->find($id, $lockMode, $lockVersion);
@ -49,7 +61,10 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
} }
public function findByEntity($entity) /**
* @return array|DocGeneratorTemplate[]
*/
public function findByEntity(string $entity, ?int $start = 0, ?int $limit = 50): array
{ {
$builder = $this->repository->createQueryBuilder('t'); $builder = $this->repository->createQueryBuilder('t');
@ -57,7 +72,11 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
->where('t.entities LIKE :entity') ->where('t.entities LIKE :entity')
->setParameter('entity', '%' . addslashes($entity) . '%'); ->setParameter('entity', '%' . addslashes($entity) . '%');
return $builder->getQuery()->execute(); return $builder
->getQuery()
->setFirstResult($start)
->setMaxResults($limit)
->getResult();
} }
public function findOneBy(array $criteria, ?array $orderBy = null): ?DocGeneratorTemplate public function findOneBy(array $criteria, ?array $orderBy = null): ?DocGeneratorTemplate