<?php
namespace App\Repository;
use App\Entity\Company;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr\GroupBy;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
/**
* @method Company|null find($id, $lockMode = null, $lockVersion = null)
* @method Company|null findOneBy(array $criteria, array $orderBy = null)
* @method Company[] findAll()
* @method Company[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CompanyRepository extends ServiceEntityRepository
{
private ?LoggerInterface $logger;
public function __construct(
ManagerRegistry $registry,
LoggerInterface $logger
) {
parent::__construct($registry, Company::class);
$this->logger = $logger;
}
public function search_all_companies()
{
$param = 0;
$qb = $this->createQueryBuilder('c');
// Récupération de toutes les entreprises
$qb->where('c.id >= :val')
->setParameter('val', '1');
$query = $qb->getQuery();
return $query->execute();
}
public function getCompanyWithCategoriesAndProducts($id)
{
return $this->createQueryBuilder('p')
->select('p')
->where('p.id = :id')
->leftJoin('p.categories', 'c')
->addSelect('partial c.{id, name}')
->leftJoin('p.products', 'd')
->addSelect('d')
->setParameter('id', $id)
->getQuery()
->getArrayResult();
}
//this is new one , the old functon is just below this one
public function getCompaniesByCatAndSubcat($catId, $subcatId)
{
return $this->createQueryBuilder('q')
->select('q,p,c,s')
->innerjoin('q.products', 'p')
->innerjoin('q.categories', 'c')
->innerjoin('p.subcategories', 's')
->andwhere('s.id = :subcatId')
->setParameter('subcatId', $subcatId)
->andwhere('c.id = :catId')
->setParameter('catId', $catId)
->andwhere('q.activated = 1')
->getQuery();
//->getArrayResult();
}
//this is new one , the old functon is just below this one
public function getCompaniesByCat($catId)
{
return $this->createQueryBuilder('q')
->select('q,p,c,s,sc')
->innerjoin('q.products', 'p')
->innerjoin('q.categories', 'c')
->innerjoin('p.subcategories', 's')
->innerjoin('s.categories', 'sc')
->andwhere('c.id = :catId')
->setParameter('catId', $catId)
->andwhere('q.activated = 1')
->getQuery()
->getArrayResult();
}
//this is new one , the old functon is just below this one
public function getCompaniesDetailsById($id)
{
return $this->createQueryBuilder('q')
->select('q,p,c,s')
->andwhere('q.id = :id')
->setParameter('id', $id)
->innerjoin('q.products', 'p')
->innerjoin('q.categories', 'c')
->innerjoin('p.subcategories', 's')
->andwhere('q.activated = 1')
->getQuery();
//->getArrayResult();
}
public function findLatestCompanies($date)
{
$companies = $this->createQueryBuilder('e')
->select('e.id, e.name, e.image, e.startingdate, e.longtitude, e.latitude, u.inscriptiondate')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = e.id')
->andWhere('u.inscriptiondate >= :date')
->andWhere('u.role = :role')
->groupBy('e.id')
->orderBy('u.inscriptiondate', 'DESC')
->setParameter('date', $date)
->setParameter('role', 'ROLE_COMPANY')
->getQuery()
->getArrayResult();
foreach ($companies as &$company) {
$categories = $this->createQueryBuilder('e')
->select('c.id as category_id, c.name as category_name')
->leftJoin('e.categories', 'c')
->where('e.id = :companyId')
->setParameter('companyId', $company['id'])
->getQuery()
->getArrayResult();
$company['categories'] = $categories;
}
return $companies;
}
public function findTheTwelveLastCompanies()
{
return $this->createQueryBuilder('e')
->select('e.id, e.name, e.description, e.latitude, e.longtitude, u.inscriptiondate')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = e.id')
->andWhere('u.role = :role')
->orderBy('u.inscriptiondate', 'DESC')
->setParameter('role', 'ROLE_COMPANY')
->setMaxResults(12)
->getQuery()
->getArrayResult();
}
// public function getCompaniesByCatAndSubcat($catId, $subcatId)
// {
// return $this->createQueryBuilder('c')
// ->where('c.activated = 1')
// ->join('c.categories', 'ca')
// ->andWhere('ca.id = :catId')
// ->join('ca.subcategories', 's')
// ->andWhere('s.id = :subcatId ')
// ->join('s.products', 'p')
// ->setParameters(['subcatId' => $subcatId, 'catId' => $catId])
// ->orderBy('c.niveau', 'DESC')
// ->getQuery();
// //->getResult();
// }
// /**
// * @return Company[] Returns an array of Company objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
public function findCompaniesByDate($date)
{
return $this->createQueryBuilder('e')
->select('e.id, e.name, e.image, e.latitude, e.longtitude, u.inscriptiondate')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = e.id')
->andWhere('u.inscriptiondate >= :date')
->andWhere('u.role = :role')
->orderBy('u.inscriptiondate', 'DESC')
->setParameter('date', $date)
->setParameter('role', 'ROLE_COMPANY')
->getQuery()
->getArrayResult();
}
public function findCompaniesToday()
{
return $this->createQueryBuilder('e')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = e.id')
->andWhere('CURRENT_DATE() = u.inscriptiondate')
->andWhere('u.role = :role')
->orderBy('u.inscriptiondate', 'DESC')
->setParameter('role', 'ROLE_COMPANY')
->setMaxResults(3)
->getQuery()
->getResult();
}
public function findCompaniesThisMonth()
{
$year = date("y");
$month = date("m");
$fromTime = new \DateTime($year . '-' . $month . '-00');
return $this->createQueryBuilder('e')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = e.id')
->andWhere('u.inscriptiondate > :val')
->andWhere('u.role = :role')
->setParameter('val', $fromTime)
->setParameter('role', 'ROLE_COMPANY')
->orderBy('u.inscriptiondate', 'DESC')
->getQuery()
->getResult();
}
public function findCompaniesSince($old_date)
{
$year = $old_date->format("y");
$month = $old_date->format("m");
$fromTime = new \DateTime($year . '-' . $month . '-00');
return $this->createQueryBuilder('c')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = c.id')
->andWhere('u.inscriptiondate >= :date')
->andWhere('u.role = :role')
->setParameter('date', $fromTime)
->setParameter('role', 'ROLE_COMPANY')
->orderBy('u.inscriptiondate', 'DESC')
->getQuery()
->getResult();
}
public function findCompaniesThisWeek()
{
$fromTime = new \DateTime();
$q = $fromTime->format('y-m-d');
$date = strtotime($q);
$n = date('W', $date);
return $this->createQueryBuilder('e')
->innerJoin('App\Entity\User', 'u', 'WITH', 'u.id = e.id')
->andWhere('WEEK(u.inscriptiondate) = :val')
->andWhere('u.role = :role')
->setParameter('val', $n)
->setParameter('role', 'ROLE_COMPANY')
->orderBy('u.inscriptiondate', 'DESC')
->getQuery()
->getResult();
}
}