Feature: [docgen] order document template by name in list and in admin

This commit is contained in:
Julien Fastré 2022-10-19 17:56:10 +02:00
parent 0cbe12a32c
commit 83ba813160
2 changed files with 23 additions and 2 deletions

View File

@ -14,6 +14,8 @@ namespace Chill\DocGeneratorBundle\Controller;
use Chill\DocGeneratorBundle\Context\ContextManager; use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\CRUD\Controller\CRUDController;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@ -84,4 +86,16 @@ class AdminDocGeneratorTemplateController extends CRUDController
return $entity; return $entity;
} }
/**
* @param QueryBuilder $query
*
* @return QueryBuilder|mixed
*/
protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator)
{
return $query->addSelect('JSON_EXTRACT(e.name, :lang) AS HIDDEN name_lang')
->setParameter('lang', $request->getLocale())
->addOrderBy('name_lang', 'ASC');
}
} }

View File

@ -15,14 +15,18 @@ use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
use Symfony\Component\HttpFoundation\RequestStack;
final class DocGeneratorTemplateRepository implements ObjectRepository final class DocGeneratorTemplateRepository implements ObjectRepository
{ {
private EntityRepository $repository; private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager) private RequestStack $requestStack;
public function __construct(EntityManagerInterface $entityManager, RequestStack $requestStack)
{ {
$this->repository = $entityManager->getRepository(DocGeneratorTemplate::class); $this->repository = $entityManager->getRepository(DocGeneratorTemplate::class);
$this->requestStack = $requestStack;
} }
public function countByEntity(string $entity): int public function countByEntity(string $entity): int
@ -71,7 +75,10 @@ final class DocGeneratorTemplateRepository implements ObjectRepository
$builder $builder
->where('t.entity LIKE :entity') ->where('t.entity LIKE :entity')
->andWhere($builder->expr()->eq('t.active', "'TRUE'")) ->andWhere($builder->expr()->eq('t.active', "'TRUE'"))
->setParameter('entity', addslashes($entity)); ->setParameter('entity', addslashes($entity))
->addSelect('JSON_EXTRACT(t.name, :lang) AS HIDDEN name_lang')
->setParameter('lang', $this->requestStack->getCurrentRequest()->getLocale())
->addOrderBy('name_lang', 'ASC');
return $builder return $builder
->getQuery() ->getQuery()