<?php
namespace App\Repository\Admin;
use App\Entity\Admin\User;
use App\Pagination\PaginatorUser;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
/**
* @extends ServiceEntityRepository<User>
*
* @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);
}
public function add(User $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(User $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}
$user->setPassword($newHashedPassword);
$this->add($user, true);
}
public function findUsers(int $page=1, array $sort, array $querys): PaginatorUser
{
if (isset($sort['request']) && $sort['request'] != null) {
$qb = $this->createQueryBuilder('p')->orderBy('p.' . $sort['request'], $sort[$sort['request']]);
} else {
$qb = $this->createQueryBuilder('p')->orderBy('p.id', 'ASC');
;
}
if (count($querys) > 0) {
foreach ($querys as $key => $query) {
$qb->andwhere('p.' . $key . ' LIKE :' . $key)->setParameter($key, "%" . $query . "%");
}
}
return (new PaginatorUser($qb))->paginate($page);
}
public function toExcel(array $sort, array $querys, int $page_current = null): array
{
if (isset($sort['request']) && $sort['request'] != null) {
$qb = $this->createQueryBuilder('p')->orderBy('p.' . $sort['request'], $sort[$sort['request']]);
} else {
$qb = $this->createQueryBuilder('p');
}
if (count($querys) > 0) {
foreach ($querys as $key => $query) {
$qb->andwhere('p.' . $key . ' LIKE :' . $key)->setParameter($key, "%" . $query . "%");
}
}
if ($page_current != null) {
$currentPage = max(1, $page_current);
$paginator = new PaginatorUser($qb);
$pageSize = $paginator->getPageSize();
$firstResult = ($currentPage - 1) * $pageSize;
$datas = $qb
->setFirstResult($firstResult)
->setMaxResults($pageSize);
}
$datas = $qb
->select('p.id , p.username, p.name, p.roles, p.email, p.position, p.company, p.last_login')
->getQuery()
->getResult();
if (isset($datas[0]['roles'])) {
foreach ($datas as $key => $data) {
$datas[$key]['roles'] = implode(" ", $data['roles']);
}
};
return $datas;
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}