<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\RouterInterface;
use App\Entity\Glossaries;
use App\Entity\GlossaryReviews;
use App\Entity\Newsfeed;
use App\Entity\SearchItems;
use App\Repository\GlossariesRepository;
use App\Form\GlossaryFormType;
use App\Form\GlossaryReviewFormType;
use App\Form\GlossaryUserFormType;
class GlossaryController extends AbstractController
{
private $em;
private $translator;
private $router;
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, RouterInterface $router)
{
$this->em = $em;
$this->translator = $translator;
$this->router = $router;
}
/**
* @Route("/glossary", name="glossary_index")
*/
public function glossary_index(TranslatorInterface $translator)
{
$repository = $this->getDoctrine()->getRepository(Glossaries::class);
if($translator->getLocale() == 'nl') {
$index = $repository->findBy(array(), array('title_nl' => 'ASC'));
} else {
$index = $repository->findBy(array(), array('title_en' => 'ASC'));
}
return $this->render('glossary_index.html.twig', [
'index' => false,
'items' => $index
]);
}
/**
* @Route("/glossary/{title}", name="glossary_by_title")
*/
public function glossary_by_title(string $title, TranslatorInterface $translator)
{
$repository = $this->getDoctrine()->getRepository(Glossaries::class);
if(is_numeric($title)) {
$glossary = $repository->findOneBy(array('id' => $title, 'approved' => 1));
} else {
if($translator->getLocale() == 'nl') {
$glossary = $repository->findOneBy(array('title_nl' => $title, 'approved' => 1));
} else {
$glossary = $repository->findOneBy(array('title_en' => $title, 'approved' => 1));
}
}
return $this->redirectToRoute('glossary',array('id' => $glossary->getId(), 'url' => $glossary->getUrl() ));
}
/**
* @Route("/glossary/{id}/{url}", name="glossary")
*/
public function glossary(Glossaries $id, string $url = null, TranslatorInterface $translator)
{
$repository = $this->getDoctrine()->getRepository(Glossaries::class);
$glossary = $repository->findOneBy(array('id' => $id, 'approved' => 1));
if($glossary->getNumReviews() > '0') {
$this->addFlash('warning', $translator->trans("Dit artikel moet gereviewd worden. Het kan daarom zijn dat de inhoud en/of de opmaak niet juist zijn."));
}
return $this->render('glossary.html.twig', [
'glossary' => $glossary,
]);
}
/**
* @Route("/moderate/glossary", name="glossary_moderate")
*/
public function moderate_glossary(Request $request, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$repository = $this->getDoctrine()->getRepository(Glossaries::class);
if($translator->getLocale() == 'nl') {
$oder_field = 'title_nl';
} else {
$oder_field = 'title_en';
}
$glossary = $repository->findBy(array(),array('num_reviews'=>'DESC' ,'approved'=>'ASC', $oder_field => 'ASC'));
return $this->render('glossary_moderate.html.twig', [
'items' => $glossary,
]);
}
/**
* @Route("/glossaries/add", name="glossary_add_user")
*/
public function add_user( EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_USER');
if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
$form = $this->createForm(GlossaryUserFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$glossary = $form->getData();
$glossary->setImage('');
$glossary->setUserId($this->getUser()->getId());
$glossary->setUsername($this->getUser()->getUsername());
$glossary->setAdded(new \DateTime());
$glossary->setApproved('0');
$glossary->setNumReviews('0');
$em->persist($glossary);
$em->flush();
$this->addFlash('success', $translator->trans("Glossarium toegevoegd en wacht op goedkeuring."));
return $this->redirectToRoute('glossary_index');
}
return $this->render('glossary_edit.html.twig', [
'moderator_form' => false,
'title' => $translator->trans("Glossarium toevoegen"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/glossary/add", name="glossary_moderate_add")
*/
public function add(EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$form = $this->createForm(GlossaryFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$glossary = $form->getData();
$glossary->setUserId($this->getUser()->getId());
$glossary->setUsername($this->getUser()->getUsername());
$glossary->setAdded(new \DateTime());
$glossary->setNumReviews('0');
$em->persist($glossary);
$em->flush();
$newsfeed = new Newsfeed();
$newsfeed->setTitleNl($glossary->getTitleNl());
$newsfeed->setTitleEn($glossary->getTitleEn());
$newsfeed->setRemoteId($glossary->getId());
$newsfeed->setType('glossary');
$newsfeed->setIsUpdate(0);
$newsfeed->setTime(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($newsfeed);
$em->flush();
if ($glossary->getApproved() == 1){
$searchitem = new SearchItems();
$searchitem->setType('glossary');
$searchitem->setPath($this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]));
$searchitem->setNameNl($glossary->getTitleNl());
$searchitem->setNameEn($glossary->getTitleEn());
$searchitem->setKeywordsNl($glossary->getTitleNl().','.$glossary->getKeywordsNl());
$searchitem->setKeywordsEn($glossary->getTitleEn().','.$glossary->getKeywordsEn());
if(empty($glossary->getEdited())) {
$searchitem->setLastUpdateTime($glossary->getAdded());
} else {
$searchitem->setLastUpdateTime($glossary->getEdited());
}
$em->persist($searchitem);
$em->flush();
}
$this->addFlash('success', $translator->trans("Glossarium toegevoegd"));
return $this->redirectToRoute('glossary_moderate');
}
return $this->render('glossary_edit.html.twig', [
'moderator_form' => true,
'title' => $translator->trans("Glossarium toevoegen"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/glossary/{id}/edit", name="glossary_moderate_edit")
*/
public function edit(Glossaries $id, EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$glossary = $id;
$form = $this->createForm(GlossaryFormType::class, $glossary);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$glossary = $form->getData();
$glossary->setApprovedUserId($this->getUser()->getId());
$glossary->setApprovedUsername($this->getUser()->getUsername());
$glossary->setEdited(new \DateTime());
$glossary->setEditedUserId($this->getUser()->getId());
$glossary->setEditedUsername($this->getUser()->getUsername());
$em->persist($glossary);
$em->flush();
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
if ($glossary->getApproved() == 1){
$newsfeed = new Newsfeed();
$newsfeed->setTitleNl($glossary->getTitleNl());
$newsfeed->setTitleEn($glossary->getTitleEn());
$newsfeed->setRemoteId($glossary->getId());
$newsfeed->setIsUpdate(1);
$newsfeed->setType('glossary');
$newsfeed->setTime(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($newsfeed);
$em->flush();
if(!$searchitem) {
$searchitem = new SearchItems();
}
$searchitem->setType('glossary');
$searchitem->setPath($this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]));
$searchitem->setNameNl($glossary->getTitleNl());
$searchitem->setNameEn($glossary->getTitleEn());
$searchitem->setKeywordsNl($glossary->getTitleNl().','.$glossary->getKeywordsNl());
$searchitem->setKeywordsEn($glossary->getTitleEn().','.$glossary->getKeywordsEn());
if(empty($glossary->getEdited())) {
$searchitem->setLastUpdateTime($glossary->getAdded());
} else {
$searchitem->setLastUpdateTime($glossary->getEdited());
}
$em->persist($searchitem);
$em->flush();
} else {
if($searchitem) {
$em->remove($searchitem);
$em->flush();
}
}
$this->addFlash('success', $translator->trans("Glossarium bijgewerkt"));
return $this->redirectToRoute('glossary_moderate');
}
return $this->render('glossary_edit.html.twig', [
'moderator_form' => true,
'title' => $translator->trans("Glossarium bewerken"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/glossary/{id}/revoke", name="glossary_moderate_revoke")
*/
public function moderate_revoke(int $id, Glossaries $glossary, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$glossary->setApprovedUserId($this->getUser()->getId());
$glossary->setApprovedUsername($this->getUser()->getUsername());
$glossary->setApproved('0');
$em->persist($glossary);
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
if($searchitem){
$em->remove($searchitem);
}
$em->flush();
$this->addFlash('success', $translator->trans("Glossarium ingetrokken"));
return $this->redirectToRoute('glossary_moderate');
}
/**
* @Route("/moderate/glossary/{id}/approve", name="glossary_moderate_approve")
*/
public function moderate_approve(int $id, Glossaries $glossary, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$glossary->setApprovedUserId($this->getUser()->getId());
$glossary->setApprovedUsername($this->getUser()->getUsername());
$glossary->setApproved('1');
$em->persist($glossary);
$newsfeed = new Newsfeed();
$newsfeed->setTitleNl($glossary->getTitleNl());
$newsfeed->setTitleEn($glossary->getTitleEn());
$newsfeed->setRemoteId($glossary->getId());
$newsfeed->setType('glossary');
$newsfeed->setIsUpdate(0);
$newsfeed->setTime(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($newsfeed);
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
if(!$searchitem) {
$searchitem = new SearchItems();
}
$searchitem->setType('glossary');
$searchitem->setPath($this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]));
$searchitem->setNameNl($glossary->getTitleNl());
$searchitem->setNameEn($glossary->getTitleEn());
$searchitem->setKeywordsNl($glossary->getTitleNl().','.$glossary->getKeywordsNl());
$searchitem->setKeywordsEn($glossary->getTitleEn().','.$glossary->getKeywordsEn());
if(empty($glossary->getEdited())) {
$searchitem->setLastUpdateTime($glossary->getAdded());
} else {
$searchitem->setLastUpdateTime($glossary->getEdited());
}
$em->persist($searchitem);
$em->flush();
$this->addFlash('success', $translator->trans("Glossarium goedgekeurd"));
return $this->redirectToRoute('glossary_moderate');
}
/**
* @Route("/moderate/glossary/{id}/delete", name="glossary_moderate_delete")
*/
public function moderate_delete(Glossaries $id, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$glossary = $id;
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('glossary', [ 'id' => $glossary->getId(), 'url' => $glossary->getUrl() ]) ));
if($searchitem){
$em->remove($searchitem);
}
$em->remove($glossary);
$em->flush();
$this->addFlash('success', $translator->trans("Glossarium verwijderd"));
return $this->redirectToRoute('glossary_moderate');
}
/**
* @Route("/moderate/glossary/{id}/reviews", name="glossary_moderate_reviews")
*/
public function moderate_review(Glossaries $id, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
return $this->render('glossaryreviews_moderate.html.twig', [
'glossary' => $id,
]);
}
/**
* @Route("/glossaries/review/{id}", name="glossary_review_add")
*/
public function glossary_review_add(Glossaries $id, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_USER');
if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
$form = $this->createForm(GlossaryReviewFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$glossary = $id;
$review = $form->getData();
$review->setUserId($this->getUser()->getId());
$review->setUsername($this->getUser()->getUsername());
$review->setGlossary($glossary);
$em->persist($review);
$em->flush();
$num_reviews = $glossary->getNumReviews();
$glossary->setNumReviews($num_reviews+1);
$em->persist($glossary);
$em->flush();
$this->addFlash('success', $translator->trans("Feedback doorgegeven"));
return $this->redirectToRoute('glossary',array('id' => $glossary->getId(), 'url' => $glossary->getUrl() ));
}
return $this->render('form_edit.html.twig', [
'title' => $translator->trans("Feedback doorgeven"),
'button_text' => $translator->trans("Insturen"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/glossary/review/{id}/read", name="glossaryreviews_moderate_read")
*/
public function review_read(GlossaryReviews $id, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$glossaryreview = $id;
$repository = $this->getDoctrine()->getRepository(GlossaryReviews::class);
$glossaryreview = $repository->findOneBy(array('id'=>$id));
return $this->render('glossaryreview.html.twig', [
'review' => $glossaryreview,
]);
}
/**
* @Route("/moderate/glossary/review/{id}/delete", name="glossaryreviews_moderate_delete")
*/
public function review_delete(int $id, GlossaryReviews $glossaryreview, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_GLOSSARY');
$glossary = $glossaryreview->getGlossary();
$glossary->setNumReviews($glossary->getNumReviews()-1);
$em->persist($glossary);
$em->flush();
$em->remove($glossaryreview);
$em->flush();
$this->addFlash('success', $translator->trans("Glossarium review verwijderd"));
return $this->redirectToRoute('glossary_moderate_reviews', array('id' => $glossary->getId() ));
}
/**
* @Route("/api/v1/glossary", methods="GET", name="api_glossary")
*/
public function api_glossary(GlossariesRepository $GlossariesRepository, Request $request, TranslatorInterface $translator)
{
$glossary = $GlossariesRepository->findAllMatching($request->query->get('query'));
$return = array();
foreach($glossary as $item) {
if($translator->getLocale() == 'nl') {
$name = $item->getTitleNl();
} else {
$name = $item->getTitleEn();
}
$return[] = array(
'id' => $item->getId(),
'value' => $name,
);
}
return $this->json($return, 200, [], ['groups' => ['main']]);
}
}