<?php
namespace App\Controller;
use App\Entity\Answer;
use App\Entity\Questions;
use App\Form\AnswerType;
use App\Form\QuestionsType;
use App\Repository\AnswerRepository;
use App\Repository\QuestionsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/forum")
*/
class ForumController extends AbstractController
{
private $questions;
public function __construct(QuestionsRepository $questions)
{
$this->questions = $questions;
}
/**
* @Route("/", name="app_accueil_forum_index")
*/
public function index(){
$allQuestions = $this->questions->findBy(
[],
['createdAt' => 'DESC']
);
return $this->render('accueil/forum_index.html.twig',[
'questions'=>$allQuestions
]);
}
/**
* @Route("/{id}", name="app_accueil_forum_question")
*/
public function question(Questions $questions){
$reponse = new Answer();
$reponse->setQstn($questions);
$form = $this->createForm(AnswerType::class,$reponse);
return $this->render('accueil/forum_question.html.twig',[
'question'=>$questions,
'form'=>$form->createView()
]);
}
/**
* @Route("/question/repondre/{id}", name="app_accueil_forum_answer")
*/
public function repondre (Request $request,Questions $questions,AnswerRepository $answerRepository) {
$reponse = new Answer();
$reponse->setQstn($questions);
$reponse->setUser($this->getUser());
$form = $this->createForm(AnswerType::class,$reponse);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$answerRepository->add($reponse);
return $this->redirectToRoute('app_accueil_forum_question', ['id'=>$reponse->getQstn()->getId()], Response::HTTP_SEE_OTHER);
}
}
/**
* @Route("/question/nouvelle", name="app_accueil_forum_new_question")
*/
public function nouvelleQuestion (Request $request,QuestionsRepository $questionsRepository) {
$question = new Questions();
$question->setUser($this->getUser());
$form = $this->createForm(QuestionsType::class,$question);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$questionsRepository->add($question);
return $this->redirectToRoute('app_accueil_forum_index');
}
return $this->render('accueil/forum_index_new_question.html.twig',[
'form'=> $form->createView()
]);
}
}