<?php
namespace App\Controller\Website;
use App\Entity\Author;
use App\Entity\Category;
use App\Entity\Conference;
use App\Entity\ContactForm;
use App\Entity\FutureConference;
use App\Entity\Journal;
use App\Entity\Post;
use App\Form\ContactFormType;
use App\Manager\PublicationManager;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Monolog\Handler\MailHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(PublicationManager $publicationManager,EntityManagerInterface $em)
{
$conferences = $this->getDoctrine()->getRepository(FutureConference::class)->findAll();
$posts=$this->getDoctrine()->getRepository(Post::class)->findAll();
$journals = $this->getDoctrine()->getRepository(Journal::class)->findAll();
$countCountries = $this->getDoctrine()->getRepository(Journal::class)->countCountries();
$countPublishers = $this->getDoctrine()->getRepository(Journal::class)->countPublishers();
$medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
$science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
$type="";
$authors=[];
if($this->getUser() && $this->getUser()->getCategories()->getValues()){
$user = $this->getUser();
$categories = $user->getCategories()->getValues();
}else{
$categories = $publicationManager->getAllCategories(30);
shuffle($categories);
$categories=array_slice($categories,0,10);
}
$journalsByCategory=[];
$noAuthorsFound=false;
foreach ($categories as $category){
// check if the category is an object or an array
if(is_array($category)){
$name=$category['name'];
}else{
$name=$category->getName();
}
// check if the category name contains at least one word from the medecine array
if(count(array_intersect(explode(" ",$name),$medecine))>0){
$type="medecine";
}elseif (count(array_intersect(explode(" ",$name),$science))>0){
$type="science";
}else{
$type="other";
}
$journalLIst=$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name);
if($journalLIst){
$journalsByCategory[$name]=
[
'journals'=>$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name),
'type'=>$type
];
}
}
// get 10 random posts
shuffle($conferences);
$conferences=array_slice($conferences,0,10);
/** @var Conference $conference */
foreach ($conferences as $conference){
// check if an image URL is valid or not
if($conference->getImage() && !$this->isImageAccessible($conference->getImage())){
$conference->setImage(null);
$this->getDoctrine()->getManager()->persist($conference);
}
}
$this->getDoctrine()->getManager()->flush();
$userWithoutSuggestions=$this->getDoctrine()->getRepository(Author::class)->findAll();
/// add for user connected custmise the categories
if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
$user = $this->getUser();
$categories = $user->getCategories()->getValues();
$categoriesNames = [];
foreach ($categories as $category) {
$categoriesNames[] = $category->getName();
}
$authors=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
if(!$authors){
$authors=$userWithoutSuggestions;
$noAuthorsFound=true;
}
}else{
$authors=$userWithoutSuggestions;
shuffle($authors);
$authors=array_slice($authors,0,10);
}
if($this->getUser() && $this->getUser()->getRole()) {
$roles=['student_phd','researcher'];
$journalsDashboard=[];
foreach ($journalsByCategory as $cat=>$journal) {
foreach ($journal['journals'] as $j) {
if (count($journalsDashboard) < 6) {
$journalsDashboard[] = ['journal'=>$j,"category"=>$cat];
}
}
}
if(in_array($this->getUser()->getRole(),$roles)){
// 3 items in array authors
$authors=array_slice($authors,0,6);
$conferences=array_slice($conferences,0,3);
$posts=array_slice($posts,0,3);
return $this->render('refonte/dashboard/studentphd.html.twig',[
'posts'=>$posts,
'conferences' => $conferences,
'journals' => $journalsDashboard,
'authorList'=>$authors,
"user"=>$this->getUser()
]);
}
}
return $this->render('refonte/pages/home.html.twig',[
'posts'=>$posts,
'conferences' => $conferences,
'journals' => count($journals),
'countCountries' => count($countCountries),
'countPublishers' => count($countPublishers),
'journalsByCategory' => $journalsByCategory,
'authors'=>$authors,
'noAuthorsFound'=>$noAuthorsFound
]);
}
/**
* @Route("/myDashboard", name="student_dashboard")
*/
public function getStudentPhdDashboard(){
return $this->render('refonte/dashboard/studentphd.html.twig');
}
/**
* @Route("/view-categories", name="check_categories")
*/
public function checkCategories(Request $request,PublicationManager $publicationManager,PaginatorInterface $paginator){
$page = $request->get("p", 1);
$perpage = 6;
$journalsByCategory=[];
$user=$this->getUser();
$medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
$science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
$type="";
if($user && $user->getCategories()->getValues()){
$allCategories = $user->getCategories()->getValues();
}else{
$allCategories = $publicationManager->getAllCategories();
}
foreach ($allCategories as $category){
if(is_array($category)){
$name=$category['name'];
}else{
$name=$category->getName();
}
if(count(array_intersect(explode(" ",$name),$medecine))>0){
$type="medecine";
}elseif (count(array_intersect(explode(" ",$name),$science))>0){
$type="science";
}else{
$type="other";
}
$journalsByCategory[$name]=
[
'journals'=>$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name),
'type'=>$type
]; }
$categories = $paginator->paginate($journalsByCategory, $page, $perpage);
$total = count($journalsByCategory);
$pages = intval(ceil($total / $perpage));
return $this->render('refonte/categories/index.html.twig',[
'journalsByCategory'=>$categories,
'page'=>$page,
'pages'=>$pages,
'total'=>$total
]);
}
/**
* @Route("/contact-us", name="contact-us")
*/
public function contactUsAction(Request $request,\Swift_Mailer $mailer){
$contact=new ContactForm();
$form=$this->createForm(ContactFormType::class,$contact);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$contact=$form->getData();
$recaptcha=$request->get('g-recaptcha-response');
if($recaptcha){
$em=$this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$admin=$this->getParameter('admin_email');
// Send email to the admin
$message = (new \Swift_Message('New Contact Form Submission'))
->setFrom($contact->getEmail())
->setTo($admin)
->setBody(
$this->renderView(
'email/contact.html.twig',
['contact' => $contact]
),
'text/html'
);
$mailer->send($message);
$this->addFlash('success','Your message has been sent successfully');
return $this->redirectToRoute('homepage');
}
}
return $this->render('addjournal/journal.html.twig',[
'form'=>$form->createView()
]);
}
/**
* @Route("/privacy-policy", name="privacy-policy")
*/
public function aboutUsAction()
{
return $this->render('refonte/pages/privacy-policy.html.twig');
}
/**
* @Route("/top-journals-list", name="top-journals-list")
*/
public function toplist()
{
return $this->render('refonte/pages/topListJournal.html.twig');
}
/**
* @Route("/view-researchers", name="check_researchers")
*/
public function checkResearchers(Request $request,PaginatorInterface $paginator,EntityManagerInterface $em)
{
ini_set('max_execution_time', -1);
$page = max((int) $request->get("p", 1), 1);
$perpage = 9;
$totalAuthors=$em->getRepository(Author::class)->findAll();
if(!$totalAuthors){
try{
$urlOpenAlex="https://api.openalex.org/authors?per-page=100&page=1";
$rawData = HttpClient::create()
->request('GET', $urlOpenAlex);
if ($rawData->getStatusCode() === 200) {
$rawData = $rawData->getContent();
$data = \json_decode($rawData);
if($data){
$data=$data->results;
foreach ($data as $author){
$authorExists=$this->getDoctrine()->getRepository(Author::class)->findOneBy(['name'=>$author->display_name]);
if(!$authorExists){
$authorEntity=new Author();
$authorEntity->setName($author->display_name);
if($author->display_name_alternatives && count($author->display_name_alternatives)>0)
{
$authorEntity->setAlternativeName($author->display_name_alternatives[0]);
}
$authorEntity->setAffiliation($author->affiliations);
$authorEntity->setLastKnownInstitutions($author->last_known_institutions);
$authorEntity->setTopics($author->topics);
$topics=$author->topics;
foreach ($topics as $topic){
if(isset($topic->field)){
$fieldsName[]=$topic->field->display_name;
}
}
$fieldsName=array_unique($fieldsName);
$authorEntity->setFields($fieldsName);
$authorEntity->setPublications($author->works_api_url);
$em->persist($authorEntity);
}
$em->flush();
}
$totalAuthors=$em->getRepository(Author::class)->findAll();
}
}
} catch (TransportExceptionInterface $e){
}
}
if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
$user = $this->getUser();
$categories = $user->getCategories()->getValues();
$categoriesNames = [];
foreach ($categories as $category) {
$categoriesNames[] = $category->getName();
}
$query=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
}else{
$query = $em->getRepository(Author::class)->getAllAuthors();
}
$authors = $paginator->paginate($query, $page, $perpage);
$total = $authors->getTotalItemCount();
$pages = (int) ceil($total / $perpage);
return $this->render('refonte/researchers/view-researchers.html.twig',
[
'authors'=>$authors,
'total'=>$total,
'page'=>$page,
'pages'=>$pages
]);
}
function isImageAccessible($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true); // No body needed, just headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Set timeout
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification (for testing)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpCode === 200);
}
}