diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index dd9d8e635..fd153fab2 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -14,6 +14,8 @@ use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; 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\AccompanyingPeriodWorkEvaluationDocument; use Chill\PersonBundle\Entity\SocialWork\Evaluation; @@ -22,15 +24,25 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\TransferException; use PhpOffice\PhpWord\TemplateProcessor; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; // TODO à mettre dans services use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; class DocGeneratorTemplateController extends AbstractController { + private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; + + private PaginatorFactory $paginatorFactory; + + public function __construct(DocGeneratorTemplateRepository $docGeneratorTemplateRepository, PaginatorFactory $paginatorFactory) + { + $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->paginatorFactory = $paginatorFactory; + } + /** * @Route( * "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}", @@ -130,26 +142,25 @@ class DocGeneratorTemplateController extends AbstractController /** * @Route( - * "{_locale}/doc/gen/templates/for/{entityClassName}", + * "/api/1.0/docgen/templates/by-entity/{entityClassName}", * name="chill_docgenerator_templates_for_entity_api" * ) */ - public function listTemplateApiAction( - string $entityClassName, - DocGeneratorTemplateRepository $templateRepository - ): Response { - $entities = $templateRepository->findByEntity($entityClassName); + 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() + ); - $ret = []; - - foreach ($entities as $entity) { - $ret[] = [ - 'id' => $entity->getId(), - 'name' => $entity->getName(), - 'description' => $entity->getDescription(), - ]; - } - - return new JsonResponse(['results' => $ret]); + return $this->json( + new Collection($entities, $paginator), + Response::HTTP_OK, + [], + [AbstractNormalizer::GROUPS => ['read']] + ); } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index f38ca139a..43d72a64f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -15,31 +15,35 @@ use Symfony\Component\Serializer\Annotation as Serializer; /** * @ORM\Entity * @ORM\Table(name="chill_docgen_template") + * @Serializer\DiscriminatorMap(typeProperty="type", mapping={ + * "docgen_template": DocGeneratorTemplate::class + * }) */ 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 = '' * this template will use '' as context + * + * @ORM\Column(type="string", length=255) */ private string $context; /** * @ORM\Column(type="text", nullable=true) + * @Serializer\Groups({"read"}) */ 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'] * this template can be selected for an AccompanyingPeriod or a SocialAction + * + * @ORM\Column(type="simple_array") */ private array $entities = []; diff --git a/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php b/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php index df0e0bd3d..007f2fabb 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php +++ b/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php @@ -25,6 +25,18 @@ final class DocGeneratorTemplateRepository implements ObjectRepository $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 { return $this->repository->find($id, $lockMode, $lockVersion); @@ -49,7 +61,10 @@ final class DocGeneratorTemplateRepository implements ObjectRepository 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'); @@ -57,7 +72,11 @@ final class DocGeneratorTemplateRepository implements ObjectRepository ->where('t.entities LIKE :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