src/Controller/GlossaryController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use App\Entity\Glossaries;
  11. use App\Entity\GlossaryReviews;
  12. use App\Entity\Newsfeed;
  13. use App\Entity\SearchItems;
  14. use App\Repository\GlossariesRepository;
  15. use App\Form\GlossaryFormType;
  16. use App\Form\GlossaryReviewFormType;
  17. use App\Form\GlossaryUserFormType;
  18. class GlossaryController extends AbstractController
  19. {
  20.     private $em;
  21.     private $translator;
  22.     private $router;
  23.     public function __construct(EntityManagerInterface $emTranslatorInterface $translatorRouterInterface $router)
  24.     {
  25.         $this->em $em;
  26.         $this->translator $translator;
  27.         $this->router $router;
  28.     }
  29.     /**
  30.      * @Route("/glossary", name="glossary_index")
  31.      */
  32.     public function glossary_index(TranslatorInterface $translator)
  33.     {
  34.         $repository $this->getDoctrine()->getRepository(Glossaries::class);
  35.         if($translator->getLocale() == 'nl') {
  36.             $index $repository->findBy(array(), array('title_nl' => 'ASC'));
  37.         } else {
  38.             $index $repository->findBy(array(), array('title_en' => 'ASC'));
  39.         }
  40.         return $this->render('glossary_index.html.twig', [
  41.             'index' => false,
  42.             'items' => $index
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route("/glossary/{title}", name="glossary_by_title")
  47.      */
  48.     public function glossary_by_title(string $titleTranslatorInterface $translator)
  49.     {
  50.         $repository $this->getDoctrine()->getRepository(Glossaries::class);
  51.         if(is_numeric($title)) {
  52.             $glossary $repository->findOneBy(array('id' => $title'approved' => 1));
  53.         } else {
  54.             if($translator->getLocale() == 'nl') {
  55.                 $glossary $repository->findOneBy(array('title_nl' => $title'approved' => 1));
  56.             } else {
  57.                 $glossary $repository->findOneBy(array('title_en' => $title'approved' => 1));
  58.             }
  59.         }
  60.         return $this->redirectToRoute('glossary',array('id' => $glossary->getId(), 'url' => $glossary->getUrl() ));
  61.     }
  62.     /**
  63.      * @Route("/glossary/{id}/{url}", name="glossary")
  64.      */
  65.     public function glossary(Glossaries $idstring $url nullTranslatorInterface $translator)
  66.     {
  67.         $repository $this->getDoctrine()->getRepository(Glossaries::class);
  68.         $glossary $repository->findOneBy(array('id' => $id'approved' => 1));
  69.         
  70.         if($glossary->getNumReviews() > '0') {
  71.             $this->addFlash('warning'$translator->trans("Dit artikel moet gereviewd worden. Het kan daarom zijn dat de inhoud en/of de opmaak niet juist zijn."));
  72.         }
  73.         return $this->render('glossary.html.twig', [
  74.             'glossary' => $glossary,
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/moderate/glossary", name="glossary_moderate")
  79.      */
  80.     public function moderate_glossary(Request $requestTranslatorInterface $translator)
  81.     {
  82.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  83.         $repository $this->getDoctrine()->getRepository(Glossaries::class);
  84.         if($translator->getLocale() == 'nl') {
  85.             $oder_field 'title_nl';
  86.         } else {
  87.             $oder_field 'title_en';
  88.         }
  89.         $glossary $repository->findBy(array(),array('num_reviews'=>'DESC' ,'approved'=>'ASC'$oder_field => 'ASC'));
  90.         return $this->render('glossary_moderate.html.twig', [
  91.             'items' => $glossary,
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route("/glossaries/add", name="glossary_add_user")
  96.      */
  97.     public function add_userEntityManagerInterface $emRequest $requestTranslatorInterface $translator)
  98.     {
  99.         $this->denyAccessUnlessGranted('ROLE_USER');
  100.         if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
  101.         $form $this->createForm(GlossaryUserFormType::class);
  102.         $form->handleRequest($request);
  103.         if ($form->isSubmitted() && $form->isValid()){
  104.             $glossary $form->getData();
  105.             $glossary->setImage('');
  106.             $glossary->setUserId($this->getUser()->getId());
  107.             $glossary->setUsername($this->getUser()->getUsername());
  108.             $glossary->setAdded(new \DateTime());
  109.             $glossary->setApproved('0');
  110.             $glossary->setNumReviews('0');
  111.             $em->persist($glossary);
  112.             $em->flush();
  113.             $this->addFlash('success'$translator->trans("Glossarium toegevoegd en wacht op goedkeuring."));
  114.             return $this->redirectToRoute('glossary_index');
  115.         }
  116.         return $this->render('glossary_edit.html.twig', [
  117.             'moderator_form' => false,
  118.             'title' => $translator->trans("Glossarium toevoegen"),
  119.             'form' => $form->createView()
  120.         ]);
  121.     }
  122.     /**
  123.      * @Route("/moderate/glossary/add", name="glossary_moderate_add")
  124.      */
  125.     public function add(EntityManagerInterface $emRequest $requestTranslatorInterface $translator)
  126.     {
  127.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  128.         $form $this->createForm(GlossaryFormType::class);
  129.         $form->handleRequest($request);
  130.         if ($form->isSubmitted() && $form->isValid()){
  131.             $glossary $form->getData();
  132.             $glossary->setUserId($this->getUser()->getId());
  133.             $glossary->setUsername($this->getUser()->getUsername());
  134.             $glossary->setAdded(new \DateTime());
  135.             $glossary->setNumReviews('0');
  136.             $em->persist($glossary);
  137.             $em->flush();
  138.             $newsfeed = new Newsfeed();
  139.             $newsfeed->setTitleNl($glossary->getTitleNl());
  140.             $newsfeed->setTitleEn($glossary->getTitleEn());
  141.             $newsfeed->setRemoteId($glossary->getId());
  142.             $newsfeed->setType('glossary');
  143.             $newsfeed->setIsUpdate(0);
  144.             $newsfeed->setTime(new \DateTime());
  145.             $em $this->getDoctrine()->getManager();
  146.             $em->persist($newsfeed);
  147.             $em->flush();
  148.             if ($glossary->getApproved() == 1){
  149.                 $searchitem = new SearchItems();
  150.                 $searchitem->setType('glossary');
  151.                 $searchitem->setPath($this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]));
  152.                 $searchitem->setNameNl($glossary->getTitleNl());
  153.                 $searchitem->setNameEn($glossary->getTitleEn());
  154.                 $searchitem->setKeywordsNl($glossary->getTitleNl().','.$glossary->getKeywordsNl());
  155.                 $searchitem->setKeywordsEn($glossary->getTitleEn().','.$glossary->getKeywordsEn());
  156.                 if(empty($glossary->getEdited())) {
  157.                     $searchitem->setLastUpdateTime($glossary->getAdded());
  158.                 } else {
  159.                     $searchitem->setLastUpdateTime($glossary->getEdited());
  160.                 }
  161.                 $em->persist($searchitem);
  162.                 $em->flush();
  163.             }
  164.             $this->addFlash('success'$translator->trans("Glossarium toegevoegd"));
  165.             return $this->redirectToRoute('glossary_moderate');
  166.         }
  167.         return $this->render('glossary_edit.html.twig', [
  168.             'moderator_form' => true,
  169.             'title' => $translator->trans("Glossarium toevoegen"),
  170.             'form' => $form->createView()
  171.         ]);
  172.     }
  173.     /**
  174.      * @Route("/moderate/glossary/{id}/edit", name="glossary_moderate_edit")
  175.      */
  176.     public function edit(Glossaries $idEntityManagerInterface $emRequest $requestTranslatorInterface $translator)
  177.     {
  178.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  179.         $glossary $id;
  180.         $form $this->createForm(GlossaryFormType::class, $glossary);
  181.         $form->handleRequest($request);
  182.         if ($form->isSubmitted() && $form->isValid()){
  183.             $glossary $form->getData();
  184.             $glossary->setApprovedUserId($this->getUser()->getId());
  185.             $glossary->setApprovedUsername($this->getUser()->getUsername());
  186.             $glossary->setEdited(new \DateTime());
  187.             $glossary->setEditedUserId($this->getUser()->getId());
  188.             $glossary->setEditedUsername($this->getUser()->getUsername());
  189.             $em->persist($glossary);
  190.             $em->flush();
  191.             $repository $em->getRepository(SearchItems::class);
  192.             $searchitem $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
  193.             if ($glossary->getApproved() == 1){
  194.                 $newsfeed = new Newsfeed();
  195.                 $newsfeed->setTitleNl($glossary->getTitleNl());
  196.                 $newsfeed->setTitleEn($glossary->getTitleEn());
  197.                 $newsfeed->setRemoteId($glossary->getId());
  198.                 $newsfeed->setIsUpdate(1);
  199.                 $newsfeed->setType('glossary');
  200.                 $newsfeed->setTime(new \DateTime());
  201.                 $em $this->getDoctrine()->getManager();
  202.                 $em->persist($newsfeed);
  203.                 $em->flush();
  204.                 if(!$searchitem) {
  205.                     $searchitem = new SearchItems();
  206.                 }
  207.                 $searchitem->setType('glossary');
  208.                 $searchitem->setPath($this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]));
  209.                 $searchitem->setNameNl($glossary->getTitleNl());
  210.                 $searchitem->setNameEn($glossary->getTitleEn());
  211.                 $searchitem->setKeywordsNl($glossary->getTitleNl().','.$glossary->getKeywordsNl());
  212.                 $searchitem->setKeywordsEn($glossary->getTitleEn().','.$glossary->getKeywordsEn());
  213.                 if(empty($glossary->getEdited())) {
  214.                     $searchitem->setLastUpdateTime($glossary->getAdded());
  215.                 } else {
  216.                     $searchitem->setLastUpdateTime($glossary->getEdited());
  217.                 }
  218.                 $em->persist($searchitem);
  219.                 $em->flush();
  220.             } else {
  221.                 if($searchitem) {
  222.                     $em->remove($searchitem);
  223.                     $em->flush();
  224.                 }
  225.             }
  226.             $this->addFlash('success'$translator->trans("Glossarium bijgewerkt"));
  227.             
  228.             return $this->redirectToRoute('glossary_moderate');
  229.         }
  230.         return $this->render('glossary_edit.html.twig', [
  231.             'moderator_form' => true,
  232.             'title' => $translator->trans("Glossarium bewerken"),
  233.             'form' => $form->createView()
  234.         ]);
  235.     }
  236.     /**
  237.      * @Route("/moderate/glossary/{id}/revoke", name="glossary_moderate_revoke")
  238.      */
  239.     public function moderate_revoke(int $idGlossaries $glossaryEntityManagerInterface $emTranslatorInterface $translator)
  240.     {
  241.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  242.         $glossary->setApprovedUserId($this->getUser()->getId());
  243.         $glossary->setApprovedUsername($this->getUser()->getUsername());
  244.         $glossary->setApproved('0');
  245.         $em->persist($glossary);
  246.         $repository $em->getRepository(SearchItems::class);
  247.         $searchitem $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
  248.         if($searchitem){
  249.             $em->remove($searchitem);
  250.         }
  251.         $em->flush();
  252.         $this->addFlash('success'$translator->trans("Glossarium ingetrokken"));
  253.         return $this->redirectToRoute('glossary_moderate');
  254.     }
  255.     /**
  256.      * @Route("/moderate/glossary/{id}/approve", name="glossary_moderate_approve")
  257.      */
  258.     public function moderate_approve(int $idGlossaries $glossaryEntityManagerInterface $emTranslatorInterface $translator)
  259.     {
  260.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  261.         $glossary->setApprovedUserId($this->getUser()->getId());
  262.         $glossary->setApprovedUsername($this->getUser()->getUsername());
  263.         $glossary->setApproved('1');
  264.         $em->persist($glossary);
  265.         $newsfeed = new Newsfeed();
  266.         $newsfeed->setTitleNl($glossary->getTitleNl());
  267.         $newsfeed->setTitleEn($glossary->getTitleEn());
  268.         $newsfeed->setRemoteId($glossary->getId());
  269.         $newsfeed->setType('glossary');
  270.         $newsfeed->setIsUpdate(0);
  271.         $newsfeed->setTime(new \DateTime());
  272.         $em $this->getDoctrine()->getManager();
  273.         $em->persist($newsfeed);
  274.         $repository $em->getRepository(SearchItems::class);
  275.         $searchitem $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
  276.         if(!$searchitem) {
  277.             $searchitem = new SearchItems();
  278.         }
  279.         $searchitem->setType('glossary');
  280.         $searchitem->setPath($this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]));
  281.         $searchitem->setNameNl($glossary->getTitleNl());
  282.         $searchitem->setNameEn($glossary->getTitleEn());
  283.         $searchitem->setKeywordsNl($glossary->getTitleNl().','.$glossary->getKeywordsNl());
  284.         $searchitem->setKeywordsEn($glossary->getTitleEn().','.$glossary->getKeywordsEn());
  285.         if(empty($glossary->getEdited())) {
  286.             $searchitem->setLastUpdateTime($glossary->getAdded());
  287.         } else {
  288.             $searchitem->setLastUpdateTime($glossary->getEdited());
  289.         }
  290.         $em->persist($searchitem);
  291.         $em->flush();
  292.         $this->addFlash('success'$translator->trans("Glossarium goedgekeurd"));
  293.         return $this->redirectToRoute('glossary_moderate');
  294.     }
  295.     /**
  296.      * @Route("/moderate/glossary/{id}/delete", name="glossary_moderate_delete")
  297.      */
  298.     public function moderate_delete(Glossaries $idEntityManagerInterface $emTranslatorInterface $translator)
  299.     {
  300.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  301.         $glossary $id;
  302.         
  303.         $repository $em->getRepository(SearchItems::class);
  304.         $searchitem $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
  305.         if($searchitem){
  306.             $em->remove($searchitem);
  307.         }
  308.         
  309.         $em->remove($glossary);
  310.         $em->flush();
  311.         $this->addFlash('success'$translator->trans("Glossarium verwijderd"));
  312.         return $this->redirectToRoute('glossary_moderate');
  313.     }
  314.     /**
  315.      * @Route("/moderate/glossary/{id}/reviews", name="glossary_moderate_reviews")
  316.      */
  317.     public function moderate_review(Glossaries $idEntityManagerInterface $emTranslatorInterface $translator)
  318.     {
  319.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  320.         return $this->render('glossaryreviews_moderate.html.twig', [
  321.             'glossary' => $id,
  322.         ]);
  323.     }
  324.     /**
  325.      * @Route("/glossaries/review/{id}", name="glossary_review_add")
  326.      */
  327.     public function glossary_review_add(Glossaries $idRequest $requestEntityManagerInterface $emTranslatorInterface $translator)
  328.     {
  329.         $this->denyAccessUnlessGranted('ROLE_USER');
  330.         if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
  331.         $form $this->createForm(GlossaryReviewFormType::class);
  332.         $form->handleRequest($request);
  333.         if ($form->isSubmitted() && $form->isValid()){
  334.             $glossary $id;
  335.             $review $form->getData();
  336.             $review->setUserId($this->getUser()->getId());
  337.             $review->setUsername($this->getUser()->getUsername());
  338.             $review->setGlossary($glossary);
  339.             $em->persist($review);
  340.             $em->flush();
  341.             $num_reviews $glossary->getNumReviews();
  342.             $glossary->setNumReviews($num_reviews+1);
  343.             $em->persist($glossary);
  344.             $em->flush();
  345.             $this->addFlash('success'$translator->trans("Feedback doorgegeven"));
  346.             return $this->redirectToRoute('glossary',array('id' => $glossary->getId(), 'url' => $glossary->getUrl() ));
  347.         }
  348.         return $this->render('form_edit.html.twig', [
  349.             'title' => $translator->trans("Feedback doorgeven"),
  350.             'button_text' => $translator->trans("Insturen"),
  351.             'form' => $form->createView()
  352.         ]);
  353.     }
  354.     /**
  355.      * @Route("/moderate/glossary/review/{id}/read", name="glossaryreviews_moderate_read")
  356.      */
  357.     public function review_read(GlossaryReviews $idEntityManagerInterface $emTranslatorInterface $translator)
  358.     {
  359.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  360.         $glossaryreview $id;
  361.         $repository $this->getDoctrine()->getRepository(GlossaryReviews::class);
  362.         $glossaryreview $repository->findOneBy(array('id'=>$id));
  363.         return $this->render('glossaryreview.html.twig', [
  364.             'review' => $glossaryreview,
  365.         ]);
  366.     }
  367.     /**
  368.      * @Route("/moderate/glossary/review/{id}/delete", name="glossaryreviews_moderate_delete")
  369.      */
  370.     public function review_delete(int $idGlossaryReviews $glossaryreviewEntityManagerInterface $emTranslatorInterface $translator)
  371.     {
  372.         $this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
  373.         $glossary $glossaryreview->getGlossary();
  374.         $glossary->setNumReviews($glossary->getNumReviews()-1);
  375.         $em->persist($glossary);
  376.         $em->flush();
  377.         
  378.         $em->remove($glossaryreview);
  379.         $em->flush();
  380.         $this->addFlash('success'$translator->trans("Glossarium review verwijderd"));
  381.         return $this->redirectToRoute('glossary_moderate_reviews', array('id' => $glossary->getId() ));
  382.     }
  383.     /**
  384.      * @Route("/api/v1/glossary", methods="GET", name="api_glossary")
  385.      */
  386.     public function api_glossary(GlossariesRepository $GlossariesRepositoryRequest $requestTranslatorInterface $translator)
  387.     {
  388.         $glossary $GlossariesRepository->findAllMatching($request->query->get('query'));
  389.         $return = array();
  390.         foreach($glossary as $item) {
  391.             if($translator->getLocale() == 'nl') {
  392.                 $name $item->getTitleNl();
  393.             } else {
  394.                 $name $item->getTitleEn();
  395.             }
  396.             $return[] = array(
  397.                 'id' => $item->getId(),
  398.                 'value' => $name,
  399.             );
  400.         }
  401.         return $this->json($return200, [], ['groups' => ['main']]);
  402.     }
  403. }