<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Contribution;
use App\Entity\DataThomsonMeta;
use App\Entity\Journal;
use App\Entity\Report;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
class JournalController extends AbstractController
{
/** @var \Swift_Mailer */
private $mailer;
/**
* JournalController constructor.
* @param \Swift_Mailer $mailer
*/
private $serializer;
public function __construct(\Swift_Mailer $mailer,SerializerInterface $serializer)
{
$this->mailer = $mailer;
$this->serializer = $serializer;
}
/**
* @Route("/journal/{id}", name="journal_show", methods={"GET"})
*/
public function showJournal($id,EntityManagerInterface $em): Response
{
$journalCategories=[];
$samePublisher=[];
$category="";
$sameAreas=[];
$sameQuartile=[];
$articlesDoaj=[];
$articles=[];
/** @var Journal $journal */
$journal = $em->getRepository(Journal::class)->find($id);
if(!$journal){
return $this->redirectToRoute('data_search_by_several_values', ['searchTerm' => 'Journal', 'searchValue' => $id]);
}
$publisher=$journal->getPublisher();
if($publisher) {
$samePublisher = $em->getRepository(Journal::class)->samePublisher($journal);
}
$area=$journal->getArea();
if(str_contains($area,';')){
$areas = explode(";", $area);
$areas = array_map('trim', $areas);
// $sameAreas = $em->getRepository(Journal::class)->sameArea($journal,$areas);
$sameAreas = $em->getRepository(Journal::class)->sameCategoryName($journal,$journal->getCategoryName());
}
$sameQuartile = $em->getRepository(Journal::class)->sameQuartile($journal);
$similatTitle = $em->getRepository(Journal::class)->similarTitle($journal);
// get articles from doaj api Based on eissn
$term = $journal->getEissn(); // eissn MUST be dynamic $journal->getEissn() or 2287-187X to test
$occurrences = substr_count($term, "-");
if($occurrences==1) {
$doaj_api = 'https://doaj.org/api/search/articles/'.$term.'?sort=last_updated:desc';
try {
$json_data = file_get_contents($doaj_api);
$response_data = json_decode($json_data);
if(isset($response_data->results)) {
$articlesDoaj = $response_data->results;
foreach ($articlesDoaj as $article) {
if (in_array($term, $article->bibjson->journal->issns)) {
$articles[] = $article;
}
}
}
} catch (\Exception $e) {}
}
// journal comments not deleted
$comments = $em->getRepository(Comment::class)->findBy(['journal'=>$journal,'deleted_at'=>null],['created_at'=>'DESC']);
return $this->render('journal/show.html.twig', [
'journal' => $journal,
'articles' => $articles,
'samePublisher' => $samePublisher,
'sameCategory' => $sameAreas,
'sameQuartile' => $sameQuartile,
'similarTitle' => $similatTitle,
'category' => $category,
'publisher'=> $publisher,
'journalCategories' => $journalCategories,
'comments' => $comments,
]);
}
/////////////////////////////////
/**
* @Route("API/journal/{id}", name="journal_show_api", methods={"GET"})
*/
public function showJournalAPI($id, EntityManagerInterface $em, SerializerInterface $serializer)
{
// Retrieve the journal from the database using the EntityManager
$journal = $em->getRepository(Journal::class)->getJournalById($id);
// If the journal is not found, redirect to a specific API route
if (!$journal) {
return $this->redirectToRoute('data_search_by_several_values', ['searchTerm' => 'Journal', 'searchValue' => $id]);
}
$publisher = $journal['publisher'];
// Prepare the data to be serialized
$data = [
'journal' => $journal,
'category' => $journal['category'],
'publisher' => $publisher,
];
// Serialize the data to JSON
$json = $serializer->serialize($data, 'json');
return new JsonResponse(json_decode($json));
}
/**
* @Route("/contribute-journal", name="contribute-journal")
*/
public function contributeJournal(Request $request,EntityManagerInterface $em){
$ok=true;
$text="";
$data=$request->get('data');
if($data) {
foreach ($data as $key => &$value) {
$value = str_replace(["\t", "\n", "\r", "&"], '', $value);
}
$data = array_map('trim', $data);
$idJournal=$request->get('journal');
$user=$this->getUser();
/** @var Journal $journal */
$journal = $em->getRepository(Journal::class)->find($idJournal);
/*if((isset($data['sjr']) && !is_numeric($data['sjr'])) || (isset($data['rank']) && !is_numeric($data['rank'])) || (isset($data['citationCount']) && !is_numeric($data['citationCount']))){
$ok=false;
$text="- SJR , Rank , Citation Count must be set to a numeric value <br> ";
}*/
if(isset($data['quartile'])){
$cases=["quartile 1", "quartile 2", "quartile 3", "quartile 4", "q1", "q2", "q3", "q4"];
$casesUp = array_map('ucfirst', $cases);
if(!in_array(strtolower($data['quartile']),$cases)){
$ok=false;
$text=$text."- The Quartile must have one of these values : ".implode(" , ", $casesUp).".";
}
}
if(isset($data['openAccess'])){
$cases=["yes", "no"];
$casesUp = array_map('ucfirst', $cases);
if(!in_array(strtolower($data['openAccess']),$cases)){
$ok=false;
$text=$text."- Open Access must have one of these values : ".implode(" , ", $casesUp).".";
}
}
if($ok){
$text="Thank you for your contribution, the admins will receive the data and check ";
$body = $this->renderView('mails/contribution.html.twig', array('journal' => $journal,'user'=>$user,'data'=>$data));
$message = (new \Swift_Message())
->setSubject("Request for contribution for the journal ".$journal->getTitle())
->setFrom([$user->getUsername()])
// ->setTo(($user->getUsername()))
->setTo(trim("researchguiderg@gmail.com"))
->setBody($body, 'text/html');
$this->mailer->send($message);
$contribution=new Contribution();
$contribution->setJournal($journal);
$contribution->setAuthor($user);
$contribution->setData($data);
$em->persist($contribution);
$em->flush();
}
}
return new JsonResponse([
"ok"=>$ok,
"text"=>$text,
]);
}
/**
* @Route("/report_journal/{journalId}", name="report_journal")
*/
public function report_journal(Request $request,$journalId,EntityManagerInterface $em){
$report=new Report();
/** @var Journal $journal */
$journal=$em->getRepository(Journal::class)->find($journalId);
$report->setJournal($journal);
$report->setReporter($this->getUser());
$report->setReason($request->get('reason'));
$em->persist($report);
$em->flush();
// add flush message
$this->addFlash('success', 'Your report has been sent to the admins, thank you for your contribution');
return $this->redirectToRoute('journal_show', ['id' => $journalId]);
}
/**
* @Route("/deleteComment/{commentId}/{journalId}", name="deleteComment")
*/
public function deleteComment(Request $request,$commentId,$journalId,EntityManagerInterface $em){
/** @var Comment $comment */
$comment=$em->getRepository(Comment::class)->find($commentId);
if($comment->getPublisher()==$this->getUser()){
$comment->setDeletedAt(new \DateTime());
$em->persist($comment);
$em->flush();
}
$this->addFlash('success', 'Your message is deleted');
return $this->redirectToRoute('journal_show', ['id' => $journalId]);
}
/**
* @Route("/latestJournals", name="latestJournals")
*/
public function latest_journals(EntityManagerInterface $em){
$journals = $em->getRepository(Journal::class)->latestJournals();
// randomize the journals order to show different journals each time and get 6 journals
shuffle($journals);
$journals = array_slice($journals, 0, 9);
return $this->render('journal/latest_journals.html.twig', [
'journals' => $journals,
]);
}
}