<?php
namespace App\Service;
use App\Entity\Category;
use App\Entity\Company;
use App\Entity\Declination;
use App\Entity\Delivery;
use App\Entity\Document;
use App\Repository\CompanyRepository;
use App\Repository\DocumentRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Twig\Environment;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
class GlobalVariables
{
// Ici les variables gobales à utiliser dans les vues (twig)
private $em;
private $router;
private $user;
private $templating;
protected ContainerBagInterface $params;
public function __construct(
EntityManagerInterface $em,
RouterInterface $router,
Security $user,
Environment $templating,
ContainerBagInterface $params
)
{
$this->em = $em;
$this->router = $router;
$this->user = $user;
$this->templating = $templating;
$this->params = $params;
}
// Récupérer les catégories
public function getCountDocumentByType($type,$status)
{
$nb = $this->em->getRepository( Document::class)->countDocuments($status, NULL, NULL, 'All', $type);
return $nb;
}
// Récupérer les catégories
public function getCategories()
{
$categories = $this->em->getRepository(Category::class)->findBy(['parent' => NULL,'isActive'=>'1'],['order'=>'ASC']);
return $categories;
}
public function getCompany()
{
$company = $this->em->getRepository(Company::class)->getCompanyInfo();
return $company;
}
// Récupérer les tailles disponibles par catégories
public function getTailles($categories = [], $newProducts = false, $promoProducts = false, $searchProduct = false)
{
// Récupérer les tailles
$declination = $this->em->getRepository(Declination::class)->find(1);
$query = $this->em->createQueryBuilder();
$query->add('select', 'v.id,v.name')
->from('App\Entity\ValueDeclination', 'v')
//->join('App\Entity\Declination', 'd', 'with', 'v.declination=d')
->join('App\Entity\GroupDeclinationValue', 'g', 'with', 'v= g.value')
->join('App\Entity\ProduitDeclinationValue', 'p', 'with', 'g.produitDeclination=p')
->join('App\Entity\Produit', 's', 'with', 'p.produit=s')
->where('g.declination = :declination')
->andWhere('s.deletedAt is null')
->andWhere('s.showInWebSite = 1');
// Tailles par catégorie
if ($categories) {
//$category = $this->em->getRepository(Category::class)->find($idCategory);
$query->andwhere('s.categories in (:category)')
->setParameter('category', $categories);
}
// Tailles pour nouveautés
if ($newProducts) {
// On va récupérer les produits crées jusqu'à 3 mois
$date = new \DateTime('now');
$date->modify('-90 day');
$query->andwhere('s.createdAt >= :date')
->setParameter('date', $date);
}
// Tailles pour promo
if ($promoProducts) {
$date = new \DateTime('now');
$query->join('App\Entity\Promotion', 'c', 'with', 's.promotion=c')
->andwhere('s.promotion is not null')
->andWhere('c.startAt <= :date')
->andWhere('c.endAt >= :date')
->setParameter('date', $date);
}
// Tailles pour produits recherchés
if ($searchProduct) {
$query->andwhere('s.name like :search OR s.description like :search OR s.reference like :search')
->setParameter('search', '%' . $searchProduct . '%');
}
$tailles = $query->setParameter('declination', $declination)
->distinct()
->orderBy('v.name', 'ASC')
->getQuery();
$taillesFilter = $tailles->getResult();
// Aucun filtre n'est sélectionné par défaut
foreach ($taillesFilter as $key => $taille) {
$taillesFilter[$key]['selected'] = false;
}
return $taillesFilter;
}
// Récupérer les couleurs par catégories
public function getCouleurs($categories = [], $newProducts = false, $promoProducts = false, $searchProduct = false)
{
// Récupérer les couleurs
$declination = $this->em->getRepository(Declination::class)->find(2);
$query = $this->em->createQueryBuilder();
$query->add('select', 'case when IDENTITY(v.parent) is null then v.id else IDENTITY(v.parent) end as id,
case when IDENTITY(v.parent) is null then v.name else d.name end as name,
case when IDENTITY(v.parent) is null then v.code else d.code end as code')
->from('App\Entity\ValueDeclination', 'v')
->leftJoin('v.parent', 'd', 'with', 'v.parent=d')
->join('App\Entity\GroupDeclinationValue', 'g', 'with', 'v= g.value')
->join('App\Entity\ProduitDeclinationValue', 'p', 'with', 'g.produitDeclination=p')
->join('App\Entity\Produit', 's', 'with', 'p.produit=s')
->where('g.declination = :declination')
->andWhere('s.deletedAt is null')
->andWhere('s.showInWebSite = 1')
->groupBy('v.id');
// Couleurs par catégorie
if ($categories) {
//$category = $this->em->getRepository(Category::class)->find($idCategory);
$query->andWhere('s.categories in (:category)')
->setParameter('category', $categories);
}
// Couleurs pour nouveautés
if ($newProducts) {
// On va récupérer les produits crées jusqu'à 3 mois
$date = new \DateTime('now');
$date->modify('-90 day');
$query->andwhere('s.createdAt >= :date')
->setParameter('date', $date);
}
// Couleurs pour promo
if ($promoProducts) {
$date = new \DateTime('now');
$query->join('App\Entity\Promotion', 'c', 'with', 's.promotion=c')
->andwhere('s.promotion is not null')
->andWhere('c.startAt <= :date')
->andWhere('c.endAt >= :date')
->setParameter('date', $date);
}
// Couleurs pour produit recherchés
if ($searchProduct) {
$query->andwhere('s.name like :search OR s.description like :search OR s.reference like :search')
->setParameter('search', '%' . $searchProduct . '%');
}
$couleurs = $query->setParameter('declination', $declination)
->distinct()
->orderBy('v.name', 'ASC')
->getQuery();
$couleursFilter = $couleurs->getResult();
// Aucun filtre n'est sélectionné par défaut
foreach ($couleursFilter as $key => $couleur) {
$couleursFilter[$key]['selected'] = false;
}
return $couleursFilter;
}
// Fonction de redirection vers la page login quand la session est terminée pour les api
public function timeout()
{
if (!$this->user->getUser()) {
return false;
}
return true;
//return new RedirectResponse($this->router->generate('home'));
}
// Fonction pour envoyer email de commande
public function orderEmail(Document $document): ?bool
{
/*$sender = 'lebureautest@gmail.com';
$password = 'yneldaxyqcokzokj';
$username = 'lebureautest';
$smtpServer = 'smtp.gmail.com';
$port = '587';*/
// Create a Transport object mailer_dsn
$transport = Transport::fromDsn($this->params->get('app.mailer_dsn'));
// Create a Mailer object
$mailer = new Mailer($transport);
// Create an Email object
//$email = (new Email());
$email = new TemplatedEmail();
// Set the "From address"
$email->from($this->params->get('app.mailer_mail'));
// Set the "From address"
$email->to($document->getClient()->getEmail());
// Set a "subject"
$email->subject('Commande SUNSHINE-ELEGANCE #' . $document->getInternalNbr());
// Set the plain-text "Body"
// $email->text('This is the plain text body of the message.\nThanks,\nAdmin');
// Set HTML "Body"
//$email->html('This is the HTML version of the message.<br>Example of inline image:<br><img src="cid:nature" width="200" height="200"><br>Thanks,<br>Admin');
$email->htmlTemplate('front/mail/order.html.twig', 'text/html');
$email->context(['document' => $document, 'max' => 200]);
// Génération de l'email
$renderer = new BodyRenderer($this->templating);
$renderer->render($email);
// Add an "Attachment"
// $email->attachFromPath('/path/to/example.txt');
// Add an "Image"
// $email->embed(fopen('/path/to/mailor.jpg', 'r'), 'nature');
// Send the message
$mailer->send($email);
return true;
}
function getCategoryUrlType(){
return isset($_ENV['CATEGORY_URL_TYPE']) ? $_ENV['CATEGORY_URL_TYPE'] : 0;
}
}