<?php
namespace Acme\SudcmsBundle\Controller\Front;
use Acme\SudcmsBundle\Entity\EcoProducts;
use Acme\SudcmsBundle\Entity\EcoProductsReferences;
use Acme\SudcmsBundle\Entity\Site;
use Acme\SudcmsBundle\Repository\ActualiteRepository;
use Acme\SudcmsBundle\Repository\AutopromoRepository;
use Acme\SudcmsBundle\Repository\CatalogRepository;
use Acme\SudcmsBundle\Repository\EcoProductsRepository;
use Acme\SudcmsBundle\Repository\FlashMessageRepository;
use Acme\SudcmsBundle\Repository\FormAdressesRepository;
use Acme\SudcmsBundle\Repository\SiteReferencementRepository;
use Acme\SudcmsBundle\Repository\SiteRepository;
use Acme\SudcmsBundle\Service\BrevoService;
use Acme\SudcmsBundle\Service\MailerService;
use Acme\SudcmsBundle\Service\OtideaUtils;
use Brevo\Client\Api\TransactionalEmailsApi;
use Brevo\Client\ApiException;
use Brevo\Client\Configuration;
use Brevo\Client\Model\SendSmtpEmail;
use Doctrine\ORM\EntityManagerInterface;
use GuzzleHttp\Client;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\UrlPackage;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
use const SITE_NAME;
class FrontHomeController extends AbstractController {
private $twig = false;
private $session;
public function __construct(private EntityManagerInterface $entityManager) {
$this->session = new Session();
}
#[Route(path: '/', name: 'front_home')]
public function index(Request $request, Environment $twig,
AutopromoRepository $autopromoRepository, OtideaUtils $otideaUtils,
FlashMessageRepository $flashMessage, SiteReferencementRepository $repoAccRef,
EcoProductsRepository $repoProd, CatalogRepository $repoCatalog,
ActualiteRepository $repoActu, SiteRepository $siteRepository) {
$this->twig = $twig;
// use to delete old data in ContactHistorique for ex.
$otideaUtils->deleteOldData();
$accRef = $repoAccRef->findOneBy(['site_lang' => $request->getLocale(), 'site_uid' => CURRENT_SITE_ID]);
if (is_null($accRef)) {
$accRef = $repoAccRef->findOneBy(['site_uid' => $this->session->get('admin_site_uid'),
'site_lang' => 'fr']);
}
//autopromo
$tabPromoZone1 = $tabPromoZone2 = $tabPromoZone3 = null;
$autoPromos = $autopromoRepository->findBy(array("isOnline" => 1, 'siteLang' => $request->getLocale(),
'siteUid' => CURRENT_SITE_ID),
array('autopromoOrder' => 'ASC'));
$productsBestSellers = $repoProd->findBestSellerHome();
$productsNews = $repoProd->findNouveauteHome();
foreach ($autoPromos as $auto) {
if ($auto->getSection() == 1)
$tabPromoZone1[] = $auto;
elseif ($auto->getSection() == 2)
$tabPromoZone2[] = $auto;
elseif ($auto->getSection() == 3)
$tabPromoZone3[] = $auto;
}
$verifVideo = $this->verifVideo();
$flashInfo = $flashMessage->findBy(array('flash_enligne' => 1, 'site_lang' => $request->getLocale(),
'site_uid' => CURRENT_SITE_ID), array('flash_ordre' => 'ASC'));
$limitActu = $siteRepository->findOneBy(['id' => CURRENT_SITE_ID]);
$tabPromoZone3_1 = [];
if ($tabPromoZone3 != null && sizeof($tabPromoZone3) > 0) {
$tabPromoZone3_1 = $tabPromoZone3[0];
}
$sellCatalog = $repoCatalog->findByCatAllCatalog(['site_lang' => $request->getLocale(),
'site_uid' => CURRENT_SITE_ID, 'categorie' => 0,
'isSell' => 1], ['cat_date_publication' => 'DESC']);
$rentCatalog = $repoCatalog->findByCatAllCatalog(['site_lang' => $request->getLocale(),
'site_uid' => CURRENT_SITE_ID, 'categorie' => 0,
'isSell' => 2], ['cat_date_publication' => 'DESC']);
return $this->render('front/front_home/index.html.twig', [
'autoPromoList' => $autoPromos,
'sellCatalog' => $sellCatalog,
'rentCatalog' => $rentCatalog,
'tabPromoZone1' => $tabPromoZone1,
'tabPromoZone2' => $tabPromoZone2,
'tabPromoZone3' => $tabPromoZone3_1,
'actualites' => $repoActu->findByCatAllActu(['site_lang' => $request->getLocale(),
'site_uid' => CURRENT_SITE_ID,
'limit' => $limitActu->getNbNewsHomePage(),
'categorie' => 0]),
'flashInfo' => $flashInfo,
'verifVideo' => $verifVideo,
'accRef' => $accRef,
'productsNews' => $productsNews,
'productsBestSellers' => $productsBestSellers,
'isHome' => true,
]);
}
public function verifVideo() {
$exist = false;
$directory[] = $this->getParameter("upload_directory_" . CURRENT_SITE_ID) . '/video_accueil';
foreach ($directory as $dir) {
if (!file_exists($dir)) {
mkdir($dir, 755);
}
}
// $allFiles = scandir($this->getParameter("upload_directory") . '/video_accueil');
$allFiles = scandir($this->getParameter("upload_directory_" . CURRENT_SITE_ID) . '/video_accueil');
$files = array_diff($allFiles, array('.', '..'));
foreach ($files as $file) {
if ($file == 'home-loop.mp4') {
$exist = true;
}
}
return $exist;
}
public function emailExemple(MailerService $mailerService, FormAdressesRepository $adressesRepository) {
$files = array();
$mailerService->fromEmail = "no-reply@domaine.fr";
$mailerService->fromName = SITE_NAME;
$mailerService->subject = "Validation de votre commande";
$allRecipients = [];
$recipients = $adressesRepository->findBy(["form" => "contact"]);
if (sizeof($recipients) > 0) {
foreach ($recipients as $element) {
$allRecipients[] = [
"nom" => $element->getNom(),
"prenom" => $element->getPrenom(),
"email" => $element->getEmail(),
"type" => $element->getType()
];
}
}
$htmlContent = $this->renderView('layouts_emails/validation_commande.html.twig', [
'articles' => '',
]);
$mailerService->sendMail($allRecipients, $htmlContent, $files);
}
}