<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
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;
/**
* @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);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(User $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(User $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->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->_em->persist($user);
$this->_em->flush();
}
/**
* @return User Returns one User
*/
public function findUserBy($user_id)
{
return $this->createQueryBuilder('u')
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, c.rasion_sociale, c.adr1, c.adr2,
c.cp, c.cedex, c.email AS email_carrier, c.tel as tel_carrier, c.ville, rc.nicename, rc.phonecode, rc.id AS ref_country_id,
c.siret,c.site_web,c.code_ape,c.forme,c.capital_social')
->leftJoin('u.carriers', 'c')
->leftJoin('c.ref_country', 'rc')
->andWhere('u.id = :user_id')
->setParameter('user_id', $user_id)
->getQuery()
->getResult()
;
}
public function findRoleByUserId($user_id)
{
return $this->createQueryBuilder('u')
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, rc.nicename, rc.phonecode, rc.id AS ref_country_id ')
->leftJoin('u.ref_country', 'rc')
->andWhere('u.id = :user_id')
->setParameter('user_id', $user_id)
->getQuery()
->getResult()
;
}
/*
public function findOneBySomeField($value): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
/**
* @return User[] Returns an array of users objects
*/
public function findAllUsersStdPrinDri($carrierId)
{
$roles = ['["ROLE_STANDARD_USER"]', '["ROLE_DRIVER_USER"]', '["ROLE_PRINCIPAL_USER"]'];
return $this->createQueryBuilder('u')
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id')
->leftJoin('u.carriers', 'c')
->leftJoin('u.ref_country', 'rc')
->andWhere('u.roles IN (:roles)')
->setParameter('roles', $roles)
->andWhere('c.id LIKE :carrier_id')
->setParameter('carrier_id', $carrierId)
->orderBy('u.id', 'ASC')
->getQuery()
->getResult();
}
public function findUserByCarrierAndRole($carrierId, $role)
{
$queryBuilder = $this->createQueryBuilder('u');
if($carrierId != null){
$queryBuilder
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id, u.contact_mailjet')
->leftJoin('u.carriers', 'c');
}else{
$queryBuilder
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, u.password, rc.phonecode, rc.id AS ref_country_id, u.contact_mailjet');
}
$queryBuilder
->leftJoin('u.ref_country', 'rc')
->andWhere('u.roles LIKE :role')
->setParameter('role', '%"'.$role.'"%');
if($carrierId != null){
$queryBuilder->andWhere('c.id LIKE :carrier_id')
->setParameter('carrier_id', $carrierId);
}
return $queryBuilder->getQuery()->getResult();
}
public function findUserByCarrier($carrierId)
{
return $this->createQueryBuilder('u')
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id')
->leftJoin('u.carriers', 'c')
->leftJoin('u.ref_country', 'rc')
->andWhere('c.id LIKE :carrier_id')
->setParameter('carrier_id', $carrierId)
->getQuery()
->getResult();
}
public function findAllUsers()
{
return $this->createQueryBuilder('u')
->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id')
->leftJoin('u.carriers', 'c')
->leftJoin('u.ref_country', 'rc')
->orderBy('u.created_at', 'DESC')
->getQuery()
->getResult();
}
}