<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Form\SpecieslistSearchFormType;
use App\Repository\TaxanomyRepository;
use App\Repository\LocationsFossilRepository;
use App\Repository\StratigraphyUnitsRepository;
Use App\Entity\LocationsFossil;
Use App\Entity\StratigraphyUnits;
Use App\Entity\Taxanomy;
class SpecieslistController extends AbstractController
{
/**
* @Route("/specieslist/", name="specieslist")
*/
public function specieslist(Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
/*
ToDo-Ante: Index voor soortenlijst. Zie:
https://paleontica.org/id_system/species_list.php
https://paleontica.org/id_system/species_list_stratigraphy.php
*/
if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
$filter = array( 'location' => '[""]', 'era' => '[""]', 'epoch' => '[""]', 'stage' => '[""]');
$form = $this->createForm(SpecieslistSearchFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$fossil = $form->getData();
if(!is_null($fossil['location_fossil'])) {
$location_fossil = json_decode($fossil['location_fossil'])[0]->id;
} else {
$location_fossil=null;
}
if(!is_null($fossil['era'])) {
$era = json_decode($fossil['era'])[0]->id;
} else {
$era=null;
}
if(!is_null($fossil['epoch'])) {
$epoch = json_decode($fossil['epoch'])[0]->id;
} else {
$epoch=null;
}
if(!is_null($fossil['stage'])) {
$stage = json_decode($fossil['stage'])[0]->id;
} else {
$stage=null;
}
if(!is_null($fossil['formation'])) {
$formation = json_decode($fossil['formation'])[0]->id;
} else {
$formation=null;
}
if(!is_null($fossil['member'])) {
$member = json_decode($fossil['member'])[0]->id;
} else {
$member=null;
}
if($location_fossil && is_null($era) && is_null($epoch) && is_null($stage) && is_null($formation) && is_null($member)) {
$type = 'location';
$id = $location_fossil;
}
elseif(is_null($location_fossil) && $era && is_null($epoch) && is_null($stage) && is_null($formation) && is_null($member)) {
$type = 'era';
$id = $era;
}
elseif(is_null($location_fossil) && $era && $epoch && is_null($stage) && is_null($formation) && is_null($member)) {
$type = 'epoch';
$id = $epoch;
}
elseif(is_null($location_fossil) && $era && $epoch && $stage && is_null($formation) && is_null($member)) {
$type = 'stage';
$id = $stage;
}
elseif(is_null($location_fossil) && is_null($era) && is_null($epoch) && is_null($stage) && $formation && is_null($member)) {
$type = 'formation';
$id = $formation;
}
elseif(is_null($location_fossil) && is_null($era) && is_null($epoch) && is_null($stage) && $formation && $member) {
$type = 'member';
$id = $member;
}
else {
return $this->render('error_message.html.twig', [
'error_title' => $translator->trans("Fout"),
'error_message' => $translator->trans("Selecteer of een locatie of een tijdperk of een formatie."),
]);
}
return $this->redirectToRoute('specieslist_item', [ 'type' => $type, 'id' => $id]);
}
return $this->render('specieslist_search.html.twig', [
'filter' => $filter,
'formname' => 'specieslist_search_form',
'title' => $translator->trans("Soortenlijst openen"),
'form' => $form->createView()
]);
}
/**
* @Route("/specieslist/{type}/{id}", name="specieslist_item")
*/
public function specieslist_item(string $type, int $id, EntityManagerInterface $em)
{
if($type == 'location') {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$title = $repository->findOneBy(array('id' => $id));
} else {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$title = $repository->findOneBy(array('type' => $type, 'id' => $id));
}
$parents=array();
if($type == 'era') {
$periodename = strtolower($title->getNameEn());
} elseif($type == 'epoch' || $type == 'stage') {
$qb = $em->createQueryBuilder('s')
->from('App\Entity\StratigraphyUnits', 's')
->addSelect('s')
->andWhere("s.type = 'era'")
->andWhere("s.age_start <= (:start)")
->andWhere("s.age_end >= (:end)")
->setParameter('start', $title->getAgeStart())
->setParameter('end', $title->getAgeEnd())
->setMaxResults(1)
;
$db_result = $qb->getQuery()->getOneOrNullResult();
$periodename = strtolower($db_result->getNameEn());
$parents[]=$db_result;
if($type == 'stage') {
$qb = $em->createQueryBuilder('s')
->from('App\Entity\StratigraphyUnits', 's')
->addSelect('s')
->andWhere("s.type = 'epoch'")
->andWhere("s.age_start <= (:start)")
->andWhere("s.age_end >= (:end)")
->setParameter('start', $title->getAgeStart())
->setParameter('end', $title->getAgeEnd())
->setMaxResults(1)
;
$db_result = $qb->getQuery()->getOneOrNullResult();
$parents[]=$db_result;
}
} else {
$periodename = null;
}
if($type == 'location') {
$query = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.location_fossil = (:location) GROUP BY f.taxanomy')
->setParameter('location', $id);
} elseif($type == 'era') {
$query = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.era = (:id) GROUP BY f.taxanomy')
->setParameter('id', $id);
} elseif($type == 'epoch') {
$query = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.epoch = (:id) GROUP BY f.taxanomy')
->setParameter('id', $id);
} elseif($type == 'stage') {
$query = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.stage = (:id) GROUP BY f.taxanomy')
->setParameter('id', $id);
} elseif($type == 'formation') {
$query = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.formation = (:title) GROUP BY f.taxanomy')
->setParameter('title', $id);
} elseif($type == 'member') {
$query = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.member = (:title) GROUP BY f.taxanomy')
->setParameter('title', $id);
}
$taxanomy_group_result = $query->getResult();
$i=0;
$taxonomies=array();
return $this->render('specieslist_item.html.twig', [
'taxonomies' => $taxanomy_group_result,
'title' => $title,
'type' => $type,
'id' => $id,
'periodname' => $periodename,
'parents' => $parents,
]);
}
/**
* @Route("/specieslist/{type}/{id}/{taxanomyid}", name="specieslist_species")
*/
public function specieslist_species(string $type, int $id, int $taxanomyid, EntityManagerInterface $em, Request $request, PaginatorInterface $paginator)
{
if($type == 'location') {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$title = $repository->findOneBy(array('id' => $id));
} else {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$title = $repository->findOneBy(array('type' => $type, 'id' => $id));
//dd($title->getNameEn());
}
if($taxanomyid == 0) {
$taxanomyid = null;
}
if($type == 'location') {
$query = $em->createQuery('SELECT DISTINCT f.genus,f.species FROM App\Entity\FossildbFossil f WHERE f.location_fossil = (:location) AND f.taxanomy = (:taxanomy) ORDER BY f.genus,f.species');
$query->setParameter('location', $id);
}
elseif($type == 'era') {
$query = $em->createQuery('SELECT DISTINCT f.genus,f.species FROM App\Entity\FossildbFossil f WHERE f.era = (:era) AND f.taxanomy = (:taxanomy) ORDER BY f.genus,f.species');
$query->setParameter('era', $id);
}
elseif($type == 'epoch') {
$query = $em->createQuery('SELECT DISTINCT f.genus,f.species FROM App\Entity\FossildbFossil f WHERE f.epoch = (:epoch) AND f.taxanomy = (:taxanomy) ORDER BY f.genus,f.species');
$query->setParameter('epoch', $id);
}
elseif($type == 'stage') {
$query = $em->createQuery('SELECT DISTINCT f.genus,f.species FROM App\Entity\FossildbFossil f WHERE f.stage = (:stage) AND f.taxanomy = (:taxanomy) ORDER BY f.genus,f.species');
$query->setParameter('stage', $id);
}
elseif($type == 'formation') {
$query = $em->createQuery('SELECT DISTINCT f.genus,f.species FROM App\Entity\FossildbFossil f WHERE f.formation = (:formation) AND f.taxanomy = (:taxanomy) ORDER BY f.genus,f.species');
$query->setParameter('formation', $id);
}
elseif($type == 'member') {
$query = $em->createQuery('SELECT DISTINCT f.genus,f.species FROM App\Entity\FossildbFossil f WHERE f.member = (:member) AND f.taxanomy = (:taxanomy) ORDER BY f.genus,f.species');
$query->setParameter('member', $id);
}
$query->setParameter('taxanomy', $taxanomyid);
$fossils = $query->getResult();
if(is_null($taxanomyid)) {
$queryglossary = $em->createQuery("SELECT g FROM App\Entity\Glossaries g WHERE g.url = 'Incertae-sedis'" );
$glossarytype = $queryglossary->getResult()[0];
$taxanomytype = array(
'namenl' => "Incerta Sedis (Onbekend)",
'nameen' => "Incerta Sedis (Unknown)",
'namescientific' => "incertae sedis",
'namescientificclarification' => null,
'glossary' => $glossarytype,
);
} else {
$querytaxanomy = $em->createQuery('SELECT t FROM App\Entity\Taxanomy t WHERE t.id = (:taxanomy)');
$querytaxanomy->setParameter('taxanomy', $taxanomyid);
$taxanomytype = $querytaxanomy->getResult()[0];
}
foreach ($fossils as $i => $fossil) {
if($type == 'location') {
$fossilsquery = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.location_fossil = (:id) AND f.taxanomy = (:taxanomy) AND f.genus = (:genus) AND f.species = (:species)');
} elseif($type == 'era') {
$fossilsquery = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.era = (:id) AND f.taxanomy = (:taxanomy) AND f.genus = (:genus) AND f.species = (:species)');
} elseif($type == 'epoch') {
$fossilsquery = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.epoch = (:id) AND f.taxanomy = (:taxanomy) AND f.genus = (:genus) AND f.species = (:species)');
} elseif($type == 'stage') {
$fossilsquery = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.stage = (:id) AND f.taxanomy = (:taxanomy) AND f.genus = (:genus) AND f.species = (:species)');
} elseif($type == 'formation') {
$fossilsquery = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.formation = (:id) AND f.taxanomy = (:taxanomy) AND f.genus = (:genus) AND f.species = (:species)');
} elseif($type == 'member') {
$fossilsquery = $em->createQuery('SELECT f FROM App\Entity\FossildbFossil f WHERE f.member = (:id) AND f.taxanomy = (:taxanomy) AND f.genus = (:genus) AND f.species = (:species)');
}
$fossilsquery->setParameter('id', $id);
$fossilsquery->setParameter('taxanomy', $taxanomyid);
$fossilsquery->setParameter('genus', $fossil['genus']);
$fossilsquery->setParameter('species', $fossil['species']);
$fossils[$i]['fossils'] = $fossilsquery->getResult();
foreach ($fossils[$i]['fossils'] as $j => $fossil) {
if($fossil->getPublication() != "") {
$fossils[$i]['publication'] = $fossil->getPublication();
}
}
}
$fossilspagnation = $paginator->paginate(
$fossils,
$request->query->getInt('page', 1),
10
);
return $this->render('specieslist_species.html.twig', [
'fossils' => $fossilspagnation,
'taxanomytype' => $taxanomytype,
'title' => $title,
'type' => $type,
'id' => $id,
]);
}
}