src/Controller/Website/DefaultController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Website;
  3. use App\Entity\Author;
  4. use App\Entity\Category;
  5. use App\Entity\Conference;
  6. use App\Entity\ContactForm;
  7. use App\Entity\FutureConference;
  8. use App\Entity\Journal;
  9. use App\Entity\Post;
  10. use App\Form\ContactFormType;
  11. use App\Manager\PublicationManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Knp\Component\Pager\PaginatorInterface;
  14. use Monolog\Handler\MailHandler;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpClient\HttpClient;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. class DefaultController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/", name="homepage")
  25.      */
  26.     public function indexAction(PublicationManager $publicationManager,EntityManagerInterface $em)
  27.     {
  28.         $conferences $this->getDoctrine()->getRepository(FutureConference::class)->findAll();
  29.         $posts=$this->getDoctrine()->getRepository(Post::class)->findAll();
  30.         $journals $this->getDoctrine()->getRepository(Journal::class)->findAll();
  31.         $countCountries $this->getDoctrine()->getRepository(Journal::class)->countCountries();
  32.         $countPublishers $this->getDoctrine()->getRepository(Journal::class)->countPublishers();
  33.         $medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
  34.         $science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
  35.         $type="";
  36.         $authors=[];
  37.         if($this->getUser() && $this->getUser()->getCategories()->getValues()){
  38.             $user $this->getUser();
  39.             $categories $user->getCategories()->getValues();
  40.         }else{
  41.             $categories $publicationManager->getAllCategories(30);
  42.             shuffle($categories);
  43.             $categories=array_slice($categories,0,10);
  44.         }
  45.         $journalsByCategory=[];
  46.         $noAuthorsFound=false;
  47.         foreach ($categories as $category){
  48.             // check if the category is an object or an array
  49.             if(is_array($category)){
  50.                 $name=$category['name'];
  51.             }else{
  52.                 $name=$category->getName();
  53.             }
  54.             // check if the category name contains at least one word from the medecine array
  55.             if(count(array_intersect(explode(" ",$name),$medecine))>0){
  56.                $type="medecine";
  57.             }elseif (count(array_intersect(explode(" ",$name),$science))>0){
  58.                 $type="science";
  59.             }else{
  60.                 $type="other";
  61.             }
  62.             $journalLIst=$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name);
  63.             if($journalLIst){
  64.                 $journalsByCategory[$name]=
  65.                     [
  66.                         'journals'=>$journalLIst,
  67.                         'type'=>$type
  68.                     ];
  69.             }
  70.         }
  71.         // get 10 random posts
  72.         shuffle($conferences);
  73.         $conferences=array_slice($conferences,0,10);
  74.         /** @var Conference $conference */
  75.         foreach ($conferences as $conference){
  76.             //  check if an image URL is valid or not
  77.             if($conference->getImage() && !$this->isImageAccessible($conference->getImage())){
  78.                 $conference->setImage(null);
  79.                 $this->getDoctrine()->getManager()->persist($conference);
  80.             }
  81.         }
  82.         $this->getDoctrine()->getManager()->flush();
  83.         $userWithoutSuggestions=$this->getDoctrine()->getRepository(Author::class)->findAll();
  84.         /// add for user connected custmise the categories
  85.         if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
  86.             $user $this->getUser();
  87.             $categories $user->getCategories()->getValues();
  88.             $categoriesNames = [];
  89.             foreach ($categories as $category) {
  90.                 $categoriesNames[] = $category->getName();
  91.             }
  92.             $authors=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
  93.             if(!$authors){
  94.                 $authors=$userWithoutSuggestions;
  95.                 $noAuthorsFound=true;
  96.             }
  97.         }else{
  98.             $authors=$userWithoutSuggestions;
  99.             shuffle($authors);
  100.             $authors=array_slice($authors,0,10);
  101.         }
  102.         // posts order by date desc
  103.         usort($posts, function($a$b) {
  104.             return $b->getCreatedAt() <=> $a->getCreatedAt();
  105.         });
  106.         if($this->getUser() && $this->getUser()->getRole()) {
  107.             $roles=['student_phd','researcher'];
  108.             $journalsDashboard=[];
  109.             foreach ($journalsByCategory as $cat=>$journal) {
  110.                 foreach ($journal['journals'] as $j) {
  111.                     if (count($journalsDashboard) < 6) {
  112.                         $journalsDashboard[] = ['journal'=>$j,"category"=>$cat];
  113.                     }
  114.                 }
  115.             }
  116.             if(in_array($this->getUser()->getRole(),$roles)){
  117.                 $funders=[];
  118.                 $country="US";
  119.                 if($user->getCountry()) {
  120.                     $country $user->getCountry();
  121.                 }
  122.                     try {
  123.                         $urlOpenAlex "https://api.openalex.org/funders?filter=country_code:$country&sort=display_name:asc&per-page=10";
  124.                         $rawData HttpClient::create()
  125.                             ->request('GET'$urlOpenAlex);
  126.                         if ($rawData->getStatusCode() === 200) {
  127.                             $rawData $rawData->getContent();
  128.                             $data \json_decode($rawData);
  129.                             if ($data) {
  130.                                 $data $data->results;
  131.                                if(!$data){
  132.                                    $country="US";
  133.                                    $urlOpenAlex "https://api.openalex.org/funders?filter=country_code:US&sort=display_name:asc&per-page=10";
  134.                                    $rawData HttpClient::create()
  135.                                        ->request('GET'$urlOpenAlex);
  136.                                    if ($rawData->getStatusCode() === 200) {
  137.                                        $rawData $rawData->getContent();
  138.                                        $data \json_decode($rawData);
  139.                                        if ($data) {
  140.                                            $data $data->results;
  141.                                        }
  142.                                    }
  143.                                }
  144.                                 foreach ($data as $funder) {
  145.                                     $funders[]=[
  146.                                         'name' => $funder->display_name,
  147.                                         'country' => $funder->country_code,
  148.                                         'description' => $funder->description,
  149.                                         'url' => $funder->homepage_url,
  150.                                         'image'=>$this->isImageAccessible($funder->image_url) ? $funder->image_url null,
  151.                                         'image_thumbnail'=>$this->isImageAccessible($funder->image_thumbnail_url) ? $funder->image_thumbnail_url null,
  152.                                         'grants'=>$funder->grants_count,
  153.                                         'works'=>$funder->works_count,
  154.                                         'cited'=>$funder->cited_by_count,
  155.                                         'hindex'=>$funder->summary_stats->h_index,
  156.                                     ];
  157.                                 }
  158.                                 // shuffle the array and get 3 items
  159.                                 shuffle($funders);
  160.                                 $funders=array_slice($funders,0,3);
  161.                             }
  162.                         }
  163.                     } catch (TransportExceptionInterface $e) {
  164.                     }
  165.                 // 3 items in array authors
  166.                 $authors=array_slice($authors,0,6);
  167.                 $conferences=array_slice($conferences,0,3);
  168.                 $posts=array_slice($posts,0,3);
  169.                 return $this->render('refonte/dashboard/studentphd.html.twig',[
  170.                     'posts'=>$posts,
  171.                     'conferences' => $conferences,
  172.                     'journals' => $journalsDashboard,
  173.                     'authorList'=>$authors,
  174.                     'funders'=>$funders,
  175.                     "user"=>$this->getUser(),
  176.                     'country'=>$country
  177.                 ]);
  178.             }
  179.         }
  180.         return $this->render('refonte/pages/home.html.twig',[
  181.             'posts'=>$posts,
  182.             'conferences' => $conferences,
  183.             'journals' => count($journals),
  184.             'countCountries' => count($countCountries),
  185.             'countPublishers' => count($countPublishers),
  186.             'journalsByCategory' => $journalsByCategory,
  187.             'authors'=>$authors,
  188.             'noAuthorsFound'=>$noAuthorsFound
  189.         ]);
  190.     }
  191.     /**
  192.      * @Route("/funders", name="funders")
  193.      */
  194.     public function funders(Request $request,PaginatorInterface $paginator){
  195.         $user=$this->getUser();
  196.         $roles=['student_phd','researcher'];
  197.         if(!in_array($user->getRole(),$roles)){
  198.             return $this->redirectToRoute('homepage');
  199.         }
  200.         $page $request->get("p"1);
  201.         $perpage 9;
  202.         $funders=[];
  203.         $country="US";
  204.         if($user->getCountry()) {
  205.             $country $user->getCountry();
  206.         }
  207.         try {
  208.             $urlOpenAlex "https://api.openalex.org/funders?filter=country_code:$country&sort=display_name:asc&per-page=100";
  209.             $rawData HttpClient::create()
  210.                 ->request('GET'$urlOpenAlex);
  211.             if ($rawData->getStatusCode() === 200) {
  212.                 $rawData $rawData->getContent();
  213.                 $data \json_decode($rawData);
  214.                 if ($data) {
  215.                     $data $data->results;
  216.                     if(!$data){
  217.                         $country="US";
  218.                         $urlOpenAlex "https://api.openalex.org/funders?filter=country_code:US&sort=display_name:asc&per-page=100";
  219.                         $rawData HttpClient::create()
  220.                             ->request('GET'$urlOpenAlex);
  221.                         if ($rawData->getStatusCode() === 200) {
  222.                             $rawData $rawData->getContent();
  223.                             $data \json_decode($rawData);
  224.                             if ($data) {
  225.                                 $data $data->results;
  226.                             }
  227.                         }
  228.                     }
  229.                     foreach ($data as $funder) {
  230.                         $funders[]=[
  231.                             'name' => $funder->display_name,
  232.                             'country' => $funder->country_code,
  233.                             'description' => $funder->description,
  234.                             'url' => $funder->homepage_url,
  235.                             'image'=>$this->isImageAccessible($funder->image_url) ? $funder->image_url null,
  236.                             'image_thumbnail'=>$this->isImageAccessible($funder->image_thumbnail_url) ? $funder->image_thumbnail_url null,
  237.                             'grants'=>$funder->grants_count,
  238.                             'works'=>$funder->works_count,
  239.                             'cited'=>$funder->cited_by_count,
  240.                             'hindex'=>$funder->summary_stats->h_index,
  241.                         ];
  242.                     }
  243.                 }
  244.             }
  245.         } catch (TransportExceptionInterface $e) {
  246.         }
  247.         $fundersPage $paginator->paginate($funders$page$perpage);
  248.         $total count($funders);
  249.         $pages intval(ceil($total $perpage));
  250.         return $this->render('pages/funders.html.twig',
  251.             [
  252.                 'funders'=>$fundersPage,
  253.                 'page'=>$page,
  254.                 'pages'=>$pages,
  255.                 'total'=>$total,
  256.                 'country'=>$country
  257.             ]);
  258.     }
  259.     /**
  260.      * @Route("/myDashboard", name="student_dashboard")
  261.      */
  262.     public function getStudentPhdDashboard(){
  263.         return $this->render('refonte/dashboard/studentphd.html.twig');
  264.     }
  265.     /**
  266.      * @Route("/view-categories", name="check_categories")
  267.      */
  268.     public function checkCategories(Request $request,PublicationManager $publicationManager,PaginatorInterface $paginator){
  269.         $page $request->get("p"1);
  270.         $perpage 6;
  271.         $journalsByCategory=[];
  272.         $user=$this->getUser();
  273.         $medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
  274.         $science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
  275.         $type="";
  276.         if($user && $user->getCategories()->getValues()){
  277.             $allCategories $user->getCategories()->getValues();
  278.         }else{
  279.             $allCategories $publicationManager->getAllCategories();
  280.         }
  281.         foreach ($allCategories as $category){
  282.             if(is_array($category)){
  283.                 $name=$category['name'];
  284.             }else{
  285.                 $name=$category->getName();
  286.             }
  287.             if(count(array_intersect(explode(" ",$name),$medecine))>0){
  288.                 $type="medecine";
  289.             }elseif (count(array_intersect(explode(" ",$name),$science))>0){
  290.                 $type="science";
  291.             }else{
  292.                 $type="other";
  293.             }
  294.             $journalLIst=$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name);
  295.             if($journalLIst) {
  296.                 $journalsByCategory[$name] =
  297.                     [
  298.                         'journals'=>$journalLIst,
  299.                         'type' => $type
  300.                     ];
  301.             }
  302.         }
  303.         $categories $paginator->paginate($journalsByCategory$page$perpage);
  304.         $total count($journalsByCategory);
  305.         $pages intval(ceil($total $perpage));
  306.         return $this->render('refonte/categories/index.html.twig',[
  307.             'journalsByCategory'=>$categories,
  308.             'page'=>$page,
  309.             'pages'=>$pages,
  310.             'total'=>$total
  311.         ]);
  312.     }
  313.     /**
  314.      * @Route("/contact-us", name="contact-us")
  315.      */
  316.     public function contactUsAction(Request $request,\Swift_Mailer $mailer){
  317.         $contact=new ContactForm();
  318.         $form=$this->createForm(ContactFormType::class,$contact);
  319.         $form->handleRequest($request);
  320.         if($form->isSubmitted() && $form->isValid()){
  321.             $contact=$form->getData();
  322.             $recaptcha=$request->get('g-recaptcha-response');
  323.             if($recaptcha){
  324.                 $em=$this->getDoctrine()->getManager();
  325.                 $em->persist($contact);
  326.                 $em->flush();
  327.                 $admin=$this->getParameter('admin_email');
  328.                 // Send email to the admin
  329.                 $message = (new \Swift_Message('New Contact Form Submission'))
  330.                     ->setFrom($contact->getEmail())
  331.                     ->setTo($admin)
  332.                     ->setBody(
  333.                         $this->renderView(
  334.                             'email/contact.html.twig',
  335.                             ['contact' => $contact]
  336.                         ),
  337.                         'text/html'
  338.                     );
  339.                 $mailer->send($message);
  340.                 $this->addFlash('success','Your message has been sent successfully');
  341.                 return $this->redirectToRoute('homepage');
  342.                 }
  343.         }
  344.         return $this->render('addjournal/journal.html.twig',[
  345.             'form'=>$form->createView()
  346.         ]);
  347.     }
  348.     /**
  349.      * @Route("/privacy-policy", name="privacy-policy")
  350.      */
  351.     public function aboutUsAction()
  352.     {
  353.         return $this->render('refonte/pages/privacy-policy.html.twig');
  354.     }
  355.     /**
  356.      * @Route("/top-journals-list", name="top-journals-list")
  357.      */
  358.     public function toplist()
  359.     {
  360.         return $this->render('refonte/pages/topListJournal.html.twig');
  361.     }
  362.     /**
  363.      * @Route("/view-researchers", name="check_researchers")
  364.      */
  365.     public function checkResearchers(Request $request,PaginatorInterface $paginator,EntityManagerInterface $em)
  366.     {
  367.         ini_set('max_execution_time', -1);
  368.         $page max((int) $request->get("p"1), 1);
  369.         $perpage 9;
  370.         $noAuthorsFound=false;
  371.         $totalAuthors=$em->getRepository(Author::class)->findAll();
  372.         if(!$totalAuthors){
  373.             try{
  374.                 $urlOpenAlex="https://api.openalex.org/authors?per-page=100&page=1";
  375.                 $rawData HttpClient::create()
  376.                     ->request('GET'$urlOpenAlex);
  377.                 if ($rawData->getStatusCode() === 200) {
  378.                     $rawData $rawData->getContent();
  379.                     $data \json_decode($rawData);
  380.                     if($data){
  381.                         $data=$data->results;
  382.                         foreach ($data as $author){
  383.                             $authorExists=$this->getDoctrine()->getRepository(Author::class)->findOneBy(['name'=>$author->display_name]);
  384.                             if(!$authorExists){
  385.                                 $authorEntity=new Author();
  386.                                 $authorEntity->setName($author->display_name);
  387.                                 if($author->display_name_alternatives && count($author->display_name_alternatives)>0)
  388.                                 {
  389.                                     $authorEntity->setAlternativeName($author->display_name_alternatives[0]);
  390.                                 }
  391.                                 $authorEntity->setAffiliation($author->affiliations);
  392.                                 $authorEntity->setLastKnownInstitutions($author->last_known_institutions);
  393.                                 $authorEntity->setTopics($author->topics);
  394.                                 $topics=$author->topics;
  395.                                 foreach ($topics as $topic){
  396.                                     if(isset($topic->field)){
  397.                                         $fieldsName[]=$topic->field->display_name;
  398.                                     }
  399.                                 }
  400.                                 $fieldsName=array_unique($fieldsName);
  401.                                 $authorEntity->setFields($fieldsName);
  402.                                 $authorEntity->setPublications($author->works_api_url);
  403.                                 $em->persist($authorEntity);
  404.                             }
  405.                             $em->flush();
  406.                         }
  407.                         $totalAuthors=$em->getRepository(Author::class)->findAll();
  408.                     }
  409.                 }
  410.             }  catch (TransportExceptionInterface $e){
  411.             }
  412.         }
  413.         if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
  414.             $user $this->getUser();
  415.             $categories $user->getCategories()->getValues();
  416.             $categoriesNames = [];
  417.             foreach ($categories as $category) {
  418.                 $categoriesNames[] = $category->getName();
  419.             }
  420.             $query=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
  421.              if(!$query){
  422.                  $query=$totalAuthors;
  423.                  $noAuthorsFound=true;
  424.              }
  425.         }else{
  426.             $query $em->getRepository(Author::class)->getAllAuthors();
  427.         }
  428.         $authors $paginator->paginate($query$page$perpage);
  429.         $total $authors->getTotalItemCount();
  430.         $pages = (int) ceil($total $perpage);
  431.         return $this->render('refonte/researchers/view-researchers.html.twig',
  432.             [
  433.                 'authors'=>$authors,
  434.                 'total'=>$total,
  435.                 'page'=>$page,
  436.                 'pages'=>$pages
  437.             ]);
  438.     }
  439.     function isImageAccessible($url) {
  440.         $ch curl_init($url);
  441.         curl_setopt($chCURLOPT_NOBODYtrue); // No body needed, just headers
  442.         curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue); // Follow redirects
  443.         curl_setopt($chCURLOPT_TIMEOUT5); // Set timeout
  444.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse); // Disable SSL verification (for testing)
  445.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  446.         curl_exec($ch);
  447.         $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  448.         curl_close($ch);
  449.         return ($httpCode === 200);
  450.     }
  451. }