src/Controller/Website/DefaultController.php line 146

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