<?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 App\Entity\Newsfeed;
use App\Entity\Frontpage;
use App\Entity\Articles;
use App\Entity\Glossaries;
use App\Entity\LocationsFossil;
use App\Entity\LocationsGeology;
use App\Entity\LocationsMuseum;
use App\Entity\LocationsSociety;
//Community
use App\Entity\FossildbFossil;
use App\Form\FrontpageFormType;
use Doctrine\ORM\EntityManagerInterface;
class FrontpageController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @Route("/", name="frontpage")
*/
public function index(EntityManagerInterface $em)
{
$repository = $this->getDoctrine()->getRepository(Frontpage::class);
$frontpage = $this->em->getRepository(Frontpage::class)->createQueryBuilder('f')
->select('f.header_article_type, f.header_article, f.header_article_enabled, f.highlight_location_type, f.highlight_location, f.highlight_article_type, f.highlight_article, f.highlight_fossildb, f.event_title, f.event_text, f.event_url, f.event_enabled')
->where('f.id = 1')
->getQuery()
->getResult()[0];
if($frontpage['header_article_type'] == 'article') {
$header_article = $this->em->getRepository(Articles::class)->createQueryBuilder('a')
->select('a.id, a.url, a.image, a.title_nl, a.title_en, a.intro_nl, a.intro_en ')
->where('a.id = '.json_decode($frontpage['header_article'])[0]->id)
->getQuery()
->getResult()[0];
}
elseif($frontpage['header_article_type'] == 'glossary') {
$header_article = $this->em->getRepository(Glossaries::class)->createQueryBuilder('a')
->select('a.id, a.url, a.image, a.title_nl, a.title_en, a.intro_nl, a.intro_en ')
->where('a.id = '.json_decode($frontpage['header_article'])[0]->id)
->getQuery()
->getResult()[0];
}
if($frontpage['highlight_location_type'] == 'fossil') {
$highlight_location = $this->em->getRepository(LocationsFossil::class)->createQueryBuilder('l')
->select('l.id, l.name_nl, l.name_en, l.description_nl, l.description_en')
->where('l.id = '.json_decode($frontpage['highlight_location'])[0]->id)
->getQuery()
->getResult()[0];
}
elseif($frontpage['highlight_location_type'] == 'geology') {
$highlight_location = $this->em->getRepository(LocationsGeology::class)->createQueryBuilder('l')
->select('l.id, l.name_nl, l.name_en, l.description_nl, l.description_en')
->where('l.id = '.json_decode($frontpage['highlight_location'])[0]->id)
->getQuery()
->getResult()[0];
}
elseif(['highlight_location_type'] == 'museum') {
$highlight_location = $this->em->getRepository(LocationsMuseum::class)->createQueryBuilder('l')
->select('l.id, l.name_nl, l.name_en, l.description_nl, l.description_en')
->where('l.id = '.json_decode($frontpage['highlight_location'])[0]->id)
->getQuery()
->getResult()[0];
}
elseif(['highlight_location_type'] == 'society') {
$highlight_location = $this->em->getRepository(LocationsSociety::class)->createQueryBuilder('l')
->select('l.id, l.name_nl, l.name_en, l.description_nl, l.description_en')
->where('l.id = '.json_decode($frontpage['highlight_location'])[0]->id)
->getQuery()
->getResult()[0];
}
if($frontpage['highlight_article_type'] == 'article') {
$highlight_article = $this->em->getRepository(Articles::class)->createQueryBuilder('a')
->select('a.id, a.url, a.image, a.title_nl, a.title_en, a.description_nl, a.description_en ')
->where('a.id = '.json_decode($frontpage['highlight_article'])[0]->id)
->getQuery()
->getResult()[0];
}
elseif($frontpage['highlight_article_type'] == 'glossary') {
$highlight_article = $this->em->getRepository(Glossaries::class)->createQueryBuilder('a')
->select('a.id, a.url, a.image, a.title_nl, a.title_en, a.description_nl, a.description_en ')
->where('a.id = '.json_decode($frontpage['highlight_article'])[0]->id)
->getQuery()
->getResult()[0];
}
$forumConn = $this->getDoctrine()->getConnection('forumdb');
//$sql = $forumConn->prepare("SELECT `phpbb_posts`.`post_id`, `phpbb_posts`.`forum_id`, `phpbb_posts`.`poster_id`, `phpbb_users`.`username`, `phpbb_posts`.`post_subject`, `phpbb_topics`.`topic_first_post_id` FROM `phpbb_posts`, `phpbb_topics`, `phpbb_users` WHERE `phpbb_topics`.`topic_id` = `phpbb_posts`. `topic_id` AND `phpbb_users`.`user_id` = `phpbb_posts`.`poster_id` ORDER BY `phpbb_posts`.`post_id` DESC LIMIT 1");
$forumQuery = $forumConn->prepare("SELECT `phpbb_posts`.`post_id`, `phpbb_posts`.`forum_id`, `phpbb_users`.`username`, `phpbb_posts`.`post_subject`, `phpbb_posts`.`post_text`, `phpbb_topics`.`topic_first_post_id` FROM `phpbb_posts`, `phpbb_topics`, `phpbb_users` WHERE `phpbb_topics`.`topic_id` = `phpbb_posts`. `topic_id` AND `phpbb_topics`.`forum_id` NOT IN ('70', '90', '94') AND `post_visibility` = 1 AND `phpbb_users`.`user_id` = `phpbb_posts`.`poster_id` ORDER BY `phpbb_posts`.`post_id` DESC LIMIT 1");
$forumQuery->execute();
$highlight_community = $forumQuery->fetchAll()[0];
$subrepository = $this->getDoctrine()->getRepository(FossildbFossil::class);
$highlight_fossildb = $subrepository->findOneBy(array('id' => $frontpage['highlight_fossildb']));
$repository = $this->getDoctrine()->getRepository(Newsfeed::class);
$newsfeed = $repository->findby(array(),array('time' => 'DESC'),35);
$newsfeed_part1 = array_slice($newsfeed, 0, 15);
$newsfeed_part2 = array_slice($newsfeed, 15, 20);
// $forumQuery = $forumConn->prepare("SELECT `phpbb_posts`.`post_id`, `phpbb_posts`.`forum_id`, `phpbb_posts`.`poster_id`, `phpbb_users`.`username`, `phpbb_posts`.`post_subject`, `phpbb_topics`.`topic_first_post_id` FROM `phpbb_posts`, `phpbb_topics`, `phpbb_users` WHERE `phpbb_topics`.`topic_id` = `phpbb_posts`. `topic_id` AND `phpbb_users`.`user_id` = `phpbb_posts`.`poster_id` ORDER BY `phpbb_posts`.`post_id` DESC LIMIT 35");
// $forumQuery->execute();
// $newsfeed_community = $forumQuery->fetchAll();
// dump($newsfeed_community);
return $this->render('frontpage.html.twig', [
'frontpage' => $frontpage,
'header_article' => $header_article,
'highlight_location' => $highlight_location,
'highlight_article' => $highlight_article,
'highlight_fossildb' => $highlight_fossildb,
'highlight_community' => $highlight_community,
'newsfeed_part1' => $newsfeed_part1,
'newsfeed_part2' => $newsfeed_part2,
]);
}
/**
* @Route("/moderate/frontpage/", name="frontpage_moderate")
*/
public function frontpage(Request $request, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FRONTPAGE');
$repository = $this->getDoctrine()->getRepository(Frontpage::class);
$frontpage = $repository->findOneBy(array('id' => 1));
$form = $this->createForm(FrontpageFormType::class, $frontpage);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$frontpage = $form->getData();
$em->persist($frontpage);
$em->flush();
}
return $this->render('frontpage_moderate.html.twig', [
'form' => $form->createView(),
'filter' => $frontpage
]);
}
}