src/Controller/Website/DefaultController.php line 150

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.                 // 3 items in array authors
  118.                 $authors=array_slice($authors,0,6);
  119.                 $conferences=array_slice($conferences,0,3);
  120.                 $posts=array_slice($posts,0,3);
  121.                 return $this->render('refonte/dashboard/studentphd.html.twig',[
  122.                     'posts'=>$posts,
  123.                     'conferences' => $conferences,
  124.                     'journals' => $journalsDashboard,
  125.                     'authorList'=>$authors,
  126.                     "user"=>$this->getUser()
  127.                 ]);
  128.             }
  129.         }
  130.         return $this->render('refonte/pages/home.html.twig',[
  131.             'posts'=>$posts,
  132.             'conferences' => $conferences,
  133.             'journals' => count($journals),
  134.             'countCountries' => count($countCountries),
  135.             'countPublishers' => count($countPublishers),
  136.             'journalsByCategory' => $journalsByCategory,
  137.             'authors'=>$authors,
  138.             'noAuthorsFound'=>$noAuthorsFound
  139.         ]);
  140.     }
  141.     /**
  142.      * @Route("/myDashboard", name="student_dashboard")
  143.      */
  144.     public function getStudentPhdDashboard(){
  145.         return $this->render('refonte/dashboard/studentphd.html.twig');
  146.     }
  147.     /**
  148.      * @Route("/view-categories", name="check_categories")
  149.      */
  150.     public function checkCategories(Request $request,PublicationManager $publicationManager,PaginatorInterface $paginator){
  151.         $page $request->get("p"1);
  152.         $perpage 6;
  153.         $journalsByCategory=[];
  154.         $user=$this->getUser();
  155.         $medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
  156.         $science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
  157.         $type="";
  158.         if($user && $user->getCategories()->getValues()){
  159.             $allCategories $user->getCategories()->getValues();
  160.         }else{
  161.             $allCategories $publicationManager->getAllCategories();
  162.         }
  163.         foreach ($allCategories as $category){
  164.             if(is_array($category)){
  165.                 $name=$category['name'];
  166.             }else{
  167.                 $name=$category->getName();
  168.             }
  169.             if(count(array_intersect(explode(" ",$name),$medecine))>0){
  170.                 $type="medecine";
  171.             }elseif (count(array_intersect(explode(" ",$name),$science))>0){
  172.                 $type="science";
  173.             }else{
  174.                 $type="other";
  175.             }
  176.             $journalLIst=$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name);
  177.             if($journalLIst) {
  178.                 $journalsByCategory[$name] =
  179.                     [
  180.                         'journals'=>$journalLIst,
  181.                         'type' => $type
  182.                     ];
  183.             }
  184.         }
  185.         $categories $paginator->paginate($journalsByCategory$page$perpage);
  186.         $total count($journalsByCategory);
  187.         $pages intval(ceil($total $perpage));
  188.         return $this->render('refonte/categories/index.html.twig',[
  189.             'journalsByCategory'=>$categories,
  190.             'page'=>$page,
  191.             'pages'=>$pages,
  192.             'total'=>$total
  193.         ]);
  194.     }
  195.     /**
  196.      * @Route("/contact-us", name="contact-us")
  197.      */
  198.     public function contactUsAction(Request $request,\Swift_Mailer $mailer){
  199.         $contact=new ContactForm();
  200.         $form=$this->createForm(ContactFormType::class,$contact);
  201.         $form->handleRequest($request);
  202.         if($form->isSubmitted() && $form->isValid()){
  203.             $contact=$form->getData();
  204.             $recaptcha=$request->get('g-recaptcha-response');
  205.             if($recaptcha){
  206.                 $em=$this->getDoctrine()->getManager();
  207.                 $em->persist($contact);
  208.                 $em->flush();
  209.                 $admin=$this->getParameter('admin_email');
  210.                 // Send email to the admin
  211.                 $message = (new \Swift_Message('New Contact Form Submission'))
  212.                     ->setFrom($contact->getEmail())
  213.                     ->setTo($admin)
  214.                     ->setBody(
  215.                         $this->renderView(
  216.                             'email/contact.html.twig',
  217.                             ['contact' => $contact]
  218.                         ),
  219.                         'text/html'
  220.                     );
  221.                 $mailer->send($message);
  222.                 $this->addFlash('success','Your message has been sent successfully');
  223.                 return $this->redirectToRoute('homepage');
  224.                 }
  225.         }
  226.         return $this->render('addjournal/journal.html.twig',[
  227.             'form'=>$form->createView()
  228.         ]);
  229.     }
  230.     /**
  231.      * @Route("/privacy-policy", name="privacy-policy")
  232.      */
  233.     public function aboutUsAction()
  234.     {
  235.         return $this->render('refonte/pages/privacy-policy.html.twig');
  236.     }
  237.     /**
  238.      * @Route("/top-journals-list", name="top-journals-list")
  239.      */
  240.     public function toplist()
  241.     {
  242.         return $this->render('refonte/pages/topListJournal.html.twig');
  243.     }
  244.     /**
  245.      * @Route("/view-researchers", name="check_researchers")
  246.      */
  247.     public function checkResearchers(Request $request,PaginatorInterface $paginator,EntityManagerInterface $em)
  248.     {
  249.         ini_set('max_execution_time', -1);
  250.         $page max((int) $request->get("p"1), 1);
  251.         $perpage 9;
  252.         $noAuthorsFound=false;
  253.         $totalAuthors=$em->getRepository(Author::class)->findAll();
  254.         if(!$totalAuthors){
  255.             try{
  256.                 $urlOpenAlex="https://api.openalex.org/authors?per-page=100&page=1";
  257.                 $rawData HttpClient::create()
  258.                     ->request('GET'$urlOpenAlex);
  259.                 if ($rawData->getStatusCode() === 200) {
  260.                     $rawData $rawData->getContent();
  261.                     $data = \json_decode($rawData);
  262.                     if($data){
  263.                         $data=$data->results;
  264.                         foreach ($data as $author){
  265.                             $authorExists=$this->getDoctrine()->getRepository(Author::class)->findOneBy(['name'=>$author->display_name]);
  266.                             if(!$authorExists){
  267.                                 $authorEntity=new Author();
  268.                                 $authorEntity->setName($author->display_name);
  269.                                 if($author->display_name_alternatives && count($author->display_name_alternatives)>0)
  270.                                 {
  271.                                     $authorEntity->setAlternativeName($author->display_name_alternatives[0]);
  272.                                 }
  273.                                 $authorEntity->setAffiliation($author->affiliations);
  274.                                 $authorEntity->setLastKnownInstitutions($author->last_known_institutions);
  275.                                 $authorEntity->setTopics($author->topics);
  276.                                 $topics=$author->topics;
  277.                                 foreach ($topics as $topic){
  278.                                     if(isset($topic->field)){
  279.                                         $fieldsName[]=$topic->field->display_name;
  280.                                     }
  281.                                 }
  282.                                 $fieldsName=array_unique($fieldsName);
  283.                                 $authorEntity->setFields($fieldsName);
  284.                                 $authorEntity->setPublications($author->works_api_url);
  285.                                 $em->persist($authorEntity);
  286.                             }
  287.                             $em->flush();
  288.                         }
  289.                         $totalAuthors=$em->getRepository(Author::class)->findAll();
  290.                     }
  291.                 }
  292.             }  catch (TransportExceptionInterface $e){
  293.             }
  294.         }
  295.         if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
  296.             $user $this->getUser();
  297.             $categories $user->getCategories()->getValues();
  298.             $categoriesNames = [];
  299.             foreach ($categories as $category) {
  300.                 $categoriesNames[] = $category->getName();
  301.             }
  302.             $query=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
  303.              if(!$query){
  304.                  $query=$totalAuthors;
  305.                  $noAuthorsFound=true;
  306.              }
  307.         }else{
  308.             $query $em->getRepository(Author::class)->getAllAuthors();
  309.         }
  310.         $authors $paginator->paginate($query$page$perpage);
  311.         $total $authors->getTotalItemCount();
  312.         $pages = (int) ceil($total $perpage);
  313.         return $this->render('refonte/researchers/view-researchers.html.twig',
  314.             [
  315.                 'authors'=>$authors,
  316.                 'total'=>$total,
  317.                 'page'=>$page,
  318.                 'pages'=>$pages
  319.             ]);
  320.     }
  321.     function isImageAccessible($url) {
  322.         $ch curl_init($url);
  323.         curl_setopt($chCURLOPT_NOBODYtrue); // No body needed, just headers
  324.         curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue); // Follow redirects
  325.         curl_setopt($chCURLOPT_TIMEOUT5); // Set timeout
  326.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse); // Disable SSL verification (for testing)
  327.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  328.         curl_exec($ch);
  329.         $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  330.         curl_close($ch);
  331.         return ($httpCode === 200);
  332.     }
  333. }