<?
php
namespace App\Controller;
use App\Entity\Announcement;
use App\Form\AnnouncementType;
use App\Repository\AnnouncementRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Security;
#[Route('/announcement')]
final class AnnouncementController extends AbstractController{
#[Route(name: 'app_announcement_index', methods: ['GET'])]
public function index(Request $request, AnnouncementRepository
$announcementRepository, security $security): Response
{ $user = $security->getUser();
// Get the search query from the request
$searchQuery = $request->query->get('search', '');
// Fetch announcements based on the search query if provided
if ($searchQuery) {
$announcements = $announcementRepository-
>findByTitleStartingWith($searchQuery);
} else {
$announcements = $announcementRepository->findAll();
return $this->render('announcement/index.html.twig', [
'announcements' => $announcements,
'search' => $searchQuery,
'user' => $user
]);
#[Route('/new', name: 'app_announcement_new', methods: ['GET',
'POST'])]
public function new(Request $request, EntityManagerInterface
$entityManager): Response
$announcement = new Announcement();
$form = $this->createForm(AnnouncementType::class,
$announcement);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($announcement);
$entityManager->flush();
return $this->redirectToRoute('app_announcement_index', [],
Response::HTTP_SEE_OTHER);
}
return $this->render('announcement/new.html.twig', [
'announcement' => $announcement,
'form' => $form,
]);
#[Route('/{id}', name: 'app_announcement_show', methods: ['GET'])]
public function show(Announcement $announcement): Response
return $this->render('announcement/show.html.twig', [
'announcement' => $announcement,
]);
#[Route('/{id}/edit', name: 'app_announcement_edit', methods: ['GET',
'POST'])]
public function edit(Request $request, Announcement $announcement,
EntityManagerInterface $entityManager): Response
$form = $this->createForm(AnnouncementType::class,
$announcement);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_announcement_index', [],
Response::HTTP_SEE_OTHER);
}
return $this->render('announcement/edit.html.twig', [
'announcement' => $announcement,
'form' => $form,
]);
#[Route('/{id}', name: 'app_announcement_delete', methods: ['POST'])]
public function delete(Request $request, Announcement $announcement,
EntityManagerInterface $entityManager): Response
if ($this->isCsrfTokenValid('delete'.$announcement->getId(), $request-
>getPayload()->getString('_token'))) {
$entityManager->remove($announcement);
$entityManager->flush();
return $this->redirectToRoute('app_announcement_index', [],
Response::HTTP_SEE_OTHER);