mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Merge branch '274-active-status-filter' into 'master'
Resolve "Add active/inactive filter to user list in admin" Closes #274 See merge request Chill-Projet/chill-bundles!694
This commit is contained in:
commit
2b09e1459c
5
.changes/unreleased/Feature-20240705-104634.yaml
Normal file
5
.changes/unreleased/Feature-20240705-104634.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
kind: Feature
|
||||
body: '[admin] filter users by active / inactive in the admin user''s list'
|
||||
time: 2024-07-05T10:46:34.365091722+02:00
|
||||
custom:
|
||||
Issue: ""
|
@ -265,6 +265,7 @@ class UserController extends CRUDController
|
||||
return $this->getFilterOrderHelperFactory()
|
||||
->create(self::class)
|
||||
->addSearchBox(['label'])
|
||||
->addCheckbox('activeFilter', [true => 'Active', false => 'Inactive'], ['Active'])
|
||||
->build();
|
||||
}
|
||||
|
||||
@ -274,11 +275,7 @@ class UserController extends CRUDController
|
||||
return parent::countEntities($action, $request, $filterOrder);
|
||||
}
|
||||
|
||||
if (null === $filterOrder->getQueryString()) {
|
||||
return parent::countEntities($action, $request, $filterOrder);
|
||||
}
|
||||
|
||||
return $this->userRepository->countByUsernameOrEmail($filterOrder->getQueryString());
|
||||
return $this->userRepository->countFilteredUsers($filterOrder->getQueryString(), $filterOrder->getCheckboxData('activeFilter'));
|
||||
}
|
||||
|
||||
protected function createFormFor(string $action, $entity, ?string $formClass = null, array $formOptions = []): FormInterface
|
||||
@ -335,16 +332,13 @@ class UserController extends CRUDController
|
||||
return parent::getQueryResult($action, $request, $totalItems, $paginator, $filterOrder);
|
||||
}
|
||||
|
||||
if (null === $filterOrder->getQueryString()) {
|
||||
return parent::getQueryResult($action, $request, $totalItems, $paginator, $filterOrder);
|
||||
}
|
||||
$queryString = $filterOrder->getQueryString();
|
||||
$activeFilter = $filterOrder->getCheckboxData('activeFilter');
|
||||
$nb = $this->userRepository->countFilteredUsers($queryString, $activeFilter);
|
||||
|
||||
return $this->userRepository->findByUsernameOrEmail(
|
||||
$filterOrder->getQueryString(),
|
||||
['usernameCanonical' => 'ASC'],
|
||||
$paginator->getItemsPerPage(),
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
);
|
||||
$paginator = $this->getPaginatorFactory()->create($nb);
|
||||
|
||||
return $this->userRepository->findFilteredUsers($queryString, $activeFilter, $paginator->getCurrentPageFirstItemNumber(), $paginator->getItemsPerPage());
|
||||
}
|
||||
|
||||
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
|
||||
@ -375,10 +369,12 @@ class UserController extends CRUDController
|
||||
$returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : [];
|
||||
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl(
|
||||
->setAction(
|
||||
$this->generateUrl(
|
||||
'admin_user_add_groupcenter',
|
||||
array_merge($returnPathParams, ['uid' => $user->getId()])
|
||||
))
|
||||
)
|
||||
)
|
||||
->setMethod('POST')
|
||||
->add(self::FORM_GROUP_CENTER_COMPOSED, ComposedGroupCenterType::class)
|
||||
->add('submit', SubmitType::class, ['label' => 'Add a new groupCenter'])
|
||||
@ -393,10 +389,12 @@ class UserController extends CRUDController
|
||||
$returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : [];
|
||||
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl(
|
||||
->setAction(
|
||||
$this->generateUrl(
|
||||
'admin_user_delete_groupcenter',
|
||||
array_merge($returnPathParams, ['uid' => $user->getId(), 'gcid' => $groupCenter->getId()])
|
||||
))
|
||||
)
|
||||
)
|
||||
->setMethod('DELETE')
|
||||
->add('submit', SubmitType::class, ['label' => 'Delete'])
|
||||
->getForm();
|
||||
|
@ -17,6 +17,7 @@ use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\NonUniqueResultException;
|
||||
use Doctrine\ORM\NoResultException;
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||
@ -26,9 +27,25 @@ final readonly class UserRepository implements UserRepositoryInterface
|
||||
{
|
||||
private EntityRepository $repository;
|
||||
|
||||
private const FIELDS = ['id', 'email', 'enabled', 'civility_id', 'civility_abbreviation', 'civility_name', 'label', 'mainCenter_id',
|
||||
'mainCenter_name', 'mainScope_id', 'mainScope_name', 'userJob_id', 'userJob_name', 'currentLocation_id', 'currentLocation_name',
|
||||
'mainLocation_id', 'mainLocation_name'];
|
||||
private const FIELDS = [
|
||||
'id',
|
||||
'email',
|
||||
'enabled',
|
||||
'civility_id',
|
||||
'civility_abbreviation',
|
||||
'civility_name',
|
||||
'label',
|
||||
'mainCenter_id',
|
||||
'mainCenter_name',
|
||||
'mainScope_id',
|
||||
'mainScope_name',
|
||||
'userJob_id',
|
||||
'userJob_name',
|
||||
'currentLocation_id',
|
||||
'currentLocation_name',
|
||||
'mainLocation_id',
|
||||
'mainLocation_name',
|
||||
];
|
||||
|
||||
public function __construct(private EntityManagerInterface $entityManager, private Connection $connection)
|
||||
{
|
||||
@ -296,6 +313,25 @@ final readonly class UserRepository implements UserRepositoryInterface
|
||||
return User::class;
|
||||
}
|
||||
|
||||
public function getResult(
|
||||
QueryBuilder $qb,
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb->select('u');
|
||||
|
||||
$qb
|
||||
->setFirstResult($start)
|
||||
->setMaxResults($limit);
|
||||
|
||||
foreach ($orderBy as $field => $direction) {
|
||||
$qb->addOrderBy('u.'.$field, $direction);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
private function queryByUsernameOrEmail(string $pattern): QueryBuilder
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder()->from(User::class, 'u');
|
||||
@ -312,4 +348,49 @@ final readonly class UserRepository implements UserRepositoryInterface
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function buildFilterBaseQuery(?string $queryString, array $isActive)
|
||||
{
|
||||
if (null !== $queryString) {
|
||||
$qb = $this->queryByUsernameOrEmail($queryString);
|
||||
} else {
|
||||
$qb = $this->entityManager->createQueryBuilder()->from(User::class, 'u');
|
||||
}
|
||||
|
||||
// Add condition based on active/inactive status
|
||||
if (in_array('Active', $isActive, true) && !in_array('Inactive', $isActive, true)) {
|
||||
$qb->andWhere('u.enabled = true');
|
||||
} elseif (in_array('Inactive', $isActive, true) && !in_array('Active', $isActive, true)) {
|
||||
$qb->andWhere('u.enabled = false');
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function findFilteredUsers(
|
||||
?string $queryString = null,
|
||||
array $isActive = ['active'],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = ['username' => 'ASC']
|
||||
): array {
|
||||
$qb = $this->buildFilterBaseQuery($queryString, $isActive);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function countFilteredUsers(
|
||||
?string $queryString = null,
|
||||
array $isActive = ['active'],
|
||||
): int {
|
||||
$qb = $this->buildFilterBaseQuery($queryString, $isActive);
|
||||
|
||||
try {
|
||||
return $qb
|
||||
->select('COUNT(u)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
} catch (NoResultException|NonUniqueResultException $e) {
|
||||
throw new \LogicException('a count query should return one result', previous: $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
<?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 ChillMainBundle\Tests\Repository;
|
||||
|
||||
use Chill\MainBundle\Repository\UserRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class UserRepositoryTest extends KernelTestCase
|
||||
{
|
||||
private UserRepository $userRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
$entityManager = static::$kernel->getContainer()->get('doctrine.orm.entity_manager');
|
||||
$connection = $entityManager->getConnection();
|
||||
$this->userRepository = new UserRepository($entityManager, $connection);
|
||||
}
|
||||
|
||||
public function testCountFilteredUsers(): void
|
||||
{
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers(null, ['Active']));
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers(null, ['Active', 'Inactive']));
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers(null, ['Inactive']));
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers('center', ['Active']));
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers('center', ['Active', 'Inactive']));
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers('center', ['Inactive']));
|
||||
self::assertIsInt($this->userRepository->countFilteredUsers('center'));
|
||||
}
|
||||
|
||||
public function testFindByFilteredUsers(): void
|
||||
{
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers(null, ['Active']));
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers(null, ['Active', 'Inactive']));
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers(null, ['Inactive']));
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers('center', ['Active']));
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers('center', ['Active', 'Inactive']));
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers('center', ['Inactive']));
|
||||
self::assertIsArray($this->userRepository->findFilteredUsers('center'));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user