<?php
namespace App\Repository\Adherent;
use App\Entity\Adherent\BrandPicture;
use App\Entity\Adherent\MagasinMarques;
use App\Entity\Webfactory\Opc\OpcMarque;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method MagasinMarques|null find($id, $lockMode = null, $lockVersion = null)
* @method MagasinMarques|null findOneBy(array $criteria, array $orderBy = null)
* @method MagasinMarques[] findAll()
* @method MagasinMarques[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class MagasinMarquesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, MagasinMarques::class);
}
public function getSelectionOrdered($magasin)
{
return $this->createQueryBuilder('mm')
->select('mm.id, mm.sort, mm.created_at, mm.updated_at, m.id as marque_id, m.name as marque_name, m.popoverInfo as popover, m.popoverInfoNl as popover_nl, om.id as opc_id')
->leftJoin(OpcMarque::class, 'om', Join::WITH, 'om.idMarque = mm.marque AND om.kAdh = :mag')
->innerJoin('mm.marque', 'm')
->andWhere('mm.magasin = :mag')
->andWhere('mm.active = 1')
->setParameter('mag', $magasin)
->orderBy('mm.sort', 'ASC')
->getQuery()
->getResult();
}
public function getBrandByCategory($magasin, $categoryId, $limit = 10, $page = 1)
{
if ($page == 1) {
$page = 0;
}
return $this->createQueryBuilder('mb')
->join('mb.marque', 'ma', 'mb.marque = ma.id')
->join('ma.category', 'categ', 'ma.category = categ.id')
->where('mb.magasin = :mag')
->andWhere('categ.id = :category')
->setParameter('mag', $magasin)
->setParameter('category', $categoryId)
->setFirstResult($page)
->setMaxResults($limit)
->getQuery()->getResult();
}
public function getSearchedBrandByCategories($magasin, $value, $filters, $limit, $offset, $order)
{
$brands = $this->createQueryBuilder('mm');
$brands->leftJoin('mm.marque', 'b')
->leftJoin('b.category', 'c')
->leftJoin('mm.magasinMarquesException', 'mme'); // Joindre la table des exceptions
if ($value) {
$brands->andWhere('b.name LIKE :searchedValue')
->setParameter('searchedValue', '%' . $value . '%');
}
$defaultConditions = $brands->expr()->orX(); // Pour accumuler les conditions des filtres des catégories (pas eco et made france)
if (!empty($filters)) {
foreach ($filters as $index => $filter) {
if (isset($filter['value'])) {
switch ($filter['value']) {
case 'eco-choice':
$brands->andWhere('b.ecoBrand = :ecoBrand')
->setParameter('ecoBrand', 1);
break;
case 'made-france':
$brands->andWhere('b.ofg = :ofg')
->setParameter('ofg', 1);
break;
default:
$paramName = 'catId_' . $index;
// On ajoute au groupe de conditions les catégories sélectionnées
$defaultConditions->add(
$brands->expr()->orX(
$brands->expr()->andX(
'c.id = :' . $paramName,
'mme.exceptionCat IS NULL' // Exclure les marques exceptionnelles
),
'mme.exceptionCat = :' . $paramName . '_exception'
)
);
$brands->setParameter($paramName, $filter['id']);
$brands->setParameter($paramName . '_exception', $filter['id']);
break;
}
}
}
// Appliquer les autres filtres seulement s'ils existent
if ($defaultConditions->count() > 0) {
$brands->andWhere($defaultConditions);
}
}
// Filtre par magasin
$brands->andWhere('mm.magasin = :mag')
->andWhere('mm.active = 1')
->setParameter('mag', $magasin);
// Gestion de la pagination
$brands->setFirstResult($offset);
$brands->setMaxResults($limit);
$brands->groupBy('b.id');
// Si un ordre est demandé (par exemple, ordre alphabétique)
if ($order != "default") {
$brands->orderBy('b.name', 'ASC');
} else {
$brands->orderBy('mm.sort', 'ASC');
}
$result = $brands->getQuery()->getResult();
return $result;
}
public function getAllBrands($magasin, $limit = 12, $page = 1, $sort)
{
if ($page == 1) {
$page = 0;
}
$brands = $this->createQueryBuilder('mb')
->leftJoin('mb.marque', 'm')
->where('mb.magasin = :mag')
->andWhere('mb.active = 1')
->setParameter('mag', $magasin)
->setFirstResult($page)
->setMaxResults($limit);
if ($sort != "default") {
$brands->orderBy('m.name', 'ASC');
} else {
$brands->orderBy('mb.sort', 'ASC');
}
return $brands->getQuery()->getResult();
}
}