src/Controller/Controller_Frontoffice/Base/BaseController.php line 109
<?php
namespace App\Controller\Controller_Frontoffice\Base;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Commande;
use App\Entity\Membres;
use App\Entity\Newsletter;
use App\Entity\Panier;
use App\Entity\Token;
use App\Entity\Articles;
use App\Entity\CodePromo;
use App\Entity\HistoriquePrixArticle;
use App\Entity\Images;
use App\Entity\Slider;
use App\Entity\SousCategories;
use App\Entity\Entreprise;
use App\Entity\Categories;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class BaseController extends AbstractController
{
#[Route('/', name: 'index')]
public function indexAction(EntityManagerInterface $entityManager,Request $request): Response
{
$session = new Session();
$panier = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
$n = count($panier);
$typeCompte = "PARTICULIER";
$requestSql = "a.prixPromo > 0 and a.prixPromo < a.prixTTC and a.etat = 1";
if($session->get("connectU") == "OK" && $session->get("userU") != null){
if($session->get("userU")->getCodeType() == 2){
$typeCompte = "PRO";
$requestSql = "a.prixPromo > 0 and a.prixPromo < a.prixProTTC and a.etat = 1";
}
}
// $result = $entityManager->createQuery("SELECT a.id FROM SousCategories a where a.idCategorie = 1 or a.idCategorie = 2")->getScalarResult();
$result = $entityManager->createQueryBuilder()
->select('a.id')
->from('App\Entity\SousCategories', 'a')
->where('a.idCategorie = :cat1 OR a.idCategorie = :cat2')
->setParameter('cat1', 1)
->setParameter('cat2', 2)
->getQuery()
->getScalarResult();
$ids = array_map('current', $result);
$repository = $entityManager->getRepository(Articles::class);
$lf= $repository->findBy(array("idSousCategorie"=>$ids,"etat"=>1),array(),8);
/************Article promos***********/
$repository = $entityManager->getRepository(Categories::class);
$cats = $repository->findAll();
$categories = array();
for($i=0;$i<count($cats);$i++){
$categories[$i]["categorie"] = $cats[$i];
$repository = $entityManager->getRepository(SousCategories::class);
$sousCats = $repository->findByIdCategorie($cats[$i]->getId());
$categories[$i]["sousCategorie"] = $sousCats;
}
$repo = $entityManager->getRepository(Articles::class);
$qb = $repo->createQueryBuilder('a');
$qb->select('a');
$qb->where($requestSql);
$qb->orderBy('a.dateAjout', 'DESC');
$qb->setMaxResults(1);
$ArticlePromo = $qb->getQuery()->getResult();
$query = $entityManager->createQuery(
'SELECT a, c.titre AS titreCat
FROM App\Entity\Articles a
INNER JOIN App\Entity\SousCategories c
WITH a.idSousCategorie = c.id
where '.$requestSql
);
$query->setMaxResults(20);
$ArticlesPromotions = $query->getResult();
/**************************** */
$legumesFruits = array();
$listArticle = array();
$query = $entityManager->createQuery(
'SELECT p
FROM App\Entity\SousCategories p
WHERE p.id in (SELECT a.idSousCategorie
FROM App\Entity\Articles a)'
);
$categorie = $query->getResult();
$query = $entityManager->createQuery(
'SELECT a, c.titre AS titreCat
FROM App\Entity\Articles a
INNER JOIN App\Entity\SousCategories c
WITH a.idSousCategorie = c.id'
);
$articles = $query->getResult();
for($i=0;$i<count($lf);$i++){
$legumesFruits[$i]["article"] = $lf[$i];
$repository = $entityManager->getRepository(SousCategories::class);
$sCat = $repository->findOneById($lf[$i]->getIdSousCategorie());
$legumesFruits[$i]["categorie"] = "--";
$legumesFruits[$i]["title"] = "--";
if($sCat){
$legumesFruits[$i]["categorie"] = $sCat->getTitre();
if($sCat->getIdCategorie() == 1){
$legumesFruits[$i]["title"] = "Légumes";
}
elseif($sCat->getIdCategorie() == 2){
$legumesFruits[$i]["title"] = "Fruits";
}
}
$listArticle[$lf[$i]->getId()] = $lf[$i];
}
$repository = $entityManager->getRepository(SousCategories::class);
$scRes = $repository->findByIdCategorie(4);
$restauration = array();
for($i=0;$i<count($scRes);$i++){
$restauration[$i]["categorie"] = $scRes[$i];
$repository = $entityManager->getRepository(Articles::class);
$rest= $repository->findBy(array("idSousCategorie"=>$scRes[$i]->getId(),"etat"=>1),array(),4);
$restauration[$i]["article"] = $rest;
for($j=0;$j<count($rest);$j++){
$listArticle[$rest[$j]->getId()] = $rest[$j];
}
}
//$result = $entityManager->createQuery("SELECT a.id FROM SousCategories a where a.idCategorie = 3")->getScalarResult();
$result = $entityManager->createQueryBuilder()
->select('a.id')
->from('App\Entity\SousCategories', 'a')
->where('a.idCategorie = :cat1')
->setParameter('cat1', 3)
->getQuery()
->getScalarResult();
$ids = array_map('current', $result);
$repository = $entityManager->getRepository(Articles::class);
$poisson = $repository->findBy(array("idSousCategorie"=>$ids,"etat"=>1),array(),6);
$listPoissons = array();
for($i=0;$i<count($poisson);$i++){
$listPoissons[$i]["article"] = $poisson[$i];
$repository = $entityManager->getRepository(SousCategories::class);
$sCat = $repository->findOneById($poisson[$i]->getIdSousCategorie());
$listPoissons[$i]["categorie"] = "--";
if($sCat){
$listPoissons[$i]["categorie"] = $sCat->getTitre();
}
$listArticle[$poisson[$i]->getId()] = $poisson[$i];
}
$repository = $entityManager->getRepository(Slider::class);
$sliders = $repository->findAll();
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new
JsonEncoder()));
$jsonArticle = $serializer->serialize($listArticle, 'json');
return $this->render('frontoffice_bundle/base/index.html.twig',
array("ArticlePromo"=>$ArticlePromo,"ArticlesPromotions"=>$ArticlesPromotions,"legumesFruits"=>$legumesFruits,"npanier"=>$n,"articles"=>$articles ,"typeCompte"=>$typeCompte,"restauration"=>$restauration,"poissons"=>$listPoissons,"sliders"=>$sliders,"articlesJson"=>$jsonArticle,"categories"=>$categorie,'controller_name' => 'BaseController'));
}
#[Route('/getData')]
public function getCategorie(EntityManagerInterface $entityManager, Request $request): JsonResponse
{
$session = new Session();
$listCat = $entityManager->getRepository(SousCategories::class)->findAll();
$serializer = new Serializer([new GetSetMethodNormalizer()], ['json' => new JsonEncoder()]);
$data = [
"categorie"=>$listCat ,
"panier"=> count($session->get("panier"))
];
$jsonArticle = $serializer->serialize($data, 'json');
return new JsonResponse($jsonArticle, JsonResponse::HTTP_OK, [], true);
}
#[Route('/reset/{tokenString}/', name: 'reset')]
public function resetPasswordAction($tokenString = "NONE" , EntityManagerInterface $entityManager,Request $request): Response
{
$session = new Session();
if($session->get("connectU") == "OK" && $session->get("userU") != null){
return $this->redirect("/monProfil/");
}
if($tokenString == "NONE"){
return $this->Redirect('/account/');
}
$response = "";
$id = $request->get("id");
$repository = $entityManager->getRepository(Token::class);
$token = $repository->findOneByIdMembre($id);
if(!$token) {
return $this->Redirect('/account/');
}
else{
if ($token->getToken() != $tokenString) {
return $this->Redirect('/account/');
}
}
$repository = $entityManager->getRepository(Membres::class);
$u = $repository->findOneById($id);
if(!$u){
return $this->Redirect('/account/');
}
if($request->getMethod() == "POST"){
$pass = $request->get("password1");
$pass2 = $request->get("password2");
if(strlen($pass) < 8){
$response = "Le mot de passe doit comprendre minimum 8 caractères";
}
else{
if($pass != $pass2){
$response = "Les deux mots de passe ne sont pas identiques";
}
else{
$u->setPassword(md5($pass));
$entityManager->flush();
$entityManager->remove($token);
$entityManager->flush();
$response = "OK";
}
}
}
return $this->render('frontoffice_bundle/Base/resetPassword.html.twig', array("response"=>$response));
}
#[Route('/addNewsletter', name: 'addNewsletter')]
public function addNewsletterAction(EntityManagerInterface $entityManager,Request $request): Response
{
$response = "Une erreur s'est produite !";
if($request->getMethod() == "POST"){
$email = $request->get("email");
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$response = "Adresse email invalide";
}
else{
$repository = $entityManager->getRepository(Newsletter::class);
$newsl = $repository->findOneByEmail($email);
if($newsl){
$response = "Vous avez dèja été enregistré";
}
else{
$newsl = new Newsletter();
$newsl->setDate(new \DateTime());
$newsl->setEmail($email);
$entityManager->persist($newsl);
$entityManager->flush();
$response = "OK";
}
}
}
return new Response($response);
}
#[Route('/propos', name: 'propos')]
public function ProposAction(Request $request): Response
{
$session = new Session();
$panier = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
$n = count($panier);
return $this->render('frontoffice_bundle/propos/propos.html.twig', array("npanier"=>$n));
}
}