<?php
namespace App\Controller;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\CommunityPosts;
use App\Form\CommunityPostFormType;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\CommunityPostsRepository;
class CommunityController extends AbstractController
{
private CommunityPostsRepository $communityPostsRepository;
public function __construct(CommunityPostsRepository $communityPostsRepository)
{
$this->communityPostsRepository = $communityPostsRepository;
}
#[Route('/community', name: 'community')]
public function index(Request $request): RedirectResponse|Response
{
$communityComment=new CommunityPosts();
$form=$this->createForm(CommunityPostFormType::class, $communityComment);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$actuallyDate=new DateTime(date("d-m-Y H:i:s"));
$communityComment=$form->getData();
$communityComment->setUserId($this->getUser());
$communityComment->setDatePostComment($actuallyDate);
$this->communityPostsRepository->add($communityComment, true);
$type="success";
$message="L'équipe vous remercie d'avoir laissé votre avis.";
$this->addFlash($type, $message);
return $this->redirectToRoute('community');
}
return $this->render('clientWeb/community.html.twig', [
'formPost' => $form->createView(),
'listComment'=>$this->communityPostsRepository->findAll(),
]);
}
}