src/Controller/Controller_Frontoffice/Article/ArticleController.php line 147
<?php
namespace App\Controller\Controller_Frontoffice\Article;
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\Articles;
use App\Entity\Token;
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;;
class ArticleController extends AbstractController
{
#[Route('/products', name: 'tous_produit')]
public function productsAction(PaginatorInterface $paginator,Request $request,SerializerInterface $serializer,EntityManagerInterface $entityManager,)
{
$session = new Session();
$typeCompte = "PARTICULIER";
if ($session->get("connectU") == "OK" && $session->get("userU") != null) {
if ($session->get("userU")->getCodeType() == 2) {
$typeCompte = "PRO";
}
}
$panier = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
$n = count($panier);
$repository = $entityManager->getRepository(Categories::class);
$categories = $repository->findAll();
$repository = $entityManager->getRepository(Articles::class);
$articles = $repository->findBy(array("etat" => 1));
$listArticles = array();
$listArticleJS = array();
for($i=0;$i<count($articles);$i++){
$listArticles[$i]["article"] = $articles[$i];
$repository = $entityManager->getRepository(SousCategories::class);
// $sCat = $repository->findOneById($articles[$i]['idSousCategorie']);
$sCat = $repository->findOneById($articles[$i]->getIdSousCategorie());
$listArticles[$i]["categorie"] = "--";
if($sCat){
$listArticles[$i]["categorie"] = $sCat->getTitre();
}
$listArticleJS[$articles[$i]->getId()] = $articles[$i];
}
$jsonArticle = $serializer->serialize($listArticleJS, 'json');
$pagination = $paginator->paginate(
$listArticles,
$request->query->getInt('page', 1),
6
);
$jsonArticle = $serializer->serialize($listArticleJS, 'json');
return $this->render('frontoffice_bundle/article/tousProducts.html.twig', [
"listArticles" => $listArticles,
"categories" => $categories,
"articles" => $pagination,
"typeCompte" => $typeCompte,
"articlesJson" => $jsonArticle,
"listarticles" => $listArticles,
"npanier"=> $n,
'controller_name' => 'ArticleController'
]);
}
#[Route('/products/{name}/{id}/', name: 'prodsuct')]
#[Route('/products/{name}/{id}/{idSC}/', name: 'product_with_sc')]
public function productAction(PaginatorInterface $paginator,Request $request,SerializerInterface $serializer,EntityManagerInterface $entityManager,)
{
$session = new Session();
$typeCompte = "PARTICULIER";
if ($session->get("connectU") == "OK" && $session->get("userU") != null) {
if ($session->get("userU")->getCodeType() == 2) {
$typeCompte = "PRO";
}
}
$panier = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
$n = count($panier);
$name = $request->attributes->get('name');
$id = $request->attributes->get('id');
$idSC = $request->attributes->get('idSC');
$repository = $entityManager->getRepository(Categories::class);
$cats = $repository->findAll();
$categories = [];
foreach ($cats as $cat) {
$categories[]["categorie"] = $cat;
$repository = $entityManager->getRepository(SousCategories::class);
$sousCats = $repository->findByIdCategorie($cat->getId());
$categories[]["sousCategorie"] = $sousCats;
}
if ($idSC == 0) {
$result = $entityManager->createQuery("SELECT a.id FROM App\Entity\SousCategories a where a.idCategorie = " . $id)->getScalarResult();
$ids = array_map('current', $result);
} else {
$ids = $idSC;
}
$repository = $entityManager->getRepository(Articles::class);
$articles = $repository->findBy(array("idSousCategorie" => $ids, "etat" => 1));
// $listArticles = [];
// $listArticleJS = [];
// foreach ($articles as $article) {
// $listArticles[]["article"] = $article;
// $repository = $entityManager->getRepository(SousCategories::class);
// $sCat = $repository->findOneById($article->getIdSousCategorie());
// $listArticles[]["categorie"] = "--";
// if ($sCat) {
// $listArticles[]["categorie"] = $sCat->getTitre();
// }
// $listArticleJS[$article->getId()] = $article;
// }
$listArticles = array();
$listArticleJS = array();
for($i=0;$i<count($articles);$i++){
$listArticles[$i]["article"] = $articles[$i];
$repository = $entityManager->getRepository(SousCategories::class);
// $sCat = $repository->findOneById($articles[$i]['idSousCategorie']);
$sCat = $repository->findOneById($articles[$i]->getIdSousCategorie());
$listArticles[$i]["categorie"] = "--";
if($sCat){
$listArticles[$i]["categorie"] = $sCat->getTitre();
}
// $listArticleJS[$articles[$i]['id']] = $articles[$i];
$listArticleJS[$articles[$i]->getId()] = $articles[$i];
}
$jsonArticle = $serializer->serialize($listArticleJS, 'json');
$pagination = $paginator->paginate(
$listArticles,
$request->query->getInt('page', 1)/*page number*/,
6/*limit per page*/
);
$jsonArticle = $serializer->serialize($listArticleJS, 'json');
return $this->render('frontoffice_bundle/article/product.html.twig', [
"nom" => $name,
"listArticles" => $listArticles,
"categories" => $categories,
"articles" => $pagination,
"typeCompte" => $typeCompte,
"articlesJson" => $jsonArticle,
"listarticles" => $listArticles,
"npanier"=>$n,
'controller_name' => 'ArticleController'
]);
}
#[Route('/product/{name}/{nameProduct}/{id}/', name: 'detailsproduct')]
public function detailsProductAction($name,$id,$nameProduct, EntityManagerInterface $entityManager,Request $request): Response
{
$session = new Session();
$typeCompte = "PARTICULIER";
if($session->get("connectU") == "OK" && $session->get("userU") != null){
if($session->get("userU")->getCodeType() == 2){
$typeCompte = "PRO";
}
}
$panier = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
$n = count($panier);
$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;
}
$repository = $entityManager->getRepository(Articles::class);
$article = $repository->findOneById($id);
if(!$article){
return $this->redirect("/");
}
$repository = $entityManager->getRepository(SousCategories::class);
$scat = $repository->findOneById($article->getIdSousCategorie());
$repository = $entityManager->getRepository(Images::class);
$images = $repository->findByIdArticle($id);
$repository = $entityManager->getRepository(HistoriquePrixArticle::class);
$historique = $repository->findBy(array("idArticle"=>$id),array("date"=>"DESC"));
$historiqueChart = array();
$index = 0;
for($i=count($historique)-1;$i>=0;$i--){
$historiqueChart[$index] = $historique[$i];
$index++;
if($index > 10){
break;
}
}
$percent = 0;
if(count($historiqueChart) > 2){
$i1 = count($historiqueChart)-1;
$i2 = count($historiqueChart)-2;
$prix1 = 0;
$prix2 = 0;
if($typeCompte == "PARTICULIER"){
$prix1 = $historiqueChart[$i1]->getPrixTTC();
if($historiqueChart[$i1]->getPrixPromo() > 0 && $historiqueChart[$i1]->getPrixPromo() < $historiqueChart[$i1]->getPrixTTC()){
$prix1 = $historiqueChart[$i1]->getPrixPromo();
}
$prix2 = $historiqueChart[$i2]->getPrixTTC();
if($historiqueChart[$i2]->getPrixPromo() > 0 && $historiqueChart[$i2]->getPrixPromo() < $historiqueChart[$i2]->getPrixTTC()){
$prix2 = $historiqueChart[$i2]->getPrixPromo();
}
}else{
$prix1 = $historiqueChart[$i1]->getPrixProTTC();
if($historiqueChart[$i1]->getPrixPromo() > 0 && $historiqueChart[$i1]->getPrixPromo() < $historiqueChart[$i1]->getPrixProTTC()){
$prix1 = $historiqueChart[$i1]->getPrixPromo();
}
$prix2 = $historiqueChart[$i2]->getPrixProTTC();
if($historiqueChart[$i2]->getPrixPromo() > 0 && $historiqueChart[$i2]->getPrixPromo() < $historiqueChart[$i2]->getPrixProTTC()){
$prix2 = $historiqueChart[$i2]->getPrixPromo();
}
}
//----
if($prix2 > 0){
$percent = (($prix1-$prix2)/$prix2)*100;
}
$percent = round($percent, 2);
}
$query = $entityManager->createQuery(
'SELECT c
FROM App\Entity\SousCategories c
INNER JOIN App\Entity\Articles a
WITH a.idSousCategorie = c.id'
);
$categoriesList = $query->getResult();
//----- articles similaires -----//
//$result = $entityManager->createQuery("SELECT a.id FROM BackofficeBundle:SousCategories a where a.idCategorie = ".$scat->getIdCategorie())->getScalarResult();
$result = $entityManager->createQueryBuilder()
->select('a.id')
->from('App\Entity\SousCategories', 'a')
->where('a.idCategorie = :cat1')
->setParameter('cat1', $scat->getIdCategorie())
->getQuery()
->getScalarResult();
$ids = array_map('current', $result);
$connection = $entityManager->getConnection();
// $statement = $connection->prepare("SELECT * FROM App\Entity\Articles where idSousCategorie IN ('".implode("','", $ids)."') AND id !=".$article->getId()." ORDER BY RAND() LIMIT 4");
$query = $entityManager->createQuery('
SELECT a
FROM App\Entity\Articles a
WHERE a.idSousCategorie IN (:ids)
AND a.id != :article_id
')
->setParameter('ids', $ids)
->setParameter('article_id', $article->getId())
->setMaxResults(4);
$articles = $query->getResult();
// $statement->execute();
// $articles = $statement->fetchAll();
//$repository = $entityManager->getRepository(Articles');
//$articles = $repository->findBy(array("idSousCategorie"=>$ids),array(),4);
$listArticles = array();
$listArticleJS = array();
for($i=0;$i<count($articles);$i++){
$listArticles[$i]["article"] = $articles[$i];
$repository = $entityManager->getRepository(SousCategories::class);
// $sCat = $repository->findOneById($articles[$i]['idSousCategorie']);
$sCat = $repository->findOneById($articles[$i]->getIdSousCategorie());
$listArticles[$i]["categorie"] = "--";
if($sCat){
$listArticles[$i]["categorie"] = $sCat->getTitre();
}
// $listArticleJS[$articles[$i]['id']] = $articles[$i];
$listArticleJS[$articles[$i]->getId()] = $articles[$i];
}
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new
JsonEncoder()));
$jsonArticle = $serializer->serialize($listArticleJS, 'json');
//------- mes derniere consultation -----//
/*$old = $session->get("articles");
if(!$old){
$old = array();
}
if(!in_array($article->getId(), $old)){
$new = array();
$index = 0;
$new[$index] = $article->getId();
$index++;
for($i=0;$i<3;$i++){
if($i == count($old)){
break;
}
else{
$new[$index] = $old[$i];
}
$index++;
}
$session->set("articles",$new);
}*/
return $this->render('frontoffice_bundle/article/detailsProduct.html.twig', array("npanier"=>$n,"nom"=>$name,"categoriesList"=>$categoriesList ,"categories"=>$categories,"historiqueChart"=>$historiqueChart,"article"=>$article,"images"=>$images,"scat"=>$scat,"typeCompte"=>$typeCompte,"articles"=>$listArticles,"articlesJson"=>$jsonArticle,"percent"=>$percent,'controller_name' => 'ArticleController'));
}
//------ promo
#[Route('/promotions', name: 'promotions')]
public function promotionsAction(EntityManagerInterface $entityManager,Request $request,PaginatorInterface $paginator): Response
{
$session = new Session();
$name="";
$id=0;
$idSC = 0;
$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";
}
}
$panier = array();
if($session->get("panier")){
$panier = $session->get("panier");
}
$n = count($panier);
$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);
$articles = $qb->getQuery()->getResult();
$listArticles = array();
$listArticleJS = array();
for($i=0;$i<count($articles);$i++){
$listArticles[$i]["article"] = $articles[$i];
$repository = $entityManager->getRepository(SousCategories::class);
$sCat = $repository->findOneById($articles[$i]->getIdSousCategorie());
$listArticles[$i]["categorie"] = "--";
$listArticles[$i]["cat"] = "--";
if($sCat){
$listArticles[$i]["categorie"] = $sCat->getTitre();
$repository = $entityManager->getRepository(Categories::class);
$c = $repository->findOneById($sCat->getIdCategorie());
if($c){
$listArticles[$i]["cat"] = $c->getTitre();
}
}
$listArticleJS[$articles[$i]->getId()] = $articles[$i];
}
$pagination = $paginator->paginate(
$listArticles, // query NOT result
$request->query->getInt('page', 1), // page number
6 // limit per page
);
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new
JsonEncoder()));
$jsonArticle = $serializer->serialize($listArticleJS, 'json');
return $this->render('frontoffice_bundle/article/promotions.html.twig',
array("nom"=>$name,"categories"=>$categories,"articles"=>$pagination,"typeCompte"=>$typeCompte,"articlesJson"=>$jsonArticle,'controller_name' => 'ArticleController',"npanier"=>$n));
}
}