<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newEncodedPassword);
$this->_em->persist($user);
$this->_em->flush();
}
public function getDatatableiTotalRecords()
{
$iTotalRecordsQuery = $this
->createQueryBuilder('u')
->select('COUNT(1)')
->andWhere('u.deletedAt IS NULL')
->getQuery()
->getSingleScalarResult();
$iTotalRecords = 0;
if($iTotalRecordsQuery){
$iTotalRecords = $iTotalRecordsQuery;
}
return $iTotalRecords;
}
public function getDatatableiTotalRecordsDisplay(?string $search, array $filters)
{
$iTotalDisplayRecordsQuery = $this
->createQueryBuilder('u')
->select('COUNT(1)')
->andWhere('u.deletedAt IS NULL')
;
$iTotalDisplayRecordsQuery = $this->applyDatatableSearch($iTotalDisplayRecordsQuery, $search);
$iTotalDisplayRecordsQuery = $this->applyDatatableFilters($iTotalDisplayRecordsQuery, $filters);
$iTotalDisplayRecordsQuery = $iTotalDisplayRecordsQuery->getQuery()->getSingleScalarResult();
$iTotalDisplayRecords = 0;
if($iTotalDisplayRecordsQuery){
$iTotalDisplayRecords = $iTotalDisplayRecordsQuery;
}
return $iTotalDisplayRecords;
}
public function getDatatableResults(int $start, int $length, ?string $search, array $filters, ?string $orderBy, ?string $orderDir){
$query = $this
->createQueryBuilder('u')
->andWhere('u.deletedAt IS NULL')
->setFirstResult($start)
->setMaxResults($length);
$query = $this->applyDatatableSearch($query, $search);
$query = $this->applyDatatableFilters($query, $filters);
$query = $this->applyDatatableOrder($query, $orderBy, $orderDir);
return $query->getQuery()->getResult();
}
private function applyDatatableSearch(QueryBuilder $query, ?string $search)
{
if($search){
$query
->andWhere("u.lastname LIKE :search OR u.firstname LIKE :search OR u.email LIKE :search")
->setParameter("search", "%$search%");
}
return $query;
}
private function applyDatatableFilters(QueryBuilder $query, array $filters)
{
foreach ($filters as $filter => $value) {
if($filter == 'roles'){
$query
->andWhere("u.$filter LIKE :$filter")
->setParameter("$filter", "%$value%");
}
else{
$query
->andWhere("u.$filter = :$filter")
->setParameter("$filter", $value);
}
}
return $query;
}
private function applyDatatableOrder(QueryBuilder $query, ?string $orderBy, ?string $orderDir)
{
if($orderBy && $orderDir){
$query->addOrderBy("u.$orderBy", "$orderDir");
}
return $query;
}
public function findOneByToken(string $token): ?User
{
return $this
->createQueryBuilder('u')
->andWhere('u.token_reset_password = :token')
->setParameter('token', $token)
->getQuery()
->getOneOrNullResult()
;
}
/**
* @param string $role
*
* @return User[]
*/
public function findByRole(string $role)
{
return $this
->createQueryBuilder('u')
->andWhere('u.deletedAt IS NULL')
->andWhere('u.roles LIKE :role')
->setParameter('role', "%$role%")
->getQuery()
->getResult();
}
}