<?php
namespace App\Controller\Articles;
use App\Entity\Articles\Article;
use App\Entity\Articles\ArticleCategorie;
use App\Entity\Articles\Categorie;
use App\Entity\MarketPlace\ArticleMarketPlace;
use App\Entity\MarketPlace\CompteMarketPlace;
use App\Entity\MarketPlace\MarketPlace;
use App\Entity\Remises\Remise;
use App\Entity\Remises\RemiseCategorieArticle;
use App\Form\Articles\CategorieModalType;
use App\Form\Articles\CategorieType;
use App\Form\Articles\SupprimerCategorieType;
use App\Library\Datatable\Util\Datatable;
use App\Security\Voter\EntityVoter;
use App\Service\FO\Logo;
use App\Service\Utilisateur\ColonneTableauService;
use Doctrine\ORM\EntityManagerInterface;
use JMS\Serializer\SerializerBuilder;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CategorieController extends AbstractController
{
/**
* @Route("/categorie/nouveau", name="dtc_categorie_ajouter")
*/
public function ajouterAction(Request $request, EntityManagerInterface $em, Logo $logoService, ValidatorInterface $validator, TranslatorInterface $translator)
{
$user = $this->getUser();
$categorie = new Categorie();
$categorie->setUtilisateur($user);
$form = $this->createForm(CategorieType::class, $categorie);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
$tokenP = $request->request->get('tokenPicture');
if ($form->isValid()) {
$em->persist($categorie);
$em->flush();
$logoService->sauvegarder($categorie, 'categorie', $tokenP);
$this->addFlash(
'notice',
$translator->trans('Catégorie ajoutée avec succès !')
);
return $this->redirectToRoute('dtc_categorie_liste');
} else {
$errors = $validator->validate($categorie);
}
}
return $this->render('Articles/Categorie/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors]);
}
/**
* @Route("/categorie/modifier/{id}", name="dtc_categorie_modifier")
*/
public function modifierAction(Request $request, Categorie $categorie, EntityManagerInterface $em, Logo $logoService,
TranslatorInterface $translator, ValidatorInterface $validator, Datatable $datatable
) {
$repo_categorie = $em->getRepository(Categorie::class);
$user = $this->getUser();
$categorie->setUtilisateur($user);
$form = $this->createForm(CategorieType::class, $categorie);
$errors = "";
$errorsSup = "";
$form->handleRequest($request);
//verifier si la categorie existe
$refExisteDeja = $repo_categorie->refExisteDeja($categorie);
if ($refExisteDeja) {
$errorsSup[] = 'Cette référence de catégorie existe déjà!';
}
if ($form->isSubmitted()) {
$droit = $this->isGranted(EntityVoter::UPDATE, Categorie::class);
if ( ! $droit) {
return $this->redirectToRoute('dtc_categorie_modifier', ["id" => $categorie->getId()]);
}
if ($form->isValid() && ! $refExisteDeja) {
$logoService->sauvegarder($categorie, 'categorie');
$logoService->sauvegarder2($categorie, 'categorielogo');
$em->persist($categorie);
$em->flush();
if ($this->getParameter('domaine_prestashop') != '') {
$categorieParent = 1;
if (is_object($categorie->getCategorieParent())) {
$categorieParent = $categorie->getCategorieParent()->getId();
}
@fopen(
'http://'.$this->getParameter('domaine_prestashop').'/67PHEAuaps4P4h3/categories.php?id_category='.$categorie->getId().'&id_category_parent='.$categorieParent,
'r'
);
}
$this->addFlash(
'notice',
$translator->trans('Catégorie sauvegardée avec succès !')
);
return $this->redirectToRoute('dtc_categorie_modifier', ["id" => $categorie->getId()]);
} else {
$errors = $validator->validate($categorie);
}
}
$this->datatableProduits($categorie, $datatable, $translator);
$this->datatableRemises($categorie, $datatable, $translator);
return $this->render('Articles/Categorie/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'errorsSup' => $errorsSup, 'categorie' => $categorie]);
}
/**
* @Route("/categorie/{page}", name="dtc_categorie_liste", defaults={"page": 1})
*/
public function listerAction($page, ColonneTableauService $serviceColonneTableau)
{
$tableau_class_cellule[] = ["className" => "colonne_id", "targets" => [0], "visible" => true, "orderable" => false];
$tableau_class_cellule[] = ["className" => "colonne_id", "targets" => [1], "visible" => $serviceColonneTableau->getColonneUtilisateur(Categorie::class, "id")];
$tableau_class_cellule[] = [
"className" => "colonne_logo",
"targets" => [2],
"visible" => $serviceColonneTableau->getColonneUtilisateur(Categorie::class, "logo"),
];
$tableau_class_cellule[] = [
"className" => "colonne_libelle",
"targets" => [3],
"visible" => $serviceColonneTableau->getColonneUtilisateur(Categorie::class, "libelle"),
];
$tableau_class_cellule[] = [
"className" => "colonne_libelle",
"targets" => [4],
"visible" => $serviceColonneTableau->getColonneUtilisateur(Categorie::class, "parent"),
];
$tableau_class_cellule[] = [
"className" => "colonne_statut",
"targets" => [5],
"visible" => $serviceColonneTableau->getColonneUtilisateur(Categorie::class, "statut"),
];
//$this->datatable();
return $this->render('Articles/Categorie/lister.html.twig', ['name' => 'categorie', 'tableauClassColonne' => $tableau_class_cellule]);
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatable(Datatable $datatable, TranslatorInterface $translator)
{
$datatable->setEntity(Categorie::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
$translator->trans("Logo") => 'x.logo',
$translator->trans("Libellé") => 'x.libelle',
$translator->trans("Parent") => 'p.libelle',
$translator->trans("Statut Web") => 'x.statut',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.categorieParent', 'p', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_categorie_modifier',
],
],
1 => [
'view' => 'FO/DataTable/logo.html.twig',
'params' => [
'edit_route' => 'dtc_categorie_modifier',
],
],
5 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'edit_route' => 'dtc_categorie_modifier',
'objet' => Categorie::class,
//'ajouterCategorie' => true
],
],
4 => [
'view' => 'FO/DataTable/statut.html.twig',
'params' => [
'edit_route' => 'dtc_categorie_changer_statut',
],
],/*
2 => array(
'view' => 'FO/DataTable/edit_texte.html.twig',
'params' => array(
'objet' => Categorie::class,
'champ' => 'setLibelle',
),
),*/
]
)
->setMultiple(
[
'delete' => [
'title' => 'Non disponible',
'route' => 'dtc_categorie_liste',
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)
->setSearchFields([2, 3]);
return $datatable;
}
/**
* @Route("/categorie/grid", name="dtc_categorie_liste_grid")
*/
public function gridAction(Datatable $datatable, TranslatorInterface $translator)
{
return $this->datatable($datatable, $translator)->execute();
}
/**
* @Route("/categorie/recherche", name="dtc_categorie_recherche")
*/
public function rechercheAction(Request $request, EntityManagerInterface $em, PaginatorInterface $paginator)
{
$q = $request->query->get('q');
$parent = $request->query->get('par');
$repo = $em->getRepository(Categorie::class);
$results = $repo->getRechercheCategorie($q, $parent);
$pagination = $paginator->paginate(
$results, /* query NOT result */
$request->query->getInt('page', 1) /*page number*/,
10 /*limit per page*/
);
$pagination_results = $pagination->getItems();
$option_placeholde = ["libelle" => "Sélectionnez", "id" => ""];
if ($request->query->getInt('page', 1) == 1) {
array_unshift($pagination_results, $option_placeholde);
}
$pagination->setItems($pagination_results);
$serializer = SerializerBuilder::create()->build();
return JsonResponse::fromJsonString($serializer->serialize($pagination, 'json'));
}
/**
* @Route("/categorie/statut/{id}", name="dtc_categorie_changer_statut")
*/
public function changeStatutAction(Categorie $categorie, EntityManagerInterface $em)
{
if ($categorie->getStatut() == 1) {
$categorie->setStatut(0);
} else {
$categorie->setStatut(1);
}
$em->persist($categorie);
$em->flush();
$headers = [
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST',
];
return new JsonResponse(['data' => '1'], 200, $headers);
}
//\Doctrine\Common\Util\Debug::dump($tabCategories);
/**
* @Route("/checkbox/categorie", name="dtc_categorie_checkbox")
*/
public function listerCategorieCheckboxAction(Request $request, EntityManagerInterface $em)
{
$id = '';
$article = [];
$repo_article = $em->getRepository(Article::class);
$repo_categorie = $em->getRepository(Categorie::class);
$repo_article_categorie = $em->getRepository(ArticleCategorie::class);
$id = $request->query->get('id');
$url = $this->generateUrl('dtc_categorie_checkbox', []);
if ($request->query->get('article') != '') {
$article = $repo_article->find($request->query->get('article'));
}
$categories = $repo_categorie->getCategorieEnfant($id);
$tabCategories = [];
foreach ($categories as $c) {
$c['checked'] = 0;
$c['totalSousCategorie'] = count($repo_categorie->findBy(["categorieParent" => $c]));
$c['selection'] = 0;
if (is_object($article) and is_object($repo_article_categorie->findOneBy(['article' => $article, 'categorie' => $c]))) {
$c['checked'] = 1;
}
$tabCategories[] = $c;
}
$categories = $tabCategories;
if ($request->isXmlHttpRequest()) {
$rendu = $this->renderView('Articles/Categorie/checkbox.html.twig', ['categories' => $categories, 'article' => $article, 'url' => $url, 'objet' => 'article']);
return new JsonResponse(['rendu' => $rendu]);
}
return $this->render('Articles/Categorie/checkbox.html.twig', ['categories' => $categories, 'article' => $article, 'url' => $url, 'objet' => 'article']);
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatableProduits($categorie, Datatable $datatable, TranslatorInterface $translator)
{
$datatable
->setDatatableId('dta-produits')
->setEntity(Article::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
$translator->trans("Img") => 'x.reference',
$translator->trans("Réf") => 'x.reference',
$translator->trans("Libellé") => 'x.libelle',
$translator->trans("Marque") => 'm.libelle',
$translator->trans("Desc courte") => 'x.descriptionCourte',
$translator->trans("Prix HT") => 'x.prixVente',
$translator->trans("Prix TTC") => 'x.prixVente',
$translator->trans("Marge") => 'x.marge',
$translator->trans("Cump") => 'x.cpump',
$translator->trans("Stock") => 'x.stock',
$translator->trans("Statut Internet") => 'x.statut',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.articleCategorie', 'ac', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->setWhere(
'ac.categorie = :categorie',
['categorie' => $categorie]
)
->addJoin('x.marque', 'm', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_article_modifier',
],
],
1 => [
'view' => 'FO/DataTable/image.html.twig',
'params' => [
'edit_route' => 'dtc_article_modifier',
],
],
2 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_article_modifier',
],
],
3 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_article_modifier',
],
],
6 => [
'view' => 'FO/DataTable/prixSuivi.html.twig',
'params' => [
'surveillance' => false,
],
],
7 => [
'view' => 'FO/DataTable/prix_article_ttc.html.twig',
],
8 => [
'view' => 'FO/DataTable/prix.html.twig',
],
9 => [
'view' => 'FO/DataTable/prix.html.twig',
],
11 => [
'view' => 'FO/DataTable/statut.html.twig',
'params' => [
'edit_route' => 'dtc_article_changer_statut',
],
],
12 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'edit_route' => 'dtc_article_modifier',
'dupliquer_route' => 'dtc_article_dupliquer',
'objet' => Article::class,
],
],
]
)->setOrder("x.id", "desc")
->setSearch(true)
->setSearchFields([2, 3, 4]);
//$datatable->setSearchFields(array(0,2));
return $datatable;
}
/**
* @Route("/categorie/produits/grid/{id}", name="dtc_categorie_produits_liste_grid")
*/
public function gridProduitsAction(Categorie $categorie, Datatable $datatable, TranslatorInterface $translator)
{
//$this->datatableProduits($categorie);
return $this->datatableProduits($categorie, $datatable, $translator)->execute();
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatableRemises($categorie, Datatable $datatable, TranslatorInterface $translator)
{
$datatable
->setDatatableId('dta-remises')
->setEntity(Remise::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
$translator->trans("Libelle") => 'x.libelle',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.remiseCategorieArticle', 'rca', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->setWhere(
'rca.categorie = :categorie',
['categorie' => $categorie]
)
->setRenderers(
[
1 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_remise_modifier',
],
],
2 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'edit_route' => 'dtc_remise_modifier',
'objet' => Remise::class,
],
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)
->setSearchFields([1]);
//$datatable->setSearchFields(array(0,2));
return $datatable;
}
/**
* Grid action
* @return Response
*/
/**
* @Route("/categorie/remises/grid/{id}", name="dtc_categorie_remises_liste_grid")
*/
public function gridRemisesAction(Categorie $categorie, Datatable $datatable, TranslatorInterface $translator)
{
//$this->datatableRemises($categorie);
return $this->datatableRemises($categorie, $datatable, $translator)->execute();
}
/**
* @Route("", name="")
*/
public function listerCategorieDossierGlobalAction(EntityManagerInterface $em)
{
$repo_article = $em->getRepository(Article::class);
$repo_categorie = $em->getRepository(Categorie::class);
$repo_article_categorie = $em->getRepository(ArticleCategorie::class);
$repo_compte_market_place = $em->getRepository(CompteMarketPlace::class);
$compteMarketPlace = $repo_compte_market_place->findAll();
$articleCategories = [];
//\Doctrine\Common\Util\Debug::dump($articleId);
$categories = $repo_categorie->findBy(['categorieParent' => 1], ['position' => 'ASC']);//CategorieParent
return $this->render('Articles/Categorie/lister-dossier-global.html.twig', [
'nbCategoriesEnfant' => [],
'categories' => $categories,
'categoriesArticle' => $articleCategories,
'compteMarketPlace' => $compteMarketPlace,
'mode' => '',
'move' => true,
]);
}
/**
* @Route("", name="")
*/
public function listerCategorieDossierAction($articleId = '', EntityManagerInterface $em)
{
$repo_article = $em->getRepository(Article::class);
$repo_categorie = $em->getRepository(Categorie::class);
$repo_article_categorie = $em->getRepository(ArticleCategorie::class);
$article = $repo_article->find($articleId);
$articleCategories = [];
if (is_object($article)) {
$categoriesArticle = $repo_article_categorie->findBy(['article' => $articleId], ['position' => 'ASC']);
foreach ($categoriesArticle as $categorieArticle) {
$articleCategories[] = $categorieArticle->getCategorie()->getId();
}
}
//\Doctrine\Common\Util\Debug::dump($articleId);
$categories = $repo_categorie->findBy(['categorieParent' => 1], ['position' => 'ASC']);//CategorieParent
return $this->render('Articles/Categorie/lister-dossier.html.twig', [
'nbCategoriesEnfant' => [],
'categories' => $categories,
'article' => $article,
'categoriesArticle' => $articleCategories,
'mode' => '',
'move' => true,
]);
}
/**
* @Route("", name="")
*/
public function listerCategorieDossierRemiseAction($remise = null, EntityManagerInterface $em)
{
$repo_remise = $em->getRepository(Remise::class);
$remise = $repo_remise->find($remise);
$repo_categorie = $em->getRepository(Categorie::class);
$repo_remise_categorie = $em->getRepository(RemiseCategorieArticle::class);
$nbCategoriesEnfant = [];
$remiseCategories = [];
if (is_object($remise)) {
//$categoriesRemise = $repo_article_categorie->findByArticle($articleId);
foreach ($remise->getRemiseCategorieArticle() as $categorieRemise) {
$boucler = true;
$remiseCategories[] = $categorieRemise->getCategorie()->getId();
if (is_object($categorieRemise->getCategorie()->getCategorieParent()) and $categorieRemise->getCategorie()->getCategorieParent()->getId() > 1) {
$parent = $categorieRemise->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($nbCategoriesEnfant);
//\Doctrine\Common\Util\Debug::dump($articleId);
$categories = $repo_categorie->findBy(['categorieParent' => 1], ['libelle' => 'ASC']);//CategorieParent
return $this->render(
'Articles/Categorie/lister-dossier-remise.html.twig',
['nbCategoriesEnfant' => $nbCategoriesEnfant, 'categories' => $categories, 'remise' => $remise, 'categoriesRemise' => $remiseCategories]
);
}
/**
* @Route("", name="")
*/
public function listerCategorieDossierArticleAction(EntityManagerInterface $em, $article = null, $mode = "")
{
$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($article)) {
//$categoriesArticle = $repo_article_categorie->findByArticle($articleId);
foreach ($article->getArticleCategorie() as $categorieArticle) {
$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;
}
}
}
}
}
//print_r($nbCategoriesEnfant);
//\Doctrine\Common\Util\Debug::dump($articleId);
$categories = $repo_categorie->findBy(['categorieParent' => 1], ['position' => 'ASC']);//CategorieParent
return $this->render('Articles/Categorie/lister-dossier.html.twig', [
'nbCategoriesEnfant' => $nbCategoriesEnfant,
'categories' => $categories,
'article' => $article,
'categoriesArticle' => $articleCategories,
'mode' => $mode,
]);
}
/**
* @Route("/categorie/supprimer/{id}", name="dtc_categorie_supprimer")
*/
public function supprimerAction(Request $request, Categorie $objet, EntityManagerInterface $em, ValidatorInterface $validator, TranslatorInterface $translator)
{
$titre_modal = $translator->trans("Demande de confirmation.");
$user = $this->getUser();
$message = '';
$errorsSup = [];
//Verifier si la categorie a des produits
if (count($objet->getArticleCategorie()) > 0) {
$errorsSup[] = 'La catégorie n\'est pas supprimable car elle contient des produits.';
}
//Verifier si la categorie a des categories enfantes
if (count($objet->getCategoriesEnfant()) > 0) {
//if ($message != '') $message .= ' ';
$errorsSup[] = 'La catégorie n\'est pas supprimable car elle contient des catégories enfants';
}
$form = $this->createForm(SupprimerCategorieType::class, $objet);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted() && count($errorsSup) == 0) {
if ($form->isValid()) {
foreach ($objet->getRemiseCategorieArticle() as $remiseCatArticle) {
$em->remove($remiseCatArticle);
}
$em->remove($objet);
$em->flush();
$this->addFlash('notice', $translator->trans('Catégorie supprimée avec succès !'));
$url = $this->generateUrl('dtc_categorie_liste');
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url]);
} else {
$errors = $validator->validate($objet);
$rendu = $this->renderView(
'Articles/Categorie/supprimer.html.twig',
['form' => $form->createView(), 'errors' => $errors, 'errorsSup' => $errorsSup, 'message' => $message]
);
return new Response(json_encode(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]));
}
}
$rendu = $this->renderView('Articles/Categorie/supprimer.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'errorsSup' => $errorsSup, 'message' => $message]);
return new Response(json_encode(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]));
}
/**
* @Route("/article/categorie/enfants", name="dtc_article_lister_categorie_enfant_liste")
*/
public function listerCategorieEnfantListeAction(Request $request, EntityManagerInterface $em)
{
$id = $request->query->get('id');
$repo_categorie = $em->getRepository(Categorie::class);
$rendu = "";
$categories = $repo_categorie->getCategorieEnfant($id);
$rendu .= '<option value="">Sélectionnez</option>';
foreach ($categories as $e) {
$rendu .= '<option value="'.$e['id'].'">';
$rendu .= $e['libelle'];
$rendu .= '</option>';
}
return new JsonResponse(['rendu' => $rendu]);
}
/**
* @Route("/article/categorie/gerer", name="dtc_gerer_cat")
*/
public function gererCategorieAction(EntityManagerInterface $em)
{
$repo_article = $em->getRepository(Article::class);
$repo_cat = $em->getRepository(Categorie::class);
$repo_cat_article = $em->getRepository(ArticleCategorie::class);
$articles = $repo_article->findall();
foreach ($articles as $article) {
$articleCat = $article->getArticleCategorie();
foreach ($articleCat as $AC) {
if ($AC->getCategorie()->getParent()->getId() == 1) {
} else {
$newAC = new ArticleCategorie();
$newAC->setArticle($article);
$newAC->setCategorie($AC->getCategorie()->getParent());
$newAC->setPosition(0);
echo "nouvelle liasion : ";
$em->persist($newAC);
$em->flush();
}
}
}
return new Response("ok");
}
/**
* @Route("/categorie/changer/positions/ajax", name="dtc_categorie_change_positions")
*/
public function changerPositionsAjaxAction(Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
$repo_cat = $em->getRepository(Categorie::class);
$errors = [];
$positions = $request->request->get('positions');
//var_dump($positions);
if (count($positions)) {
foreach ($positions as $key => $position) {
$categorie = $repo_cat->find($position);
if (is_object($categorie)) {
$categorie->setPosition($key);
$em->persist($categorie);
}
}
$em->flush();
$this->addFlash(
'notice',
$translator->trans('Positions modifiées avec succès !')
);
}
$rendu = $this->renderView('layout-errors.html.twig', ['errors' => $errors]);
return new JsonResponse(['rendu' => $rendu,]);
}
/**
* @Route("/categorie/ajouter/modal", name="dtc_categorie_ajouter_modal")
*/
public function ajouterModalAction(Request $request, EntityManagerInterface $em, Logo $logoService, TranslatorInterface $translator, ValidatorInterface $validator)
{
$repo_categorie = $em->getRepository(Categorie::class);
$titre_modal = $translator->trans("Créer une nouvelle catégorie");
$errors = "";
$errorsSup = [];
$categorie = new Categorie;
$parentId = $request->query->get('parent');
if ($parentId == '') {
$parentId = 1;
}//par defaut le parent est la categorie racine.
if ($parentId != '') {
$parent = $repo_categorie->find($parentId);
if (is_object($parent)) {
$titre_modal .= ' enfant de '.$parent->getLibelle().'('.$parent->getReference().')';
$categorie->setCategorieParent($parent);
}
}
$user = $this->getUser();
$categorie->setUtilisateur($user);
$form = $this->createForm(CategorieModalType::class, $categorie);
$form->handleRequest($request);
//verifier si la categorie existe
$catTmp = $repo_categorie->findBy([
'categorieParent' => $categorie->getCategorieParent(),
'reference' => $categorie->getReference(),
]);
if (count($catTmp) > 0) {
$errorsSup[] = 'Cette référence de catégorie existe déjà!';
}
if ($form->isSubmitted()) {
if ($form->isValid() && count($catTmp) == 0) {
$em->persist($categorie);
$em->flush();
//print_r($request->request->all());
$tokenP = $request->request->get('tokenPicture');
$logoService->sauvegarder($categorie, 'categorie', $tokenP);
$tokenP = $request->request->get('tokenPicture2');
$logoService->sauvegarder2($categorie, 'categorielogo', $tokenP);
/*
$em->persist($categorie);
$em->flush();
*
*/
/*
$tokenP = $request->request->get('tokenPicture');
$this->addFlash(
'notice',
$$tokenP
);
$logoService->sauvegarder($categorie,'categorie',$tokenP);
*
*/
$url = $this->generateUrl('dtc_categorie_liste', []);
//$url="";
$this->addFlash(
'notice',
$translator->trans('Categorie ajoutée avec succès !')
);
return new JsonResponse(['rendu' => '', 'errorsSup' => $errorsSup, 'valide' => '1', 'url' => $url, 'type' => '', 'libelle' => '']);
} else {
$errors = $validator->validate($categorie);
}
}
$rendu = $this->renderView(
'Articles/Categorie/ajouter-modal.html.twig',
['form' => $form->createView(), 'errorsSup' => $errorsSup, 'errors' => $errors, 'errorsSup' => $errorsSup, 'categorie' => $categorie]
);
return new Response(json_encode(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]));
}
public function listerCategorieDossierArticleAjaxAction(Request $request, EntityManagerInterface $em, $article = null, $mode = "", $mpId = '', $compteMarketPlace = '', $amp = '')
{
$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);
//echo "AAAA";
if (is_object($articleMarketPlace)) {
//echo "BBBBBB";
//$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;
}
}
}
}
} else {
/*
$categorieArticle = $repo_categorie->find(12928);
$boucler = true;
$articleCategories[] = $categorieArticle->getId();
if(is_object($categorieArticle->getCategorieParent()) and $categorieArticle->getCategorieParent()->getId() > 1) {
$parent = $categorieArticle->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;
}
}
*/
//$articleCategories[] = 12928;
}
} 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;
}
}
}
}
}
}
//echo "AZERTYU ".$article->getId();
//print_r($articleCategories);
//\Doctrine\Common\Util\Debug::dump($articleId);
//$categories = $repo_categorie->findBy(array('categorieParent'=>1),array('libelle'=>'ASC'));//CategorieParent
//echo "type".$type;
if ($type == "recherche") {
//$rendu = $this->renderView('Articles/Categorie/lister-dossier-ajax.html.twig', array('nbCategoriesEnfant'=>array(),'categories' =>$categories,'article'=>$article, 'categoriesArticle' => $articleCategories,"marketPlace"=>$marketPlace));
//return new JsonResponse(array('rendu'=>$rendu), 200, array('Content-Type'=>'application/json'));
if (is_object($marketPlace)) {
$categories = $repo_categorie->getCategorieMarketPlace($marketPlace, $s);
} else {
$categories = $repo_categorie->getCategorieRecherche($s);
}
$rendu = $this->renderView('Articles/Categorie/lister-dossier-ajax.html.twig', [
'nbCategoriesEnfant' => $nbCategoriesEnfant,
'categories' => $categories,
'article' => $article,
'categoriesArticle' => $articleCategories,
'marketPlace' => $marketPlace,
'compteMarketPlace' => $compteMarketPlace,
'articleMarketPlace' => $articleMarketPlace,
'type' => $type,
'mode' => $mode,
]);
return new JsonResponse(['rendu' => $rendu]);
} else {
$categories = $repo_categorie->getCategorieMarketPlace($marketPlace);
return $this->render('Articles/Categorie/lister-dossier-ajax.html.twig', [
'nbCategoriesEnfant' => $nbCategoriesEnfant,
'categories' => $categories,
'article' => $article,
'categoriesArticle' => $articleCategories,
'marketPlace' => $marketPlace,
'compteMarketPlace' => $compteMarketPlace,
'articleMarketPlace' => $articleMarketPlace,
'type' => $type,
'mode' => $mode,
]);
}
}
/**
* @Route("/categorie/enfants/{id}", name="dtc_categorie_lister_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);
if (count($article->getArticleCategorie()) > 0) {
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;
}
}
}
}
} else {
/*
$categorieArticle = $repo_categorie->find(12928);
$boucler = true;
$articleCategories[] = $categorieArticle->getId();
if(is_object($categorieArticle->getCategorieParent()) and $categorieArticle->getCategorieParent()->getId() > 1) {
$parent = $categorieArticle->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',
[
'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]);
}
}