diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWork/EvaluationController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWork/EvaluationController.php index 13a4aca99..4d0aba918 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWork/EvaluationController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWork/EvaluationController.php @@ -13,14 +13,50 @@ namespace Chill\PersonBundle\Controller\SocialWork; use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Pagination\PaginatorInterface; +use Chill\MainBundle\Templating\Listing\FilterOrderHelper; +use Chill\PersonBundle\Repository\SocialWork\EvaluationRepository; use Symfony\Component\HttpFoundation\Request; class EvaluationController extends CRUDController { + public function __construct(private readonly EvaluationRepository $repository) {} protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator) { $query->addOrderBy('e.id', 'ASC'); return parent::orderQuery($action, $query, $request, $paginator); } + + protected function getQueryResult( + string $action, + Request $request, + int $totalItems, + PaginatorInterface $paginator, + ?FilterOrderHelper $filterOrder = null, + ) { + if (0 === $totalItems) { + return []; + } + + if (!$filterOrder instanceof FilterOrderHelper) { + return parent::getQueryResult($action, $request, $totalItems, $paginator, $filterOrder); + } + + $queryString = $filterOrder->getQueryString(); + $activeFilter = $filterOrder->getCheckboxData('activeFilter'); + $nb = $this->repository->countFilteredEvaluations($queryString, $activeFilter); + + $paginator = $this->getPaginatorFactory()->create($nb); + + return $this->repository->findFilteredEvaluations($queryString, $activeFilter, $paginator->getCurrentPageFirstItemNumber(), $paginator->getItemsPerPage()); + } + + protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper + { + return $this->getFilterOrderHelperFactory() + ->create(self::class) + ->addSearchBox(['label']) + ->addCheckbox('activeFilter', [true => 'Active', false => 'Inactive'], ['Active']) + ->build(); + } } diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWork/GoalController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWork/GoalController.php index df57b012a..1655356bc 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWork/GoalController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWork/GoalController.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Controller\SocialWork; use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Pagination\PaginatorInterface; +use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Symfony\Component\HttpFoundation\Request; class GoalController extends CRUDController @@ -23,4 +24,13 @@ class GoalController extends CRUDController return parent::orderQuery($action, $query, $request, $paginator); } + + protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper + { + return $this->getFilterOrderHelperFactory() + ->create(self::class) + ->addSearchBox(['label']) + ->addCheckbox('activeFilter', [true => 'Active', false => 'Inactive'], ['Active']) + ->build(); + } } diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWork/ResultController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWork/ResultController.php index 274f7136e..775106a40 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWork/ResultController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWork/ResultController.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Controller\SocialWork; use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Pagination\PaginatorInterface; +use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Symfony\Component\HttpFoundation\Request; class ResultController extends CRUDController @@ -23,4 +24,13 @@ class ResultController extends CRUDController return parent::orderQuery($action, $query, $request, $paginator); } + + protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper + { + return $this->getFilterOrderHelperFactory() + ->create(self::class) + ->addSearchBox(['label']) + ->addCheckbox('activeFilter', [true => 'Active', false => 'Inactive'], ['Active']) + ->build(); + } } diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialActionController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialActionController.php index 69bbf7ace..a54c411f7 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialActionController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialActionController.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Controller\SocialWork; use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Pagination\PaginatorInterface; +use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Symfony\Component\HttpFoundation\Request; class SocialActionController extends CRUDController @@ -23,4 +24,13 @@ class SocialActionController extends CRUDController return parent::orderQuery($action, $query, $request, $paginator); } + + protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper + { + return $this->getFilterOrderHelperFactory() + ->create(self::class) + ->addSearchBox(['label']) + ->addCheckbox('activeFilter', [true => 'Active', false => 'Inactive'], ['Active']) + ->build(); + } } diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialIssueController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialIssueController.php index 316417583..d47a56a8a 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialIssueController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWork/SocialIssueController.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Controller\SocialWork; use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Pagination\PaginatorInterface; +use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; @@ -37,4 +38,13 @@ class SocialIssueController extends CRUDController return parent::orderQuery($action, $query, $request, $paginator); } + + protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper + { + return $this->getFilterOrderHelperFactory() + ->create(self::class) + ->addSearchBox(['label']) + ->addCheckbox('activeFilter', [true => 'Active', false => 'Inactive'], ['Active']) + ->build(); + } } diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php index 1c89a98f6..b09ee57b8 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php @@ -11,15 +11,19 @@ declare(strict_types=1); namespace Chill\PersonBundle\Repository\SocialWork; +use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\NonUniqueResultException; +use Doctrine\ORM\NoResultException; +use Doctrine\ORM\QueryBuilder; final readonly class EvaluationRepository implements EvaluationRepositoryInterface { private EntityRepository $repository; - public function __construct(EntityManagerInterface $entityManager) + public function __construct(private EntityManagerInterface $entityManager) { $this->repository = $entityManager->getRepository(Evaluation::class); } @@ -65,4 +69,79 @@ final readonly class EvaluationRepository implements EvaluationRepositoryInterfa { return Evaluation::class; } + + public function getResult( + QueryBuilder $qb, + ?int $start = 0, + ?int $limit = 50, + ?array $orderBy = [], + ): array { + $qb->select('e'); + + $qb + ->setFirstResult($start) + ->setMaxResults($limit); + + foreach ($orderBy as $field => $direction) { + $qb->addOrderBy('e.'.$field, $direction); + } + + return $qb->getQuery()->getResult(); + } + + private function queryByTitle(string $pattern): QueryBuilder + { + $qb = $this->entityManager->createQueryBuilder()->from(Evaluation::class, 'e'); + + $qb->expr()->like('e.title', 'CONCAT(\'%\', LOWER(UNACCENT(:pattern)), \'%\')'); + + $qb->setParameter('pattern', $pattern); + + return $qb; + } + + public function buildFilterBaseQuery(?string $queryString, array $isActive): QueryBuilder + { + if (null !== $queryString) { + $qb = $this->queryByTitle($queryString); + } else { + $qb = $this->entityManager->createQueryBuilder()->from(Evaluation::class, 'e'); + } + + // Add condition based on active/inactive status + if (in_array('Active', $isActive, true) && !in_array('Inactive', $isActive, true)) { + $qb->andWhere('e.active = true'); + } elseif (in_array('Inactive', $isActive, true) && !in_array('Active', $isActive, true)) { + $qb->andWhere('e.active = false'); + } + + return $qb; + } + + public function findFilteredEvaluations( + ?string $queryString = null, + array $isActive = ['active'], + ?int $start = 0, + ?int $limit = 50, + ?array $orderBy = ['title' => 'ASC'], + ): array { + $qb = $this->buildFilterBaseQuery($queryString, $isActive); + + return $this->getResult($qb, $start, $limit, $orderBy); + } + + public function countFilteredEvaluations( + ?string $queryString = null, + array $isActive = ['active'], + ): int { + $qb = $this->buildFilterBaseQuery($queryString, $isActive); + + try { + return $qb + ->select('COUNT(e)') + ->getQuery()->getSingleScalarResult(); + } catch (NoResultException|NonUniqueResultException $e) { + throw new \LogicException('a count query should return one result', previous: $e); + } + } } diff --git a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Evaluation/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Evaluation/index.html.twig index 556065e42..af7989b8f 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Evaluation/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Evaluation/index.html.twig @@ -2,6 +2,9 @@ {% block admin_content %} {% embed '@ChillMain/CRUD/_index.html.twig' %} + + {% block filter_order %}{{ filter_order|chill_render_filter_order_helper }}{% endblock %} + {% block table_entities_thead_tr %}