src/Repository/Admin/UserRepository.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Admin;
  3. use App\Entity\Admin\User;
  4. use App\Pagination\PaginatorUser;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  10. /**
  11.  * @extends ServiceEntityRepository<User>
  12.  *
  13.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method User[]    findAll()
  16.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  19. {
  20.     public function __construct(ManagerRegistry $registry)
  21.     {
  22.         parent::__construct($registryUser::class);
  23.     }
  24.     public function add(User $entitybool $flush false): void
  25.     {
  26.         $this->getEntityManager()->persist($entity);
  27.         if ($flush) {
  28.             $this->getEntityManager()->flush();
  29.         }
  30.     }
  31.     public function remove(User $entitybool $flush false): void
  32.     {
  33.         $this->getEntityManager()->remove($entity);
  34.         if ($flush) {
  35.             $this->getEntityManager()->flush();
  36.         }
  37.     }
  38.     /**
  39.      * Used to upgrade (rehash) the user's password automatically over time.
  40.      */
  41.     public function upgradePassword(PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  42.     {
  43.         if (!$user instanceof User) {
  44.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'\get_class($user)));
  45.         }
  46.         $user->setPassword($newHashedPassword);
  47.         $this->add($usertrue);
  48.     }
  49.     public function findUsers(int $page=1, array $sort, array $querys): PaginatorUser
  50.     {
  51.         if (isset($sort['request']) && $sort['request'] != null) {
  52.             $qb $this->createQueryBuilder('p')->orderBy('p.' $sort['request'], $sort[$sort['request']]);
  53.         } else {
  54.             $qb $this->createQueryBuilder('p')->orderBy('p.id''ASC');
  55.             ;
  56.         }
  57.         if (count($querys) > 0) {
  58.             foreach ($querys as $key => $query) {
  59.                 $qb->andwhere('p.' $key ' LIKE :' $key)->setParameter($key"%" $query "%");
  60.             }
  61.         }
  62.         return (new PaginatorUser($qb))->paginate($page);
  63.     }
  64.     public function toExcel(array $sort, array $querysint $page_current null): array
  65.     {
  66.         if (isset($sort['request']) && $sort['request'] != null) {
  67.             $qb $this->createQueryBuilder('p')->orderBy('p.' $sort['request'], $sort[$sort['request']]);
  68.         } else {
  69.             $qb $this->createQueryBuilder('p');
  70.         }
  71.         if (count($querys) > 0) {
  72.             foreach ($querys as $key => $query) {
  73.                 $qb->andwhere('p.' $key ' LIKE :' $key)->setParameter($key"%" $query "%");
  74.             }
  75.         }
  76.         if ($page_current != null) {
  77.             $currentPage max(1$page_current);
  78.             $paginator = new PaginatorUser($qb);
  79.             $pageSize $paginator->getPageSize();
  80.             $firstResult = ($currentPage 1) * $pageSize;
  81.             $datas $qb
  82.                 ->setFirstResult($firstResult)
  83.                 ->setMaxResults($pageSize);
  84.         }
  85.         $datas $qb
  86.             ->select('p.id , p.username, p.name, p.roles, p.email, p.position, p.company, p.last_login')
  87.             ->getQuery()
  88.             ->getResult();
  89.         if (isset($datas[0]['roles'])) {
  90.             foreach ($datas as $key => $data) {
  91.                 $datas[$key]['roles'] = implode(" "$data['roles']);
  92.             }
  93.         };
  94.         return $datas;
  95.     }
  96.     //    /**
  97.     //     * @return User[] Returns an array of User objects
  98.     //     */
  99.     //    public function findByExampleField($value): array
  100.     //    {
  101.     //        return $this->createQueryBuilder('u')
  102.     //            ->andWhere('u.exampleField = :val')
  103.     //            ->setParameter('val', $value)
  104.     //            ->orderBy('u.id', 'ASC')
  105.     //            ->setMaxResults(10)
  106.     //            ->getQuery()
  107.     //            ->getResult()
  108.     //        ;
  109.     //    }
  110.     //    public function findOneBySomeField($value): ?User
  111.     //    {
  112.     //        return $this->createQueryBuilder('u')
  113.     //            ->andWhere('u.exampleField = :val')
  114.     //            ->setParameter('val', $value)
  115.     //            ->getQuery()
  116.     //            ->getOneOrNullResult()
  117.     //        ;
  118.     //    }
  119. }