src/Controller/QuestionAnswerController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Entity\QuestionAnswer;
  7. use App\Entity\Structure;
  8. class QuestionAnswerController extends AbstractController
  9. {    
  10.     /**
  11.      * @Route("/question-answer/{id}", name="showQuestionAnswer", requirements={"id"="\d+"})
  12.      */
  13.     public function showQuestionAnswer(Request $request$id)
  14.     {
  15.         $questionAnswer $this->getDoctrine()->getRepository(QuestionAnswer::class)->find($id);
  16.         if (!$questionAnswer) {
  17.             throw $this->createNotFoundException('Pas de question-reponse avec l\'id ' $id '.');
  18.     
  19.             // the above is just a shortcut for:
  20.             // throw new NotFoundHttpException('The product does not exist');
  21.         }
  22.         if (!$questionAnswer->getPublished()) {
  23.             $redirectUrl $this->_notPublished($request);
  24.             if($redirectUrl) return $this->redirect($redirectUrl);
  25.         }
  26.         $slug $questionAnswer->getSlug();
  27.         if($slug && $slug !== "") {
  28.             return $this->redirect($this->generateUrl('showQuestionAnswerBySlug', ['slug' => $slug]));
  29.         } else {
  30.             return $this->render('questionAnswer/show.html.twig', [
  31.                 'questionAnswer' => $questionAnswer
  32.             ]);
  33.         }
  34.     }
  35.     
  36.     /**
  37.      * @Route("/question-answer/{slug}", name="showQuestionAnswerBySlug")
  38.      */
  39.     public function showQuestionAnswerBySlug(Request $request$slug)
  40.     {
  41.         $questionAnswer $this->getDoctrine()->getRepository(QuestionAnswer::class)->findOneBySlug($slug);
  42.         if (!$questionAnswer) {
  43.             throw $this->createNotFoundException('Pas de question-reponse avec le slug "' $slug '".');
  44.         }
  45.         if (!$questionAnswer->getPublished()) {
  46.             $redirectUrl $this->_notPublished($request);
  47.             if($redirectUrl) return $this->redirect($redirectUrl);
  48.         }
  49.         return $this->render('questionAnswer/show.html.twig', [
  50.             'questionAnswer' => $questionAnswer
  51.         ]);
  52.     }
  53.     /**
  54.      * action if entity not published
  55.      * @return string $redirectUrl
  56.      */
  57.     private function _notPublished($request) {
  58.         if(!$this->isGranted('ROLE_ADMIN')) {
  59.             $requestUri $request->getRequestUri();
  60.             $baseUrl $request->getSchemeAndHttpHost();
  61.             $this->addFlash(
  62.                 'error-popup',
  63.                 'L\'url demandée : "' $baseUrl $requestUri '" n\'existe pas.'
  64.             );
  65.             $referer $request->headers->get('referer');
  66.             if(null != $referer && substr($referer0strlen($baseUrl)) === $baseUrl) {
  67.                 // on retourne de là oú l'on vient, si l'on vient de ce site
  68.                 $redirectUrl substr($refererstrlen($baseUrl));
  69.             } else {
  70.                 // pas de referer trouvé ou appel depuis site extérieur, on va donc à la home
  71.                 $redirectUrl $this->generateUrl('home');
  72.             }
  73.             
  74.         } else { 
  75.             // rôle admin, on laisse consulter
  76.             $this->addFlash(
  77.                 'notice',
  78.                 'Cette question-réponse n\'est pas publiée. En tant qu\'admin vous pouvez toutefois la voir.'
  79.             );
  80.             $redirectUrl null;
  81.         }
  82.         return $redirectUrl;
  83.     }
  84.     
  85.     /**
  86.      * @Route("/question-answer/teasers/{ids}", name="showSearchResult")
  87.      * @param string $ids liste d'ids séparées par des tirets 
  88.      * @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle 
  89.      */
  90.     public function showSearchResult($ids$listWithThumbnails false)
  91.     {
  92.         $ids explode("-"$ids); // $ids devient un array d'ids
  93.         $questionAnswers = [];
  94.         foreach ($ids as $id) {
  95.             $questionAnswer $this->getDoctrine()->getRepository(QuestionAnswer::class)->find($id);
  96.             if($questionAnswer) {
  97.                 $key sprintf('%08d'$questionAnswer->getPriority()) . "_" substr($questionAnswer->getName(), 020) . "_" sprintf('%08d'$questionAnswer->getId()); // pour ksort par la suite
  98.                 if(! isset($questionAnswers[$key])) $questionAnswers[$key] = $questionAnswer;
  99.             }
  100.         }
  101.         krsort($questionAnswers);
  102.         return $this->render('questionAnswer/searchResult.html.twig', [
  103.             'listWithThumbnails' => $listWithThumbnails,
  104.             'questionAnswers' => $questionAnswers
  105.         ]);
  106.     }
  107.     
  108.     /**
  109.      * affiche la liste des questionAnswers partageant un tag avec la questionAnswers donnee 
  110.      * @Route("/same-questionanswers/{id}/{limit}", name="listSame", requirements={"id"="\d+"})
  111.      * @return QuestionAnswer[]  
  112.      */
  113.     public function listSame($id$limit 10)
  114.     {        
  115.         $questionAnswerRepository $this->getDoctrine()->getRepository(QuestionAnswer::class);
  116.         $questionAnswer $questionAnswerRepository->find($id);
  117.         if(!$questionAnswer) throw $this->createNotFoundException('Pas de structure avec l\'id "' $id '".');
  118.         $tags $questionAnswer->getTags();
  119.         $questionAnswers $questionAnswerRepository->findBySameTags($tags$limit$questionAnswer);
  120.         
  121.         $structureRepository $this->getDoctrine()->getRepository(Structure::class);
  122.         $structures $structureRepository->findBySameTags($tags$limit);
  123.         // dump($structures); exit;
  124.         return $this->render('questionAnswer/listSame.html.twig', [
  125.             'questionAnswers' => $questionAnswers,
  126.             'structures' => $structures
  127.         ]);
  128.     }
  129. }