<?php
namespace App\Controller;
use App\Entity\Article;
use App\Repository\ArticleRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
class BlogController extends AbstractController
{
#[Route('/blog', name: 'app_blog')]
public function index(Request $request, ArticleRepository $articleRepository, PaginatorInterface $paginator): Response
{
$lesArticles = $paginator->paginate(
$articleRepository->findBy(['statut' => true], ['id' => 'desc']),
$request->query->getInt('page', 1), /*page number*/
5 /*limit per page*/
);
return $this->render('blog/index.html.twig', [
'articles' => $lesArticles,
'actif1' => '',
'actif2' => '',
'actif3' => '',
'actif4' => 'active',
'actif5' => '',
]);
}
#[Route('/admin/blog', name: 'app_admin_blog')]
public function blog(ArticleRepository $articleRepository): Response
{
$lesArticles = $articleRepository->findBy([], ['id' => 'desc']);
// dd($lesArticles);
return $this->render('blog/article_liste.html.twig', [
'articles' => $lesArticles,
'active1' => '',
'active2' => '',
'active3' => '',
'active4' => '',
'active5' => '',
'active6' => 'active',
'active7' => '',
'actif1' => '',
'actif2' => '',
'actif3' => '',
'actif4' => 'active',
'actif5' => '',
]);
}
#[Route('/admin/blog/article', name: 'app_admin_blog_article')]
public function blog_article(): Response
{
return $this->render('blog/article_add.html.twig', [
'active1' => '',
'active2' => '',
'active3' => '',
'active4' => '',
'active5' => '',
'active6' => 'active',
'active7' => '',
'actif1' => '',
'actif2' => '',
'actif3' => '',
'actif4' => 'active',
'actif5' => '',
]);
}
#[Route('/blog/{id}-{titre}', name: 'app_admin_blog_detail')]
public function blog_detail($id, ArticleRepository $articleRepository): Response
{
$article = $articleRepository->find($id);
return $this->render('blog/article_detail.html.twig',[
'article'=>$article,
'articles' => $articleRepository->findBy([],['id' => 'desc']),
'actif1' => '',
'actif2' => '',
'actif3' => '',
'actif4' => 'active',
'actif5' => '',
]);
}
#[Route('/admin/blog/{id}/article', name: 'app_admin_blog_update')]
public function blog_update($id, ArticleRepository $articleRepository): Response
{
$article = $articleRepository->find($id);
return $this->render('blog/article_update.html.twig', [
'article'=>$article,
'active1' => '',
'active2' => '',
'active3' => '',
'active4' => '',
'active5' => '',
'active6' => 'active',
'active7' => '',
'actif1' => '',
'actif2' => '',
'actif3' => '',
'actif4' => 'active',
'actif5' => '',
]);
}
#[Route('/admin/blog/{id}/article/delete', name: 'app_admin_blog_delete')]
public function blog_delete($id, ArticleRepository $articleRepository, EntityManagerInterface $emi): Response
{
$article = $articleRepository->find($id);
$article->setStatut(false);
$emi->persist($article);
$emi->flush();
return $this->redirectToRoute('app_admin_blog');
}
#[Route('/admin/blog/article/save', name: 'app_admin_blog_article_save')]
public function blog_article_save(Request $request, EntityManagerInterface $emi): Response
{
if ($request->isMethod('post')) {
$data = $request->request->all();
// dd($data);
$datas = $request->request->all();
$file = $request->files->get('fichier');
$article = new Article();
$article->setTitre($datas['titre']);
$article->setMiniDescription($datas['miniDescription']);
$article->setDescription($datas['description']);
$article->setUser($this->getUser());
if (isset($data['statut'])) {
$article->setStatut(true);
}else{
$article->setStatut(false);
}
$article->setCreatedAt(new \DateTimeImmutable());
if ($file->guessExtension() === 'jpg' || $file->guessExtension() === 'jpeg' || $file->guessExtension() === 'png' || $file->guessExtension() === 'gif') {
// this is needed to safely include the file name as part of the URL
$safeFilename = uniqid() . '.' . $file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
'images/articles',
$safeFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$article->setImage($safeFilename);
$emi->persist($article);
$emi->flush();
$this->addFlash('notif', 'Article bien enregistré');
} else {
$this->addFlash('notif', 'Le fichier n\'a pas pu etre envoyer.. sassurez-vous qu\'il s\'agit bien d\'un fichier Image');
}
}
return $this->redirectToRoute('app_admin_blog');
}
#[Route('/admin/blog/article/{id}/update', name: 'app_admin_blog_article_update')]
public function blog_article_update(Request $request, EntityManagerInterface $emi,$id,ArticleRepository $articleRepository): Response
{
if ($request->isMethod('post')) {
$data = $request->request->all();
// dd($data);
$datas = $request->request->all();
$file = $request->files->get('fichier');
$article = $articleRepository->find($id); //mod
$article->setTitre($datas['titre']);
$article->setMiniDescription($datas['miniDescription']);
$article->setDescription($datas['description']);
$article->setUser($this->getUser());
if (isset($data['statut'])) {
$article->setStatut(true);
}else{
$article->setStatut(false);
}
$article->setCreatedAt(new \DateTimeImmutable());
if ($file != null ) {
if ($file->guessExtension() === 'jpg' || $file->guessExtension() === 'jpeg' || $file->guessExtension() === 'png' || $file->guessExtension() === 'gif') {
// this is needed to safely include the file name as part of the URL
$safeFilename = uniqid() . '.' . $file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
'images/articles',
$safeFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$article->setImage($safeFilename);
$emi->persist($article);
$emi->flush();
$this->addFlash('notif', 'Article bien modifié');
} else {
$this->addFlash('notif', 'Le fichier n\'a pas pu etre envoyer.. sassurez-vous qu\'il s\'agit bien d\'un fichier Image');
}
}else{
$articl = $articleRepository->find($id);
$article->setImage($articl->getImage());
$emi->persist($article);
$emi->flush();
}
}
return $this->redirectToRoute('app_admin_blog');
}
}