<?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 Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Form\FossildbSearchFormType;
use App\Form\FossildbModeratorSearchFormType;
use App\Form\FossildbFossilFormType;
use App\Form\FossildbFossilEditFormType;
use App\Form\FossildbFossilModeratorEditFormType;
use App\Form\FossildbReviewsFormType;
use App\Form\FossildbFossilAddPhotoFormType;
use App\Repository\TaxanomyRepository;
use App\Repository\LocationsFossilRepository;
use App\Repository\StratigraphyUnitsRepository;
use App\Repository\FossildbFossilRepository;
use App\Entity\LocationsFossil;
use App\Entity\StratigraphyUnits;
use App\Entity\Taxanomy;
use App\Entity\FossildbFossil;
use App\Entity\FossildbFossilEdits;
use App\Entity\FossildbPhotos;
use App\Entity\FossildbReviews;
use App\Entity\Literature;
use App\Entity\UploadQueue;
use App\Entity\Newsfeed;
use App\Entity\User;
use App\Entity\UserProfile;
use App\Entity\SearchItems;
class FossildbController extends AbstractController
{
private $session;
private $translator;
private $em;
private $router;
public function __construct(SessionInterface $session, TranslatorInterface $translator, EntityManagerInterface $em, RouterInterface $router)
{
$this->session = $session;
$this->em = $em;
$this->translator = $translator;
$this->router = $router;
}
private function taxanomy_tree(?int $id, EntityManagerInterface $em) {
if($id != null) {
$qb = $em->createQueryBuilder('f')
->from('App\Entity\Taxanomy', 'f')
->addSelect('f')
->andWhere("f.id = (:id)")
->orderBy('f.id','ASC')
->setParameter('id', $id)
->setMaxResults(1)
;
$db_result = $qb->getQuery()->getResult();
if ($db_result[0]->getParentId() != '0' || $db_result[0]->getParentId() != null) {
$tree = $this->taxanomy_tree($db_result[0]->getParentId(),$em);
$tree[] = $db_result[0];
} else {
$tree = array($db_result[0]);
}
return $tree;
}
}
/**
* @Route("/fossildb", name="fossildb")
*/
public function index(Request $request, EntityManagerInterface $em, PaginatorInterface $paginator)
{
$form = $this->createForm(FossildbSearchFormType::class);
$form->handleRequest($request);
$taxanomy = array();
$location = array();
$era = array();
$genus = array();
$species = array();
$user = array();
if ($form->isSubmitted() && $form->isValid()){
$filter = $form->getData();
$page = 1;
} else {
if(!$request->query->get('page')){
$filter=array(
'taxanomy'=> '[]',
'location' => '[]',
'era' => '[]',
'genus' => '[]',
'species' => '[]',
'user' => '[]',
'id' => null
);
$form->get('taxanomy')->setData("");
$form->get('location')->setData("");
$form->get('era')->setData("");
$form->get('genus')->setData("");
$form->get('species')->setData("");
$form->get('id')->setData(null);
$form->get('user')->setData("");
$page = false;
} elseif($request->query->get('filter') && $request->query->get('page')) {
$page = $request->query->get('page');
$filter = $request->query->get('filter');
if(isset($filter['taxanomy']) && $filter['taxanomy'] != '[""]') { $form->get('taxanomy')->setData($filter['taxanomy']); } else { $form->get('taxanomy')->setData(''); $filter['taxanomy'] = null; };
if(isset($filter['location']) && $filter['location'] != '[""]') { $form->get('location')->setData($filter['location']); } else { $form->get('location')->setData(''); $filter['location'] = null; };
if(isset($filter['era']) && $filter['era'] != '[""]') { $form->get('era')->setData($filter['era']); } else { $form->get('era')->setData(''); $filter['era'] = null; };
if(isset($filter['genus']) && $filter['genus'] != '[""]') { $form->get('genus')->setData($filter['genus']); } else { $form->get('genus')->setData(null); $filter['genus'] = null;};
if(isset($filter['species']) && $filter['species'] != '[""]') { $form->get('species')->setData($filter['species']); } else { $form->get('species')->setData(null); $filter['species'] = null;};
if(isset($filter['id']) && $filter['id'] != '') { $form->get('id')->setData($filter['id']); } else { $form->get('id')->setData(null); $filter['id'] = null;};
if(isset($filter['user']) && $filter['user'] != '[""]') { $form->get('user')->setData($filter['user']); } else { $form->get('user')->setData(''); $filter['user'] = null; };
} else {
dd("Unexpected Error");
}
}
$filter_taxanomy = json_decode($filter['taxanomy']);
$filter_location = json_decode($filter['location']);
$filter_era = json_decode($filter['era']);
$filter_genus = json_decode($filter['genus']);
$filter_species = json_decode($filter['species']);
$filter_user = json_decode($filter['user']);
if(is_array($filter_taxanomy)) {
foreach($filter_taxanomy as $id => $item) {
$taxanomy[] = $item->id;
}
}
if(is_array($filter_location)) {
foreach($filter_location as $id => $item) {
$location[] = $item->id;
}
}
if(is_array($filter_era)) {
foreach($filter_era as $id => $item) {
$era[] = $item->id;
}
}
if(is_array($filter_genus)) {
foreach($filter_genus as $id => $item) {
$genus[] = $item->value;
}
}
if(is_array($filter_species)) {
foreach($filter_species as $id => $item) {
$species[] = $item->value;
}
}
if(is_array($filter_user)) {
foreach($filter_user as $id => $item) {
$user[] = $item->id;
}
}
if(is_null($filter['taxanomy'])) { $filter['taxanomy'] = '[""]'; }
if(is_null($filter['location'])) { $filter['location'] = '[""]'; }
if(is_null($filter['era'])) { $filter['era'] = '[""]'; }
if(!is_numeric($filter['id'])) { $filter['id'] = null; }
if(is_null($filter['genus'])) { $filter['genus'] = '[""]'; }
if(is_null($filter['species'])) { $filter['species'] = '[""]'; }
if(is_null($filter['user'])) { $filter['user'] = '[""]'; }
$qb = $em->createQueryBuilder('f')
->from('App\Entity\FossildbFossil', 'f')
->addSelect('f')
->andWhere('f.approved = 1')
->orderBy('f.id','DESC')
;
if(!empty($taxanomy)) {
$qb->andWhere("f.taxanomy IN (:taxanomy)")
->setParameter('taxanomy', array_values($taxanomy))
;
}
if(!empty($location)) {
$qb->andWhere("f.location_fossil IN (:location)")
->setParameter('location', array_values($location))
;
}
if(!empty($era)) {
$qb->andWhere("f.era IN (:era)")
->setParameter('era', array_values($era))
;
}
if(!empty($filter['id'])) {
$qb->andWhere("f.id = :id")
->setParameter('id', $filter['id'])
;
}
if(!empty($genus)) {
$qb->andWhere("f.genus IN (:genus)")
->setParameter('genus', array_values($genus))
;
}
if(!empty($species)) {
$qb->andWhere("f.species IN (:species)")
->setParameter('species', array_values($species))
;
}
if(!empty($user)) {
$qb->andWhere("f.user_id IN (:user)")
->setParameter('user', array_values($user))
;
}
$result = $paginator->paginate(
$qb, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
10 /*limit per page*/
);
$result->setParam('filter', $filter);
$result->setCustomParameters([
'align' => 'right', # center|right (for template: twitter_bootstrap_v4_pagination)
]);
foreach($result as $id => $object) {
if($object->getTaxanomy() && is_null($object->getTaxanomyTree())) {
$constructed_taxanomy_tree=$this->taxanomy_tree($object->getTaxanomy()->getId(), $em);
$taxanomy_tree = array();
foreach($constructed_taxanomy_tree as $item => $object) {
$taxanomy_tree[$item]['namescientific'] = $object->getNameScientific();
$taxanomy_tree[$item]['namescientificclarification'] = $object->getNameScientificClarification();
$taxanomy_tree[$item]['namenl'] = $object->getNameNl();
$taxanomy_tree[$item]['nameen'] = $object->getNameEn();
$taxanomy_tree[$item]['author'] = $object->getAuthor();
$taxanomy_tree[$item]['authoryear'] = $object->getAuthorYear();
if($object->getGlossary()){
$taxanomy_tree[$item]['glossary']['id'] = $object->getGlossary()->getId();
$taxanomy_tree[$item]['glossary']['url'] = $object->getGlossary()->getUrl();
$taxanomy_tree[$item]['glossary']['titlenl'] = $object->getGlossary()->getTitleNl();
$taxanomy_tree[$item]['glossary']['titleen'] = $object->getGlossary()->getTitleEn();
} else {
$taxanomy_tree[$item]['glossary'] = null;
}
$taxanomy_tree[$item]['taxanomyrank']['namescientific'] = $object->getTaxanomyRank()->getNameScientific();
$taxanomy_tree[$item]['taxanomyrank']['namenl'] = $object->getTaxanomyRank()->getNameNl();
$taxanomy_tree[$item]['taxanomyrank']['nameen'] = $object->getTaxanomyRank()->getNameEn();
}
$result[$id]->setTaxanomyTree(array_reverse($taxanomy_tree));
$em->persist($result[$id]);
$em->flush();
}
}
return $this->render('fossildb.html.twig', [
'form' => $form->createView(),
'filter' => $filter,
'result' => $result,
'page' => $page,
]);
}
/**
* @Route("/fossildb/fossil/{id}", name="fossildb_item")
*/
public function fossildb_item(FossildbFossil $id)
{
return $this->render('fossildb_single.html.twig', [
'item' => $id,
]);
}
/**
* @Route("/fossildb/add", name="fossildb_add")
*/
public function fossildb_add(Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_USER');
if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
$filter = array( 'genus' => '[""]', 'species' => '[""]', 'publication' => '[""]', 'literature' => '[""]', 'location' => '[""]', 'taxanomy' => '[""]', 'era' => '[""]', 'epoch' => '[""]', 'stage' => '[""]');
$form = $this->createForm(FossildbFossilFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$fossil = $form->getData();
if(!is_null($fossil['location_fossil'])) {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$location_fossil = $repository->findOneBy(array('id' => json_decode($fossil['location_fossil'])[0]->id));
} else {
$location_fossil=null;
}
if(!is_null($fossil['taxanomy'])) {
$repository = $this->getDoctrine()->getRepository(Taxanomy::class);
$taxanomy = $repository->findOneBy(array('id' => json_decode($fossil['taxanomy'])[0]->id));
} else {
$taxanomy=null;
}
if(!is_null($fossil['era'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$era = $repository->findOneBy(array('id' => json_decode($fossil['era'])[0]->id));
} else {
$era=null;
}
if(!is_null($fossil['epoch'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$epoch = $repository->findOneBy(array('id' => json_decode($fossil['epoch'])[0]->id));
} else {
$epoch=null;
}
if(!is_null($fossil['stage'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$stage = $repository->findOneBy(array('id' => json_decode($fossil['stage'])[0]->id));
} else {
$stage=null;
}
if(!is_null($fossil['formation'])) {
$formation_item = json_decode($fossil['formation'])[0];
if (property_exists($formation_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$formation = $repository->findOneBy(array('id' => $formation_item->id));
} else {
$new_formation_item = new StratigraphyUnits;
$new_formation_item->setType("member");
$new_formation_item->setNameNl($formation_item->value);
$new_formation_item->setNameEn($formation_item->value);
$em->persist($new_formation_item);
$em->flush();
$formation = $new_formation_item->getId();
}
} else {
$formation=null;
}
if(!is_null($fossil['member'])) {
$member_item = json_decode($fossil['member'])[0];
if (property_exists($member_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$member = $repository->findOneBy(array('id' => $member_item->id));
} else {
$new_member_item = new StratigraphyUnits;
$new_member_item->setType("member");
$new_member_item->setNameNl($member_item->value);
$new_member_item->setNameEn($member_item->value);
$em->persist($new_member_item);
$em->flush();
$member = $new_member_item;
}
} else {
$member=null;
}
if(!is_null($fossil['literature'])) {
$repository = $this->getDoctrine()->getRepository(Literature::class);
$literature = $repository->findOneBy(array('id' => json_decode($fossil['literature'])[0]->id));
} else {
$literature=null;
}
$fossildbFossil = new FossildbFossil;
if(json_decode($fossil['genus'])[0]) {
$fossildbFossil->setGenus(json_decode($fossil['genus'])[0]->value);
}
if(json_decode($fossil['species'])[0]) {
$fossildbFossil->setSpecies(json_decode($fossil['species'])[0]->value);
}
if(json_decode($fossil['publication'])[0]) {
$fossildbFossil->setPublication(json_decode($fossil['publication'])[0]->value);
}
$fossildbFossil->setLocationFossil($location_fossil);
$fossildbFossil->setTaxanomy($taxanomy);
$fossildbFossil->setEra($era);
$fossildbFossil->setEpoch($epoch);
$fossildbFossil->setStage($stage);
$fossildbFossil->setFormation($formation);
$fossildbFossil->setMember($member);
$fossildbFossil->setDateYear($fossil['date_year']);
$fossildbFossil->setDateMonth($fossil['date_month']);
$fossildbFossil->setDateDay($fossil['date_day']);
$fossildbFossil->setCommentNl($fossil['comment_nl']);
$fossildbFossil->setCommentEn($fossil['comment_en']);
$fossildbFossil->setUserNumber($fossil['user_number']);
$fossildbFossil->setLiterature($literature);
$constructed_taxanomy_tree=$this->taxanomy_tree(json_decode($fossil['taxanomy'])[0]->id, $em);
$taxanomy_tree = array();
foreach($constructed_taxanomy_tree as $item => $object) {
$taxanomy_tree[$item]['namescientific'] = $object->getNameScientific();
$taxanomy_tree[$item]['namescientificclarification'] = $object->getNameScientificClarification();
$taxanomy_tree[$item]['namenl'] = $object->getNameNl();
$taxanomy_tree[$item]['nameen'] = $object->getNameEn();
$taxanomy_tree[$item]['author'] = $object->getAuthor();
$taxanomy_tree[$item]['authoryear'] = $object->getAuthorYear();
if($object->getGlossary()){
$taxanomy_tree[$item]['glossary']['id'] = $object->getGlossary()->getId();
$taxanomy_tree[$item]['glossary']['url'] = $object->getGlossary()->getUrl();
$taxanomy_tree[$item]['glossary']['titlenl'] = $object->getGlossary()->getTitleNl();
$taxanomy_tree[$item]['glossary']['titleen'] = $object->getGlossary()->getTitleEn();
} else {
$taxanomy_tree[$item]['glossary'] = null;
}
$taxanomy_tree[$item]['taxanomyrank']['namescientific'] = $object->getTaxanomyRank()->getNameScientific();
$taxanomy_tree[$item]['taxanomyrank']['namenl'] = $object->getTaxanomyRank()->getNameNl();
$taxanomy_tree[$item]['taxanomyrank']['nameen'] = $object->getTaxanomyRank()->getNameEn();
}
$fossildbFossil->setTaxanomyTree(array_reverse($taxanomy_tree));
$fossildbFossil->setAdded(new \DateTime());
$fossildbFossil->setUserId($this->getUser()->getId());
$fossildbFossil->setUsername($this->getUser()->getUsername());
$fossildbFossil->setApproved(0);
$fossildbFossil->setNumReviews(0);
$fossildbFossil->setNumNewPhotos(0);
$fossildbFossil->setHasEdits(0);
$em->persist($fossildbFossil);
$em->flush();
if(!is_null($fossil['upload'])) {
$upload = json_decode($fossil['upload']);
foreach($upload as $id => $item) {
if(!is_null($item)) {
$repository = $this->getDoctrine()->getRepository(UploadQueue::class);
$queuefile = $repository->findOneBy(array('system' => 'fossildb','filename' => $item->name));
if(!is_null($queuefile)) {
$fossildbPhoto = new FossildbPhotos;
$fossildbPhoto->setFossildbFossil($fossildbFossil);
$fossildbPhoto->setType('fossil');
$fossildbPhoto->setFilename($item->name);
$fossildbPhoto->setAdded(new \DateTime());
$fossildbPhoto->setUserId($this->getUser()->getId());
$fossildbPhoto->setUsername($this->getUser()->getUsername());
$fossildbPhoto->setApproved(0);
$fp = $em->createQueryBuilder('fp')
->from('App\Entity\FossildbPhotos', 'fp')
->addSelect('count(fp.id)')
->andWhere("fp.is_primary = 1")
->andWhere("fp.FossildbFossil = (:id)")
->setParameter('id', $fossildbFossil->getId())
->getQuery()
->getSingleScalarResult();
;
if($fp == 0) {
$fossildbPhoto->setIsPrimary(1);
} else {
$fossildbPhoto->setIsPrimary(0);
}
$em->persist($fossildbPhoto);
$em->remove($queuefile);
$fossildbFossil->setNumNewPhotos($fossildbFossil->getNumNewPhotos()+1);
$em->persist($fossildbFossil);
$em->flush();
}
}
}
}
$this->addFlash('success', $translator->trans("Toegevoegd aan Fossielendatabase"));
if($fossil["add_new_as_this"] === true) {
$filter = array( 'genus' => $fossil['genus'], 'species' => $fossil['species'], 'publication' => $fossil['publication'], 'literature' => $fossil['literature'], 'location' => $fossil['location_fossil'], 'taxanomy' => $fossil['taxanomy'], 'era' => $fossil['era'], 'epoch' => $fossil['epoch'], 'stage' => $fossil['stage']);
} else {
return $this->redirectToRoute('fossildb');
}
}
if(is_null($filter['genus'])) {$filter['genus'] = '[""]';}
if(is_null($filter['species'])) {$filter['species'] = '[""]';}
if(is_null($filter['publication'])) {$filter['publication'] = '[""]';}
if(is_null($filter['literature'])) {$filter['literature'] = '[""]';}
if(is_null($filter['location'])) {$filter['location'] = '[""]';}
if(is_null($filter['taxanomy'])) {$filter['taxanomy'] = '[""]';}
if(is_null($filter['era'])) {$filter['era'] = '[""]';}
if(is_null($filter['epoch'])) {$filter['epoch'] = '[""]';}
if(is_null($filter['stage'])) {$filter['stage'] = '[""]';}
return $this->render('fossildb_edit.html.twig', [
'filter' => $filter,
'formname' => 'fossildb_fossil_form',
'title' => $translator->trans("Fossiel toevoegen"),
'button_text' => $translator->trans("Insturen"),
'form' => $form->createView()
]);
}
/**
* @Route("/fossildb/fossil/{item}/edit", name="fossildb_edit")
*/
public function fossildb_edit(FossildbFossil $item, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_USER');
if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
if($this->getUser()->getId() == $item->getUserId()){
$filter = array( 'genus' => '[""]', 'species' => '[""]', 'publication' => '[""]', 'literature' => '[""]', 'location' => '[""]', 'taxanomy' => '[""]', 'era' => '[""]', 'epoch' => '[""]', 'stage' => '[""]');
$fossil=array();
$fossil['id'] = $item->getId();
$repository = $this->getDoctrine()->getRepository(FossildbFossilEdits::class);
$edited_fossil = $repository->findOneBy(array('fossil' => $item->getId()));
if($edited_fossil){
$edited = 1;
} else {
$edited = 0;
}
if ($edited == 1 && ($edited_fossil->getGenus() != $item->getGenus())){
$fossil['genus'] = json_encode(array(array('value' => $edited_fossil->getGenus())));
$fossil['edited']['genus'] = 1;
} else {
$fossil['genus'] = json_encode(array(array('value' => $item->getGenus())));
$fossil['edited']['genus'] = 0;
}
if ($edited == 1 && ($edited_fossil->getSpecies() != $item->getSpecies())){
$fossil['species'] = json_encode(array(array('value' => $edited_fossil->getSpecies())));
$fossil['edited']['species'] = 1;
} else {
$fossil['species'] = json_encode(array(array('value' => $item->getSpecies())));
$fossil['edited']['species'] = 0;
}
if ($edited == 1 && ($edited_fossil->getPublication() != $item->getPublication())){
$fossil['publication'] = json_encode(array(array('value' => $edited_fossil->getPublication())));
$fossil['edited']['publication'] = 1;
} else {
$fossil['publication'] = json_encode(array(array('value' => $item->getPublication())));
$fossil['edited']['publication'] = 0;
}
if ($edited == 1 && ($edited_fossil->getTaxanomy() != $item->getTaxanomy())){
if($translator->getLocale() == 'nl') {
$name = $edited_fossil->getTaxanomy()->getNameNl();
} else {
$name = $edited_fossil->getTaxanomy()->getNameEn();
}
if($edited_fossil->getTaxanomy()->getNameScientificClarification() != '' || is_null($edited_fossil->getTaxanomy()->getNameScientificClarification())) {
$scientific = $edited_fossil->getTaxanomy()->getNameScientific() . " " . $edited_fossil->getTaxanomy()->getNameScientificClarification();
$filename = $edited_fossil->getTaxanomy()->getNameScientific() . $edited_fossil->getTaxanomy()->getNameScientificClarification();
} else {
$scientific = $edited_fossil->getTaxanomy()->getNameScientific();
$filename = $edited_fossil->getTaxanomy()->getNameScientific();
}
$fossil['taxanomy'] = json_encode(array(array(
'id' => $edited_fossil->getTaxanomy()->getId(),
'value' => $name." (".$scientific.")",
'filename' => $filename,
)));
$fossil['edited']['taxanomy'] = 1;
} else {
if(!is_null($item->getTaxanomy())) {
if($translator->getLocale() == 'nl') {
$name = $item->getTaxanomy()->getNameNl();
} else {
$name = $item->getTaxanomy()->getNameEn();
}
if($item->getTaxanomy()->getNameScientificClarification() != '' || is_null($item->getTaxanomy()->getNameScientificClarification())) {
$scientific = $item->getTaxanomy()->getNameScientific() . " " . $item->getTaxanomy()->getNameScientificClarification();
$filename = $item->getTaxanomy()->getNameScientific() . $item->getTaxanomy()->getNameScientificClarification();
} else {
$scientific = $item->getTaxanomy()->getNameScientific();
$filename = $item->getTaxanomy()->getNameScientific();
}
$fossil['taxanomy'] = json_encode(array(array(
'id' => $item->getTaxanomy()->getId(),
'value' => $name." (".$scientific.")",
'filename' => $filename,
)));
} else { $fossil['taxanomy'] = json_encode(array()); }
$fossil['edited']['taxanomy'] = 0;
}
if ($edited == 1 && ($edited_fossil->getLocationFossil() != $item->getLocationFossil())){
if(!is_null($edited_fossil->getLocationFossil())) {
$fossil['location_fossil'] = json_encode(array(array(
'id' => $edited_fossil->getLocationFossil()->getId(),
'value' => $edited_fossil->getLocationFossil()->getNameNl(),
'name_en' => $edited_fossil->getLocationFossil()->getNameEn(),
'location' => $edited_fossil->getLocationFossil()->getLocation()
)));
} else { $fossil['location_fossil'] = json_encode(array()); }
$fossil['edited']['location_fossil'] = 0;
} else {
if(!is_null($item->getLocationFossil())) {
$fossil['location_fossil'] = json_encode(array(array(
'id' => $item->getLocationFossil()->getId(),
'value' => $item->getLocationFossil()->getNameNl(),
'name_en' => $item->getLocationFossil()->getNameEn(),
'location' => $item->getLocationFossil()->getLocation()
)));
} else { $fossil['location_fossil'] = json_encode(array()); }
$fossil['edited']['location_fossil'] = 0;
}
if ($edited == 1 && ($edited_fossil->getEra() != $item->getEra())){
if(!is_null($edited_fossil->getEra())) {
$fossil['era'] = json_encode(array(array(
'id' => $edited_fossil->getEra()->getId(),
'age_start' => $edited_fossil->getEra()->getAgeStart(),
'age_end' => $edited_fossil->getEra()->getAgeEnd(),
'value' => $edited_fossil->getEra()->getNameNl(),
'name_en' => $edited_fossil->getEra()->getNameEn(),
'color' => $edited_fossil->getEra()->getColor()
)));
} else { $fossil['era'] = json_encode(array()); }
$fossil['edited']['era'] = 1;
} else {
if(!is_null($item->getEra())) {
$fossil['era'] = json_encode(array(array(
'id' => $item->getEra()->getId(),
'age_start' => $item->getEra()->getAgeStart(),
'age_end' => $item->getEra()->getAgeEnd(),
'value' => $item->getEra()->getNameNl(),
'name_en' => $item->getEra()->getNameEn(),
'color' => $item->getEra()->getColor()
)));
} else { $fossil['era'] = json_encode(array()); }
$fossil['edited']['era'] = 0;
}
if ($edited == 1 && ($edited_fossil->getEpoch() != $item->getEpoch())){
if(!is_null($item->getEpoch())) {
$fossil['epoch'] = json_encode(array(array(
'id' => $edited_fossil->getEpoch()->getId(),
'age_start' => $edited_fossil->getEpoch()->getAgeStart(),
'age_end' => $edited_fossil->getEpoch()->getAgeEnd(),
'value' => $edited_fossil->getEpoch()->getNameNl(),
'name_en' => $edited_fossil->getEpoch()->getNameEn(),
'color' => $edited_fossil->getEpoch()->getColor()
)));
} else { $fossil['epoch'] = json_encode(array()); }
$fossil['edited']['epoch'] = 1;
} else {
if(!is_null($item->getEpoch())) {
$fossil['epoch'] = json_encode(array(array(
'id' => $item->getEpoch()->getId(),
'age_start' => $item->getEpoch()->getAgeStart(),
'age_end' => $item->getEpoch()->getAgeEnd(),
'value' => $item->getEpoch()->getNameNl(),
'name_en' => $item->getEpoch()->getNameEn(),
'color' => $item->getEpoch()->getColor()
)));
} else { $fossil['epoch'] = json_encode(array()); }
$fossil['edited']['epoch'] = 0;
}
if ($edited == 1 && ($edited_fossil->getStage() != $item->getStage())){
if(!is_null($item->getStage())) {
$fossil['stage'] = json_encode(array(array(
'id' => $edited_fossil->getStage()->getId(),
'age_start' => $edited_fossil->getStage()->getAgeStart(),
'age_end' => $edited_fossil->getStage()->getAgeEnd(),
'value' => $edited_fossil->getStage()->getNameNl(),
'name_en' => $edited_fossil->getStage()->getNameEn(),
'color' => $edited_fossil->getStage()->getColor()
)));
} else { $fossil['stage'] = json_encode(array()); }
$fossil['edited']['stage'] = 1;
} else {
if(!is_null($item->getStage())) {
$fossil['stage'] = json_encode(array(array(
'id' => $item->getStage()->getId(),
'age_start' => $item->getStage()->getAgeStart(),
'age_end' => $item->getStage()->getAgeEnd(),
'value' => $item->getStage()->getNameNl(),
'name_en' => $item->getStage()->getNameEn(),
'color' => $item->getStage()->getColor()
)));
} else { $fossil['stage'] = json_encode(array()); }
$fossil['edited']['stage'] = 0;
}
if ($edited == 1 && ($edited_fossil->getFormation() != $item->getFormation())){
if(!is_null($item->getFormation())) {
$fossil['formation'] = json_encode(array(array(
'id' => $edited_fossil->getFormation()->getId(),
'age_start' => $edited_fossil->getFormation()->getAgeStart(),
'age_end' => $edited_fossil->getFormation()->getAgeEnd(),
'value' => $edited_fossil->getFormation()->getNameNl(),
'name_en' => $edited_fossil->getFormation()->getNameEn(),
'color' => $edited_fossil->getFormation()->getColor()
)));
} else { $fossil['formation'] = json_encode(array()); }
$fossil['edited']['formation'] = 1;
} else {
if(!is_null($item->getFormation())) {
$fossil['formation'] = json_encode(array(array(
'id' => $item->getFormation()->getId(),
'age_start' => $item->getFormation()->getAgeStart(),
'age_end' => $item->getFormation()->getAgeEnd(),
'value' => $item->getFormation()->getNameNl(),
'name_en' => $item->getFormation()->getNameEn(),
'color' => $item->getFormation()->getColor()
)));
} else { $fossil['formation'] = json_encode(array()); }
$fossil['edited']['formation'] = 0;
}
if ($edited == 1 && ($edited_fossil->getMember() != $item->getMember())){
if(!is_null($item->getMember())) {
$fossil['member'] = json_encode(array(array(
'id' => $edited_fossil->getMember()->getId(),
'age_start' => $edited_fossil->getMember()->getAgeStart(),
'age_end' => $edited_fossil->getMember()->getAgeEnd(),
'value' => $edited_fossil->getMember()->getNameNl(),
'name_en' => $edited_fossil->getMember()->getNameEn(),
'color' => $edited_fossil->getMember()->getColor()
)));
} else { $fossil['member'] = json_encode(array()); }
$fossil['edited']['member'] = 1;
} else {
if(!is_null($item->getMember())) {
$fossil['member'] = json_encode(array(array(
'id' => $item->getMember()->getId(),
'age_start' => $item->getMember()->getAgeStart(),
'age_end' => $item->getMember()->getAgeEnd(),
'value' => $item->getMember()->getNameNl(),
'name_en' => $item->getMember()->getNameEn(),
'color' => $item->getMember()->getColor()
)));
} else { $fossil['member'] = json_encode(array()); }
$fossil['edited']['member'] = 0;
}
if ($edited == 1 && ($edited_fossil->getDateYear() != $item->getDateYear())){
$fossil['date_year'] = $edited_fossil->getDateYear();
$fossil['edited']['date_year'] = 1;
} else {
$fossil['date_year'] = $item->getDateYear();
$fossil['edited']['date_year'] = 0;
}
if ($edited == 1 && ($edited_fossil->getDateMonth() != $item->getDateMonth())){
$fossil['date_month'] = $edited_fossil->getDateMonth();
$fossil['edited']['date_month'] = 1;
} else {
$fossil['date_month'] = $item->getDateMonth();
$fossil['edited']['date_month'] = 0;
}
if ($edited == 1 && $edited_fossil->getDateDay() != $item->getDateDay()){
$fossil['date_day'] = $edited_fossil->getDateDay();
$fossil['edited']['date_day'] = 1;
} else {
$fossil['date_day'] = $item->getDateDay();
$fossil['edited']['date_day'] = 0;
}
if ($edited == 1 && ($edited_fossil->getCommentNl() != $item->getCommentNl())){
$fossil['comment_nl'] = $edited_fossil->getCommentNl();
$fossil['edited']['comment_nl'] = 1;
} else {
$fossil['comment_nl'] = $item->getCommentNl();
$fossil['edited']['comment_nl'] = 0;
}
if ($edited == 1 && ($edited_fossil->getCommentEn() != $item->getCommentEn())){
$fossil['comment_en'] = $edited_fossil->getCommentEn();
$fossil['edited']['comment_en'] = 1;
} else {
$fossil['comment_en'] = $item->getCommentEn();
$fossil['edited']['comment_en'] = 0;
}
if ($edited == 1 && ($edited_fossil->getUserNumber() != $item->getUserNumber())){
$fossil['user_number'] = $edited_fossil->getUserNumber();
$fossil['edited']['user_number'] = 1;
} else {
$fossil['user_number'] = $item->getUserNumber();
$fossil['edited']['user_number'] = 0;
}
if ($edited == 1 && ($edited_fossil->getLiterature() != $item->getLiterature())){
if(!is_null($item->getLiterature())) {
$fossil['literature'] = json_encode(array(array(
'id' => $edited_fossil->getLiterature()->getId(),
'value' => $edited_fossil->getLiterature()->getAuthor()." (".$edited_fossil->getLiterature()->getyear().") - ".$edited_fossil->getLiterature()->getTitle(),
'title' => $edited_fossil->getLiterature()->getTitle(),
'year' => $edited_fossil->getLiterature()->getyear()
)));
} else { $fossil['literature'] = json_encode(array()); }
$fossil['edited']['literature'] = 1;
} else {
if(!is_null($item->getLiterature())) {
$fossil['literature'] = json_encode(array(array(
'id' => $item->getLiterature()->getId(),
'value' => $item->getLiterature()->getAuthor()." (".$item->getLiterature()->getyear().") - ".$item->getLiterature()->getTitle(),
'title' => $item->getLiterature()->getTitle(),
'year' => $item->getLiterature()->getyear()
)));
} else { $fossil['literature'] = json_encode(array()); }
$fossil['edited']['literature'] = 0;
}
$filter = array( 'genus' => $fossil['genus'], 'species' => $fossil['species'], 'publication' => $fossil['publication'], 'taxanomy' => $fossil['taxanomy'], 'location' => $fossil['location_fossil'], 'era' => $fossil['era'], 'epoch' => $fossil['epoch'], 'stage' => $fossil['stage'], 'literature' => $fossil['literature']);
$fossil['photos'] = $item->getFossildbPhotos();
$form = $this->createForm(FossildbFossilEditFormType::class, $fossil);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$fossil_old = $fossil;
$fossil = $form->getData();
if(!is_null($fossil['location_fossil']) && ($fossil_old['location_fossil'] != $fossil['location_fossil'])) {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$location_fossil = $repository->findOneBy(array('id' => json_decode($fossil['location_fossil'])[0]->id));
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getLocationFossil())) {
$location_fossil=$edited_fossil->getLocationFossil();
} else {
$location_fossil=null;
}
}
if(!is_null($fossil['taxanomy']) && ($fossil_old['taxanomy'] != $fossil['taxanomy'])) {
$repository = $this->getDoctrine()->getRepository(Taxanomy::class);
$taxanomy = $repository->findOneBy(array('id' => json_decode($fossil['taxanomy'])[0]->id));
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getTaxanomy())) {
$taxanomy=$edited_fossil->getTaxanomy();
} else {
$taxanomy=null;
}
}
if(!is_null($fossil['era']) && (json_decode($fossil_old['era'])[0]->id != json_decode($fossil['era'])[0]->id)) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$era = $repository->findOneBy(array('id' => json_decode($fossil['era'])[0]->id));
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getEra())) {
$era=$edited_fossil->getEra();
} else {
$era=null;
}
}
if(!is_null($fossil['epoch']) && (json_decode($fossil_old['epoch'])[0]->id != json_decode($fossil['epoch'])[0]->id)) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$epoch = $repository->findOneBy(array('id' => json_decode($fossil['epoch'])[0]->id));
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getEpoch())) {
$epoch=$edited_fossil->getEpoch();
} else {
$epoch=null;
}
}
if(!is_null($fossil['stage']) && (json_decode($fossil_old['stage'])[0]->id != json_decode($fossil['stage'])[0]->id)) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$stage = $repository->findOneBy(array('id' => json_decode($fossil['stage'])[0]->id));
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getStage())) {
$stage=$edited_fossil->getStage();
} else {
$stage=null;
}
}
if(!is_null($fossil['formation']) && ($fossil_old['formation'] != $fossil['formation'])) {
$formation_item = json_decode($fossil['formation'])[0];
if (property_exists($formation_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$formation = $repository->findOneBy(array('id' => $formation_item->id));
} else {
$new_formation_item = new StratigraphyUnits;
$new_formation_item->setType("member");
$new_formation_item->setNameNl($formation_item->value);
$new_formation_item->setNameEn($formation_item->value);
$em->persist($new_formation_item);
$em->flush();
$formation = $new_formation_item->getId();
}
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getFormation())) {
$formation=$edited_fossil->getFormation();
} else {
$formation=null;
}
}
if(!is_null($fossil['member']) && ($fossil_old['member'] != $fossil['member'])) {
$member_item = json_decode($fossil['member'])[0];
if (property_exists($member_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$member = $repository->findOneBy(array('id' => $member_item->id));
} else {
$new_member_item = new StratigraphyUnits;
$new_member_item->setType("member");
$new_member_item->setNameNl($member_item->value);
$new_member_item->setNameEn($member_item->value);
$em->persist($new_member_item);
$em->flush();
$member = $new_member_item;
}
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getMember())) {
$member=$edited_fossil->getMember();
} else {
$member=null;
}
}
if(!is_null($fossil['literature']) && ($fossil_old['literature'] != $fossil['literature'])) {
$repository = $this->getDoctrine()->getRepository(Literature::class);
$literature = $repository->findOneBy(array('id' => json_decode($fossil['literature'])[0]->id));
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getLiterature())) {
$literature=$edited_fossil->getLiterature();
} else {
$literature=null;
}
}
if($fossil_old['date_year'] != $fossil['date_year']) {
$date_year=$fossil['date_year'];
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getDateYear())) {
$date_year=$edited_fossil->getDateYear();
} else {
$date_year=null;
}
}
if($fossil_old['date_month'] != $fossil['date_month']) {
$date_month=$fossil['date_month'];
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getDateMonth())) {
$date_month=$edited_fossil->getDateMonth();
} else {
$date_month=null;
}
}
/*if($fossil_old['date_day'] != $fossil['date_day']) {
$date_day=$fossil['date_day'];
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getDateDay())) {
$date_day=$edited_fossil->getDateDay();
} else {
$date_day=null;
}
}*/
if($fossil_old['date_day'] != $fossil['date_day']) {
$date_day=$fossil['date_day'];
} else {
$date_day=$fossil_old['date_day'];
}
if($fossil_old['comment_nl'] != $fossil['comment_nl']) {
$comment_nl=$fossil['comment_nl'];
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getCommentNl())) {
$comment_nl=$edited_fossil->getCommentNl();
} else {
$comment_nl=null;
}
}
if($fossil_old['comment_en'] != $fossil['comment_en']) {
$comment_en=$fossil['comment_en'];
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getCommentEn())) {
$comment_en=$edited_fossil->getCommentEn();
} else {
$comment_en=null;
}
}
if($fossil_old['user_number'] != $fossil['user_number']) {
$user_number=$fossil['user_number'];
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getUserNumber())) {
$user_number=$edited_fossil->getUserNumber();
} else {
$user_number=null;
}
}
$repository = $this->getDoctrine()->getRepository(FossildbFossilEdits::class);
$fossildbFossil = $repository->findOneBy(array('fossil' => $item));
if(!$fossildbFossil) {
$fossildbFossil = new FossildbFossilEdits;
}
$fossildbFossil->setFossil($item);
if(json_decode($fossil['genus'])[0] && ($fossil_old['genus'] != $fossil['genus'])) {
$fossildbFossil->setGenus(json_decode($fossil['genus'])[0]->value);
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getGenus())) {
$fossildbFossil->setGenus($edited_fossil->getGenus());
} else {
$fossildbFossil->setGenus(null);
}
}
if(json_decode($fossil['species'])[0] && ($fossil_old['species'] != $fossil['species'])) {
$fossildbFossil->setSpecies(json_decode($fossil['species'])[0]->value);
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getSpecies())) {
$fossildbFossil->setSpecies($edited_fossil->getSpecies());
} else {
$fossildbFossil->setSpecies(null);
}
}
if(json_decode($fossil['publication'])[0] && ($fossil_old['publication'] != $fossil['publication'])) {
$fossildbFossil->setPublication(json_decode($fossil['publication'])[0]->value);
} else {
if(!is_null($edited_fossil) && !is_null($edited_fossil->getPublication())) {
$fossildbFossil->setPublication($edited_fossil->getPublication());
} else {
$fossildbFossil->setPublication(null);
}
}
$fossildbFossil->setLocationFossil($location_fossil);
$fossildbFossil->setTaxanomy($taxanomy);
$fossildbFossil->setEra($era);
$fossildbFossil->setEpoch($epoch);
$fossildbFossil->setStage($stage);
$fossildbFossil->setFormation($formation);
$fossildbFossil->setMember($member);
$fossildbFossil->setDateYear($date_year);
$fossildbFossil->setDateMonth($date_month);
$fossildbFossil->setDateDay($date_day);
$fossildbFossil->setCommentNl($comment_nl);
$fossildbFossil->setCommentEn($comment_en);
$fossildbFossil->setUserNumber($user_number);
$fossildbFossil->setLiterature($literature);
if(
$fossildbFossil->getLocationFossil() == null &&
$fossildbFossil->getTaxanomy() == null &&
$fossildbFossil->getGenus() == null &&
$fossildbFossil->getSpecies() == null &&
$fossildbFossil->getPublication() == null &&
$fossildbFossil->getLiterature() == null &&
$fossildbFossil->getEra() == null &&
$fossildbFossil->getEpoch() == null &&
$fossildbFossil->getStage() == null &&
$fossildbFossil->getFormation() == null &&
$fossildbFossil->getMember() == null &&
$fossildbFossil->getDateDay() == null &&
$fossildbFossil->getDateMonth() == null &&
$fossildbFossil->getDateYear() == null &&
$fossildbFossil->getCommentNl() == null &&
$fossildbFossil->getCommentEn() == null &&
$fossildbFossil->getUserNumber() == null
) {
} else {
$em->persist($fossildbFossil);
$item->setHasEdits(1);
$em->persist($item);
$em->flush();
}
$fossil_id = $item->getId();
if(!is_null($fossil['upload'])) {
$upload = json_decode($fossil['upload']);
foreach($upload as $id => $item) {
if(!is_null($item)) {
$repository = $this->getDoctrine()->getRepository(FossildbFossil::class);
$fossildbFossil = $repository->findOneBy(array('id' => $fossil_old['id']));
$repository = $this->getDoctrine()->getRepository(UploadQueue::class);
$queuefile = $repository->findOneBy(array('system' => 'fossildb','filename' => $item->name));
if(!is_null($queuefile)) {
$fossildbPhoto = new FossildbPhotos;
$fossildbPhoto->setFossildbFossil($fossildbFossil);
$fossildbPhoto->setType('fossil');
$fossildbPhoto->setFilename($item->name);
$fossildbPhoto->setAdded(new \DateTime());
$fossildbPhoto->setUserId($this->getUser()->getId());
$fossildbPhoto->setUsername($this->getUser()->getUsername());
$fossildbPhoto->setApproved(0);
$fp = $em->createQueryBuilder('fp')
->from('App\Entity\FossildbPhotos', 'fp')
->addSelect('count(fp.id)')
->andWhere("fp.is_primary = 1")
->andWhere("fp.FossildbFossil = (:id)")
->setParameter('id', $item->id)
->getQuery()
->getSingleScalarResult();
;
if($fp == 0) {
$fossildbPhoto->setIsPrimary(1);
} else {
$fossildbPhoto->setIsPrimary(0);
}
$em->persist($fossildbPhoto);
$em->remove($queuefile);
$fossildbFossil->setNumNewPhotos($fossildbFossil->getNumNewPhotos()+1);
$em->persist($fossildbFossil);
$em->persist($item);
$em->flush();
}
}
}
}
$this->addFlash('success', $translator->trans("Fossiel bijgewerkt. De wijzigingen zijn moeten nog goed gekeurd worden."));
return $this->redirectToRoute('fossildb');
}
if(is_null($filter['genus'])) {$filter['genus'] = '[""]';}
if(is_null($filter['species'])) {$filter['species'] = '[""]';}
if(is_null($filter['publication'])) {$filter['publication'] = '[""]';}
if(is_null($filter['literature'])) {$filter['literature'] = '[""]';}
if(is_null($filter['location'])) {$filter['location'] = '[""]';}
if(is_null($filter['taxanomy'])) {$filter['taxanomy'] = '[""]';}
if(is_null($filter['era'])) {$filter['era'] = '[""]';}
if(is_null($filter['epoch'])) {$filter['epoch'] = '[""]';}
if(is_null($filter['stage'])) {$filter['stage'] = '[""]';}
return $this->render('fossildb_edit.html.twig', [
'filter' => $filter,
'formname' => 'fossildb_fossil_edit_form',
'edit' => true,
'item' => $fossil,
'title' => $translator->trans("Fossiel toevoegen"),
'button_text' => $translator->trans("Bewerken"),
'form' => $form->createView()
]);
} else {
return $this->render('message.html.twig', [
'title' => $translator->trans("Geen toegang"),
'message' => $translator->trans("Dit fossiel is niet van jou en je kan hem daarom niet bewerken."),
]);
}
}
/**
* @Route("/fossildb/fossil/{id}/review", name="fossildb_review_add")
*/
public function fossildb_review_add(FossildbFossil $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(FossildbReviewsFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$review = $form->getData();
$review->setUserId($this->getUser()->getId());
$review->setUsername($this->getUser()->getUsername());
$review->setFossildbFossil($id);
$id->setNumReviews($id->getNumReviews()+1);
$em->persist($review);
$em->persist($id);
$em->flush();
$this->addFlash('success', $translator->trans("Feedback doorgegeven"));
return $this->redirectToRoute('fossildb_item',array('id' => $id->getId() ));
}
return $this->render('form_edit.html.twig', [
'title' => $translator->trans("Feedback doorgeven"),
'button_text' => $translator->trans("Insturen"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/fossildb/{id}/review", name="fossildb_moderate_reviews")
*/
public function moderate_review(FossildbFossil $id, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
return $this->render('fossildbreviews_moderate.html.twig', [
'fossildb' => $id,
]);
}
/**
* @Route("/moderate/fossildb/{fossildb}/review/{id}/read", name="fossildbreviews_moderate_read")
*/
public function review_read(FossildbFossil $fossildb, FossildbReviews $id, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
return $this->render('fossildbreview.html.twig', [
'review' => $id,
'fossildb' => $fossildb,
]);
}
/**
* @Route("/moderate/fossildb/{fossildb}/review/{id}/delete", name="fossildbreviews_moderate_delete")
*/
public function review_delete(FossildbFossil $fossildb, FossildbReviews $id, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$fossildb->setNumReviews($fossildb->getNumReviews()-1);
$em->persist($fossildb);
$em->remove($id);
$em->flush();
$this->addFlash('success', $translator->trans("Fossiel review verwijderd"));
return $this->redirectToRoute('fossildb_moderate_reviews', array('id' => $fossildb->getId(), ));
}
/**
* @Route("/moderate/fossildb/{item}/photos", name="fossildb_moderate_photos")
*/
public function moderate_photos(FossildbFossil $item, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
return $this->render('fossildbphotos_moderate.html.twig', [
'fossildbfossil' => $item,
]);
}
/**
* @Route("/fossildb/fossil/{id}/photo/add", name="fossildb_photo_add")
*/
public function fossildb_photo_add(FossildbFossil $id, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_USER');
if($_ENV['DISABLE_FORMS'] == "true") { return $this->render('maintenance_form_disabled.html.twig'); }
if ($id->getUserId() == $this->getUser()->getId() || $this->isGranted('ROLE_MOD_FOSSILDB'))
{
$form = $this->createForm(FossildbFossilAddPhotoFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$fossil = $form->getData();
if(!is_null($fossil['upload'])) {
$upload = json_decode($fossil['upload']);
foreach($upload as $i => $item) {
if(!is_null($item)) {
$repository = $this->getDoctrine()->getRepository(UploadQueue::class);
$queuefile = $repository->findOneBy(array('system' => 'fossildb','filename' => $item->name));
if(!is_null($queuefile)) {
$fossildbPhoto = new FossildbPhotos;
$fossildbPhoto->setFossildbFossil($id);
$fossildbPhoto->setType('fossil');
$fossildbPhoto->setFilename($item->name);
$fossildbPhoto->setAdded(new \DateTime());
$fossildbPhoto->setUserId($this->getUser()->getId());
$fossildbPhoto->setUsername($this->getUser()->getUsername());
$fossildbPhoto->setApproved(0);
$fp = $em->createQueryBuilder('fp')
->from('App\Entity\FossildbPhotos', 'fp')
->addSelect('count(fp.id)')
->andWhere("fp.is_primary = 1")
->andWhere("fp.FossildbFossil = (:id)")
->setParameter('id', $id->getId())
->getQuery()
->getSingleScalarResult();
;
if($fp == 0) {
$fossildbPhoto->setIsPrimary(1);
} else {
$fossildbPhoto->setIsPrimary(0);
}
$em->persist($fossildbPhoto);
$em->remove($queuefile);
$id->setNumNewPhotos($id->getNumNewPhotos()+1);
$em->persist($id);
$em->flush();
}
}
}
}
$this->addFlash('success', $translator->trans("Foto's toegevoegd"));
return $this->redirectToRoute('fossildb_item', array('id' => $id->getId()));
}
return $this->render('fossildb_add_photo.html.twig', [
'title' => $translator->trans("Foto's toevoegen aan fossiel"),
'button_text' => $translator->trans("Toevoegen"),
'formname' => 'fossildb_fossil_add_photo_form',
'form' => $form->createView()
]);
} else {
return $this->render('bundles\TwigBundle\Exception\error403.html.twig', [
]);
}
}
/**
* @Route("/moderate/fossildb/{item}/photos/{photo}/approve", name="fossildb_photos_moderate_approve")
*/
public function moderate_photos_approve(FossildbFossil $item, FossildbPhotos $photo, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$repository = $this->getDoctrine()->getRepository(FossildbPhotos::class);
$query_primary = $repository->findOneBy(array('FossildbFossil' => $item, 'is_primary' => 1));
$photo->setApprovedUserId($this->getUser()->getId());
$photo->setApprovedUsername($this->getUser()->getUsername());
$photo->setApproved('1');
if(!$query_primary) {
$photo->setIsPrimary('1');
} else {
$photo->setIsPrimary('0');
}
$em->persist($photo);
$item->setNumNewPhotos($item->getNumNewPhotos()-1);
$em->persist($item);
$em->flush();
$this->addFlash('success', $translator->trans("Identificatie foto goedgekeurd"));
return $this->redirectToRoute('fossildb_moderate_photos', array('item' => $item->getId()) );
}
/**
* @Route("/moderate/fossildb/{item}/photos/{photo}/delete", name="fossildb_photos_moderate_delete")
*/
public function moderate_photo_delete(FossildbFossil $item, FossildbPhotos $photo, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
if($photo->getApproved() == 0){
$item->setNumNewPhotos($item->getNumNewPhotos()-1);
$em->persist($item);
}
$filename = $this->getParameter('fossildbphoto_directory')."/".substr($photo->getFilename(),0,2)."/".$photo->getFilename();
$filename_small_thumbnail = $this->getParameter('fossildbphotothumbnail_small_directory')."/".substr($photo->getFilename(),0,2)."/".$photo->getFilename();
$filename_tiny_thumbnail = $this->getParameter('fossildbphotothumbnail_tiny_directory')."/".substr($photo->getFilename(),0,2)."/".$photo->getFilename();
$filesystem = new Filesystem();
$filesystem->remove($filename);
$filesystem->remove($filename_small_thumbnail);
$filesystem->remove($filename_tiny_thumbnail);
$em->remove($photo);
$em->flush();
$this->addFlash('success', $translator->trans("Locatie foto verwijderd"));
return $this->redirectToRoute('fossildb_moderate_photos', array('item' => $item->getId()) );
}
/**
* @Route("/moderate/fossildb/", name="fossildb_moderate")
*/
public function fossildb_moderate(Request $request, EntityManagerInterface $em, PaginatorInterface $paginator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$form = $this->createForm(FossildbModeratorSearchFormType::class);
$form->handleRequest($request);
$taxanomy = array();
$location = array();
$era = array();
$genus = array();
$species = array();
$user = array();
if ($form->isSubmitted() && $form->isValid()){
$filter = $form->getData();
} else {
if(!$request->query->get('page')){
$filter=array(
'taxanomy'=> '[]',
'location' => '[]',
'era' => '[]',
'genus' => '[]',
'species' => '[]',
'user' => '[]',
'id' => null,
'property' => 'all'
);
$form->get('taxanomy')->setData("");
$form->get('location')->setData("");
$form->get('era')->setData("");
$form->get('genus')->setData("");
$form->get('species')->setData("");
$form->get('user')->setData("");
$form->get('id')->setData(null);
$form->get('property')->setData("all");
} elseif($request->query->get('filter') && $request->query->get('page')) {
$filter = $request->query->get('filter');
if(isset($filter['taxanomy']) && $filter['taxanomy'] != '[""]') { $form->get('taxanomy')->setData($filter['taxanomy']); } else { $form->get('taxanomy')->setData(''); $filter['taxanomy'] = null; };
if(isset($filter['location']) && $filter['location'] != '[""]') { $form->get('location')->setData($filter['location']); } else { $form->get('location')->setData(''); $filter['location'] = null; };
if(isset($filter['era']) && $filter['era'] != '[""]') { $form->get('era')->setData($filter['era']); } else { $form->get('era')->setData(''); $filter['era'] = null; };
if(isset($filter['genus']) && $filter['genus'] != '[""]') { $form->get('genus')->setData($filter['genus']); } else { $form->get('genus')->setData(null); $filter['genus'] = null;};
if(isset($filter['species']) && $filter['species'] != '[""]') { $form->get('species')->setData($filter['species']); } else { $form->get('species')->setData(null); $filter['species'] = null;};
if(isset($filter['user']) && $filter['user'] != '[""]') { $form->get('user')->setData($filter['user']); } else { $form->get('user')->setData(null); $filter['user'] = null;};
if(isset($filter['id']) && $filter['id'] != '') { $form->get('id')->setData($filter['id']); } else { $form->get('id')->setData(null); $filter['id'] = null;};
if(isset($filter['property']) && $filter['property'] != 'all') { $form->get('property')->setData($filter['property']); } else { $form->get('property')->setData('all'); $filter['property'] = 'all'; };
} else {
dd("Unexpected Error");
}
}
$filter_taxanomy = json_decode($filter['taxanomy']);
$filter_location = json_decode($filter['location']);
$filter_era = json_decode($filter['era']);
$filter_genus = json_decode($filter['genus']);
$filter_species = json_decode($filter['species']);
$filter_user = json_decode($filter['user']);
if(is_array($filter_taxanomy)) {
foreach($filter_taxanomy as $id => $item) {
$taxanomy[] = $item->id;
}
}
if(is_array($filter_location)) {
foreach($filter_location as $id => $item) {
$location[] = $item->id;
}
}
if(is_array($filter_era)) {
foreach($filter_era as $id => $item) {
$era[] = $item->id;
}
}
if(is_array($filter_genus)) {
foreach($filter_genus as $id => $item) {
$genus[] = $item->value;
}
}
if(is_array($filter_species)) {
foreach($filter_species as $id => $item) {
$species[] = $item->value;
}
}
if(is_array($filter_user)) {
foreach($filter_user as $id => $item) {
$user[] = $item->id;
}
}
if(is_null($filter['taxanomy'])) { $filter['taxanomy'] = '[""]'; }
if(is_null($filter['location'])) { $filter['location'] = '[""]'; }
if(is_null($filter['era'])) { $filter['era'] = '[""]'; }
if(is_null($filter['genus'])) { $filter['genus'] = '[""]'; }
if(is_null($filter['species'])) { $filter['species'] = '[""]'; }
if(is_null($filter['user'])) { $filter['user'] = '[""]'; }
if(!is_numeric($filter['id'])) { $filter['id'] = null; }
$qb = $em->createQueryBuilder('f')
->from('App\Entity\FossildbFossil', 'f')
->addSelect('f')
->orderBy('f.id','DESC')
->AddOrderBy('f.approved','DESC')
->AddOrderBy('f.num_new_photos','DESC')
->AddOrderBy('f.num_reviews','ASC')
;
if(!empty($taxanomy)) {
$qb->andWhere("f.taxanomy IN (:taxanomy)")
->setParameter('taxanomy', array_values($taxanomy))
;
}
if(!empty($location)) {
$qb->andWhere("f.location_fossil IN (:location)")
->setParameter('location', array_values($location))
;
}
if(!empty($era)) {
$qb->andWhere("f.era IN (:era)")
->setParameter('era', array_values($era))
;
}
if(!empty($genus)) {
$qb->andWhere("f.genus = :genus")
->setParameter('genus', array_values($genus))
;
}
if(!empty($species)) {
$qb->andWhere("f.species = :species")
->setParameter('species', array_values($species))
;
}
if(!empty($user)) {
$qb->andWhere("f.user_id = :user")
->setParameter('user', array_values($user))
;
}
if(!empty($filter['id'])) {
$qb->andWhere("f.id = :id")
->setParameter('id', $filter['id'])
;
}
if($filter['property'] == 'new') {
$qb->andWhere("f.approved = 0");
}
elseif($filter['property'] == 'edited') {
$qb->andWhere("f.has_edits = 1");
}
elseif($filter['property'] == 'reviews') {
$qb->andWhere("f.num_reviews > 0");
}
elseif($filter['property'] == 'photos') {
$qb->andWhere("f.num_new_photos > 0");
}
$result = $paginator->paginate(
$qb, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
10 /*limit per page*/
);
$result->setParam('filter', $filter);
$result->setCustomParameters([
'align' => 'right', # center|right (for template: twitter_bootstrap_v4_pagination)
]);
return $this->render('fossildb_moderate.html.twig', [
'form' => $form->createView(),
'filter' => $filter,
'result' => $result,
]);
}
/**
* @Route("/moderate/fossildb/{item}/edit", name="fossildb_moderate_edit")
*/
public function fossildb_moderate_edit(FossildbFossil $item, EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$fossil=array();
$fossil['genus'] = json_encode(array(array('value' => $item->getGenus())));
$fossil['species'] = json_encode(array(array('value' => $item->getSpecies())));
$fossil['publication'] = json_encode(array(array('value' => $item->getPublication())));
if(!is_null($item->getTaxanomy())) {
if($translator->getLocale() == 'nl') {
$name = $item->getTaxanomy()->getNameNl();
} else {
$name = $item->getTaxanomy()->getNameEn();
}
if($item->getTaxanomy()->getNameScientificClarification() != '' || is_null($item->getTaxanomy()->getNameScientificClarification())) {
$scientific = $item->getTaxanomy()->getNameScientific() . " " . $item->getTaxanomy()->getNameScientificClarification();
$filename = $item->getTaxanomy()->getNameScientific() . $item->getTaxanomy()->getNameScientificClarification();
} else {
$scientific = $item->getTaxanomy()->getNameScientific();
$filename = $item->getTaxanomy()->getNameScientific();
}
$fossil['taxanomy'] = json_encode(array(array(
'id' => $item->getTaxanomy()->getId(),
'value' => $name." (".$scientific.")",
'filename' => $filename,
)));
} else { $fossil['taxanomy'] = json_encode(array()); }
if(!is_null($item->getLocationFossil())) {
$fossil['location_fossil'] = json_encode(array(array(
'id' => $item->getLocationFossil()->getId(),
'value' => $item->getLocationFossil()->getNameNl(),
'name_en' => $item->getLocationFossil()->getNameEn(),
'location' => $item->getLocationFossil()->getLocation()
)));
} else { $fossil['location_fossil'] = json_encode(array()); }
if(!is_null($item->getEra())) {
$fossil['era'] = json_encode(array(array(
'id' => $item->getEra()->getId(),
'age_start' => $item->getEra()->getAgeStart(),
'age_end' => $item->getEra()->getAgeEnd(),
'value' => $item->getEra()->getNameNl(),
'name_en' => $item->getEra()->getNameEn(),
'color' => $item->getEra()->getColor()
)));
} else { $fossil['era'] = json_encode(array()); }
if(!is_null($item->getEpoch())) {
$fossil['epoch'] = json_encode(array(array(
'id' => $item->getEpoch()->getId(),
'age_start' => $item->getEpoch()->getAgeStart(),
'age_end' => $item->getEpoch()->getAgeEnd(),
'value' => $item->getEpoch()->getNameNl(),
'name_en' => $item->getEpoch()->getNameEn(),
'color' => $item->getEpoch()->getColor()
)));
} else { $fossil['epoch'] = json_encode(array()); }
if(!is_null($item->getStage())) {
$fossil['stage'] = json_encode(array(array(
'id' => $item->getStage()->getId(),
'age_start' => $item->getStage()->getAgeStart(),
'age_end' => $item->getStage()->getAgeEnd(),
'value' => $item->getStage()->getNameNl(),
'name_en' => $item->getStage()->getNameEn(),
'color' => $item->getStage()->getColor()
)));
} else { $fossil['stage'] = json_encode(array()); }
if(!is_null($item->getFormation())) {
$fossil['formation'] = json_encode(array(array(
'id' => $item->getFormation()->getId(),
'age_start' => $item->getFormation()->getAgeStart(),
'age_end' => $item->getFormation()->getAgeEnd(),
'value' => $item->getFormation()->getNameNl(),
'name_en' => $item->getFormation()->getNameEn(),
'color' => $item->getFormation()->getColor()
)));
} else { $fossil['formation'] = json_encode(array()); }
if(!is_null($item->getMember())) {
$fossil['member'] = json_encode(array(array(
'id' => $item->getMember()->getId(),
'age_start' => $item->getMember()->getAgeStart(),
'age_end' => $item->getMember()->getAgeEnd(),
'value' => $item->getMember()->getNameNl(),
'name_en' => $item->getMember()->getNameEn(),
'color' => $item->getMember()->getColor()
)));
} else { $fossil['member'] = json_encode(array()); }
$fossil['date_year'] = $item->getDateYear();
$fossil['date_month'] = $item->getDateMonth();
$fossil['date_day'] = $item->getDateDay();
$fossil['comment_nl'] = $item->getCommentNl();
$fossil['comment_en'] = $item->getCommentEn();
$fossil['user_number'] = $item->getUserNumber();
$fossil['approved'] = $item->getApproved();
if(!is_null($item->getLiterature())) {
$fossil['literature'] = json_encode(array(array(
'id' => $item->getLiterature()->getId(),
'value' => $item->getLiterature()->getAuthor()." (".$item->getLiterature()->getyear().") - ".$item->getLiterature()->getTitle(),
'title' => $item->getLiterature()->getTitle(),
'year' => $item->getLiterature()->getyear()
)));
} else { $fossil['literature'] = json_encode(array()); }
$filter = array( 'genus' => $fossil['genus'], 'species' => $fossil['species'], 'publication' => $fossil['publication'], 'taxanomy' => $fossil['taxanomy'], 'location' => $fossil['location_fossil'], 'era' => $fossil['era'], 'epoch' => $fossil['epoch'], 'stage' => $fossil['stage'], 'literature' => $fossil['literature']);
$form = $this->createForm(FossildbFossilModeratorEditFormType::class, $fossil);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$fossil = $form->getData();
if(!is_null($fossil['location_fossil'])) {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$location_fossil = $repository->findOneBy(array('id' => json_decode($fossil['location_fossil'])[0]->id));
} else {
$location_fossil=null;
}
if(!is_null($fossil['taxanomy'])) {
$repository = $this->getDoctrine()->getRepository(Taxanomy::class);
$taxanomy = $repository->findOneBy(array('id' => json_decode($fossil['taxanomy'])[0]->id));
} else {
$taxanomy=null;
}
if(!is_null($fossil['era'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$era = $repository->findOneBy(array('id' => json_decode($fossil['era'])[0]->id));
} else {
$era=null;
}
if(!is_null($fossil['epoch'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$epoch = $repository->findOneBy(array('id' => json_decode($fossil['epoch'])[0]->id));
} else {
$epoch=null;
}
if(!is_null($fossil['stage'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$stage = $repository->findOneBy(array('id' => json_decode($fossil['stage'])[0]->id));
} else {
$stage=null;
}
if(!is_null($fossil['formation'])) {
$formation_item = json_decode($fossil['formation'])[0];
if (property_exists($formation_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$formation = $repository->findOneBy(array('id' => $formation_item->id));
} else {
$new_formation_item = new StratigraphyUnits;
$new_formation_item->setType("member");
$new_formation_item->setNameNl($formation_item->value);
$new_formation_item->setNameEn($formation_item->value);
$em->persist($new_formation_item);
$em->flush();
$formation = $new_formation_item->getId();
}
} else {
$formation=null;
}
if(!is_null($fossil['member'])) {
$member_item = json_decode($fossil['member'])[0];
if (property_exists($member_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$member = $repository->findOneBy(array('id' => $member_item->id));
} else {
$new_member_item = new StratigraphyUnits;
$new_member_item->setType("member");
$new_member_item->setNameNl($member_item->value);
$new_member_item->setNameEn($member_item->value);
$em->persist($new_member_item);
$em->flush();
$member = $new_member_item;
}
} else {
$member=null;
}
if(!is_null($fossil['literature'])) {
$repository = $this->getDoctrine()->getRepository(Literature::class);
$literature = $repository->findOneBy(array('id' => json_decode($fossil['literature'])[0]->id));
} else {
$literature=null;
}
$fossildbFossil = $item;
if(json_decode($fossil['genus'])[0]) {
$fossildbFossil->setGenus(json_decode($fossil['genus'])[0]->value);
}
if(json_decode($fossil['species'])[0]) {
$fossildbFossil->setSpecies(json_decode($fossil['species'])[0]->value);
}
if(json_decode($fossil['publication'])[0]) {
$fossildbFossil->setPublication(json_decode($fossil['publication'])[0]->value);
}
$fossildbFossil->setLocationFossil($location_fossil);
$fossildbFossil->setTaxanomy($taxanomy);
$fossildbFossil->setEra($era);
$fossildbFossil->setEpoch($epoch);
$fossildbFossil->setStage($stage);
$fossildbFossil->setFormation($formation);
$fossildbFossil->setMember($member);
$fossildbFossil->setDateYear($fossil['date_year']);
$fossildbFossil->setDateMonth($fossil['date_month']);
$fossildbFossil->setDateDay($fossil['date_day']);
$fossildbFossil->setCommentNl($fossil['comment_nl']);
$fossildbFossil->setCommentEn($fossil['comment_en']);
$fossildbFossil->setUserNumber($fossil['user_number']);
$fossildbFossil->setLiterature($literature);
$constructed_taxanomy_tree=$this->taxanomy_tree(json_decode($fossil['taxanomy'])[0]->id, $em);
$taxanomy_tree = array();
foreach($constructed_taxanomy_tree as $i => $object) {
$taxanomy_tree[$i]['namescientific'] = $object->getNameScientific();
$taxanomy_tree[$i]['namescientificclarification'] = $object->getNameScientificClarification();
$taxanomy_tree[$i]['namenl'] = $object->getNameNl();
$taxanomy_tree[$i]['nameen'] = $object->getNameEn();
$taxanomy_tree[$i]['author'] = $object->getAuthor();
$taxanomy_tree[$i]['authoryear'] = $object->getAuthorYear();
if($object->getGlossary()){
$taxanomy_tree[$i]['glossary']['id'] = $object->getGlossary()->getId();
$taxanomy_tree[$i]['glossary']['url'] = $object->getGlossary()->getUrl();
$taxanomy_tree[$i]['glossary']['titlenl'] = $object->getGlossary()->getTitleNl();
$taxanomy_tree[$i]['glossary']['titleen'] = $object->getGlossary()->getTitleEn();
} else {
$taxanomy_tree[$i]['glossary'] = null;
}
$taxanomy_tree[$i]['taxanomyrank']['namescientific'] = $object->getTaxanomyRank()->getNameScientific();
$taxanomy_tree[$i]['taxanomyrank']['namenl'] = $object->getTaxanomyRank()->getNameNl();
$taxanomy_tree[$i]['taxanomyrank']['nameen'] = $object->getTaxanomyRank()->getNameEn();
}
$fossildbFossil->setTaxanomyTree(array_reverse($taxanomy_tree));
$fossildbFossil->setEdited(new \DateTime());
$fossildbFossil->setEditedUserId($this->getUser()->getId());
$fossildbFossil->setEditedUsername($this->getUser()->getUsername());
$fossildbFossil->setApproved($fossil["approved"]);
$fossildbFossil->setNumReviews($item->getNumReviews());
$fossildbFossil->setNumNewPhotos($item->getNumNewPhotos());
$em->persist($fossildbFossil);
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('fossildb_item', [ 'id' => $fossildbFossil->getId() ]) ));
if ($fossildbFossil->getApproved() == 1){
$newsfeed = new Newsfeed();
$newsfeed->setTitleNl($fossildbFossil->getGenus()." ".$fossildbFossil->getSpecies());
$newsfeed->setTitleEn($fossildbFossil->getGenus()." ".$fossildbFossil->getSpecies());
$newsfeed->setRemoteId($fossildbFossil->getId());
$newsfeed->setIsUpdate(1);
$newsfeed->setType('fossildb');
$newsfeed->setTime(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($newsfeed);
if(!$searchitem) {
$searchitem = new SearchItems();
}
$searchitem->setType('fossildb');
$searchitem->setPath($this->router->generate('fossildb_item', [ 'id' => $fossildbFossil->getId() ]));
$name_nl_add = '';
$name_en_add = '';
$keywords_nl_add = '';
$keywords_en_add = '';
if($fossildbFossil->getTaxanomy()) {
$name_nl_add = $fossildbFossil->getTaxanomy()->getNameNl().' -> ';
$name_en_add = $fossildbFossil->getTaxanomy()->getNameEn().' -> ';
$keywords_nl_add = $fossildbFossil->getTaxanomy()->getNameScientific().','.$fossildbFossil->getTaxanomy()->getNameNl().',';
$keywords_en_add = $fossildbFossil->getTaxanomy()->getNameScientific().','.$fossildbFossil->getTaxanomy()->getNameEn().',';
}
if(empty($item->getGenus()) && empty($item->getSpecies())){
$name_nl_add .= '[Ongedetermineerd]';
$name_en_add .= '[Unidentified]';
}
$searchitem->setNameNl($name_nl_add.$fossildbFossil->getGenus().' '.$fossildbFossil->getSpecies());
$searchitem->setNameEn($name_en_add.$fossildbFossil->getGenus().' '.$fossildbFossil->getSpecies());
$searchitem->setKeywordsNl($keywords_nl_add.$fossildbFossil->getGenus().','.$fossildbFossil->getSpecies());
$searchitem->setKeywordsEn($keywords_en_add.$fossildbFossil->getGenus().','.$fossildbFossil->getSpecies());
if(empty($fossildbFossil->getEdited())) {
$searchitem->setLastUpdateTime($fossildbFossil->getAdded());
} else {
$searchitem->setLastUpdateTime($fossildbFossil->getEdited());
}
$em->persist($searchitem);
$em->flush();
} else {
if($searchitem) {
$em->remove($searchitem);
$em->flush();
}
}
$em->flush();
$repository = $em->getRepository(FossildbFossil::class);
$totalsubmissions = $repository->createQueryBuilder('a')
->select('count(a.id)')
->andWhere("a.user_id = (:user)")
->andWhere("a.approved = 1")
->setParameter('user', $item->getUserId())
->getQuery()
->getSingleScalarResult();
$repository = $em->getRepository(User::class);
$user = $repository->findOneBy(array('id' => $item->getUserId()));
$repository = $this->getDoctrine()->getRepository(UserProfile::class);
$userprofile = $repository->findOneBy(array('user' => $user));
if(!$userprofile) {
$userprofile = new UserProfile();
$userprofile->setUser($user);
}
$userprofile->setFossildbSubmissions($totalsubmissions);
$em->persist($userprofile);
$em->flush();
$this->addFlash('success', $translator->trans("Fossiel bijgewerkt."));
return $this->redirectToRoute('fossildb_moderate');
}
return $this->render('fossildb_edit.html.twig', [
'moderate' => 1,
'item' => $item,
'filter' => $filter,
'formname' => 'fossildb_fossil_moderator_edit_form',
'title' => $translator->trans("Fossiel bijwerken"),
'button_text' => $translator->trans("Bijwerken"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/fossildb/{item}/edits", name="fossildb_moderate_approve_edits")
*/
public function fossildb_moderate_approve_edits(FossildbFossil $item, EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$filter = array( 'genus' => '[""]', 'species' => '[""]', 'publication' => '[""]', 'literature' => '[""]', 'location' => '[""]', 'taxanomy' => '[""]', 'era' => '[""]', 'epoch' => '[""]', 'stage' => '[""]');
$fossil=array();
$fossil['id'] = $item->getId();
$repository = $this->getDoctrine()->getRepository(FossildbFossilEdits::class);
$edited_fossil = $repository->findOneBy(array('fossil' => $item->getId()));
if($edited_fossil){
$edited = 1;
} else {
$edited = 0;
}
if ($edited == 1 && ($edited_fossil->getGenus() != $item->getGenus())){
$fossil['genus'] = json_encode(array(array('value' => $edited_fossil->getGenus())));
$fossil['edited']['genus'] = 1;
} else {
$fossil['genus'] = json_encode(array(array('value' => $item->getGenus())));
$fossil['edited']['genus'] = 0;
}
if ($edited == 1 && ($edited_fossil->getSpecies() != $item->getSpecies())){
$fossil['species'] = json_encode(array(array('value' => $edited_fossil->getSpecies())));
$fossil['edited']['species'] = 1;
} else {
$fossil['species'] = json_encode(array(array('value' => $item->getSpecies())));
$fossil['edited']['species'] = 0;
}
if ($edited == 1 && ($edited_fossil->getPublication() != $item->getPublication())){
$fossil['publication'] = json_encode(array(array('value' => $edited_fossil->getPublication())));
$fossil['edited']['publication'] = 1;
} else {
$fossil['publication'] = json_encode(array(array('value' => $item->getPublication())));
$fossil['edited']['publication'] = 0;
}
if ($edited == 1 && ($edited_fossil->getTaxanomy() != $item->getTaxanomy())){
if($translator->getLocale() == 'nl') {
$name = $edited_fossil->getTaxanomy()->getNameNl();
} else {
$name = $edited_fossil->getTaxanomy()->getNameEn();
}
if($edited_fossil->getTaxanomy()->getNameScientificClarification() != '' || is_null($edited_fossil->getTaxanomy()->getNameScientificClarification())) {
$scientific = $edited_fossil->getTaxanomy()->getNameScientific() . " " . $edited_fossil->getTaxanomy()->getNameScientificClarification();
$filename = $edited_fossil->getTaxanomy()->getNameScientific() . $edited_fossil->getTaxanomy()->getNameScientificClarification();
} else {
$scientific = $edited_fossil->getTaxanomy()->getNameScientific();
$filename = $edited_fossil->getTaxanomy()->getNameScientific();
}
$fossil['taxanomy'] = json_encode(array(array(
'id' => $edited_fossil->getTaxanomy()->getId(),
'value' => $name." (".$scientific.")",
'filename' => $filename,
)));
$fossil['edited']['taxanomy'] = 1;
} else {
if(!is_null($item->getTaxanomy())) {
if($translator->getLocale() == 'nl') {
$name = $item->getTaxanomy()->getNameNl();
} else {
$name = $item->getTaxanomy()->getNameEn();
}
if($item->getTaxanomy()->getNameScientificClarification() != '' || is_null($item->getTaxanomy()->getNameScientificClarification())) {
$scientific = $item->getTaxanomy()->getNameScientific() . " " . $item->getTaxanomy()->getNameScientificClarification();
$filename = $item->getTaxanomy()->getNameScientific() . $item->getTaxanomy()->getNameScientificClarification();
} else {
$scientific = $item->getTaxanomy()->getNameScientific();
$filename = $item->getTaxanomy()->getNameScientific();
}
$fossil['taxanomy'] = json_encode(array(array(
'id' => $item->getTaxanomy()->getId(),
'value' => $name." (".$scientific.")",
'filename' => $filename,
)));
} else { $fossil['taxanomy'] = json_encode(array()); }
$fossil['edited']['taxanomy'] = 0;
}
if ($edited == 1 && ($edited_fossil->getLocationFossil() != $item->getLocationFossil())){
if(!is_null($edited_fossil->getLocationFossil())) {
$fossil['location_fossil'] = json_encode(array(array(
'id' => $edited_fossil->getLocationFossil()->getId(),
'value' => $edited_fossil->getLocationFossil()->getNameNl(),
'name_en' => $edited_fossil->getLocationFossil()->getNameEn(),
'location' => $edited_fossil->getLocationFossil()->getLocation()
)));
} else { $fossil['location_fossil'] = json_encode(array()); }
$fossil['edited']['location_fossil'] = 0;
} else {
if(!is_null($item->getLocationFossil())) {
$fossil['location_fossil'] = json_encode(array(array(
'id' => $item->getLocationFossil()->getId(),
'value' => $item->getLocationFossil()->getNameNl(),
'name_en' => $item->getLocationFossil()->getNameEn(),
'location' => $item->getLocationFossil()->getLocation()
)));
} else { $fossil['location_fossil'] = json_encode(array()); }
$fossil['edited']['location_fossil'] = 0;
}
if ($edited == 1 && ($edited_fossil->getEra() != $item->getEra())){
if(!is_null($edited_fossil->getEra())) {
$fossil['era'] = json_encode(array(array(
'id' => $edited_fossil->getEra()->getId(),
'age_start' => $edited_fossil->getEra()->getAgeStart(),
'age_end' => $edited_fossil->getEra()->getAgeEnd(),
'value' => $edited_fossil->getEra()->getNameNl(),
'name_en' => $edited_fossil->getEra()->getNameEn(),
'color' => $edited_fossil->getEra()->getColor()
)));
} else { $fossil['era'] = json_encode(array()); }
$fossil['edited']['era'] = 1;
} else {
if(!is_null($item->getEra())) {
$fossil['era'] = json_encode(array(array(
'id' => $item->getEra()->getId(),
'age_start' => $item->getEra()->getAgeStart(),
'age_end' => $item->getEra()->getAgeEnd(),
'value' => $item->getEra()->getNameNl(),
'name_en' => $item->getEra()->getNameEn(),
'color' => $item->getEra()->getColor()
)));
} else { $fossil['era'] = json_encode(array()); }
$fossil['edited']['era'] = 0;
}
if ($edited == 1 && ($edited_fossil->getEpoch() != $item->getEpoch())){
if(!is_null($item->getEpoch())) {
$fossil['epoch'] = json_encode(array(array(
'id' => $edited_fossil->getEpoch()->getId(),
'age_start' => $edited_fossil->getEpoch()->getAgeStart(),
'age_end' => $edited_fossil->getEpoch()->getAgeEnd(),
'value' => $edited_fossil->getEpoch()->getNameNl(),
'name_en' => $edited_fossil->getEpoch()->getNameEn(),
'color' => $edited_fossil->getEpoch()->getColor()
)));
} else { $fossil['epoch'] = json_encode(array()); }
$fossil['edited']['epoch'] = 1;
} else {
if(!is_null($item->getEpoch())) {
$fossil['epoch'] = json_encode(array(array(
'id' => $item->getEpoch()->getId(),
'age_start' => $item->getEpoch()->getAgeStart(),
'age_end' => $item->getEpoch()->getAgeEnd(),
'value' => $item->getEpoch()->getNameNl(),
'name_en' => $item->getEpoch()->getNameEn(),
'color' => $item->getEpoch()->getColor()
)));
} else { $fossil['epoch'] = json_encode(array()); }
$fossil['edited']['epoch'] = 0;
}
if ($edited == 1 && ($edited_fossil->getStage() != $item->getStage())){
if(!is_null($item->getStage())) {
$fossil['stage'] = json_encode(array(array(
'id' => $edited_fossil->getStage()->getId(),
'age_start' => $edited_fossil->getStage()->getAgeStart(),
'age_end' => $edited_fossil->getStage()->getAgeEnd(),
'value' => $edited_fossil->getStage()->getNameNl(),
'name_en' => $edited_fossil->getStage()->getNameEn(),
'color' => $edited_fossil->getStage()->getColor()
)));
} else { $fossil['stage'] = json_encode(array()); }
$fossil['edited']['stage'] = 1;
} else {
if(!is_null($item->getStage())) {
$fossil['stage'] = json_encode(array(array(
'id' => $item->getStage()->getId(),
'age_start' => $item->getStage()->getAgeStart(),
'age_end' => $item->getStage()->getAgeEnd(),
'value' => $item->getStage()->getNameNl(),
'name_en' => $item->getStage()->getNameEn(),
'color' => $item->getStage()->getColor()
)));
} else { $fossil['stage'] = json_encode(array()); }
$fossil['edited']['stage'] = 0;
}
if ($edited == 1 && ($edited_fossil->getFormation() != $item->getFormation())){
if(!is_null($item->getFormation())) {
$fossil['formation'] = json_encode(array(array(
'id' => $edited_fossil->getFormation()->getId(),
'age_start' => $edited_fossil->getFormation()->getAgeStart(),
'age_end' => $edited_fossil->getFormation()->getAgeEnd(),
'value' => $edited_fossil->getFormation()->getNameNl(),
'name_en' => $edited_fossil->getFormation()->getNameEn(),
'color' => $edited_fossil->getFormation()->getColor()
)));
} else { $fossil['formation'] = json_encode(array()); }
$fossil['edited']['formation'] = 1;
} else {
if(!is_null($item->getFormation())) {
$fossil['formation'] = json_encode(array(array(
'id' => $item->getFormation()->getId(),
'age_start' => $item->getFormation()->getAgeStart(),
'age_end' => $item->getFormation()->getAgeEnd(),
'value' => $item->getFormation()->getNameNl(),
'name_en' => $item->getFormation()->getNameEn(),
'color' => $item->getFormation()->getColor()
)));
} else { $fossil['formation'] = json_encode(array()); }
$fossil['edited']['formation'] = 0;
}
if ($edited == 1 && ($edited_fossil->getMember() != $item->getMember())){
if(!is_null($item->getMember())) {
$fossil['member'] = json_encode(array(array(
'id' => $edited_fossil->getMember()->getId(),
'age_start' => $edited_fossil->getMember()->getAgeStart(),
'age_end' => $edited_fossil->getMember()->getAgeEnd(),
'value' => $edited_fossil->getMember()->getNameNl(),
'name_en' => $edited_fossil->getMember()->getNameEn(),
'color' => $edited_fossil->getMember()->getColor()
)));
} else { $fossil['member'] = json_encode(array()); }
$fossil['edited']['member'] = 1;
} else {
if(!is_null($item->getMember())) {
$fossil['member'] = json_encode(array(array(
'id' => $item->getMember()->getId(),
'age_start' => $item->getMember()->getAgeStart(),
'age_end' => $item->getMember()->getAgeEnd(),
'value' => $item->getMember()->getNameNl(),
'name_en' => $item->getMember()->getNameEn(),
'color' => $item->getMember()->getColor()
)));
} else { $fossil['member'] = json_encode(array()); }
$fossil['edited']['member'] = 0;
}
if ($edited == 1 && ($edited_fossil->getDateYear() != $item->getDateYear())){
$fossil['date_year'] = $edited_fossil->getDateYear();
$fossil['edited']['date_year'] = 1;
} else {
$fossil['date_year'] = $item->getDateYear();
$fossil['edited']['date_year'] = 0;
}
if ($edited == 1 && ($edited_fossil->getDateMonth() != $item->getDateMonth())){
$fossil['date_month'] = $edited_fossil->getDateMonth();
$fossil['edited']['date_month'] = 1;
} else {
$fossil['date_month'] = $item->getDateMonth();
$fossil['edited']['date_month'] = 0;
}
if ($edited == 1 && $edited_fossil->getDateDay() != $item->getDateDay()){
$fossil['date_day'] = $edited_fossil->getDateDay();
$fossil['edited']['date_day'] = 1;
} else {
$fossil['date_day'] = $item->getDateDay();
$fossil['edited']['date_day'] = 0;
}
if ($edited == 1 && ($edited_fossil->getCommentNl() != $item->getCommentNl())){
$fossil['comment_nl'] = $edited_fossil->getCommentNl();
$fossil['edited']['comment_nl'] = 1;
} else {
$fossil['comment_nl'] = $item->getCommentNl();
$fossil['edited']['comment_nl'] = 0;
}
if ($edited == 1 && ($edited_fossil->getCommentEn() != $item->getCommentEn())){
$fossil['comment_en'] = $edited_fossil->getCommentEn();
$fossil['edited']['comment_en'] = 1;
} else {
$fossil['comment_en'] = $item->getCommentEn();
$fossil['edited']['comment_en'] = 0;
}
if ($edited == 1 && ($edited_fossil->getUserNumber() != $item->getUserNumber())){
$fossil['user_number'] = $edited_fossil->getUserNumber();
$fossil['edited']['user_number'] = 1;
} else {
$fossil['user_number'] = $item->getUserNumber();
$fossil['edited']['user_number'] = 0;
}
if ($edited == 1 && ($edited_fossil->getLiterature() != $item->getLiterature())){
if(!is_null($item->getLiterature())) {
$fossil['literature'] = json_encode(array(array(
'id' => $edited_fossil->getLiterature()->getId(),
'value' => $edited_fossil->getLiterature()->getAuthor()." (".$edited_fossil->getLiterature()->getyear().") - ".$edited_fossil->getLiterature()->getTitle(),
'title' => $edited_fossil->getLiterature()->getTitle(),
'year' => $edited_fossil->getLiterature()->getyear()
)));
} else { $fossil['literature'] = json_encode(array()); }
$fossil['edited']['literature'] = 1;
} else {
if(!is_null($item->getLiterature())) {
$fossil['literature'] = json_encode(array(array(
'id' => $item->getLiterature()->getId(),
'value' => $item->getLiterature()->getAuthor()." (".$item->getLiterature()->getyear().") - ".$item->getLiterature()->getTitle(),
'title' => $item->getLiterature()->getTitle(),
'year' => $item->getLiterature()->getyear()
)));
} else { $fossil['literature'] = json_encode(array()); }
$fossil['edited']['literature'] = 0;
}
$filter = array( 'genus' => $fossil['genus'], 'species' => $fossil['species'], 'publication' => $fossil['publication'], 'taxanomy' => $fossil['taxanomy'], 'location' => $fossil['location_fossil'], 'era' => $fossil['era'], 'epoch' => $fossil['epoch'], 'stage' => $fossil['stage'], 'literature' => $fossil['literature']);
$fossil['photos'] = $item->getFossildbPhotos();
$form = $this->createForm(FossildbFossilEditFormType::class, $fossil);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$fossil_old = $fossil;
$fossil = $form->getData();
if($fossil_old['location_fossil'] != $fossil['location_fossil']) {
if(!is_null($fossil['location_fossil'])) {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$location_fossil = $repository->findOneBy(array('id' => json_decode($fossil['location_fossil'])[0]->id));
} else {
$location_fossil = null;
}
} else {
if(!is_null($fossil_old['location_fossil'])) {
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$location_fossil = $repository->findOneBy(array('id' => json_decode($fossil_old['location_fossil'])[0]->id));
} else {
$location_fossil = null;
}
}
if($fossil_old['taxanomy'] != $fossil['taxanomy']) {
if(!is_null($fossil['taxanomy'])) {
$repository = $this->getDoctrine()->getRepository(Taxanomy::class);
$taxanomy = $repository->findOneBy(array('id' => json_decode($fossil['taxanomy'])[0]->id));
} else {
$taxanomy = null;
}
} else {
if(!is_null($fossil_old['taxanomy'])) {
$repository = $this->getDoctrine()->getRepository(Taxanomy::class);
$taxanomy = $repository->findOneBy(array('id' => json_decode($fossil_old['taxanomy'])[0]->id));
} else {
$taxanomy = null;
}
}
if($fossil_old['era'] != $fossil['era']) {
if(!is_null($fossil['era'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$era = $repository->findOneBy(array('id' => json_decode($fossil['era'])[0]->id));
} else {
$era = null;
}
} else {
if(!is_null($fossil_old['era'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$era = $repository->findOneBy(array('id' => json_decode($fossil_old['era'])[0]->id));
} else {
$era = null;
}
}
if($fossil_old['epoch'] != $fossil['epoch']) {
if(!is_null($fossil['epoch'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$epoch = $repository->findOneBy(array('id' => json_decode($fossil['epoch'])[0]->id));
} else {
$epoch = null;
}
} else {
if(!is_null($fossil_old['epoch'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$epoch = $repository->findOneBy(array('id' => json_decode($fossil_old['epoch'])[0]->id));
} else {
$epoch = null;
}
}
if($fossil_old['stage'] != $fossil['stage']) {
if(!is_null($fossil['stage'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$stage = $repository->findOneBy(array('id' => json_decode($fossil['stage'])[0]->id));
} else {
$stage = null;
}
} else {
if(!is_null($fossil_old['stage'])) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$stage = $repository->findOneBy(array('id' => json_decode($fossil_old['stage'])[0]->id));
} else {
$stage = null;
}
}
if($fossil_old['formation'] != $fossil['formation']) {
$formation_item = json_decode($fossil['formation'])[0];
if (is_null($formation_item)) {
$formation = null;
} elseif (property_exists($formation_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$formation = $repository->findOneBy(array('id' => $formation_item->id));
} else {
$new_formation_item = new StratigraphyUnits;
$new_formation_item->setType("member");
$new_formation_item->setNameNl($formation_item->value);
$new_formation_item->setNameEn($formation_item->value);
$em->persist($new_formation_item);
$em->flush();
$formation = $new_formation_item->getId();
}
} else {
if(!is_null($fossil_old['formation'])){
$formation_item = json_decode($fossil_old['formation'])[0];
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$formation = $repository->findOneBy(array('id' => $formation_item->id));
} else {
$formation = null;
}
}
if($fossil_old['member'] != $fossil['member']) {
$member_item = json_decode($fossil['member'])[0];
if (is_null($member_item)) {
$member = null;
} elseif (property_exists($member_item, 'id')) {
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$member = $repository->findOneBy(array('id' => $member_item->id));
} else {
$new_member_item = new StratigraphyUnits;
$new_member_item->setType("member");
$new_member_item->setNameNl($member_item->value);
$new_member_item->setNameEn($member_item->value);
$em->persist($new_member_item);
$em->flush();
$member = $new_member_item;
}
} else {
if(!is_null($fossil_old['member'])){
$member_item = json_decode($fossil_old['member'])[0];
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$member = $repository->findOneBy(array('id' => $member_item->id));
} else {
$member = null;
}
}
if($fossil_old['literature'] != $fossil['literature']) {
if(!is_null($fossil['literature'])){
$repository = $this->getDoctrine()->getRepository(Literature::class);
$literature = $repository->findOneBy(array('id' => json_decode($fossil['literature'])[0]->id));
} else {
$literature = null;
}
} else {
if(!is_null($fossil_old['literature'])){
$repository = $this->getDoctrine()->getRepository(Literature::class);
$literature = $repository->findOneBy(array('id' => json_decode($fossil_old['literature'])[0]->id));
} else {
$literature = null;
}
}
if($fossil_old['date_year'] != $fossil['date_year']) {
$date_year=$fossil['date_year'];
} else {
$date_year=$fossil_old['date_year'];
}
if($fossil_old['date_month'] != $fossil['date_month']) {
$date_month=$fossil['date_month'];
} else {
$date_month=$fossil_old['date_month'];
}
if($fossil_old['date_day'] != $fossil['date_day']) {
$date_day=$fossil['date_day'];
} else {
$date_day=$fossil_old['date_day'];
}
if($fossil_old['comment_nl'] != $fossil['comment_nl']) {
$comment_nl=$fossil['comment_nl'];
} else {
$comment_nl=$fossil_old['comment_nl'];
}
if($fossil_old['comment_en'] != $fossil['comment_en']) {
$comment_en=$fossil['comment_en'];
} else {
$comment_en=$fossil_old['comment_en'];
}
if($fossil_old['user_number'] != $fossil['user_number']) {
$user_number=$fossil['user_number'];
} else {
$user_number=$fossil_old['user_number'];
}
$repository = $this->getDoctrine()->getRepository(FossildbFossil::class);
$fossildbFossil = $repository->findOneBy(array('id' => $item->getId()));
if($fossil_old['genus'] != $fossil['genus']) {
if(!is_null($fossil['genus'])) {
$fossildbFossil->setGenus(json_decode($fossil['genus'])[0]->value);
} else {
$fossildbFossil->setGenus(null);
}
} else {
if(!is_null($fossil_old['genus'])) {
$fossildbFossil->setGenus(json_decode($fossil_old['genus'])[0]->value);
} else {
$fossildbFossil->setGenus(null);
}
}
if($fossil_old['species'] != $fossil['species']) {
if(!is_null($fossil['species'])) {
$fossildbFossil->setSpecies(json_decode($fossil['species'])[0]->value);
} else {
$fossildbFossil->setSpecies(null);
}
} else {
if(!is_null($fossil_old['species'])) {
$fossildbFossil->setSpecies(json_decode($fossil_old['species'])[0]->value);
} else {
$fossildbFossil->setSpecies(null);
}
}
if($fossil_old['publication'] != $fossil['publication']) {
if(!is_null($fossil['publication'])) {
$fossildbFossil->setPublication(json_decode($fossil['publication'])[0]->value);
} else {
$fossildbFossil->setPublication(null);
}
} else {
if(!is_null($fossil_old['publication'])) {
$fossildbFossil->setPublication(json_decode($fossil_old['publication'])[0]->value);
} else {
$fossildbFossil->setPublication(null);
}
}
$fossildbFossil->setLocationFossil($location_fossil);
$fossildbFossil->setTaxanomy($taxanomy);
$fossildbFossil->setEra($era);
$fossildbFossil->setEpoch($epoch);
$fossildbFossil->setStage($stage);
$fossildbFossil->setFormation($formation);
$fossildbFossil->setMember($member);
$fossildbFossil->setDateYear($date_year);
$fossildbFossil->setDateMonth($date_month);
$fossildbFossil->setDateDay($date_day);
$fossildbFossil->setCommentNl($comment_nl);
$fossildbFossil->setCommentEn($comment_en);
$fossildbFossil->setUserNumber($user_number);
$fossildbFossil->setLiterature($literature);
$fossil_id = $item->getId();
if(!is_null($fossil['upload'])) {
$upload = json_decode($fossil['upload']);
foreach($upload as $id => $item) {
if(!is_null($item)) {
$repository = $this->getDoctrine()->getRepository(FossildbFossil::class);
$fossildbFossil = $repository->findOneBy(array('id' => $fossil_old['id']));
$repository = $this->getDoctrine()->getRepository(UploadQueue::class);
$queuefile = $repository->findOneBy(array('system' => 'fossildb','filename' => $item->name));
if(!is_null($queuefile)) {
$fossildbPhoto = new FossildbPhotos;
$fossildbPhoto->setFossildbFossil($fossildbFossil);
$fossildbPhoto->setType('fossil');
$fossildbPhoto->setFilename($item->name);
$fossildbPhoto->setAdded(new \DateTime());
$fossildbPhoto->setUserId($this->getUser()->getId());
$fossildbPhoto->setUsername($this->getUser()->getUsername());
$fossildbPhoto->setApproved(0);
$fp = $em->createQueryBuilder('fp')
->from('App\Entity\FossildbPhotos', 'fp')
->addSelect('count(fp.id)')
->andWhere("fp.is_primary = 1")
->andWhere("fp.FossildbFossil = (:id)")
->setParameter('id', $item->id)
->getQuery()
->getSingleScalarResult();
;
if($fp == 0) {
$fossildbPhoto->setIsPrimary(1);
} else {
$fossildbPhoto->setIsPrimary(0);
}
$em->persist($fossildbPhoto);
$em->remove($queuefile);
$fossildbFossil->setNumNewPhotos($fossildbFossil->getNumNewPhotos()+1);
$em->persist($fossildbFossil);
$em->persist($item);
$em->flush();
}
}
}
}
$em->persist($fossildbFossil);
$em->flush();
$repository = $this->getDoctrine()->getRepository(FossildbFossilEdits::class);
$editedfossil = $repository->findOneBy(array('fossil' => $item->getId()));
$em->remove($editedfossil);
$em->flush();
$this->addFlash('success', $translator->trans("Fossiel bijgewerkt. De wijzigingen zijn moeten nog goed gekeurd worden."));
return $this->redirectToRoute('fossildb_moderate');
}
return $this->render('fossildb_edit.html.twig', [
'filter' => $filter,
'formname' => 'fossildb_fossil_edit_form',
'edit' => true,
'approve_or_deny' => true,
'item' => $fossil,
'title' => $translator->trans("Fossiel bewerkingen"),
'button_text' => $translator->trans("Bewerken"),
'form' => $form->createView()
]);
}
/**
* @Route("/moderate/fossildb/{item}/edits/deny", name="fossildb_moderate_deny_edits")
*/
public function fossildb_moderate_deny_edits(FossildbFossil $item, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$repository = $this->getDoctrine()->getRepository(FossildbFossilEdits::class);
$editedfossil = $repository->findOneBy(array('fossil' => $item->getId()));
$em->remove($editedfossil);
$em->flush();
return $this->redirectToRoute('fossildb_moderate');
}
/**
* @Route("/moderate/fossildb/{item}/approve", name="fossildb_moderate_approve")
*/
public function fossildb_moderate_approve(FossildbFossil $item, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$item->setApprovedUserId($this->getUser()->getId());
$item->setApprovedUsername($this->getUser()->getUsername());
$item->setApproved('1');
$em->persist($item);
$newsfeed = new Newsfeed();
$newsfeed->setTitleNl($item->getGenus()." ".$item->getSpecies());
$newsfeed->setTitleEn($item->getGenus()." ".$item->getSpecies());
$newsfeed->setRemoteId($item->getId());
$newsfeed->setIsUpdate(0);
$newsfeed->setType('fossildb');
$newsfeed->setTime(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($newsfeed);
$em->flush();
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('fossildb_item', [ 'id' => $item->getId() ]) ));
if(!$searchitem) {
$searchitem = new SearchItems();
}
$searchitem->setType('fossildb');
$searchitem->setPath($this->router->generate('fossildb_item', [ 'id' => $item->getId() ]));
$name_nl_add = '';
$name_en_add = '';
$keywords_nl_add = '';
$keywords_en_add = '';
if($item->getTaxanomy()) {
$name_nl_add = $item->getTaxanomy()->getNameNl().' -> ';
$name_en_add = $item->getTaxanomy()->getNameEn().' -> ';
$keywords_nl_add = $item->getTaxanomy()->getNameScientific().','.$item->getTaxanomy()->getNameNl().',';
$keywords_en_add = $item->getTaxanomy()->getNameScientific().','.$item->getTaxanomy()->getNameEn().',';
}
if(empty($item->getGenus()) && empty($item->getSpecies())){
$name_nl_add .= '[Ongedetermineerd]';
$name_en_add .= '[Unidentified]';
}
$searchitem->setNameNl($name_nl_add.$item->getGenus().' '.$item->getSpecies());
$searchitem->setNameEn($name_en_add.$item->getGenus().' '.$item->getSpecies());
$searchitem->setKeywordsNl($keywords_nl_add.$item->getGenus().','.$item->getSpecies());
$searchitem->setKeywordsEn($keywords_en_add.$item->getGenus().','.$item->getSpecies());
if(empty($item->getEdited())) {
$searchitem->setLastUpdateTime($item->getAdded());
} else {
$searchitem->setLastUpdateTime($item->getEdited());
}
$em->persist($searchitem);
$em->flush();
$repository = $em->getRepository(FossildbFossil::class);
$totalsubmissions = $repository->createQueryBuilder('a')
->select('count(a.id)')
->andWhere("a.user_id = (:user)")
->andWhere("a.approved = 1")
->setParameter('user', $item->getUserId())
->getQuery()
->getSingleScalarResult();
$repository = $em->getRepository(User::class);
$user = $repository->findOneBy(array('id' => $item->getUserId()));
$repository = $this->getDoctrine()->getRepository(UserProfile::class);
$userprofile = $repository->findOneBy(array('user' => $user));
if(!$userprofile) {
$userprofile = new UserProfile();
$userprofile->setUser($user);
}
$userprofile->setFossildbSubmissions($totalsubmissions);
$em->persist($userprofile);
$em->flush();
$this->addFlash('success', $translator->trans("Fossiel goedgekeurd."));
return $this->redirectToRoute('fossildb_moderate');
}
/**
* @Route("/moderate/fossildb/{item}/delete", name="fossildb_moderate_delete")
*/
public function moderate_delete(FossildbFossil $item, EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->denyAccessUnlessGranted('ROLE_MOD_FOSSILDB');
$repository = $em->getRepository(SearchItems::class);
$searchitem = $repository->findOneBy(array('path' => $this->router->generate('fossildb_item', [ 'id' => $item->getId() ]) ));
if($searchitem){
$em->remove($searchitem);
}
$em->remove($item);
$em->flush();
$repository = $em->getRepository(FossildbFossil::class);
$totalsubmissions = $repository->createQueryBuilder('a')
->select('count(a.id)')
->andWhere("a.user_id = (:user)")
->andWhere("a.approved = 1")
->setParameter('user', $item->getUserId())
->getQuery()
->getSingleScalarResult();
$repository = $em->getRepository(User::class);
$user = $repository->findOneBy(array('id' => $item->getUserId()));
$repository = $this->getDoctrine()->getRepository(UserProfile::class);
$userprofile = $repository->findOneBy(array('user' => $user));
if(!$userprofile) {
$userprofile = new UserProfile();
$userprofile->setUser($user);
}
$userprofile->setFossildbSubmissions($totalsubmissions);
$em->persist($userprofile);
$em->flush();
$this->addFlash('success', $translator->trans("Fossiel verwijderd uit Fossielendatabase"));
return $this->redirectToRoute('fossildb_moderate');
}
/**
* @Route("/api/v1/fossildb/getgenus", methods="GET", name="api_fossildb_genus")
*/
public function getGenusApi(FossildbFossilRepository $fossildbFossilRepository, Request $request)
{
$fossildbGenus = $fossildbFossilRepository->findAllGenusMatching($request->query->get('query'));
return $this->json($fossildbGenus, 200, [], ['groups' => ['main']]);
}
/**
* @Route("/api/v1/fossildb/getspecies", methods="GET", name="api_fossildb_species")
*/
public function getSpeciesApi(FossildbFossilRepository $fossildbFossilRepository, Request $request)
{
$fossildbSpecies = $fossildbFossilRepository->findAllSpeciesMatching($request->query->get('query'));
return $this->json($fossildbSpecies, 200, [], ['groups' => ['main']]);
}
/**
* @Route("/api/v1/fossildb/getpublication", methods="GET", name="api_fossildb_publication")
*/
public function getPublicationApi(FossildbFossilRepository $fossildbFossilRepository, Request $request)
{
$fossildbPublications = $fossildbFossilRepository->findAllPublicationsMatching($request->query->get('query'));
return $this->json($fossildbPublications, 200, [], ['groups' => ['main']]);
}
/**
* @Route("/api/v1/fossildb/gettaxanomy", methods="GET", name="api_fossildb_taxaonmy")
*/
public function getTaxanomyApi(TaxanomyRepository $taxanomyRepository, Request $request, TranslatorInterface $translator)
{
$taxanomy = $taxanomyRepository->findAllMatching($request->query->get('query'));
$return = array();
foreach($taxanomy as $item) {
if($translator->getLocale() == 'nl') {
$name = $item->getNameNl();
} else {
$name = $item->getNameEn();
}
if($item->getNameScientificClarification() != '' || !is_null($item->getNameScientificClarification())) {
$scientific = $item->getNameScientific() . " " . $item->getNameScientificClarification();
$filename = strtolower($item->getNameScientific()) . strtolower($item->getNameScientificClarification());
} else {
$scientific = $item->getNameScientific();
$filename = strtolower($item->getNameScientific());
}
$return[] = array(
'id' => $item->getId(),
'value' => $name." (".$scientific.")",
'filename' => $filename,
);
}
return $this->json($return, 200, [], ['groups' => ['main']]);
}
#ToDo-Ante: Probably a duplicate
/**
* @Route("/api/v1/fossildb/getlocation", methods="GET", name="api_fossildb_location")
*/
public function getLocation(LocationsFossilRepository $LocationsFossilRepository, Request $request)
{
$user = $this->getUser();
$location = $LocationsFossilRepository->findAllMatching($request->query->get('query'),25,$user);
return $this->json($location, 200, [], ['groups' => ['main']]);
}
/**
* @Route("/api/v1/fossildb/getstratigraphyunits/{type}", methods="GET", name="api_fossildb_stratigraphyunits")
*/
public function getStratigraphyUnitsApi(string $type, StratigraphyUnitsRepository $stratigraphyUnitsRepository, Request $request, TranslatorInterface $translator)
{
if($request->query->get('age_start') && $request->query->get('age_end')) {
$stratigraphyUnits = $stratigraphyUnitsRepository->findAllMatching($type, $request->query->get('query'), 5, $request->query->get('age_start'), $request->query->get('age_end'));
} else {
$stratigraphyUnits = $stratigraphyUnitsRepository->findAllMatching($type, $request->query->get('query'));
}
$return = array();
foreach($stratigraphyUnits as $item) {
if($translator->getLocale() == 'nl') {
$name = $item->getNameNl();
} else {
$name = $item->getNameEn();
}
$return[] = array(
'id' => $item->getId(),
'age_start' => $item->getAgeStart(),
'age_end' => $item->getAgeEnd(),
'value' => $name,
'name_en' => $item->getNameEn(),
'color' => $item->getColor(),
);
}
return $this->json($return, 200, [], ['groups' => ['main']]);
}
// /**
// * @Route("/api/v1/fossildb/getgenus/{genus}", methods="GET", name="api_fossildb_genus")
// */
// public function getGenusApi(string $genus, Request $request)
// {
// $query = $em->createQuery('SELECT DISTINCT f.genus FROM App\Entity\FossildbFossil f WHERE f.genus = (:genus)');
// $query->setParameter('genus', $genus);
// if($request->query->get('age_start') && $request->query->get('age_end')) {
// $stratigraphyUnits = $stratigraphyUnitsRepository->findAllMatching($type, $request->query->get('query'), 5, $request->query->get('age_start'), $request->query->get('age_end'));
// } else {
// $stratigraphyUnits = $stratigraphyUnitsRepository->findAllMatching($type, $request->query->get('query'));
// }
// return $this->json($stratigraphyUnits, 200, [], ['groups' => ['main']]);
// }
/**
* @Route("/api/v1/fossildb/add", methods="POST", name="api_fossildb_add")
*/
public function api_fossildb_add(Request $request, StratigraphyUnitsRepository $stratigraphyUnitsRepository, EntityManagerInterface $em)
{
$this->denyAccessUnlessGranted('ROLE_USER');
/* Sample Data
{
"location":137, (int, required)
"taxanomy":14, (int, required)
"literature":220, (int)
"era":1005, (int, required)
"epoch":1025, (int)
"stage":1089, (int)
"formation":1236, (int or string eg. "Slite")
"member":1619, (int or string eg. "Lanaye")
"genus":"Cosmopolitodus", (string)
"species":"hastalis", (string)
"author":"Agassiz", (string)
"author_year":1843, (int)
"found_day":31, (int)
"found_month":12, (int)
"found_year":1999, (int)
"comment_nl":"vrij tekst veld", (string)
"comment_en":"free text field", (string)
"user_number":"123AB" (string)
returns: { error: 0|1, message: <id>|<reason> }
}
*/
if($request->getContent() != "" && is_array($data = json_decode($request->getContent(), true))) {
$error = 0;
$message = "";
// Check required fields
if(!array_key_exists('location',$data)||$data['location']=="") { $error = 1;$message=$message."Location is required. "; }
elseif(!is_integer($data['location'])) { $error = 1;$message=$message."Location data is not a number. "; }
else{
$repository = $this->getDoctrine()->getRepository(LocationsFossil::class);
$data['location'] = $repository->findOneBy(array('id' => $data['location']));
}
if(!array_key_exists('taxanomy',$data)||$data['taxanomy']=="") { $error = 1;$message=$message."Taxanomy is required. "; }
elseif(!is_integer($data['taxanomy'])) { $error = 1;$message=$message."Taxanomy data is not a number. "; }
else{
$repository = $this->getDoctrine()->getRepository(Taxanomy::class);
$data['taxanomy'] = $repository->findOneBy(array('id' => $data['taxanomy']));
}
if(!array_key_exists('era',$data)||$data['era']=="") { $error = 1;$message=$message."Era is required. "; }
elseif(!is_integer($data['era'])) { $error = 1;$message=$message."Era data is not a number. "; }
else{
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$data['era'] = $repository->findOneBy(array('id' => $data['era']));
}
// Check for missing, empty or incorrect fields
if(!array_key_exists('literature',$data)) { $data['literature'] = null; }
elseif(!is_integer($data['literature'])) { $error = 1;$message=$message."Literature data is not a number. "; }
else{
$repository = $this->getDoctrine()->getRepository(Literature::class);
$data['literature'] = $repository->findOneBy(array('id' => $data['literature']));
}
if(!array_key_exists('epoch',$data)) { $data['epoch'] = null; }
elseif(!is_integer($data['epoch'])) { $error = 1;$message=$message."Epoch data is not a number. "; }
else{
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$data['epoch'] = $repository->findOneBy(array('id' => $data['epoch']));
}
if(!array_key_exists('stage',$data)) { $data['stage'] = null; }
elseif(!is_integer($data['stage'])) { $error = 1;$message=$message."Stage data is not a number. "; }
else{
$repository = $this->getDoctrine()->getRepository(StratigraphyUnits::class);
$data['stage'] = $repository->findOneBy(array('id' => $data['stage']));
}
$data['formation_new']=0;
if(!array_key_exists('formation',$data) || is_null($data['formation'])) { $data['formation'] = null; }
elseif(!is_integer($data['formation'])) {
$formation = $stratigraphyUnitsRepository->findAllMatching('formation', $data['formation']);
if(count($formation)>0){$data['formation'] = $formation[0];}
else{$data['formation_new']=1;}
}
$data['member_new']=0;
if(!array_key_exists('member',$data) || is_null($data['member'])) { $data['member'] = null; }
elseif(!is_integer($data['member'])) {
$member = $stratigraphyUnitsRepository->findAllMatching('member', $data['member']);
if(count($member)>0){$data['member'] = $member[0];}
else{$data['member_new']=1;}
}
if(!array_key_exists('genus',$data)) { $data['genus'] = ""; }
if(!array_key_exists('species',$data)) { $data['species'] = ""; }
if(!array_key_exists('publication',$data)) { $data['publication'] = ""; }
if(!array_key_exists('found_day',$data)) { $data['found_day'] = ""; }
elseif(!is_integer($data['found_day'])) {$error = 1;$message=$message."found_day data is not a number. "; }
elseif(!preg_match('/^\d{1,2}$/',$data['found_day'])) {$error = 1;$message=$message."found_day data not a day. "; }
if(!array_key_exists('found_month',$data)) { $data['found_month'] = ""; }
elseif(!is_integer($data['found_month'])) {$error = 1;$message=$message."found_month data is not a number. "; }
elseif(!preg_match('/^\d{1,2}$/',$data['found_month'])) {$error = 1;$message=$message."found_month data not a month. "; }
if(!array_key_exists('found_year',$data)) { $data['found_year'] = ""; }
elseif(!is_integer($data['found_year'])) {$error = 1;$message=$message."found_year data is not a number. "; }
elseif(!preg_match('/^\d{4}$/',$data['found_year'])) {$error = 1;$message=$message."found_year data not a year. "; }
if(!array_key_exists('comment_nl',$data)) { $data['comment_nl'] = ""; }
if(!array_key_exists('comment_en',$data)) { $data['comment_en'] = ""; }
if(!array_key_exists('user_number',$data)) { $data['user_number'] = ""; }
if($error == 0){
$fossildbFossil = new FossildbFossil;
$fossildbFossil->setLocationFossil($data['location']);
$fossildbFossil->setTaxanomy($data['taxanomy']);
$fossildbFossil->setLiterature($data['literature']);
$fossildbFossil->setEra($data['era']);
$fossildbFossil->setEpoch($data['epoch']);
$fossildbFossil->setStage($data['stage']);
if($data['formation_new'] == 1){
$new_formation_item = new StratigraphyUnits;
$new_formation_item->setType("formation");
$new_formation_item->setNameNl($data['formation']);
$new_formation_item->setNameEn($data['formation']);
$em->persist($new_formation_item);
$em->flush();
$data['formation'] = $new_formation_item->getId();
}
$fossildbFossil->setFormation($data['formation']);
if($data['member_new'] == 1){
$new_member_item = new StratigraphyUnits;
$new_member_item->setType("member");
$new_member_item->setNameNl($data['member']);
$new_member_item->setNameEn($data['member']);
$em->persist($new_member_item);
$em->flush();
$data['member'] = $new_member_item->getId();
}
$fossildbFossil->setMember($data['member']);
$fossildbFossil->setGenus($data['genus']);
$fossildbFossil->setSpecies($data['species']);
$fossildbFossil->setPublication($data['publication']);
$fossildbFossil->setDateDay($data['found_day']);
$fossildbFossil->setDateMonth($data['found_month']);
$fossildbFossil->setDateYear($data['found_year']);
$fossildbFossil->setCommentNl($data['comment_nl']);
$fossildbFossil->setCommentEn($data['comment_en']);
$fossildbFossil->setUserNumber($data['user_number']);
$constructed_taxanomy_tree=$this->taxanomy_tree($data['taxanomy']->getId(), $em);
$taxanomy_tree = array();
foreach($constructed_taxanomy_tree as $item => $object) {
$taxanomy_tree[$item]['namescientific'] = $object->getNameScientific();
$taxanomy_tree[$item]['namescientificclarification'] = $object->getNameScientificClarification();
$taxanomy_tree[$item]['namenl'] = $object->getNameNl();
$taxanomy_tree[$item]['nameen'] = $object->getNameEn();
$taxanomy_tree[$item]['author'] = $object->getAuthor();
$taxanomy_tree[$item]['authoryear'] = $object->getAuthorYear();
if($object->getGlossary()){
$taxanomy_tree[$item]['glossary']['id'] = $object->getGlossary()->getId();
$taxanomy_tree[$item]['glossary']['url'] = $object->getGlossary()->getUrl();
$taxanomy_tree[$item]['glossary']['titlenl'] = $object->getGlossary()->getTitleNl();
$taxanomy_tree[$item]['glossary']['titleen'] = $object->getGlossary()->getTitleEn();
} else {
$taxanomy_tree[$item]['glossary'] = null;
}
$taxanomy_tree[$item]['taxanomyrank']['namescientific'] = $object->getTaxanomyRank()->getNameScientific();
$taxanomy_tree[$item]['taxanomyrank']['namenl'] = $object->getTaxanomyRank()->getNameNl();
$taxanomy_tree[$item]['taxanomyrank']['nameen'] = $object->getTaxanomyRank()->getNameEn();
}
$fossildbFossil->setTaxanomyTree(array_reverse($taxanomy_tree));
$fossildbFossil->setAdded(new \DateTime());
$fossildbFossil->setUserId($this->getUser()->getId());
$fossildbFossil->setUsername($this->getUser()->getUsername());
$fossildbFossil->setApproved(0);
$fossildbFossil->setNumReviews(0);
$fossildbFossil->setNumNewPhotos(0);
$fossildbFossil->setHasEdits(0);
$em->persist($fossildbFossil);
$em->flush();
$message = $fossildbFossil->getid();
}
$return = array("error" => $error, 'message' => $message);
} else {
$return = array("error" => 1, "message" => "No valid data was recieved");
}
if($return['error'] == 1) {
$status = 422;
} else {
$status = 200;
}
return new JsonResponse($return, $status);
}
}