<?php
namespace App\Controller\MarketPlace;
use App\Entity\Articles\Article;
use App\Entity\Articles\ArticleCategorie;
use App\Entity\Articles\Categorie;
use App\Entity\MarketPlace\ArticleMarketPlace;
use App\Entity\MarketPlace\CategorieErpCategorieMarketPlace;
use App\Entity\MarketPlace\CompteMarketPlace;
use App\Entity\MarketPlace\MarketPlace;
use App\Form\MarketPlace\AssocierCategorieErpCategorieMarketPlaceType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CategorieErpCategorieMarketPlaceController extends AbstractController
{
/**
* @Route("/associer-categorie-marketplace/{categorie}/{comptemarketplace}", name="dtc_associer_categorie_market_place")
*/
public function associerAction(Request $request, Categorie $categorie, CompteMarketPlace $comptemarketplace, EntityManagerInterface $em, TranslatorInterface $translator,
ValidatorInterface $validator
) {
$user = $this->getUser();
$errors = [];
$repo_CategorieErpCategorieMarketPlace = $em->getRepository(CategorieErpCategorieMarketPlace::class);
$repo_categorie = $em->getRepository(Categorie::class);
$categorieErpCategorieMarketPlace_obj = $repo_CategorieErpCategorieMarketPlace->findOneBy(["categorieErp" => $categorie, "compteMarketPlace" => $comptemarketplace]);
if ( ! is_object($categorieErpCategorieMarketPlace_obj)) {
$categorieErpCategorieMarketPlace_obj = new CategorieErpCategorieMarketPlace;
$categorieErpCategorieMarketPlace_obj->setCategorieErp($categorie);
$categorieErpCategorieMarketPlace_obj->setCompteMarketPlace($comptemarketplace);
}
$form = $this->createForm(AssocierCategorieErpCategorieMarketPlaceType::class, $categorieErpCategorieMarketPlace_obj);
$get = $request->query->all();
$titre_modal = $translator->trans("Associer catégorie");
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
$type_json = "";
if ($form->isValid()) {
$post = $request->request->all();
$valeur = "";
if (array_key_exists('categories', $post) && count($post["categories"]) > 0) {
$cat_obj = $repo_categorie->find($post["categories"][0]);
if (is_object($cat_obj)) {
$categorieErpCategorieMarketPlace_obj->setCategorieMarketPlace($cat_obj);
$em->persist($categorieErpCategorieMarketPlace_obj);
$em->flush();
$valeur = $cat_obj->getLibelle();
}
} else {
$em->remove($categorieErpCategorieMarketPlace_obj);
$em->flush();
}
return new JsonResponse(
[
'rendu' => '',
'valide' => '1',
'url' => '',
'valeur' => $valeur,
'type' => 'associerCategorieMarketPlace',
'categorie' => $categorie->getId(),
'comptemarketplace' => $comptemarketplace->getId(),
]
);
} else {
$errors = $validator->validate($categorieErpCategorieMarketPlace_obj);
$rendu = $this->renderView(
'MarketPlace/CategorieErpCategorieMarketPlace/associer.html.twig',
['form' => $form->createView(), 'errors' => $errors, 'comptemarketplace' => $comptemarketplace]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
$rendu = $this->renderView(
'MarketPlace/CategorieErpCategorieMarketPlace/associer.html.twig',
['form' => $form->createView(), 'type' => '', 'errors' => $errors, 'comptemarketplace' => $comptemarketplace]
);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
/**
* @Route("", name="")
*/
public function listerCategorieErpAjaxAction(Request $request, $article = null, $mode = "", $mpId = '', $compteMarketPlace = '', $amp = '', EntityManagerInterface $em)
{
$get = $request->query->all();
$type = "";
$s = "";
if (is_array($get) && array_key_exists('mpid', $get)) {
$mpId = $get["mpid"];
$s = $get["sc"];
$article = $get["article"];
$cmp_id = $get["cmp"];
$amp = $get["amp"];
$mode = $get["mode"];
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
//$articleMarketPlace = $repo_article_market_place->find($amp_id);
$repo_compte_market_place = $em->getRepository(CompteMarketPlace::class);
$compteMarketPlace = $repo_compte_market_place->find($cmp_id);
$type = "recherche";
}
//echo "test".$mode;
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
$repo_market_place = $em->getRepository(MarketPlace::class);
$marketPlace = $repo_market_place->find($mpId);
//echo $marketPlace->getLibelle();
$repo_article = $em->getRepository(Article::class);
$article = $repo_article->find($article);
$repo_categorie = $em->getRepository(Categorie::class);
$repo_article_categorie = $em->getRepository(ArticleCategorie::class);
$nbCategoriesEnfant = [];
$articleCategories = [];
if (is_object($compteMarketPlace)) {
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
//$article = $repo_article_market_place->findOneBy(array("article"=>$article,"compteMarketPlace"=>$compteMarketPlace));
$article = $repo_article_market_place->find($amp);
}
$articleMarketPlace = $repo_article_market_place->find($amp);
if (is_object($articleMarketPlace)) {
//echo "AAAAAAAAAAAAA";
//$categoriesArticle = $repo_article_categorie->findByArticle($articleId);
if (count($articleMarketPlace->getArticleCategorie()) > 0) {
foreach ($articleMarketPlace->getArticleCategorie() as $categorieArticle) {
//echo "<div>C ".$categorieArticle->getCategorie()->getLibelle()."</div>";
$boucler = true;
$articleCategories[] = $categorieArticle->getCategorie()->getId();
if (is_object($categorieArticle->getCategorie()->getCategorieParent()) and $categorieArticle->getCategorie()->getCategorieParent()->getId() > 1) {
$parent = $categorieArticle->getCategorie()->getCategorieParent();
while ($boucler) {
if ( ! array_key_exists($parent->getId(), $nbCategoriesEnfant)) {
$nbCategoriesEnfant[$parent->getId()] = 1;
} else {
$nbCategoriesEnfant[$parent->getId()]++;
}
if (is_object($parent->getCategorieParent()) and $parent->getCategorieParent()->getId() > 1) {
$parent = $parent->getCategorieParent();
} else {
$boucler = false;
}
}
}
}
}
} elseif (is_object($article)) {
//$categoriesArticle = $repo_article_categorie->findByArticle($articleId);
if (count($article->getArticleCategorie()) > 0) {
foreach ($article->getArticleCategorie() as $categorieArticle) {
//echo "<div>C ".$categorieArticle->getCategorie()->getLibelle()."</div>";
$boucler = true;
$articleCategories[] = $categorieArticle->getCategorie()->getId();
if (is_object($categorieArticle->getCategorie()->getCategorieParent()) and $categorieArticle->getCategorie()->getCategorieParent()->getId() > 1) {
$parent = $categorieArticle->getCategorie()->getCategorieParent();
while ($boucler) {
if ( ! array_key_exists($parent->getId(), $nbCategoriesEnfant)) {
$nbCategoriesEnfant[$parent->getId()] = 1;
} else {
$nbCategoriesEnfant[$parent->getId()]++;
}
if (is_object($parent->getCategorieParent()) and $parent->getCategorieParent()->getId() > 1) {
$parent = $parent->getCategorieParent();
} else {
$boucler = false;
}
}
}
}
}
}
$categories = $repo_categorie->getCategorieMarketPlace($marketPlace);
//return $this->render('Articles/Categorie/lister-dossier-ajax.html.twig', array(
return $this->render('MarketPlace/CategorieErpCategorieMarketPlace/lister-dossier-categorie-marketplace-ajax.html.twig', [
'nbCategoriesEnfant' => $nbCategoriesEnfant,
'categories' => $categories,
'article' => $article,
'categoriesArticle' => $articleCategories,
'marketPlace' => $marketPlace,
'compteMarketPlace' => $compteMarketPlace,
'articleMarketPlace' => $articleMarketPlace,
'type' => $type,
'mode' => $mode,
]);
}
/**
* @Route("/associer-categorie-marketplace/enfants/{id}", name="dtc_associer_categorie_market_placelister_enfants")
*/
public function listerCategorieDossierEnfantsAjaxAction(Request $request, Categorie $categorie, EntityManagerInterface $em)
{
//echo $categorie->getId();
$articleMarketPlaceId = "";
$repo_categorie = $em->getRepository(Categorie::class);
$repo_article = $em->getRepository(Article::class);
$repo_article_categorie = $em->getRepository(ArticleCategorie::class);
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
$repo_market_place = $em->getRepository(MarketPlace::class);
$categories = $repo_categorie->findBy(["categorieParent" => $categorie]);
$type = $request->query->get('type');
$mode = $request->query->get('mode');
$niveau = intval($request->query->get('niveau')) + 1;
$articleId = intval($request->query->get('article'));
$marketPlaceId = intval($request->query->get('marketPlaceId'));
$typecategorie = $request->query->get('typecategorie');
$article = "";
//echo $mode." aid:".$articleId;
if ($typecategorie != "erp" && $mode != "catArtMp") {
$article = $repo_article_market_place->find($articleId);
} else {
$article = $repo_article->find($articleId);
}
//echo $article->getId();
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
$repo_market_place = $em->getRepository(MarketPlace::class);
$articleMarketPlace = "";
if ($marketPlaceId != "" && $articleId != "" && $mode != "catArtMp") {
$repo_article_market_place = $em->getRepository(ArticleMarketPlace::class);
//$article = $repo_article_market_place->findOneBy(array("article"=>$article,"compteMarketPlace"=>$compteMarketPlace));
$article = $repo_article_market_place->find($articleId);
$articleMarketPlace = $repo_article_market_place->find($articleId);
$articleMarketPlaceId = $articleMarketPlace->getId();
}
$articleCategories = [];
$nbCategoriesEnfant = [];
if (is_object($article)) {
//$categoriesArticle = $repo_article_categorie->findByArticle($articleId);
foreach ($article->getArticleCategorie() as $categorieArticle) {
$boucler = true;
$articleCategories[] = $categorieArticle->getCategorie()->getId();
//echo "<div>ART ".$article->getId()." CAT ".$categorieArticle->getCategorie()->getId()."</div>";
if (is_object($categorieArticle->getCategorie()->getCategorieParent()) and $categorieArticle->getCategorie()->getCategorieParent()->getId() > 1) {
$parent = $categorieArticle->getCategorie()->getCategorieParent();
while ($boucler) {
if ( ! array_key_exists($parent->getId(), $nbCategoriesEnfant)) {
$nbCategoriesEnfant[$parent->getId()] = 1;
} else {
$nbCategoriesEnfant[$parent->getId()]++;
}
if (is_object($parent->getCategorieParent()) and $parent->getCategorieParent()->getId() > 1) {
$parent = $parent->getCategorieParent();
} else {
$boucler = false;
}
}
}
}
}
//print_r($articleCategories);
//exit;
/*
'nbCategoriesEnfant'=>$nbCategoriesEnfant,
'categories' =>$categories,
'article'=>$article,
'categoriesArticle' => $articleCategories,
'mode' => $mode,
'marketPlace' => $marketPlace,
'compteMarketPlace' => $compteMarketPlace,
'articleMarketPlace' => $articleMarketPlace,
*/
//echo count($categories);
//$rendu = $this->render('Articles/Categorie/lister-dossier-ajax.html.twig', array('nbCategoriesEnfant'=>array(),'categories' =>$categories,'article'=>"", 'categoriesArticle' => "","marketPlace"=>""));
//$rendu = $this->renderView('Articles/Categorie/lister-dossier-ligne-ajax_1.html.twig',
$rendu = $this->renderView(
'MarketPlace/CategorieErpCategorieMarketPlace/lister-dossier-ligne-ajax_1.html.twig',
[
'nbCategoriesEnfant' => $nbCategoriesEnfant,
'niveau' => $niveau,
'categories' => $categories,
'ajax' => '1',
'mode' => $mode,
'parents' => '',
'parent' => $categorie->getId(),
'categorie' => $categorie,
'article' => $article,
'categoriesArticle' => $articleCategories,
"marketPlace" => "",
"marketPlaceId" => $marketPlaceId,
"articleMarketPlace" => $articleMarketPlace,
"articleMarketPlaceId" => $articleMarketPlaceId,
"type" => $type,
"typecategorie" => $typecategorie,
]
);
return new JsonResponse(['rendu' => $rendu]);
}
}