src/Service/GlobalVariables.php line 122

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Category;
  4. use App\Entity\Company;
  5. use App\Entity\Declination;
  6. use App\Entity\Delivery;
  7. use App\Entity\Document;
  8. use App\Repository\CompanyRepository;
  9. use App\Repository\DocumentRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Mailer\Transport;
  15. use Symfony\Component\Mailer\Mailer;
  16. use Symfony\Component\Mime\Email;
  17. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  18. use Twig\Environment;
  19. use Symfony\Bridge\Twig\Mime\BodyRenderer;
  20. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  21. class GlobalVariables
  22. {
  23.     // Ici les variables gobales à utiliser dans les vues (twig)
  24.     private $em;
  25.     private $router;
  26.     private $user;
  27.     private $templating;
  28.     protected ContainerBagInterface $params;
  29.     public function __construct(
  30.         EntityManagerInterface $em,
  31.         RouterInterface        $router,
  32.         Security               $user,
  33.         Environment            $templating,
  34.         ContainerBagInterface  $params
  35.     )
  36.     {
  37.         $this->em $em;
  38.         $this->router $router;
  39.         $this->user $user;
  40.         $this->templating $templating;
  41.         $this->params $params;
  42.     }
  43.     // Récupérer les catégories
  44.     public function getCountDocumentByType($type,$status)
  45.     {
  46.         $nb $this->em->getRepositoryDocument::class)->countDocuments($statusNULLNULL'All'$type);
  47.         return $nb;
  48.     }
  49.     // Récupérer les catégories
  50.     public function getCategories()
  51.     {
  52.         $categories $this->em->getRepository(Category::class)->findBy(['parent' => NULL,'isActive'=>'1'],['order'=>'ASC']);
  53.         return $categories;
  54.     }
  55.     public function getCompany()
  56.     {
  57.         $company $this->em->getRepository(Company::class)->getCompanyInfo();
  58.         return $company;
  59.     }
  60.     // Récupérer les tailles disponibles par catégories
  61.     public function getTailles($categories = [], $newProducts false$promoProducts false$searchProduct false)
  62.     {
  63.         // Récupérer les tailles
  64.         $declination $this->em->getRepository(Declination::class)->find(1);
  65.         $query $this->em->createQueryBuilder();
  66.         $query->add('select''v.id,v.name')
  67.             ->from('App\Entity\ValueDeclination''v')
  68.             //->join('App\Entity\Declination', 'd', 'with', 'v.declination=d')
  69.             ->join('App\Entity\GroupDeclinationValue''g''with''v= g.value')
  70.             ->join('App\Entity\ProduitDeclinationValue''p''with''g.produitDeclination=p')
  71.             ->join('App\Entity\Produit''s''with''p.produit=s')
  72.             ->where('g.declination = :declination')
  73.             ->andWhere('s.deletedAt is null')
  74.             ->andWhere('s.showInWebSite = 1');
  75.         // Tailles par catégorie
  76.         if ($categories) {
  77.             //$category = $this->em->getRepository(Category::class)->find($idCategory);
  78.             $query->andwhere('s.categories in (:category)')
  79.                 ->setParameter('category'$categories);
  80.         }
  81.         // Tailles pour nouveautés
  82.         if ($newProducts) {
  83.             // On va récupérer les produits crées jusqu'à 3 mois
  84.             $date = new \DateTime('now');
  85.             $date->modify('-90 day');
  86.             $query->andwhere('s.createdAt >= :date')
  87.                 ->setParameter('date'$date);
  88.         }
  89.         // Tailles pour promo
  90.         if ($promoProducts) {
  91.             $date = new \DateTime('now');
  92.             $query->join('App\Entity\Promotion''c''with''s.promotion=c')
  93.                 ->andwhere('s.promotion is not null')
  94.                 ->andWhere('c.startAt <= :date')
  95.                 ->andWhere('c.endAt >= :date')
  96.                 ->setParameter('date'$date);
  97.         }
  98.         // Tailles pour produits recherchés
  99.         if ($searchProduct) {
  100.             $query->andwhere('s.name like :search OR s.description like :search OR s.reference like :search')
  101.                 ->setParameter('search''%' $searchProduct '%');
  102.         }
  103.         $tailles $query->setParameter('declination'$declination)
  104.             ->distinct()
  105.             ->orderBy('v.name''ASC')
  106.             ->getQuery();
  107.         $taillesFilter $tailles->getResult();
  108.         // Aucun filtre n'est sélectionné par défaut
  109.         foreach ($taillesFilter as $key => $taille) {
  110.             $taillesFilter[$key]['selected'] = false;
  111.         }
  112.         return $taillesFilter;
  113.     }
  114.     // Récupérer les couleurs par catégories
  115.     public function getCouleurs($categories = [], $newProducts false$promoProducts false$searchProduct false)
  116.     {
  117.         // Récupérer les couleurs
  118.         $declination $this->em->getRepository(Declination::class)->find(2);
  119.         $query $this->em->createQueryBuilder();
  120.         $query->add('select''case when IDENTITY(v.parent) is null then v.id else IDENTITY(v.parent) end as id,
  121.         case when IDENTITY(v.parent) is null then v.name else d.name end as name,
  122.         case when IDENTITY(v.parent) is null then v.code else d.code end as code')
  123.             ->from('App\Entity\ValueDeclination''v')
  124.             ->leftJoin('v.parent''d''with''v.parent=d')
  125.             ->join('App\Entity\GroupDeclinationValue''g''with''v= g.value')
  126.             ->join('App\Entity\ProduitDeclinationValue''p''with''g.produitDeclination=p')
  127.             ->join('App\Entity\Produit''s''with''p.produit=s')
  128.             ->where('g.declination = :declination')
  129.             ->andWhere('s.deletedAt is null')
  130.             ->andWhere('s.showInWebSite = 1')
  131.             ->groupBy('v.id');
  132.         // Couleurs par catégorie
  133.         if ($categories) {
  134.             //$category = $this->em->getRepository(Category::class)->find($idCategory);
  135.             $query->andWhere('s.categories in (:category)')
  136.                 ->setParameter('category'$categories);
  137.         }
  138.         // Couleurs pour nouveautés
  139.         if ($newProducts) {
  140.             // On va récupérer les produits crées jusqu'à 3 mois
  141.             $date = new \DateTime('now');
  142.             $date->modify('-90 day');
  143.             $query->andwhere('s.createdAt >= :date')
  144.                 ->setParameter('date'$date);
  145.         }
  146.         // Couleurs pour promo
  147.         if ($promoProducts) {
  148.             $date = new \DateTime('now');
  149.             $query->join('App\Entity\Promotion''c''with''s.promotion=c')
  150.                 ->andwhere('s.promotion is not null')
  151.                 ->andWhere('c.startAt <= :date')
  152.                 ->andWhere('c.endAt >= :date')
  153.                 ->setParameter('date'$date);
  154.         }
  155.         // Couleurs pour produit recherchés
  156.         if ($searchProduct) {
  157.             $query->andwhere('s.name like :search OR s.description like :search OR s.reference like :search')
  158.                 ->setParameter('search''%' $searchProduct '%');
  159.         }
  160.         $couleurs $query->setParameter('declination'$declination)
  161.             ->distinct()
  162.             ->orderBy('v.name''ASC')
  163.             ->getQuery();
  164.         $couleursFilter $couleurs->getResult();
  165.         // Aucun filtre n'est sélectionné par défaut
  166.         foreach ($couleursFilter as $key => $couleur) {
  167.             $couleursFilter[$key]['selected'] = false;
  168.         }
  169.         return $couleursFilter;
  170.     }
  171.     // Fonction de redirection vers la page login quand la session est terminée pour les api
  172.     public function timeout()
  173.     {
  174.         if (!$this->user->getUser()) {
  175.             return false;
  176.         }
  177.         return true;
  178.         //return new RedirectResponse($this->router->generate('home'));
  179.     }
  180.     // Fonction pour envoyer email de commande
  181.     public function orderEmail(Document $document): ?bool
  182.     {
  183.         /*$sender = 'lebureautest@gmail.com';
  184.         $password = 'yneldaxyqcokzokj';
  185.         $username = 'lebureautest';
  186.         $smtpServer = 'smtp.gmail.com';
  187.         $port = '587';*/
  188.         // Create a Transport object  mailer_dsn
  189.         $transport Transport::fromDsn($this->params->get('app.mailer_dsn'));
  190.         // Create a Mailer object
  191.         $mailer = new Mailer($transport);
  192.         // Create an Email object
  193.         //$email = (new Email());
  194.         $email = new TemplatedEmail();
  195.         // Set the "From address"
  196.         $email->from($this->params->get('app.mailer_mail'));
  197.         // Set the "From address"
  198.         $email->to($document->getClient()->getEmail());
  199.         // Set a "subject"
  200.         $email->subject('Commande SUNSHINE-ELEGANCE #' $document->getInternalNbr());
  201.         // Set the plain-text "Body"
  202.         // $email->text('This is the plain text body of the message.\nThanks,\nAdmin');
  203.         // Set HTML "Body"
  204.         //$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');
  205.         $email->htmlTemplate('front/mail/order.html.twig''text/html');
  206.         $email->context(['document' => $document'max' => 200]);
  207.         // Génération de l'email
  208.         $renderer = new BodyRenderer($this->templating);
  209.         $renderer->render($email);
  210.         // Add an "Attachment"
  211.         // $email->attachFromPath('/path/to/example.txt');
  212.         // Add an "Image"
  213.         // $email->embed(fopen('/path/to/mailor.jpg', 'r'), 'nature');
  214.         // Send the message
  215.         $mailer->send($email);
  216.         return true;
  217.     }
  218.     function getCategoryUrlType(){
  219.         return isset($_ENV['CATEGORY_URL_TYPE']) ? $_ENV['CATEGORY_URL_TYPE'] : 0;
  220.     }
  221. }