<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\QuestionAnswer;
use App\Entity\Structure;
class QuestionAnswerController extends AbstractController
{
/**
* @Route("/question-answer/{id}", name="showQuestionAnswer", requirements={"id"="\d+"})
*/
public function showQuestionAnswer(Request $request, $id)
{
$questionAnswer = $this->getDoctrine()->getRepository(QuestionAnswer::class)->find($id);
if (!$questionAnswer) {
throw $this->createNotFoundException('Pas de question-reponse avec l\'id ' . $id . '.');
// the above is just a shortcut for:
// throw new NotFoundHttpException('The product does not exist');
}
if (!$questionAnswer->getPublished()) {
$redirectUrl = $this->_notPublished($request);
if($redirectUrl) return $this->redirect($redirectUrl);
}
$slug = $questionAnswer->getSlug();
if($slug && $slug !== "") {
return $this->redirect($this->generateUrl('showQuestionAnswerBySlug', ['slug' => $slug]));
} else {
return $this->render('questionAnswer/show.html.twig', [
'questionAnswer' => $questionAnswer
]);
}
}
/**
* @Route("/question-answer/{slug}", name="showQuestionAnswerBySlug")
*/
public function showQuestionAnswerBySlug(Request $request, $slug)
{
$questionAnswer = $this->getDoctrine()->getRepository(QuestionAnswer::class)->findOneBySlug($slug);
if (!$questionAnswer) {
throw $this->createNotFoundException('Pas de question-reponse avec le slug "' . $slug . '".');
}
if (!$questionAnswer->getPublished()) {
$redirectUrl = $this->_notPublished($request);
if($redirectUrl) return $this->redirect($redirectUrl);
}
return $this->render('questionAnswer/show.html.twig', [
'questionAnswer' => $questionAnswer
]);
}
/**
* action if entity not published
* @return string $redirectUrl
*/
private function _notPublished($request) {
if(!$this->isGranted('ROLE_ADMIN')) {
$requestUri = $request->getRequestUri();
$baseUrl = $request->getSchemeAndHttpHost();
$this->addFlash(
'error-popup',
'L\'url demandée : "' . $baseUrl . $requestUri . '" n\'existe pas.'
);
$referer = $request->headers->get('referer');
if(null != $referer && substr($referer, 0, strlen($baseUrl)) === $baseUrl) {
// on retourne de là oú l'on vient, si l'on vient de ce site
$redirectUrl = substr($referer, strlen($baseUrl));
} else {
// pas de referer trouvé ou appel depuis site extérieur, on va donc à la home
$redirectUrl = $this->generateUrl('home');
}
} else {
// rôle admin, on laisse consulter
$this->addFlash(
'notice',
'Cette question-réponse n\'est pas publiée. En tant qu\'admin vous pouvez toutefois la voir.'
);
$redirectUrl = null;
}
return $redirectUrl;
}
/**
* @Route("/question-answer/teasers/{ids}", name="showSearchResult")
* @param string $ids liste d'ids séparées par des tirets
* @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle
*/
public function showSearchResult($ids, $listWithThumbnails = false)
{
$ids = explode("-", $ids); // $ids devient un array d'ids
$questionAnswers = [];
foreach ($ids as $id) {
$questionAnswer = $this->getDoctrine()->getRepository(QuestionAnswer::class)->find($id);
if($questionAnswer) {
$key = sprintf('%08d', $questionAnswer->getPriority()) . "_" . substr($questionAnswer->getName(), 0, 20) . "_" . sprintf('%08d', $questionAnswer->getId()); // pour ksort par la suite
if(! isset($questionAnswers[$key])) $questionAnswers[$key] = $questionAnswer;
}
}
krsort($questionAnswers);
return $this->render('questionAnswer/searchResult.html.twig', [
'listWithThumbnails' => $listWithThumbnails,
'questionAnswers' => $questionAnswers
]);
}
/**
* affiche la liste des questionAnswers partageant un tag avec la questionAnswers donnee
* @Route("/same-questionanswers/{id}/{limit}", name="listSame", requirements={"id"="\d+"})
* @return QuestionAnswer[]
*/
public function listSame($id, $limit = 10)
{
$questionAnswerRepository = $this->getDoctrine()->getRepository(QuestionAnswer::class);
$questionAnswer = $questionAnswerRepository->find($id);
if(!$questionAnswer) throw $this->createNotFoundException('Pas de structure avec l\'id "' . $id . '".');
$tags = $questionAnswer->getTags();
$questionAnswers = $questionAnswerRepository->findBySameTags($tags, $limit, $questionAnswer);
$structureRepository = $this->getDoctrine()->getRepository(Structure::class);
$structures = $structureRepository->findBySameTags($tags, $limit);
// dump($structures); exit;
return $this->render('questionAnswer/listSame.html.twig', [
'questionAnswers' => $questionAnswers,
'structures' => $structures
]);
}
}