<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\Post;
use App\Entity\User;
use App\Entity\Forms;
use ReflectionObject;
use App\Entity\Widget;
use App\Form\PostType;
use App\Utils\Slugger;
use App\Entity\CpField;
use App\Entity\Product;
use App\Entity\UrlPost;
use App\Entity\Category;
use App\Entity\Language;
use App\Entity\PostBloc;
use App\Entity\TypePost;
use App\Entity\MetasCles;
use App\Entity\CpPostField;
use App\Entity\PostArchive;
use App\Entity\Redirection;
use App\Security\PostVoter;
use App\Entity\CategoryBloc;
use App\Entity\CmsAffichage;
use App\Entity\MediaGallery;
use App\Entity\PostCategory;
use App\Service\EmailService;
use App\Service\FileUploader;
use App\Service\ImageService;
use App\Entity\ParametreLabel;
use App\Service\DuplicatePost;
use App\Entity\GallerieBlocPost;
use App\Service\RefactorSlugPost;
use App\Entity\UrlPostTranslation;
use App\Repository\PostRepository;
use App\Repository\UrlPostRepository;
use App\Repository\LanguageRepository;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\String\Slugger\SluggerInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* @Route("/console")
*/
class PostController extends AbstractController
{
private $imageService;
private $emailService;
private $languages;
private $em;
public function __construct(EntityManagerInterface $em,ImageService $imageService,EmailService $emailService,LanguageRepository $languages)
{
$this->em = $em;
$this->imageService = $imageService;
$this->emailService = $emailService;
$this->languages = $languages->findActifLang();
}
/**
* @Route("/post/{type}", name="admin_post_list")
*/
public function admin_index($type,Request $request)
{
$_locale = $request->getLocale();
$user = $this->getUser();
$IdSite = $this->getUser()->getSite()->getId();
$typePost = $this->getDoctrine()->getRepository(TypePost::class)->find($type);
$posts = $this->getDoctrine()->getRepository(Post::class)->findAllPostByType($_locale,$type,$IdSite);
return $this->render('admin/dashboards/post/_list.html.twig', [
'posts' => $posts,
'i_type_post'=> $type,
'system_name'=> $typePost->getSystemName(),
]);
}
/**
* @Route("/post/duplicate/{id}-{type}", name="admin_post_duplicate")
*/
public function duplicatePost(Request $request,DuplicatePost $duplicatePost,$id,$type)
{
$post = $this->getDoctrine()->getRepository(Post::class)->find($id);
$duplicatePost->duplicatePost($post);
return $this->redirectToRoute('admin_post_list',['type'=>$type]);
}
/**
* @Route("/post/add/{type}", name="admin_post_add")
*/
public function admin_add($type,Request $request,LanguageRepository $language, UrlPostRepository $url_post,FileUploader $fileUploader)
{
$_locale = $request->getLocale();
$liste_categories = $this->getDoctrine()->getRepository(Category::class)->findCategorieActif($_locale,$type);
$categories = $this->buildTree($liste_categories);
$type_post = $this->getDoctrine()->getRepository(TypePost::class)->find($type);
$form = $this->createForm(PostType::class, null, array(
'action' => $this->generateUrl($request->get('_route'),array('type'=>$type)),
))->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$new_post = new Post();
$data = $form->getData();
$image = $request->request->get('image');
if($image){
$new_post->setImage($image);
$this->imageService->thumbnail($image);
}
$image_bandeau = $request->request->get('image_bandeau');
if($image_bandeau){
$new_post->setImageBandeau($image_bandeau);
$this->imageService->thumbnail($image_bandeau);
}
$image_couverture = $request->request->get('image_couverture');
if($image_couverture){
$new_post->setImageCouverture($image_couverture);
$this->imageService->thumbnail($image_couverture);
}
$url_video = $request->request->get('video_url');
if($url_video != ''){
$new_post->setUrlVideo($url_video);
}
if (isset($data['authorization_api'])) {
$new_post->setAuthorizationApi($data['authorization_api']);
}
$new_post->setActifHome($data['actif_home']);
$new_post->setTypePost($type_post);
$new_post->setAuthor($this->getUser());
$new_post->setTagManagerHead($data['tag_managerhead']);
$new_post->setTagManagerBody($data['tag_managerbody']);
if ($this->languages) {
foreach ($this->languages as $language) {
if (empty($data['canonical'])) {
$new_post->translate($language->getLanguageCode())->setCanonical($data['slug']);
}else {
$new_post->translate($language->getLanguageCode())->setCanonical($data['canonical']);
}
$new_post->translate($language->getLanguageCode())->setActif($data['actif']);
$new_post->translate($language->getLanguageCode())->setTitleAffichage($data['title_affichage']);
$new_post->translate($language->getLanguageCode())->setSchema($data['schema']);
$new_post->translate($language->getLanguageCode())->setTitle($data['titre']);
$new_post->translate($language->getLanguageCode())->setSummary($data['resume']);
$new_post->translate($language->getLanguageCode())->setSlug($data['slug']);
$new_post->translate($language->getLanguageCode())->setContent($data['texte']);
$new_post->translate($language->getLanguageCode())->setImageAlt($data['alt_image']);
$new_post->translate($language->getLanguageCode())->setImageBandeauAlt($data['alt_image_bandeau']);
$new_post->translate($language->getLanguageCode())->setImageCouvertureAlt($data['alt_image_couverture']);
if (empty($data['meta_title'])) {
$new_post->translate($language->getLanguageCode())->setMetaTitle($data['titre']);
}else{
$new_post->translate($language->getLanguageCode())->setMetaTitle($data['meta_title']);
}
if (empty($data['meta_description']) && !empty($data['texte'])) {
$new_post->translate($language->getLanguageCode())->setMetaDescription(trim(substr(strip_tags(html_entity_decode($data['texte'], ENT_QUOTES | ENT_HTML5, 'UTF-8')), 0, 150).'...'));
}else{
$new_post->translate($language->getLanguageCode())->setMetaDescription($data['meta_description']);
}
$new_post->translate($language->getLanguageCode())->setMetaRobots($data['meta_robots']);
$new_post->translate($language->getLanguageCode())->setSettigMetaTitle($data['settig_meta_title']);
$new_post->translate($language->getLanguageCode())->setSettigMetaDesc($data['settig_meta_Desc']);
$new_post->translate($language->getLanguageCode())->setTraduite(false);
$new_post->translate($language->getLanguageCode())->setTitreLien($data['titre_lien']);
$new_post->translate($language->getLanguageCode())->setTypeLien($data['type_lien']);
$new_post->translate($language->getLanguageCode())->setLienExterne($data['lien_externe']);
$new_post->translate($language->getLanguageCode())->setLienInterne($data['lien_interne']);
$new_post->translate($language->getLanguageCode())->setTelephone($data['telephone']);
}
}
$this->em->persist($new_post);
$new_post->mergeNewTranslations();
$this->em->flush();
$new_url_post = new UrlPost();
$new_url_post->setIdModule($new_post->getId());
$new_url_post->setType('Post');
if($this->languages){
foreach($this->languages as $language)
{
$new_url_post->translate($language->getLanguageCode())->setSlug($data['slug']);
}
}
$this->em->persist($new_url_post);
$new_url_post->mergeNewTranslations();
$this->em->flush();
// persist category post
$reqCategories = $request->request->get('Categories');
if($reqCategories){
foreach($reqCategories as $id){
$category = $this->getDoctrine()->getRepository(Category::class)->find($id);
$post_category = new PostCategory();
$post_category->setCategory($category);
$post_category->setPost($new_post);
$this->em->persist($post_category);
}
}
$this->em->flush();
$this->addFlash('success', 'Vos données ont été enregistrées avec succès.');
if($request->request->get('saveAndstay') === '1'){
return $this->redirectToRoute('admin_post_edit',['id'=> $new_post->getId(),'type'=>$type]);
}else{
return $this->redirectToRoute('admin_post_list',['type'=> $type ]);
}
}
return $this->render('admin/dashboards/post/_create.html.twig', array(
'form' => $form->createView(),
'type_post'=> $type_post,
'categories'=> $categories
));
}
/**
* @Route("/post/edit/{id}/{type}", name="admin_post_edit")
*/
public function admin_edit($id,$type,Request $request,DuplicatePost $duplicate_post,RefactorSlugPost $refractorslug,LanguageRepository $language,UrlPostRepository $urlpost,FileUploader $fileUploader,SluggerInterface $slugger,RefactorSlugPost $refactorSlug)
{
$_locale = $request->getLocale();
$post = $this->getDoctrine()->getRepository(Post::class)->find($id);
$type_post = $this->getDoctrine()->getRepository(TypePost::class)->find($type);
$liste_categories = $this->getDoctrine()->getRepository(Category::class)->findCategorieActif($_locale,$type);
$categories = $this->buildTree($liste_categories);
$cp_field = $this->getDoctrine()->getRepository(CpField::class)->findBy(['PostType'=>$type]);
$liste_metas = [];
$liste_label = $this->getDoctrine()->getRepository(ParametreLabel::class)->findAll();
$MetasCles = $this->getDoctrine()->getRepository(MetasCles::class)->findAll();
$NameModule = $type_post->translate($_locale)->getSystemName();
if($MetasCles){
foreach($MetasCles as $meta){
if(in_array(strtolower($NameModule),explode(",",$meta->getModuleMetas()))){
$liste_metas[] = $meta;
}
}
}
$list_bloc = $this->getDoctrine()->getRepository(PostBloc::class)->findBlocNotDeleted($id);
$ancien_slug = $post->translate($_locale)->getSlug();
$Url = [];
$canonicalUrl = [];
$ListeSlug = $this->getDoctrine()->getRepository(URLPost::class)->findAll();
if($ListeSlug){
foreach($ListeSlug as $value){
$slug = $value->translate($_locale)->getSlug();
$post_slug = $this->getDoctrine()->getRepository(Post::class)->find($value->getIdModule());
if($post_slug){
if($refractorslug->getPath($post_slug,$slug) != null){
$canonicalUrl[]= $refractorslug->getPath($post_slug,$slug);
}
}
if($ancien_slug != $slug){
$Url[] = $slug;
}
}
}
$data = ['actif'=> $post->translate($_locale)->getActif(),
'actif_home'=> $post->getActifHome(),
'authorization_api'=> $post->getAuthorizationApi(),
'titre'=> $post->translate($_locale)->getTitle(),
'slug'=> $post->translate($_locale)->getSlug(),
'title_affichage'=> $post->translate($_locale)->getTitleAffichage(),
'resume'=> $post->translate($_locale)->getSummary(),
'texte'=> $post->translate($_locale)->getContent(),
'alt_image'=> $post->translate($_locale)->getImageAlt(),
'alt_image_bandeau'=> $post->translate($_locale)->getImageBandeauAlt(),
'alt_image_couverture'=> $post->translate($_locale)->getImageCouvertureAlt(),
'date_publication'=> $post->getDatePublication(),
'meta_title'=> $post->translate($_locale)->getMetaTitle(),
'meta_description'=> $post->translate($_locale)->getMetaDescription(),
'meta_robots'=> $post->translate($_locale)->getMetaRobots(),
'settig_meta_title'=> $post->translate($_locale)->getSettigMetaTitle(),
'settig_meta_Desc'=> $post->translate($_locale)->getSettigMetaDesc(),
'tag_managerhead'=> $post->getTagManagerHead(),
'tag_managerbody'=> $post->getTagManagerBody(),
'titre_lien'=> $post->translate($_locale)->getTitreLien(),
'type_lien'=> $post->translate($_locale)->getTypeLien(),
'lien_externe'=> $post->translate($_locale)->getLienExterne(),
'lien_interne'=> $post->translate($_locale)->getLienInterne(),
'telephone'=> $post->translate($_locale)->getTelephone(),
];
$form = $this->createForm(PostType::class, $data,[
'defaultValue'=>$post->translate($_locale)->getTypeLien() != null?$post->translate($_locale)->getTypeLien():1,
'action' => $this->generateUrl($request->get('_route'),array('id'=>$id,'type'=>$type))
])->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
if($ancien_slug != $data['slug']){
$url_post_existe = $this->getDoctrine()->getRepository(UrlPost::class)->findUrlBySlug($ancien_slug);
if($url_post_existe){
$url_post_existe->setType('Post');
$url_post_existe->setIdModule($post->getId());
$url_post_existe->translate($_locale)->setSlug($data['slug']);
$this->em->flush();
}
if(in_array($data['slug'], $Url)) {
$this->addFlash('error', 'URL existe déjà!');
return $this->redirectToRoute('admin_post_edit',['id'=> $post->getId(),'type'=> $type]);
}else{
$redirection = $this->getDoctrine()->getRepository(Redirection::class)->findOneBy(['id_module'=>$post->getId(),'type_module'=>'Post','locale'=>$_locale]);
if($refactorSlug->getPath($post,$data['slug']) != null){
if($redirection){
$redirection->setOld($refactorSlug->getPath($post,$ancien_slug));
$redirection->setNew($refactorSlug->getPath($post,$data['slug']));
$redirection->setIdModule($post->getId());
$redirection->setTypeModule('Post');
$redirection->setLocale($_locale);
$this->em->flush();
}else{
$newRedirection = new Redirection();
$newRedirection->setOld($refactorSlug->getPath($post,$ancien_slug));
$newRedirection->setNew($refactorSlug->getPath($post,$data['slug']));
$newRedirection->setIdModule($post->getId());
$newRedirection->setTypeModule('Post');
$newRedirection->setActive(true);
$newRedirection->setSite($this->getUser()->getSite());
$newRedirection->setLocale($_locale);
$this->em->persist($newRedirection);
$this->em->flush();
}
}
$post->translate($_locale)->setSlug($data['slug']);
$url_post = $this->getDoctrine()->getRepository(UrlPost::class)->findOneBy(['id_module'=>$post->getId(),'type'=> 'Post']);
$url_post->translate($_locale)->setSlug($data['slug']);
$this->em->flush();
}
}
$duplicate_post->ArchivePost($post);
$image = $request->request->get('image');
if(!empty($image)){
$post->setImage($image);
$this->imageService->thumbnail($image);
}else{
$post->setImage(null);
}
$image_bandeau = $request->request->get('image_bandeau');
if(!empty($image_bandeau)){
$post->setImageBandeau($image_bandeau);
$this->imageService->thumbnail($image_bandeau);
}else{
$post->setImageBandeau(null);
}
$image_couverture = $request->request->get('image_couverture');
if(!empty($image_couverture)){
$post->setImageCouverture($image_couverture);
$this->imageService->thumbnail($image_couverture);
}else{
$post->setImageCouverture(null);
}
$url_video = $request->request->get('video_url');
if(!empty($url_video)){
$post->setUrlVideo($url_video);
}else{
$post->setUrlVideo(null);
}
if (isset($data['authorization_api'])) {
$post->setAuthorizationApi($data['authorization_api']);
}
$post->setActifHome($data['actif_home']);
$post->setTypePost($type_post);
$post->setAuthor($this->getUser());
$post->setTagManagerHead($data['tag_managerhead']);
$post->setTagManagerBody($data['tag_managerbody']);
if (empty($data['canonical'])) {
$post->translate($_locale)->setCanonical($data['slug']);
}else {
$post->translate($_locale)->setCanonical($data['canonical']);
}
$post->translate($_locale)->setActif($data['actif']);
$post->translate($_locale)->setTitleAffichage($data['title_affichage']);
$post->translate($_locale)->setSchema($data['schema']);
$post->translate($_locale)->setTitle($data['titre']);
$post->translate($_locale)->setSummary($data['resume']);
$post->translate($_locale)->setSlug($data['slug']);
$post->translate($_locale)->setContent($data['texte']);
$post->translate($_locale)->setImageAlt($data['alt_image']);
$post->translate($_locale)->setImageBandeauAlt($data['alt_image_bandeau']);
$post->translate($_locale)->setImageCouvertureAlt($data['alt_image_couverture']);
if (empty($data['meta_title'])) {
$post->translate($_locale)->setMetaTitle($data['titre']);
}else{
$post->translate($_locale)->setMetaTitle($data['meta_title']);
}
if (empty($data['meta_description']) && !empty($data['texte'])) {
$post->translate($_locale)->setMetaDescription(trim(substr(strip_tags(html_entity_decode($data['texte'], ENT_QUOTES | ENT_HTML5, 'UTF-8')), 0, 150).'...'));
}else{
$post->translate($_locale)->setMetaDescription($data['meta_description']);
}
$post->translate($_locale)->setMetaRobots($data['meta_robots']);
$post->translate($_locale)->setSettigMetaTitle($data['settig_meta_title']);
$post->translate($_locale)->setSettigMetaDesc($data['settig_meta_Desc']);
$post->translate($_locale)->setTraduite(false);
$post->translate($_locale)->setTitreLien($data['titre_lien']);
$post->translate($_locale)->setTypeLien($data['type_lien']);
$post->translate($_locale)->setLienExterne($data['lien_externe']);
$post->translate($_locale)->setLienInterne($data['lien_interne']);
$post->translate($_locale)->setTelephone($data['telephone']);
$this->em->flush();
// persist champs personnalisée after persist post
$post_field = $request->request->get('post_field');
$list_post_field = $post->getPostField();
if($cp_field){
$index = 0;
foreach($cp_field as $field){
$PostField = $this->getDoctrine()->getRepository(CpPostField::class)->findOneBy(['CpField'=>$field,'IdPost'=>$post->getId()]);
if($PostField){
if($field->translate($_locale)->getType() == 'text'){
$PostField->setTextValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'textarea'){
$PostField->setTextareaValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'checkbox'){
if(isset($post_field[$index])){
$PostField->setCheckboxValue(true);
}else{
$PostField->setCheckboxValue(false);
}
}
if($field->translate($_locale)->getType() == 'date'){
$PostField->setDateValue(new \DateTime());
}
if($field->translate($_locale)->getType() == 'radio'){
$PostField->setRadioValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'link'){
$PostField->setLinkValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'image'){
$delete_image_p = $request->request->get('delete_image_p');
if($delete_image_p){
$PostField->setImageValue(null);
}
$image_p = $request->files->get('image_p');
if ($image_p) {
$originalFilename = pathinfo($image_p->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$image_p->guessExtension();
$PostField->setImageValue($newFilename);
$image_p->move($this->getParameter('image_directory'), $newFilename);
}
}
$PostField->setIdPost($post->getId());
$this->em->flush();
}else{
$PostField = new CpPostField();
$PostField->setCpField($field);
if($field->translate($_locale)->getType() == 'text'){
$PostField->setTextValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'textarea'){
$PostField->setTextareaValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'checkbox'){
if(isset($post_field[$index])){
$PostField->setCheckboxValue(true);
}else{
$PostField->setCheckboxValue(false);
}
}
if($field->translate($_locale)->getType() == 'date'){
$PostField->setDateValue(new \DateTime());
}
if($field->translate($_locale)->getType() == 'radio'){
$PostField->setRadioValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'link'){
$PostField->setLinkValue($post_field[$index]);
}
if($field->translate($_locale)->getType() == 'image'){
$delete_image_p = $request->request->get('delete_image_p');
if($delete_image_p){
$PostField->setImageValue(null);
}
$image_p = $request->files->get('image_p');
if ($image_p) {
$originalFilename = pathinfo($image_p->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$image_p->guessExtension();
$PostField->setImageValue($newFilename);
$image_p->move($this->getParameter('image_directory'), $newFilename);
}
}
$PostField->setIdPost($post->getId());
$this->em->persist($PostField);
$post->addPostField($PostField);
$this->em->flush();
}
$index++;
}
}
// Modification entity post categories
$req_categories = $request->request->get('Categories');
$post_categorie = $post->getPostCategory();
$list_Categorie = [];
if($post_categorie){
foreach($post_categorie as $val){
$list_Categorie[]= $val->getCategory()->getId();
if($req_categories == null){
$this->em->remove($val);
}else{
if (!in_array($val->getCategory()->getId(), $req_categories)) {
$this->em->remove($val);
}
}
}
}
if($req_categories){
foreach($req_categories as $id){
$category = $this->getDoctrine()->getRepository(Category::class)->find($id);
$PostCategoryExiste = $this->getDoctrine()->getRepository(PostCategory::class)->findOneBy(['Category'=>$category,'Post'=>$post]);
if(!$PostCategoryExiste){
$newPostCategory = new PostCategory();
$newPostCategory->setCategory($category);
$newPostCategory->setPost($post);
$this->em->persist($newPostCategory);
}
}
}
$this->em->flush();
$this->addFlash('success', 'Vos données ont été modifié avec succès.');
if($request->request->get('saveAndstay') === '1'){
return $this->redirectToRoute('admin_post_edit',['id'=> $post->getId(),'type'=>$type]);
}else{
return $this->redirectToRoute('admin_post_list',['type'=> $type]);
}
}
return $this->render('admin/dashboards/post/_edit.html.twig', [
'form'=> $form->createView(),
'post'=> $post,
'type_post'=> $type_post,
'categories'=> $categories,
'cp_field'=> $cp_field,
'liste_metas'=> $liste_metas,
'liste_label'=> $liste_label,
'listBloc'=> $list_bloc,
'NbBloc'=> count($list_bloc),
]);
}
/**
* @Route("/post/remove/{id}/{type}", name="admin_post_remove")
*/
public function admin_remove(Request $request, $id,$type=null)
{
$em = $this->getDoctrine()->getManager();
$post = $this->getDoctrine()->getRepository(Post::class)->find($id);
if($post){
$date_now = new \DateTime('now');
$date_creation = $post->getCreatedAt()->modify('+15 day');
if (strtotime($date_creation->format('d-m-Y')) > strtotime($date_now->format('d-m-Y'))) {
$post->setPostArchive(true);
$post->setDateArchive(new \DateTime('now'));
$em->flush();
}else{
$this->addFlash('error', 'Vous n\'avez pas la possibilité d\'effacer ce contenu.');
return $this->redirectToRoute('admin_post_list',['type'=>$type]);
}
// $url = $this->getDoctrine()->getRepository(UrlPost::class)->findOneBy(['id_module'=>$post->getId(),'type'=>'Post']);
// if($url){
// $em->remove($url);
// }
// $redirection = $this->getDoctrine()->getRepository(Redirection::class)->findOneBy(['id_module'=>$post->getId(),'type_module'=>'Post']);
// if($redirection){
// $em->remove($redirection);
// }
// if($post->getPostCategory()){
// foreach($post->getPostCategory() as $value){
// $post->removePostCategory($value);
// }
// }
// if($post->getPostBloc()){
// foreach($post->getPostBloc() as $value){
// $post->removePostBloc($value);
// }
// }
// if($post->getMenu()){
// foreach ($post->getMenu() as $value) {
// $post->removeMenu($value);
// }
// }
// if($post->getPostField()){
// foreach ($post->getPostField() as $value) {
// $post->removePostField($value);
// }
// }
// if($post->getWidget()){
// foreach ($post->getWidget() as $value) {
// $post->removeWidget($value);
// }
// }
// $em->remove($post);
// $em->flush();
$this->addFlash('success', 'Vos données a été supprimée avec succès.');
return $this->redirectToRoute('admin_post_list',['type'=>$type]);
}else{
$this->addFlash('error', 'Error de la suppression du contenu');
return $this->redirectToRoute('admin_post_list',['type'=>$type]);
}
}
/**
* @Route("/post/change_etat/{id}/{type}", name="admin_post_change_etat")
*/
public function admin_change_etat($id,$type=1,Request $request)
{
if(isset($_POST) && !empty($_POST))
{
$repo = $this->getDoctrine()->getRepository('App:Post');
$post = $repo->find($id);
if(isset($post) && !empty($post)){
if(isset($_POST['active']) && !empty($_POST['active']))
$post->setActif(false);
else
$post->setActif(true);
$post->setUpdatedAt(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
$this->addFlash('success', 'L\'état de la catégorie est modifié!');
return $this->redirectToRoute('admin_post_list',['type'=>$type]);
}
else
{
$this->addFlash('error', 'Erreure de modification de l\'état de la catégorie');
return $this->redirectToRoute('admin_post_list',['type'=>$type]);
}
}
}
// /**
// * @Route("/bloc-post/change_etat", name="admin_bloc_change_etat", condition="request.isXmlHttpRequest()")
// */
// public function admin_bloc_change_etat(Request $request)
// {
// if ($request->isXmlHttpRequest()){
// $id = $request->request->get('id');
// $_locale = $request->getLocale();
// $em = $this->getDoctrine()->getManager();
// $bloc = $this->getDoctrine()->getRepository(PostBloc::class)->find($id);
// if(in_array($bloc->getTypeBloc(),['1','2','3','4','10','9','12','7'])){
// if($bloc->translate($_locale)->getActifContent() == true){
// $bloc->translate($_locale)->setActifContent(false);
// }else{
// $bloc->translate($_locale)->setActifContent(true);
// }
// }else{
// if($bloc->getTypeBloc() == '5'){
// if($bloc->getMediaGallery()->translate($_locale)->getActif() == true){
// $bloc->getMediaGallery()->translate($_locale)->setActif(false);
// }else{
// $bloc->getMediaGallery()->translate($_locale)->setActif(true);
// }
// }else{
// if($bloc->getActif() == true){
// $bloc->setActif(false);
// }else{
// $bloc->setActif(true);
// }
// }
// }
// $em->flush();
// return new JsonResponse('Etat Bloc has been changed!');
// }
// }
/**
* @Route("/edit-bloc/{id}/{type}",methods={"GET", "POST"}, name="admin_edit_bloc")
*/
public function EditBloc(Request $request,$id,$type = 'Post',LanguageRepository $language)
{
if ($type == 'Post') {
$Bloc = $this->getDoctrine()->getRepository(PostBloc::class)->find($id);
}else{
$Bloc = $this->getDoctrine()->getRepository(CategoryBloc::class)->find($id);
}
$images = null;
//dump($Bloc);
if($Bloc->getTypeBloc() == '11' || $Bloc->getTypeBloc() == '12'){
$images = $this->getDoctrine()->getRepository(GallerieBlocPost::class)->findBy(['IdBloc'=>$Bloc->getId()]);
}
if ($request->isMethod('POST')) {
$em = $this->getDoctrine()->getManager();
$_locale = $request->getLocale();
$a_languages = $language->findActifLang();
$type_bloc = $Bloc->getTypeBloc();
$titre = $request->request->get('titre_bloc');
$contenu = $request->request->get('contenu');
$editeur = $request->request->get('description');
$contenu2 = $request->request->get('contenu2');
$titre_template = $request->request->get('titre_template');
$contenu3 = $request->request->get('contenu3');
$link = $request->request->get('link');
$video = $request->request->get('bloc_video_url');
$image = $request->request->get('image');
if($type_bloc == '1'){
$titre_lien = $request->request->get('titre_lien');
$type_lien = $request->request->get('TypeLienBloc');
$lien_interne = $request->request->get('lien_interne');
$lien_externe = $request->request->get('lien_externe');
$telephone = $request->request->get('telephone');
$bg_color = $request->request->get('bgcolor');
$Bloc->translate($_locale)->setTitle($titre);
$Bloc->translate($_locale)->setContent($editeur);
$Bloc->setBgColor($bg_color);
if ($type_lien == '2') {
$Bloc->translate($_locale)->setTitreLien($titre_lien);
$Bloc->translate($_locale)->setTypeLien((int)$type_lien);
$Bloc->translate($_locale)->setLienInterne($lien_interne);
$Bloc->translate($_locale)->setLienExterne(null);
$Bloc->translate($_locale)->setTelephone(null);
}elseif($type_lien == '3'){
$Bloc->translate($_locale)->setTitreLien($titre_lien);
$Bloc->translate($_locale)->setTypeLien((int)$type_lien);
$Bloc->translate($_locale)->setLienInterne(null);
$Bloc->translate($_locale)->setLienExterne($lien_externe);
$Bloc->translate($_locale)->setTelephone(null);
}elseif($type_lien == '4'){
$Bloc->translate($_locale)->setTitreLien($titre_lien);
$Bloc->translate($_locale)->setTypeLien((int)$type_lien);
$Bloc->translate($_locale)->setLienInterne(null);
$Bloc->translate($_locale)->setLienExterne(null);
$Bloc->translate($_locale)->setTelephone($telephone);
}elseif($type_lien == '1'){
$Bloc->translate($_locale)->setTitreLien($titre_lien);
$Bloc->translate($_locale)->setTypeLien((int)$type_lien);
$Bloc->translate($_locale)->setLienInterne(null);
$Bloc->translate($_locale)->setLienExterne(null);
$Bloc->translate($_locale)->setTelephone(null);
}
}
if($type_bloc == '2'){
$Bloc->translate($_locale)->setTitle($titre);
$Bloc->translate($_locale)->setlink($link);
}
if($type_bloc == '3'){
$Bloc->translate($_locale)->setTitle($titre);
$Bloc->setImage($image);
}
if($type_bloc == '4'){
if(!empty($contenu2)){
$Bloc->translate($_locale)->setContent2($contenu2);
}
}
if($type_bloc == '7'){
$Bloc->translate($_locale)->setTitle($titre_template);
if(!empty($contenu3)){
$Bloc->translate($_locale)->setTemplate($contenu3);
}
}
if($type_bloc == '9'){
if(!empty($video)){
$Bloc->setVideoUrl($video);
}
}
if($type_bloc == '10'){
$titre_editeur = $request->request->get('editeur3_titre');
$contenu_editeur = $request->request->get('editeur3_content');
$position_editeur = $request->request->get('editeur3_position');
$image_editeur = $request->request->get('editeur3_image');
$video_editeur = $request->request->get('editeur3_video_url');
$grid_image = $request->request->get('grid_image');
$Bloc->translate($_locale)->setTitle($titre_editeur);
$Bloc->translate($_locale)->setContent($contenu_editeur);
$Bloc->setPositionText($position_editeur);
$Bloc->setImage(!empty($image_editeur)?$image_editeur:null);
$Bloc->setVideoUrl(!empty($video_editeur)?$video_editeur:null);
$Bloc->setGridCarousel($grid_image);
}
if($type_bloc == '12'){
$carousel_text = $request->request->get('carousel_text');
$text_position = $request->request->get('text_position');
$grid_carousel = $request->request->get('carousel_grid');
$Bloc->translate($_locale)->setContent($carousel_text);
$Bloc->setPositionText($text_position);
$Bloc->setGridCarousel($grid_carousel);
}
// $Bloc->translate($_locale)->setTraduite(true);
// $Bloc->mergeNewTranslations();
$em->flush();
$this->addFlash('success', 'Le contenu a été modifié');
if ($type == 'Post') {
return $this->redirectToRoute('admin_post_edit',['id'=>$Bloc->getPost()->getId(),'type'=> $Bloc->getPost()->getTypePost()->getId()]);
}else{
return $this->redirectToRoute('admin_category_edit',['id'=>$Bloc->getCategory()->getId(),'IdPost'=> $Bloc->getCategory()->getTypePost()->getId(),'type'=> 'categorie1']);
}
}
// dump($Bloc->getTypeBloc());
if($Bloc->getTypeBloc() == '11'){
return $this->render('admin/dashboards/post/edit_bloc_carousel.html.twig',[
'bloc'=> $Bloc,
'images'=> $images,
'type'=> $type
]);
}
if($Bloc->getTypeBloc() == '12'){
return $this->render('admin/dashboards/post/edit_bloc_carousel2.html.twig',[
'bloc'=> $Bloc,
'images'=> $images,
'type'=> $type
]);
}
return $this->render('admin/dashboards/post/edit_bloc.html.twig',[
'bloc'=> $Bloc,
'images'=> $images,
'type'=> $type
]);
}
/**
* @Route("/edit-order-post",methods={"GET","POST"}, name="admin_edit_post_order", condition="request.isXmlHttpRequest()")
*/
public function EditOrder(Request $request)
{
if ($request->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$data = $request->request->get('order');
$index = 1;
foreach($data as $id){
$post = $this->getDoctrine()->getRepository(Post::class)->find($id);
$post->setPosition($index);
$em->flush();
$index++;
}
return new JsonResponse('L\'order à éte changée avec success!');
}
}
public function RenderTemplate(Request $request)
{
return $this->render('modele/template_static.html.twig');
}
function buildTree(array $elements, $parentId = 0) {
$branch = array();
if($elements){
foreach ($elements as $element) {
if ($element['IdParent'] == $parentId && $element['IdParent2'] == 0) {
$children = $this->buildTree($elements, $element['id']);
if ($children) {
foreach($children as $child){
$element['child'][$child['id']] = $child;
$children2 = $this->buildTree2($elements, $child['id']);
if($children2){
$element['child'][$child['id']]['child2'] = $children2;
}
}
}
$branch[] = $element;
}
}
}
return $branch;
}
function buildTree2(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['IdParent2'] == $parentId) {
$branch[] = $element;
}
}
return $branch;
}
}