src/Controller/Admin/PostController.php line 779

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Admin;
  4. use App\Entity\Post;
  5. use App\Entity\User;
  6. use App\Entity\Forms;
  7. use ReflectionObject;
  8. use App\Entity\Widget;
  9. use App\Form\PostType;
  10. use App\Utils\Slugger;
  11. use App\Entity\CpField;
  12. use App\Entity\Product;
  13. use App\Entity\UrlPost;
  14. use App\Entity\Category;
  15. use App\Entity\Language;
  16. use App\Entity\PostBloc;
  17. use App\Entity\TypePost;
  18. use App\Entity\MetasCles;
  19. use App\Entity\CpPostField;
  20. use App\Entity\PostArchive;
  21. use App\Entity\Redirection;
  22. use App\Security\PostVoter;
  23. use App\Entity\CategoryBloc;
  24. use App\Entity\CmsAffichage;
  25. use App\Entity\MediaGallery;
  26. use App\Entity\PostCategory;
  27. use App\Service\EmailService;
  28. use App\Service\FileUploader;
  29. use App\Service\ImageService;
  30. use App\Entity\ParametreLabel;
  31. use App\Service\DuplicatePost;
  32. use App\Entity\GallerieBlocPost;
  33. use App\Service\RefactorSlugPost;
  34. use App\Entity\UrlPostTranslation;
  35. use App\Repository\PostRepository;
  36. use App\Repository\UrlPostRepository;
  37. use App\Repository\LanguageRepository;
  38. use Doctrine\ORM\PersistentCollection;
  39. use Doctrine\ORM\EntityManagerInterface;
  40. use Symfony\Component\Filesystem\Filesystem;
  41. use Symfony\Component\HttpFoundation\Request;
  42. use Symfony\Component\HttpFoundation\Response;
  43. use Symfony\Component\Routing\Annotation\Route;
  44. use Symfony\Component\HttpFoundation\JsonResponse;
  45. use Symfony\Component\String\Slugger\SluggerInterface;
  46. use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
  47. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  48. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  49. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  50. /**
  51.  * @Route("/console")
  52.  */
  53. class PostController extends AbstractController
  54. {
  55.     private $imageService;
  56.     private $emailService;
  57.     private $languages;
  58.     private $em;
  59.     public function __construct(EntityManagerInterface $em,ImageService $imageService,EmailService $emailService,LanguageRepository $languages)
  60.     {
  61.        $this->em $em;
  62.        $this->imageService $imageService;
  63.        $this->emailService $emailService;
  64.        $this->languages $languages->findActifLang();
  65.     }
  66.     /**
  67.       * @Route("/post/{type}", name="admin_post_list")      
  68.      */
  69.     public function admin_index($type,Request $request)
  70.     {
  71.         $_locale $request->getLocale();    
  72.         $user $this->getUser();
  73.         $IdSite $this->getUser()->getSite()->getId();
  74.         $typePost $this->getDoctrine()->getRepository(TypePost::class)->find($type);
  75.         $posts $this->getDoctrine()->getRepository(Post::class)->findAllPostByType($_locale,$type,$IdSite);
  76.         return $this->render('admin/dashboards/post/_list.html.twig', [
  77.             'posts' => $posts,
  78.             'i_type_post'=> $type,
  79.             'system_name'=> $typePost->getSystemName(),
  80.         ]);
  81.     }
  82.     /**
  83.       * @Route("/post/duplicate/{id}-{type}", name="admin_post_duplicate")      
  84.      */
  85.     public function duplicatePost(Request $request,DuplicatePost $duplicatePost,$id,$type)
  86.     {
  87.         $post $this->getDoctrine()->getRepository(Post::class)->find($id);
  88.         $duplicatePost->duplicatePost($post);
  89.         return $this->redirectToRoute('admin_post_list',['type'=>$type]);
  90.     }
  91.     /**
  92.      * @Route("/post/add/{type}", name="admin_post_add")
  93.      */
  94.     public function admin_add($type,Request $request,LanguageRepository $languageUrlPostRepository $url_post,FileUploader $fileUploader)
  95.     {
  96.         $_locale  $request->getLocale();
  97.         $liste_categories $this->getDoctrine()->getRepository(Category::class)->findCategorieActif($_locale,$type);
  98.         $categories $this->buildTree($liste_categories);
  99.  
  100.         $type_post $this->getDoctrine()->getRepository(TypePost::class)->find($type);
  101.         $form $this->createForm(PostType::class, null, array(
  102.             'action' => $this->generateUrl($request->get('_route'),array('type'=>$type)),
  103.         ))->handleRequest($request);
  104.             
  105.         if ($form->isSubmitted() && $form->isValid()) {
  106.             $new_post = new Post();
  107.             $data $form->getData();
  108.             $image $request->request->get('image');
  109.             if($image){
  110.                 $new_post->setImage($image);
  111.                 $this->imageService->thumbnail($image);
  112.             }
  113.             
  114.             $image_bandeau $request->request->get('image_bandeau');
  115.             if($image_bandeau){
  116.                 $new_post->setImageBandeau($image_bandeau);
  117.                 $this->imageService->thumbnail($image_bandeau);
  118.             }
  119.             $image_couverture $request->request->get('image_couverture');
  120.             if($image_couverture){
  121.                 $new_post->setImageCouverture($image_couverture);
  122.                 $this->imageService->thumbnail($image_couverture);
  123.             }
  124.             $url_video $request->request->get('video_url');
  125.             if($url_video != ''){
  126.                 $new_post->setUrlVideo($url_video);
  127.             }
  128.             if (isset($data['authorization_api'])) {
  129.                 $new_post->setAuthorizationApi($data['authorization_api']);
  130.             }
  131.      
  132.             $new_post->setActifHome($data['actif_home']);
  133.             $new_post->setTypePost($type_post);
  134.             $new_post->setAuthor($this->getUser());
  135.             $new_post->setTagManagerHead($data['tag_managerhead']);
  136.             $new_post->setTagManagerBody($data['tag_managerbody']);
  137.             if ($this->languages) {
  138.                 foreach ($this->languages as $language) {
  139.                     if (empty($data['canonical'])) {
  140.                         $new_post->translate($language->getLanguageCode())->setCanonical($data['slug']);
  141.                     }else {
  142.                         $new_post->translate($language->getLanguageCode())->setCanonical($data['canonical']);
  143.                     }
  144.                     $new_post->translate($language->getLanguageCode())->setActif($data['actif']);
  145.                     $new_post->translate($language->getLanguageCode())->setTitleAffichage($data['title_affichage']);
  146.                     $new_post->translate($language->getLanguageCode())->setSchema($data['schema']);
  147.                     $new_post->translate($language->getLanguageCode())->setTitle($data['titre']);
  148.                     $new_post->translate($language->getLanguageCode())->setSummary($data['resume']);
  149.                     $new_post->translate($language->getLanguageCode())->setSlug($data['slug']);
  150.                     $new_post->translate($language->getLanguageCode())->setContent($data['texte']);
  151.                     $new_post->translate($language->getLanguageCode())->setImageAlt($data['alt_image']);
  152.                     $new_post->translate($language->getLanguageCode())->setImageBandeauAlt($data['alt_image_bandeau']);
  153.                     $new_post->translate($language->getLanguageCode())->setImageCouvertureAlt($data['alt_image_couverture']);
  154.                     if (empty($data['meta_title'])) {
  155.                         $new_post->translate($language->getLanguageCode())->setMetaTitle($data['titre']);
  156.                     }else{
  157.                         $new_post->translate($language->getLanguageCode())->setMetaTitle($data['meta_title']);
  158.                     }
  159.                     if (empty($data['meta_description']) && !empty($data['texte'])) {
  160.                         $new_post->translate($language->getLanguageCode())->setMetaDescription(trim(substr(strip_tags(html_entity_decode($data['texte'], ENT_QUOTES ENT_HTML5'UTF-8')), 0150).'...'));
  161.                     }else{
  162.                         $new_post->translate($language->getLanguageCode())->setMetaDescription($data['meta_description']);
  163.                     }
  164.                     $new_post->translate($language->getLanguageCode())->setMetaRobots($data['meta_robots']);
  165.                     $new_post->translate($language->getLanguageCode())->setSettigMetaTitle($data['settig_meta_title']);
  166.                     $new_post->translate($language->getLanguageCode())->setSettigMetaDesc($data['settig_meta_Desc']);  
  167.                     $new_post->translate($language->getLanguageCode())->setTraduite(false);
  168.                     $new_post->translate($language->getLanguageCode())->setTitreLien($data['titre_lien']);
  169.                     $new_post->translate($language->getLanguageCode())->setTypeLien($data['type_lien']);
  170.                     $new_post->translate($language->getLanguageCode())->setLienExterne($data['lien_externe']);
  171.                     $new_post->translate($language->getLanguageCode())->setLienInterne($data['lien_interne']);
  172.                     $new_post->translate($language->getLanguageCode())->setTelephone($data['telephone']);
  173.                 }
  174.             }
  175.             $this->em->persist($new_post);                   
  176.             $new_post->mergeNewTranslations();
  177.             $this->em->flush(); 
  178.             $new_url_post = new UrlPost();
  179.             $new_url_post->setIdModule($new_post->getId()); 
  180.             $new_url_post->setType('Post'); 
  181.             if($this->languages){
  182.                 foreach($this->languages as $language)
  183.                 {
  184.                     $new_url_post->translate($language->getLanguageCode())->setSlug($data['slug']);
  185.                 }
  186.             }
  187.             $this->em->persist($new_url_post);                   
  188.             $new_url_post->mergeNewTranslations();
  189.             $this->em->flush(); 
  190.             // persist category post 
  191.             $reqCategories  =  $request->request->get('Categories');
  192.             if($reqCategories){
  193.                   foreach($reqCategories as $id){
  194.                        $category $this->getDoctrine()->getRepository(Category::class)->find($id);
  195.                        $post_category = new PostCategory();
  196.                        $post_category->setCategory($category);
  197.                        $post_category->setPost($new_post);
  198.                        $this->em->persist($post_category);
  199.                   }
  200.             }
  201.             $this->em->flush();
  202.             $this->addFlash('success''Vos données ont été enregistrées avec succès.');
  203.             if($request->request->get('saveAndstay') === '1'){
  204.                 return $this->redirectToRoute('admin_post_edit',['id'=> $new_post->getId(),'type'=>$type]);
  205.             }else{
  206.                 return $this->redirectToRoute('admin_post_list',['type'=> $type ]);
  207.             }
  208.         }
  209.                 
  210.         return $this->render('admin/dashboards/post/_create.html.twig', array(
  211.             'form' => $form->createView(),
  212.             'type_post'=> $type_post,
  213.             'categories'=> $categories
  214.         ));
  215.     }
  216.     /**
  217.      * @Route("/post/edit/{id}/{type}", name="admin_post_edit")
  218.      */
  219.     public function admin_edit($id,$type,Request $request,DuplicatePost $duplicate_post,RefactorSlugPost $refractorslug,LanguageRepository $language,UrlPostRepository $urlpost,FileUploader $fileUploader,SluggerInterface $slugger,RefactorSlugPost $refactorSlug)
  220.     {
  221.         
  222.         $_locale $request->getLocale();
  223.         $post $this->getDoctrine()->getRepository(Post::class)->find($id);
  224.         $type_post $this->getDoctrine()->getRepository(TypePost::class)->find($type);
  225.         $liste_categories $this->getDoctrine()->getRepository(Category::class)->findCategorieActif($_locale,$type);
  226.         $categories $this->buildTree($liste_categories);
  227.         $cp_field $this->getDoctrine()->getRepository(CpField::class)->findBy(['PostType'=>$type]);
  228.         $liste_metas = [];
  229.         $liste_label $this->getDoctrine()->getRepository(ParametreLabel::class)->findAll();
  230.         $MetasCles $this->getDoctrine()->getRepository(MetasCles::class)->findAll();
  231.         $NameModule $type_post->translate($_locale)->getSystemName();
  232.         if($MetasCles){
  233.             foreach($MetasCles as $meta){
  234.                 if(in_array(strtolower($NameModule),explode(",",$meta->getModuleMetas()))){
  235.                     $liste_metas[] = $meta;
  236.                 }
  237.            }
  238.         }
  239.         
  240.         $list_bloc $this->getDoctrine()->getRepository(PostBloc::class)->findBlocNotDeleted($id);
  241.         $ancien_slug $post->translate($_locale)->getSlug();
  242.         $Url = [];
  243.         $canonicalUrl = [];
  244.         $ListeSlug $this->getDoctrine()->getRepository(URLPost::class)->findAll();
  245.         if($ListeSlug){
  246.             foreach($ListeSlug as $value){
  247.                 $slug $value->translate($_locale)->getSlug();
  248.                 $post_slug $this->getDoctrine()->getRepository(Post::class)->find($value->getIdModule());
  249.                 if($post_slug){
  250.                     if($refractorslug->getPath($post_slug,$slug) != null){
  251.                         $canonicalUrl[]= $refractorslug->getPath($post_slug,$slug);
  252.                     }
  253.                     
  254.                 }
  255.            
  256.                 if($ancien_slug != $slug){
  257.                     $Url[] = $slug;
  258.                 }
  259.             }
  260.         }
  261.         $data = ['actif'=> $post->translate($_locale)->getActif(), 
  262.                  'actif_home'=> $post->getActifHome(), 
  263.                  'authorization_api'=> $post->getAuthorizationApi(), 
  264.                  'titre'=> $post->translate($_locale)->getTitle(), 
  265.                  'slug'=> $post->translate($_locale)->getSlug(), 
  266.                  'title_affichage'=> $post->translate($_locale)->getTitleAffichage(), 
  267.                  'resume'=> $post->translate($_locale)->getSummary(), 
  268.                  'texte'=> $post->translate($_locale)->getContent(), 
  269.                  'alt_image'=> $post->translate($_locale)->getImageAlt(),
  270.                  'alt_image_bandeau'=> $post->translate($_locale)->getImageBandeauAlt(),  
  271.                  'alt_image_couverture'=> $post->translate($_locale)->getImageCouvertureAlt(),  
  272.                  'date_publication'=> $post->getDatePublication(),
  273.                  'meta_title'=> $post->translate($_locale)->getMetaTitle(),  
  274.                  'meta_description'=> $post->translate($_locale)->getMetaDescription(),  
  275.                  'meta_robots'=> $post->translate($_locale)->getMetaRobots(), 
  276.                  'settig_meta_title'=> $post->translate($_locale)->getSettigMetaTitle(), 
  277.                  'settig_meta_Desc'=> $post->translate($_locale)->getSettigMetaDesc(), 
  278.                  'tag_managerhead'=> $post->getTagManagerHead(), 
  279.                  'tag_managerbody'=> $post->getTagManagerBody(), 
  280.                  'titre_lien'=> $post->translate($_locale)->getTitreLien(),
  281.                  'type_lien'=> $post->translate($_locale)->getTypeLien(),
  282.                  'lien_externe'=> $post->translate($_locale)->getLienExterne(),
  283.                  'lien_interne'=> $post->translate($_locale)->getLienInterne(),
  284.                  'telephone'=> $post->translate($_locale)->getTelephone(),
  285.                 ];
  286.         $form $this->createForm(PostType::class, $data,[
  287.             'defaultValue'=>$post->translate($_locale)->getTypeLien() != null?$post->translate($_locale)->getTypeLien():1,
  288.             'action' => $this->generateUrl($request->get('_route'),array('id'=>$id,'type'=>$type))
  289.         ])->handleRequest($request);  
  290.         if ($form->isSubmitted() && $form->isValid()) {
  291.             $data $form->getData();
  292.             if($ancien_slug != $data['slug']){
  293.                 $url_post_existe $this->getDoctrine()->getRepository(UrlPost::class)->findUrlBySlug($ancien_slug);
  294.                 if($url_post_existe){
  295.                     $url_post_existe->setType('Post');
  296.                     $url_post_existe->setIdModule($post->getId());
  297.                     $url_post_existe->translate($_locale)->setSlug($data['slug']);
  298.                     $this->em->flush();
  299.                 }
  300.             
  301.                 if(in_array($data['slug'], $Url)) {
  302.                     $this->addFlash('error''URL existe déjà!');
  303.                     return $this->redirectToRoute('admin_post_edit',['id'=> $post->getId(),'type'=> $type]); 
  304.                 }else{
  305.                     $redirection $this->getDoctrine()->getRepository(Redirection::class)->findOneBy(['id_module'=>$post->getId(),'type_module'=>'Post','locale'=>$_locale]);                   
  306.                     if($refactorSlug->getPath($post,$data['slug']) != null){
  307.                         if($redirection){
  308.                             $redirection->setOld($refactorSlug->getPath($post,$ancien_slug));
  309.                             $redirection->setNew($refactorSlug->getPath($post,$data['slug']));
  310.                             $redirection->setIdModule($post->getId());
  311.                             $redirection->setTypeModule('Post');
  312.                             $redirection->setLocale($_locale);
  313.                             $this->em->flush();
  314.                         }else{
  315.                             $newRedirection = new Redirection();
  316.                             $newRedirection->setOld($refactorSlug->getPath($post,$ancien_slug));
  317.                             $newRedirection->setNew($refactorSlug->getPath($post,$data['slug']));
  318.                             $newRedirection->setIdModule($post->getId());
  319.                             $newRedirection->setTypeModule('Post');
  320.                             $newRedirection->setActive(true);
  321.                             $newRedirection->setSite($this->getUser()->getSite());
  322.                             $newRedirection->setLocale($_locale);
  323.                             $this->em->persist($newRedirection);
  324.                             $this->em->flush();
  325.                         }
  326.                     }
  327.                 
  328.                     $post->translate($_locale)->setSlug($data['slug']);
  329.                     $url_post $this->getDoctrine()->getRepository(UrlPost::class)->findOneBy(['id_module'=>$post->getId(),'type'=> 'Post']);
  330.                     $url_post->translate($_locale)->setSlug($data['slug']);
  331.                     $this->em->flush();
  332.                 }
  333.             }
  334.             $duplicate_post->ArchivePost($post);
  335.             $image $request->request->get('image');
  336.             if(!empty($image)){
  337.                 $post->setImage($image);
  338.                 $this->imageService->thumbnail($image);
  339.             }else{
  340.                 $post->setImage(null);
  341.             }
  342.             $image_bandeau $request->request->get('image_bandeau');
  343.             if(!empty($image_bandeau)){
  344.                 $post->setImageBandeau($image_bandeau);
  345.                 $this->imageService->thumbnail($image_bandeau);
  346.             }else{
  347.                 $post->setImageBandeau(null);
  348.             }
  349.             $image_couverture $request->request->get('image_couverture');
  350.             if(!empty($image_couverture)){
  351.                 $post->setImageCouverture($image_couverture);
  352.                 $this->imageService->thumbnail($image_couverture);
  353.             }else{
  354.                 $post->setImageCouverture(null);
  355.             }
  356.             $url_video $request->request->get('video_url');
  357.             if(!empty($url_video)){
  358.                 $post->setUrlVideo($url_video);
  359.             }else{
  360.                 $post->setUrlVideo(null);
  361.             }
  362.             if (isset($data['authorization_api'])) {
  363.                 $post->setAuthorizationApi($data['authorization_api']);
  364.             }
  365.             $post->setActifHome($data['actif_home']);
  366.             $post->setTypePost($type_post);
  367.             $post->setAuthor($this->getUser());
  368.             $post->setTagManagerHead($data['tag_managerhead']);
  369.             $post->setTagManagerBody($data['tag_managerbody']);
  370.             if (empty($data['canonical'])) {
  371.                 $post->translate($_locale)->setCanonical($data['slug']);
  372.             }else {
  373.                 $post->translate($_locale)->setCanonical($data['canonical']);
  374.             }
  375.             $post->translate($_locale)->setActif($data['actif']);
  376.             $post->translate($_locale)->setTitleAffichage($data['title_affichage']);
  377.             $post->translate($_locale)->setSchema($data['schema']);
  378.             $post->translate($_locale)->setTitle($data['titre']);
  379.             $post->translate($_locale)->setSummary($data['resume']);
  380.             $post->translate($_locale)->setSlug($data['slug']);
  381.             $post->translate($_locale)->setContent($data['texte']);
  382.             $post->translate($_locale)->setImageAlt($data['alt_image']);
  383.             $post->translate($_locale)->setImageBandeauAlt($data['alt_image_bandeau']);
  384.             $post->translate($_locale)->setImageCouvertureAlt($data['alt_image_couverture']);
  385.             if (empty($data['meta_title'])) {
  386.                 $post->translate($_locale)->setMetaTitle($data['titre']);
  387.             }else{
  388.                 $post->translate($_locale)->setMetaTitle($data['meta_title']);
  389.             }
  390.             if (empty($data['meta_description']) && !empty($data['texte'])) {
  391.                 $post->translate($_locale)->setMetaDescription(trim(substr(strip_tags(html_entity_decode($data['texte'], ENT_QUOTES ENT_HTML5'UTF-8')), 0150).'...'));
  392.             }else{
  393.                 $post->translate($_locale)->setMetaDescription($data['meta_description']);
  394.             }
  395.             $post->translate($_locale)->setMetaRobots($data['meta_robots']);
  396.             $post->translate($_locale)->setSettigMetaTitle($data['settig_meta_title']);
  397.             $post->translate($_locale)->setSettigMetaDesc($data['settig_meta_Desc']);  
  398.             $post->translate($_locale)->setTraduite(false);
  399.             $post->translate($_locale)->setTitreLien($data['titre_lien']);
  400.             $post->translate($_locale)->setTypeLien($data['type_lien']);
  401.             $post->translate($_locale)->setLienExterne($data['lien_externe']);
  402.             $post->translate($_locale)->setLienInterne($data['lien_interne']);
  403.             $post->translate($_locale)->setTelephone($data['telephone']);
  404.             $this->em->flush();
  405.             // persist champs personnalisée after persist post 
  406.             $post_field $request->request->get('post_field');
  407.             $list_post_field $post->getPostField();
  408.             if($cp_field){
  409.                 $index 0;
  410.                 foreach($cp_field as $field){
  411.                     $PostField $this->getDoctrine()->getRepository(CpPostField::class)->findOneBy(['CpField'=>$field,'IdPost'=>$post->getId()]);
  412.                     if($PostField){
  413.                         if($field->translate($_locale)->getType() == 'text'){
  414.                             $PostField->setTextValue($post_field[$index]);
  415.                         }
  416.                 
  417.                         if($field->translate($_locale)->getType() == 'textarea'){
  418.                             $PostField->setTextareaValue($post_field[$index]);
  419.                         }
  420.                         if($field->translate($_locale)->getType() == 'checkbox'){
  421.                             if(isset($post_field[$index])){
  422.                                 $PostField->setCheckboxValue(true);
  423.                             }else{
  424.                                 $PostField->setCheckboxValue(false);
  425.                             }
  426.                         }
  427.                 
  428.                         if($field->translate($_locale)->getType() == 'date'){
  429.                             $PostField->setDateValue(new \DateTime());
  430.                         }
  431.                         
  432.                         if($field->translate($_locale)->getType() == 'radio'){
  433.                             $PostField->setRadioValue($post_field[$index]);
  434.                         }
  435.                         if($field->translate($_locale)->getType() == 'link'){
  436.                             $PostField->setLinkValue($post_field[$index]);
  437.                         }
  438.                         if($field->translate($_locale)->getType() == 'image'){
  439.                             $delete_image_p $request->request->get('delete_image_p');
  440.                             if($delete_image_p){
  441.                                 $PostField->setImageValue(null);
  442.                             }
  443.                             $image_p $request->files->get('image_p');
  444.                             if ($image_p) {
  445.                                 $originalFilename pathinfo($image_p->getClientOriginalName(), PATHINFO_FILENAME);
  446.                                 $safeFilename $slugger->slug($originalFilename);
  447.                                 $newFilename =  $safeFilename.'-'.uniqid().'.'.$image_p->guessExtension();
  448.                                 $PostField->setImageValue($newFilename);
  449.                                 $image_p->move($this->getParameter('image_directory'), $newFilename);
  450.                             }
  451.                         }
  452.                 
  453.                         $PostField->setIdPost($post->getId());
  454.                         $this->em->flush();
  455.                         
  456.                     }else{
  457.                         $PostField = new CpPostField();
  458.                         $PostField->setCpField($field);
  459.                         if($field->translate($_locale)->getType() == 'text'){
  460.                             $PostField->setTextValue($post_field[$index]);
  461.                         }
  462.     
  463.                         if($field->translate($_locale)->getType() == 'textarea'){
  464.                             $PostField->setTextareaValue($post_field[$index]);
  465.                         }
  466.                         if($field->translate($_locale)->getType() == 'checkbox'){
  467.                             if(isset($post_field[$index])){
  468.                                 $PostField->setCheckboxValue(true);
  469.                             }else{
  470.                                 $PostField->setCheckboxValue(false);
  471.                             }
  472.                         }
  473.                 
  474.                         if($field->translate($_locale)->getType() == 'date'){
  475.                             $PostField->setDateValue(new \DateTime());
  476.                         }
  477.                         
  478.                         if($field->translate($_locale)->getType() == 'radio'){
  479.                             $PostField->setRadioValue($post_field[$index]);
  480.                         }
  481.                         if($field->translate($_locale)->getType() == 'link'){
  482.                             $PostField->setLinkValue($post_field[$index]);
  483.                         }
  484.                         if($field->translate($_locale)->getType() == 'image'){
  485.                             $delete_image_p $request->request->get('delete_image_p');
  486.                             if($delete_image_p){
  487.                                 $PostField->setImageValue(null);
  488.                             }
  489.                             $image_p $request->files->get('image_p');
  490.                             if ($image_p) {
  491.                                 $originalFilename pathinfo($image_p->getClientOriginalName(), PATHINFO_FILENAME);
  492.                                 $safeFilename $slugger->slug($originalFilename);
  493.                                 $newFilename =  $safeFilename.'-'.uniqid().'.'.$image_p->guessExtension();
  494.                                 $PostField->setImageValue($newFilename);
  495.                                 $image_p->move($this->getParameter('image_directory'), $newFilename);
  496.                             }
  497.                         }
  498.     
  499.                         $PostField->setIdPost($post->getId());
  500.                         $this->em->persist($PostField);
  501.                         $post->addPostField($PostField);
  502.                         $this->em->flush();
  503.                     }
  504.                     $index++;
  505.                 }
  506.             }
  507.             // Modification entity post categories
  508.             $req_categories $request->request->get('Categories');
  509.             $post_categorie $post->getPostCategory();
  510.             $list_Categorie = [];
  511.             if($post_categorie){
  512.                 foreach($post_categorie as $val){
  513.                     $list_Categorie[]= $val->getCategory()->getId();
  514.                     if($req_categories == null){
  515.                         $this->em->remove($val);
  516.                     }else{
  517.                         if (!in_array($val->getCategory()->getId(), $req_categories)) {
  518.                             $this->em->remove($val);
  519.                         }
  520.                     }
  521.                 }
  522.             }
  523.             if($req_categories){
  524.                 foreach($req_categories as $id){
  525.                         $category $this->getDoctrine()->getRepository(Category::class)->find($id);
  526.                         $PostCategoryExiste $this->getDoctrine()->getRepository(PostCategory::class)->findOneBy(['Category'=>$category,'Post'=>$post]);
  527.                         if(!$PostCategoryExiste){
  528.                             $newPostCategory = new PostCategory();
  529.                             $newPostCategory->setCategory($category);
  530.                             $newPostCategory->setPost($post);
  531.                             $this->em->persist($newPostCategory);
  532.                         }
  533.                 }
  534.             }
  535.             $this->em->flush(); 
  536.          
  537.            
  538.             $this->addFlash('success''Vos données ont été modifié avec succès.');
  539.             if($request->request->get('saveAndstay') === '1'){
  540.                 return $this->redirectToRoute('admin_post_edit',['id'=> $post->getId(),'type'=>$type]);
  541.             }else{
  542.                 return $this->redirectToRoute('admin_post_list',['type'=> $type]);  
  543.             }
  544.         }
  545.  
  546.         return $this->render('admin/dashboards/post/_edit.html.twig', [
  547.             'form'=> $form->createView(),
  548.             'post'=> $post,
  549.             'type_post'=> $type_post,
  550.             'categories'=> $categories,
  551.             'cp_field'=> $cp_field,
  552.             'liste_metas'=> $liste_metas,
  553.             'liste_label'=> $liste_label,
  554.             'listBloc'=> $list_bloc,
  555.             'NbBloc'=> count($list_bloc),
  556.         ]);
  557.     }
  558.     /**
  559.     * @Route("/post/remove/{id}/{type}", name="admin_post_remove")
  560.     */
  561.     public function admin_remove(Request $request$id,$type=null
  562.     {
  563.         $em $this->getDoctrine()->getManager();
  564.         $post $this->getDoctrine()->getRepository(Post::class)->find($id);
  565.         if($post){
  566.             $date_now = new \DateTime('now');
  567.             $date_creation $post->getCreatedAt()->modify('+15 day');
  568.             if (strtotime($date_creation->format('d-m-Y')) > strtotime($date_now->format('d-m-Y'))) {
  569.                 $post->setPostArchive(true);
  570.                 $post->setDateArchive(new \DateTime('now'));
  571.                 $em->flush();
  572.             }else{
  573.                 $this->addFlash('error''Vous n\'avez pas la possibilité d\'effacer ce contenu.');
  574.                 return $this->redirectToRoute('admin_post_list',['type'=>$type]); 
  575.             }
  576.             
  577.             // $url = $this->getDoctrine()->getRepository(UrlPost::class)->findOneBy(['id_module'=>$post->getId(),'type'=>'Post']);
  578.             // if($url){
  579.             //     $em->remove($url);
  580.             // }
  581.             // $redirection = $this->getDoctrine()->getRepository(Redirection::class)->findOneBy(['id_module'=>$post->getId(),'type_module'=>'Post']);
  582.             // if($redirection){
  583.             //     $em->remove($redirection);
  584.             // }
  585.             // if($post->getPostCategory()){
  586.             //     foreach($post->getPostCategory() as $value){
  587.             //         $post->removePostCategory($value);
  588.             //     }
  589.             // }
  590.    
  591.             // if($post->getPostBloc()){
  592.             //     foreach($post->getPostBloc() as $value){
  593.             //         $post->removePostBloc($value);
  594.             //     }
  595.             // }
  596.             // if($post->getMenu()){
  597.             //     foreach ($post->getMenu() as $value) {
  598.             //         $post->removeMenu($value);
  599.             //     }
  600.             // }
  601.             // if($post->getPostField()){
  602.             //     foreach ($post->getPostField() as $value) {
  603.             //         $post->removePostField($value);
  604.             //     }
  605.             // }
  606.             // if($post->getWidget()){
  607.             //     foreach ($post->getWidget() as $value) {
  608.             //         $post->removeWidget($value);
  609.             //     }
  610.             // }
  611.             // $em->remove($post);
  612.             // $em->flush();
  613.             $this->addFlash('success''Vos données a été supprimée avec succès.');           
  614.             return $this->redirectToRoute('admin_post_list',['type'=>$type]); 
  615.         }else{
  616.             $this->addFlash('error''Error de la suppression du contenu');
  617.             return $this->redirectToRoute('admin_post_list',['type'=>$type]); 
  618.         }
  619.     }
  620.     /**
  621.     * @Route("/post/change_etat/{id}/{type}", name="admin_post_change_etat")
  622.     */
  623.     public function admin_change_etat($id,$type=1,Request $request)
  624.     {
  625.         if(isset($_POST) && !empty($_POST))
  626.         {
  627.             $repo $this->getDoctrine()->getRepository('App:Post');
  628.             $post $repo->find($id);
  629.             if(isset($post) && !empty($post)){
  630.                 if(isset($_POST['active']) && !empty($_POST['active']))
  631.                     $post->setActif(false);
  632.                 else 
  633.                     $post->setActif(true);
  634.             
  635.                 $post->setUpdatedAt(new \DateTime());
  636.                 $em $this->getDoctrine()->getManager();
  637.                 $em->persist($post);
  638.                 $em->flush(); 
  639.                 $this->addFlash('success''L\'état de la catégorie est modifié!');
  640.                 return $this->redirectToRoute('admin_post_list',['type'=>$type]);
  641.             }
  642.             else 
  643.             {
  644.                 $this->addFlash('error''Erreure de modification de l\'état de la catégorie');
  645.                 return $this->redirectToRoute('admin_post_list',['type'=>$type]); 
  646.             }
  647.         }
  648.     }
  649.     // /**
  650.     // * @Route("/bloc-post/change_etat", name="admin_bloc_change_etat", condition="request.isXmlHttpRequest()")
  651.     // */
  652.     // public function admin_bloc_change_etat(Request $request)
  653.     // {
  654.     //     if ($request->isXmlHttpRequest()){
  655.     //         $id =  $request->request->get('id');
  656.     //         $_locale = $request->getLocale();
  657.     //         $em = $this->getDoctrine()->getManager();
  658.     //         $bloc = $this->getDoctrine()->getRepository(PostBloc::class)->find($id);
  659.     //         if(in_array($bloc->getTypeBloc(),['1','2','3','4','10','9','12','7'])){
  660.     //             if($bloc->translate($_locale)->getActifContent() == true){
  661.     //                 $bloc->translate($_locale)->setActifContent(false);
  662.     //             }else{
  663.     //                 $bloc->translate($_locale)->setActifContent(true);
  664.     //             }
  665.     //         }else{
  666.     //             if($bloc->getTypeBloc() == '5'){
  667.     //                 if($bloc->getMediaGallery()->translate($_locale)->getActif() == true){
  668.     //                     $bloc->getMediaGallery()->translate($_locale)->setActif(false);
  669.     //                 }else{
  670.     //                     $bloc->getMediaGallery()->translate($_locale)->setActif(true);
  671.     //                 }
  672.     //             }else{
  673.     //                 if($bloc->getActif() == true){
  674.     //                     $bloc->setActif(false);
  675.     //                 }else{
  676.     //                     $bloc->setActif(true);
  677.     //                 }
  678.     //             }
  679.     //         }
  680.     //         $em->flush();
  681.     //         return new JsonResponse('Etat Bloc has been changed!');
  682.     //     }
  683.      
  684.     // }
  685.     /**
  686.      * @Route("/edit-bloc/{id}/{type}",methods={"GET", "POST"}, name="admin_edit_bloc")
  687.      */
  688.     public function EditBloc(Request $request,$id,$type 'Post',LanguageRepository $language)
  689.     {
  690.         if ($type == 'Post') {
  691.             $Bloc $this->getDoctrine()->getRepository(PostBloc::class)->find($id);
  692.         }else{
  693.             $Bloc $this->getDoctrine()->getRepository(CategoryBloc::class)->find($id);
  694.         }
  695.         $images null;
  696.         //dump($Bloc);
  697.         if($Bloc->getTypeBloc() == '11' || $Bloc->getTypeBloc() == '12'){
  698.             $images $this->getDoctrine()->getRepository(GallerieBlocPost::class)->findBy(['IdBloc'=>$Bloc->getId()]);
  699.         }
  700.         if ($request->isMethod('POST')) {
  701.             $em $this->getDoctrine()->getManager();
  702.             $_locale $request->getLocale();
  703.             $a_languages $language->findActifLang(); 
  704.             $type_bloc $Bloc->getTypeBloc();
  705.             $titre =  $request->request->get('titre_bloc');
  706.             $contenu =  $request->request->get('contenu');
  707.             $editeur =  $request->request->get('description');
  708.             $contenu2 =  $request->request->get('contenu2');
  709.             $titre_template =  $request->request->get('titre_template');
  710.             $contenu3 =  $request->request->get('contenu3');
  711.             $link =  $request->request->get('link');
  712.             $video =  $request->request->get('bloc_video_url');
  713.             $image =  $request->request->get('image');
  714.   
  715.  
  716.             if($type_bloc == '1'){
  717.                 $titre_lien =  $request->request->get('titre_lien');
  718.                 $type_lien =  $request->request->get('TypeLienBloc');
  719.                 $lien_interne =  $request->request->get('lien_interne');
  720.                 $lien_externe =  $request->request->get('lien_externe');
  721.                 $telephone =  $request->request->get('telephone');
  722.                 $bg_color $request->request->get('bgcolor');
  723.                 $Bloc->translate($_locale)->setTitle($titre);
  724.                 $Bloc->translate($_locale)->setContent($editeur);
  725.                 $Bloc->setBgColor($bg_color);
  726.                 if ($type_lien == '2') {
  727.                     $Bloc->translate($_locale)->setTitreLien($titre_lien);
  728.                     $Bloc->translate($_locale)->setTypeLien((int)$type_lien);
  729.                     $Bloc->translate($_locale)->setLienInterne($lien_interne);
  730.                     $Bloc->translate($_locale)->setLienExterne(null);
  731.                     $Bloc->translate($_locale)->setTelephone(null);
  732.                 }elseif($type_lien == '3'){
  733.                     $Bloc->translate($_locale)->setTitreLien($titre_lien);
  734.                     $Bloc->translate($_locale)->setTypeLien((int)$type_lien);
  735.                     $Bloc->translate($_locale)->setLienInterne(null);
  736.                     $Bloc->translate($_locale)->setLienExterne($lien_externe);
  737.                     $Bloc->translate($_locale)->setTelephone(null);
  738.                 }elseif($type_lien == '4'){
  739.                     $Bloc->translate($_locale)->setTitreLien($titre_lien);
  740.                     $Bloc->translate($_locale)->setTypeLien((int)$type_lien);
  741.                     $Bloc->translate($_locale)->setLienInterne(null);
  742.                     $Bloc->translate($_locale)->setLienExterne(null);
  743.                     $Bloc->translate($_locale)->setTelephone($telephone);
  744.                 }elseif($type_lien == '1'){
  745.                     $Bloc->translate($_locale)->setTitreLien($titre_lien);
  746.                     $Bloc->translate($_locale)->setTypeLien((int)$type_lien);
  747.                     $Bloc->translate($_locale)->setLienInterne(null);
  748.                     $Bloc->translate($_locale)->setLienExterne(null);
  749.                     $Bloc->translate($_locale)->setTelephone(null);
  750.                 }
  751.             }
  752.             if($type_bloc == '2'){
  753.                 $Bloc->translate($_locale)->setTitle($titre);
  754.                 $Bloc->translate($_locale)->setlink($link);
  755.             }
  756.             if($type_bloc == '3'){
  757.                 $Bloc->translate($_locale)->setTitle($titre);
  758.                 $Bloc->setImage($image);
  759.             }
  760.             if($type_bloc == '4'){
  761.                 if(!empty($contenu2)){
  762.                     $Bloc->translate($_locale)->setContent2($contenu2);
  763.                 }
  764.             }
  765.             if($type_bloc == '7'){
  766.                 $Bloc->translate($_locale)->setTitle($titre_template);
  767.                 if(!empty($contenu3)){
  768.                     $Bloc->translate($_locale)->setTemplate($contenu3);
  769.                 }
  770.             }
  771.             if($type_bloc == '9'){
  772.                 if(!empty($video)){
  773.                     $Bloc->setVideoUrl($video);
  774.                 }
  775.             }
  776.             if($type_bloc == '10'){
  777.                 $titre_editeur =  $request->request->get('editeur3_titre');
  778.                 $contenu_editeur =  $request->request->get('editeur3_content');
  779.                 $position_editeur =  $request->request->get('editeur3_position');
  780.                 $image_editeur =  $request->request->get('editeur3_image');
  781.                 $video_editeur =  $request->request->get('editeur3_video_url');
  782.                 $grid_image =  $request->request->get('grid_image');
  783.                 $Bloc->translate($_locale)->setTitle($titre_editeur);
  784.                 $Bloc->translate($_locale)->setContent($contenu_editeur);
  785.                 $Bloc->setPositionText($position_editeur);
  786.                 $Bloc->setImage(!empty($image_editeur)?$image_editeur:null);
  787.                 $Bloc->setVideoUrl(!empty($video_editeur)?$video_editeur:null);
  788.                 $Bloc->setGridCarousel($grid_image);
  789.             }
  790.             if($type_bloc == '12'){
  791.                 $carousel_text $request->request->get('carousel_text');
  792.                 $text_position $request->request->get('text_position');
  793.                 $grid_carousel $request->request->get('carousel_grid');
  794.                 $Bloc->translate($_locale)->setContent($carousel_text);
  795.                 $Bloc->setPositionText($text_position);
  796.                 $Bloc->setGridCarousel($grid_carousel);
  797.             }
  798.             // $Bloc->translate($_locale)->setTraduite(true);
  799.             // $Bloc->mergeNewTranslations();
  800.             $em->flush();
  801.             $this->addFlash('success''Le contenu a été modifié');
  802.             if ($type == 'Post') {
  803.                 return $this->redirectToRoute('admin_post_edit',['id'=>$Bloc->getPost()->getId(),'type'=> $Bloc->getPost()->getTypePost()->getId()]);
  804.             }else{
  805.                 return $this->redirectToRoute('admin_category_edit',['id'=>$Bloc->getCategory()->getId(),'IdPost'=> $Bloc->getCategory()->getTypePost()->getId(),'type'=> 'categorie1']);
  806.             }
  807.           
  808.         }
  809.         // dump($Bloc->getTypeBloc());
  810.         if($Bloc->getTypeBloc() == '11'){
  811.             return $this->render('admin/dashboards/post/edit_bloc_carousel.html.twig',[
  812.                 'bloc'=> $Bloc,
  813.                 'images'=> $images,
  814.                 'type'=> $type
  815.             ]);
  816.         }
  817.         if($Bloc->getTypeBloc() == '12'){
  818.             return $this->render('admin/dashboards/post/edit_bloc_carousel2.html.twig',[
  819.                 'bloc'=> $Bloc,
  820.                 'images'=> $images,
  821.                 'type'=> $type
  822.             ]);
  823.         }
  824.         return $this->render('admin/dashboards/post/edit_bloc.html.twig',[
  825.             'bloc'=> $Bloc,
  826.             'images'=> $images,
  827.             'type'=> $type
  828.         ]);
  829.     }
  830.     /**
  831.      * @Route("/edit-order-post",methods={"GET","POST"}, name="admin_edit_post_order", condition="request.isXmlHttpRequest()")
  832.      */
  833.     public function EditOrder(Request $request)
  834.     {
  835.         if ($request->isXmlHttpRequest()) {
  836.             $em $this->getDoctrine()->getManager();
  837.             $data =  $request->request->get('order');
  838.             $index 1;
  839.             foreach($data as $id){
  840.                 $post $this->getDoctrine()->getRepository(Post::class)->find($id);
  841.                 $post->setPosition($index);
  842.                 $em->flush();
  843.                 $index++;
  844.             }
  845.             return new JsonResponse('L\'order à éte changée avec success!');
  846.         }
  847.     }
  848.     public function RenderTemplate(Request $request)
  849.     {
  850.         return $this->render('modele/template_static.html.twig');
  851.     }
  852.     function buildTree(array $elements$parentId 0) {
  853.         $branch = array();
  854.         if($elements){
  855.             foreach ($elements as $element) {
  856.                 if ($element['IdParent'] == $parentId && $element['IdParent2'] == 0) {
  857.                     $children $this->buildTree($elements$element['id']);
  858.     
  859.                     if ($children) {
  860.                         foreach($children as $child){
  861.                             $element['child'][$child['id']] = $child;
  862.                             $children2 $this->buildTree2($elements$child['id']);  
  863.                             if($children2){
  864.                                 $element['child'][$child['id']]['child2'] = $children2;   
  865.                             }
  866.                         }
  867.                     }
  868.     
  869.                     $branch[] = $element;
  870.                 }
  871.             }
  872.         }
  873.         return $branch;
  874.     }
  875.     function buildTree2(array $elements$parentId 0) {
  876.         $branch = array();
  877.         foreach ($elements as $element) {
  878.             if ($element['IdParent2'] == $parentId) {
  879.                 $branch[] = $element;
  880.             }
  881.         }
  882.     
  883.         return $branch;
  884.     }
  885. }