<?php
namespace App\Controller;
use App\Entity\Article;
use App\Entity\Event;
use App\Entity\Gallery;
use App\Entity\Project;
use App\Entity\TeamMember;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;
use App\Entity\ContactMessage;
class MainController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @Route("/", name="app_home")
*/
public function index(): Response
{
$events = $this->em->getRepository(Event::class)->findBy([], ['date' => 'ASC'], 3);
$articles = $this->em->getRepository(Article::class)->findBy([], ['createdAt' => 'DESC'], 3);
$galleries = $this->em->getRepository(Gallery::class)->findBy([], ['createdAt' => 'DESC'], 4);
$projects = $this->em->getRepository(Project::class)->findBy([], ['id' => 'DESC'], 3);
return $this->render('pages/index.html.twig', [
'events' => $events,
'articles' => $articles,
'galleries' => $galleries,
'projects' => $projects
]);
}
/**
* @Route("/association", name="app_association")
*/
public function association(): Response
{
$teamMembers = $this->em->getRepository(TeamMember::class)->findBy([], ['displayOrder' => 'ASC']);
$projects = $this->em->getRepository(Project::class)->findBy([], ['id' => 'DESC'], 3);
return $this->render('pages/association.html.twig', [
'team_members' => $teamMembers,
'projects' => $projects
]);
}
/**
* @Route("/festival", name="app_festival")
*/
public function festival(): Response
{
return $this->render('events/festival.html.twig');
}
/**
* @Route("/evenements", name="app_evenements")
*/
public function evenements(Request $request): Response
{
$page = $request->query->getInt('page', 1);
$category = $request->query->get('category');
$search = $request->query->get('search');
$limit = 6;
$offset = ($page - 1) * $limit;
$qb = $this->em->getRepository(Event::class)->createQueryBuilder('e')
->orderBy('e.date', 'ASC');
if ($category && $category !== 'Toutes les catégories') {
$qb->andWhere('e.category = :category')
->setParameter('category', $category);
}
if ($search) {
$qb->andWhere('e.title LIKE :search OR e.description LIKE :search OR e.location LIKE :search')
->setParameter('search', '%' . $search . '%');
}
// Count total
$countQb = clone $qb;
$totalEvents = $countQb->select('count(e.id)')->getQuery()->getSingleScalarResult();
$totalPages = ceil($totalEvents / $limit);
// Get events
$events = $qb->setMaxResults($limit)
->setFirstResult($offset)
->getQuery()
->getResult();
// Get categories for filter
$categoriesQuery = $this->em->createQuery('SELECT DISTINCT e.category FROM App\Entity\Event e WHERE e.category IS NOT NULL');
$categories = array_column($categoriesQuery->getScalarResult(), 'category');
return $this->render('events/evenements.html.twig', [
'events' => $events,
'current_page' => $page,
'total_pages' => $totalPages,
'current_category' => $category,
'search_query' => $search,
'categories' => $categories
]);
}
/**
* @Route("/evenement/{id}", name="app_event_show")
*/
public function eventShow($id): Response
{
$event = $this->em->getRepository(Event::class)->find($id);
if (!$event) {
throw $this->createNotFoundException('Événement non trouvé');
}
return $this->render('events/event_show.html.twig', [
'event' => $event
]);
}
/**
* @Route("/sponsoring", name="app_sponsoring")
*/
public function sponsoring(): Response
{
return $this->render('pages/sponsoring.html.twig');
}
/**
* @Route("/blog", name="app_blog")
*/
public function blog(Request $request): Response
{
$page = $request->query->getInt('page', 1);
$category = $request->query->get('category');
$limit = 8;
$offset = ($page - 1) * $limit;
$criteria = [];
if ($category) {
$criteria['category'] = $category;
}
$repo = $this->em->getRepository(Article::class);
$articles = $repo->findBy($criteria, ['createdAt' => 'DESC'], $limit, $offset);
$totalArticles = $repo->count($criteria);
$totalPages = ceil($totalArticles / $limit);
// Fetch the featured article (only on page 1, without category filter)
$featuredArticle = null;
if ($page === 1 && !$category) {
$featuredArticle = $repo->findOneBy(['isFeatured' => true], ['createdAt' => 'DESC']);
// Remove the featured article from the list if it's there
if ($featuredArticle) {
$articles = array_filter($articles, function ($a) use ($featuredArticle) {
return $a->getId() !== $featuredArticle->getId();
});
}
}
// Fetch categories for sidebar
$categoriesQuery = $this->em->createQuery('SELECT DISTINCT a.category FROM App\Entity\Article a WHERE a.category IS NOT NULL');
$categories = array_column($categoriesQuery->getScalarResult(), 'category');
return $this->render('blog/blog.html.twig', [
'articles' => $articles,
'featured_article' => $featuredArticle,
'current_page' => $page,
'total_pages' => $totalPages,
'current_category' => $category,
'categories' => $categories
]);
}
/**
* @Route("/blog/{id}", name="app_article_show")
*/
public function articleShow($id): Response
{
$article = $this->em->getRepository(Article::class)->find($id);
if (!$article) {
throw $this->createNotFoundException('Article non trouvé');
}
$recentArticles = $this->em->getRepository(Article::class)->findBy([], ['createdAt' => 'DESC'], 3);
return $this->render('blog/article_show.html.twig', [
'article' => $article,
'recent_articles' => $recentArticles
]);
}
/**
* @Route("/galerie", name="app_galerie")
*/
public function galerie(Request $request): Response
{
$page = $request->query->getInt('page', 1);
$category = $request->query->get('category');
$limit = 10;
$offset = ($page - 1) * $limit;
$criteria = [];
if ($category) {
$criteria['category'] = $category;
}
$repo = $this->em->getRepository(Gallery::class);
$images = $repo->findBy($criteria, ['createdAt' => 'DESC'], $limit, $offset);
$totalImages = $repo->count($criteria);
$totalPages = ceil($totalImages / $limit);
// Fetch categories for filters
$categoriesQuery = $this->em->createQuery('SELECT DISTINCT g.category FROM App\Entity\Gallery g WHERE g.category IS NOT NULL');
$categories = array_column($categoriesQuery->getScalarResult(), 'category');
return $this->render('pages/galerie.html.twig', [
'images' => $images,
'current_page' => $page,
'total_pages' => $totalPages,
'current_category' => $category,
'categories' => $categories
]);
}
/**
* @Route("/contact", name="app_contact")
*/
public function contact(): Response
{
return $this->render('pages/contact.html.twig');
}
/**
* @Route("/conditions-utilisation", name="app_cgu")
*/
public function cgu(): Response
{
return $this->render('pages/cgu.html.twig');
}
/**
* @Route("/politique-de-confidentialite", name="app_privacy")
*/
public function privacy(): Response
{
return $this->render('pages/privacy.html.twig');
}
/**
* @Route("/politique-de-remboursement", name="app_refund")
*/
public function refund(): Response
{
return $this->render('pages/refund.html.twig');
}
/**
* @Route("/espace-membre", name="app_espace_membre")
*/
public function espaceMembre(\Symfony\Component\HttpFoundation\Request $request): Response
{
$memberId = $request->getSession()->get('member_id');
if (!$memberId) {
return $this->redirectToRoute('app_login');
}
$member = $this->em->getRepository(\App\Entity\Member::class)->find($memberId);
if (!$member) {
$request->getSession()->remove('member_id');
return $this->redirectToRoute('app_login');
}
return $this->render('user/espace_membre.html.twig', [
'member' => $member
]);
}
/**
* @Route("/submit-contact", name="app_contact_submit", methods={"POST"})
*/
public function submitContact(Request $request, MailerInterface $mailer): Response
{
$type = $request->request->get('type', 'contact');
$name = $request->request->get('name');
$email = $request->request->get('email');
$subject = $request->request->get('subject');
$messageContent = $request->request->get('message');
$msg = new ContactMessage();
$msg->setName($name);
$msg->setEmail($email);
$msg->setSubject($subject);
$msg->setMessage($messageContent);
$msg->setType($type);
$this->em->persist($msg);
$this->em->flush();
try {
$emailObj = (new Email())
->from(new Address('wabenin@azilink.com', 'WA BENIN'))
->replyTo($email)
->to('wabenin@azilink.com')
->subject($type === 'sponsoring' ? 'Nouveau Sponsor: ' . $subject : 'Nouveau Message: ' . $subject)
->text("De: $name ($email)\n\n$messageContent");
$mailer->send($emailObj);
} catch (\Exception $e) {
}
// Flash message or query param could be used here
if ($type === 'sponsoring') {
return $this->redirectToRoute('app_sponsoring', ['success' => 1]);
}
return $this->redirectToRoute('app_contact', ['success' => 1]);
}
}