<?php
namespace App\Repository;
use App\Entity\Product;
use App\Model\AppConstant;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Product>
*
* @method Product|null find($id, $lockMode = null, $lockVersion = null)
* @method Product|null findOneBy(array $criteria, array $orderBy = null)
* @method Product[] findAll()
* @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProductRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Product::class);
}
public function add(Product $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Product $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function getSearchQuery($product, $options = null){
$qb = $this->createQueryBuilder('p');
$qb->join('p.produitQteStock', 's');
$qb->addOrderBy('s.isAvailableStock', 'desc');
$parameters = [];
if($product->getName()!=null) {
if ($GLOBALS['app_default_locale'] == 'fr') {
$qb->andWhere('LOWER(p.name) LIKE LOWER(:name)');
$parameters['name'] = '%'.$product->getName(). '%';
} else {
$qb->andWhere('LOWER(p.name_english) LIKE LOWER(:name_english)');
$parameters['name_english'] = '%'.$product->getName(). '%';
}
}
if($product->getDescription()!=null) {
$qb->andWhere('LOWER(p.description) LIKE LOWER(:description)');
$parameters['description'] = '%'.$product->getDescription(). '%';
}
if($product->getCost()!=null) {
$qb->andWhere('p.cost = :cost');
$parameters['cost'] = $product->getCost();
}
if($product->getProductCategory()!=null) {
$qb->andWhere('p.productCategory = :productCategory');
$parameters['productCategory'] = $product->getProductCategory();
}
if($options != null){
if($options['favorite']!=null && $options['favorite'] == 'my_favorite') {
$qb->leftJoin('p.favoriteProducts', 'favoriteProducts');
$qb->andWhere('favoriteProducts.user = :user');
$parameters['user'] = $options['user'];
// $qb->orderBy('p.'.$options['orderBy'], $options['order']);
}
if($options['prixMin']!=null) {
$qb->andWhere('p.cost >= :prixMin');
$parameters['prixMin'] = $options['prixMin'];
}
if($options['prixMax']!=null) {
$qb->andWhere('p.cost <= :prixMax');
$parameters['prixMax'] = $options['prixMax'];
}
if($options['langue']!=null) {
$qb->andWhere('p.langue = :langue');
$parameters['langue'] = $options['langue'];
}else if(isset($options['admin']) && $options['admin'] == false){
$langue = $product::getLanguageIntegerByDefaultLocale();
$qb->andwhere('p.langue = :langue OR p.langue = :all');
$parameters['langue'] = $langue;
$parameters['all'] = $product::$_langue['ALL'];
}
$qb->addOrderBy('s.isAvailableStock', 'desc');
$withOrderOption = ($options['orderBy']!=null && $options['order']!=null);
if($withOrderOption) {
$qb->addOrderBy('p.'.$options['orderBy'], $options['order']);
}
if(!$withOrderOption){
$qb->addOrderBy('p.name', 'asc');
}
}
$qb->andWhere('p.status > :deletedStatus');
$parameters['deletedStatus'] = AppConstant::STATUS_DELETED;
$qb->andWhere('p.productCategory is not null');
$qb->setParameters($parameters);
return $qb->getQuery();
}
public function findAllByDefaultLocale()
{
$qb = $this->createQueryBuilder('p');
$langue = Product::getLanguageIntegerByDefaultLocale();
$qb->andwhere('p.langue = :langue OR p.langue = :all');
$parameters['langue'] = $langue;
$parameters['all'] = Product::$_langue['ALL'];
$qb->andWhere('p.status > :deletedStatus');
$parameters['deletedStatus'] = AppConstant::STATUS_DELETED;
$qb->setParameters($parameters);
return $qb->getQuery()->getResult();
}
public function getFindAllQuery(){
return $this->createQueryBuilder('p')->getQuery();
}
public function getFeaturedProducts($nbr=4): array
{
return $this->createQueryBuilder('p')
->andWhere('p.featured = 1')
->andWhere('p.status > :deletedStatus')
->setParameters([
"deletedStatus" => AppConstant::STATUS_DELETED
])
->setMaxResults($nbr)
->addOrderBy('p.majDate', 'desc')
->getQuery()
->getResult()
;
}
public function getMonthProducts($nbr=1): array
{
return $this->createQueryBuilder('p')
->andWhere('p.isMonthProduct = 1')
->andWhere('p.status > :deletedStatus')
->setParameters([
"deletedStatus" => AppConstant::STATUS_DELETED
])
->addOrderBy('p.majDate', 'desc')
->setMaxResults($nbr)
->getQuery()
->getResult()
;
}
/**
* @return Product[] Returns an array of Product objects
*/
public function findPropositionsProduit($produitIdExclude = null, $nbr=4): array
{
$parameters = [];
$qb = $this->createQueryBuilder('p');
if($produitIdExclude != null){
$qb
->andWhere('p.id != :produitIdExclude');
$parameters ["produitIdExclude"] = $produitIdExclude;
}
$qb->andWhere('p.status > :deletedStatus');
$parameters ["deletedStatus"] = AppConstant::STATUS_DELETED;
return $qb
->andWhere('p.productCategory is not null')
->setMaxResults($nbr)
->setParameters($parameters)
->getQuery()
->getResult()
;
}
// /**
// * @return Product[] Returns an array of Product objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('p.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Product
// {
// return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}