src/Controller/Website/DefaultController.php line 101

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\ContactForm;
  6. use App\Entity\FutureConference;
  7. use App\Entity\Journal;
  8. use App\Entity\Post;
  9. use App\Form\ContactFormType;
  10. use App\Manager\PublicationManager;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Monolog\Handler\MailHandler;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpClient\HttpClient;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  20. class DefaultController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/", name="homepage")
  24.      */
  25.     public function indexAction(PublicationManager $publicationManager,EntityManagerInterface $em)
  26.     {
  27.         $conferences $this->getDoctrine()->getRepository(FutureConference::class)->findAll();
  28.         $posts=$this->getDoctrine()->getRepository(Post::class)->findAll();
  29.         $journals $this->getDoctrine()->getRepository(Journal::class)->findAll();
  30.         $countCountries $this->getDoctrine()->getRepository(Journal::class)->countCountries();
  31.         $countPublishers $this->getDoctrine()->getRepository(Journal::class)->countPublishers();
  32.         $medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
  33.         $science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
  34.         $type="";
  35.         $authors=[];
  36.         if($this->getUser() && $this->getUser()->getCategories()->getValues()){
  37.             $user $this->getUser();
  38.             $categories $user->getCategories()->getValues();
  39.         }else{
  40.             $categories $publicationManager->getAllCategories(30);
  41.             shuffle($categories);
  42.             $categories=array_slice($categories,0,10);
  43.         }
  44.         $journalsByCategory=[];
  45.         foreach ($categories as $category){
  46.             // check if the category is an object or an array
  47.             if(is_array($category)){
  48.                 $name=$category['name'];
  49.             }else{
  50.                 $name=$category->getName();
  51.             }
  52.             // check if the category name contains at least one word from the medecine array
  53.             if(count(array_intersect(explode(" ",$name),$medecine))>0){
  54.                $type="medecine";
  55.             }elseif (count(array_intersect(explode(" ",$name),$science))>0){
  56.                 $type="science";
  57.             }else{
  58.                 $type="other";
  59.             }
  60.             $journalsByCategory[$name]=
  61.                 [
  62.                     'journals'=>$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name),
  63.                     'type'=>$type
  64.                 ];
  65.         }
  66.         // get 10 random posts
  67.         shuffle($conferences);
  68.         $conferences=array_slice($conferences,0,10);
  69.         $userWithoutSuggestions=[];
  70.         /// add for user connected custmise the categories
  71.         if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
  72.             $user $this->getUser();
  73.             $categories $user->getCategories()->getValues();
  74.             $categoriesNames = [];
  75.             foreach ($categories as $category) {
  76.                 $categoriesNames[] = $category->getName();
  77.             }
  78.             $authors=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
  79.         }else{
  80.             $authors=$this->getDoctrine()->getRepository(Author::class)->findAll();
  81.             shuffle($authors);
  82.             $authors=array_slice($authors,0,10);
  83.             $userWithoutSuggestions=$authors;
  84.         }
  85.            if($this->getUser() && !$authors) {
  86.                $authors $userWithoutSuggestions;
  87.             }
  88.         return $this->render('refonte/pages/home.html.twig',[
  89.             'posts'=>$posts,
  90.             'conferences' => $conferences,
  91.             'journals' => count($journals),
  92.             'countCountries' => count($countCountries),
  93.             'countPublishers' => count($countPublishers),
  94.             'journalsByCategory' => $journalsByCategory,
  95.             'authors'=>$authors
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/view-categories", name="check_categories")
  100.      */
  101.     public function checkCategories(Request $request,PublicationManager $publicationManager,PaginatorInterface $paginator){
  102.         $page $request->get("p"1);
  103.         $perpage 6;
  104.         $journalsByCategory=[];
  105.         $user=$this->getUser();
  106.         $medecine=["Medicine","Biology","Health","Nursing","Medical","Psychology","Public"];
  107.         $science= ["Science","Engineering","Physics","Chemistry","Mathematics","Technology","Neuroscience","Environmental","Studies"];
  108.         $type="";
  109.         if($user && $user->getCategories()->getValues()){
  110.             $allCategories $user->getCategories()->getValues();
  111.         }else{
  112.             $allCategories $publicationManager->getAllCategories();
  113.         }
  114.         foreach ($allCategories as $category){
  115.             if(is_array($category)){
  116.                 $name=$category['name'];
  117.             }else{
  118.                 $name=$category->getName();
  119.             }
  120.             if(count(array_intersect(explode(" ",$name),$medecine))>0){
  121.                 $type="medecine";
  122.             }elseif (count(array_intersect(explode(" ",$name),$science))>0){
  123.                 $type="science";
  124.             }else{
  125.                 $type="other";
  126.             }
  127.             $journalsByCategory[$name]=
  128.                 [
  129.                     'journals'=>$this->getDoctrine()->getRepository(Journal::class)->getJournalByCategory($name),
  130.                     'type'=>$type
  131.                 ];        }
  132.         $categories $paginator->paginate($journalsByCategory$page$perpage);
  133.         $total count($journalsByCategory);
  134.         $pages intval(ceil($total $perpage));
  135.         return $this->render('refonte/categories/index.html.twig',[
  136.             'journalsByCategory'=>$categories,
  137.             'page'=>$page,
  138.             'pages'=>$pages,
  139.             'total'=>$total
  140.         ]);
  141.     }
  142.     /**
  143.      * @Route("/contact-us", name="contact-us")
  144.      */
  145.     public function contactUsAction(Request $request,\Swift_Mailer $mailer){
  146.         $contact=new ContactForm();
  147.         $form=$this->createForm(ContactFormType::class,$contact);
  148.         $form->handleRequest($request);
  149.         if($form->isSubmitted() && $form->isValid()){
  150.             $contact=$form->getData();
  151.             $recaptcha=$request->get('g-recaptcha-response');
  152.             if($recaptcha){
  153.                 $em=$this->getDoctrine()->getManager();
  154.                 $em->persist($contact);
  155.                 $em->flush();
  156.                 $admin=$this->getParameter('admin_email');
  157.                 // Send email to the admin
  158.                 $message = (new \Swift_Message('New Contact Form Submission'))
  159.                     ->setFrom($contact->getEmail())
  160.                     ->setTo($admin)
  161.                     ->setBody(
  162.                         $this->renderView(
  163.                             'email/contact.html.twig',
  164.                             ['contact' => $contact]
  165.                         ),
  166.                         'text/html'
  167.                     );
  168.                 $mailer->send($message);
  169.                 $this->addFlash('success','Your message has been sent successfully');
  170.                 return $this->redirectToRoute('homepage');
  171.                 }
  172.         }
  173.         return $this->render('addjournal/journal.html.twig',[
  174.             'form'=>$form->createView()
  175.         ]);
  176.     }
  177.     /**
  178.      * @Route("/privacy-policy", name="privacy-policy")
  179.      */
  180.     public function aboutUsAction()
  181.     {
  182.         return $this->render('refonte/pages/privacy-policy.html.twig');
  183.     }
  184.     /**
  185.      * @Route("/top-journals-list", name="top-journals-list")
  186.      */
  187.     public function toplist()
  188.     {
  189.         return $this->render('refonte/pages/topListJournal.html.twig');
  190.     }
  191.     /**
  192.      * @Route("/view-researchers", name="check_researchers")
  193.      */
  194.     public function checkResearchers(Request $request,PaginatorInterface $paginator,EntityManagerInterface $em)
  195.     {
  196.         ini_set('max_execution_time', -1);
  197.         $page max((int) $request->get("p"1), 1);
  198.         $perpage 9;
  199.         $totalAuthors=$em->getRepository(Author::class)->findAll();
  200.         if(!$totalAuthors){
  201.             try{
  202.                 $urlOpenAlex="https://api.openalex.org/authors?per-page=100&page=1";
  203.                 $rawData HttpClient::create()
  204.                     ->request('GET'$urlOpenAlex);
  205.                 if ($rawData->getStatusCode() === 200) {
  206.                     $rawData $rawData->getContent();
  207.                     $data = \json_decode($rawData);
  208.                     if($data){
  209.                         $data=$data->results;
  210.                         foreach ($data as $author){
  211.                             $authorExists=$this->getDoctrine()->getRepository(Author::class)->findOneBy(['name'=>$author->display_name]);
  212.                             if(!$authorExists){
  213.                                 $authorEntity=new Author();
  214.                                 $authorEntity->setName($author->display_name);
  215.                                 if($author->display_name_alternatives && count($author->display_name_alternatives)>0)
  216.                                 {
  217.                                     $authorEntity->setAlternativeName($author->display_name_alternatives[0]);
  218.                                 }
  219.                                 $authorEntity->setAffiliation($author->affiliations);
  220.                                 $authorEntity->setLastKnownInstitutions($author->last_known_institutions);
  221.                                 $authorEntity->setTopics($author->topics);
  222.                                 $topics=$author->topics;
  223.                                 foreach ($topics as $topic){
  224.                                     if(isset($topic->field)){
  225.                                         $fieldsName[]=$topic->field->display_name;
  226.                                     }
  227.                                 }
  228.                                 $fieldsName=array_unique($fieldsName);
  229.                                 $authorEntity->setFields($fieldsName);
  230.                                 $authorEntity->setPublications($author->works_api_url);
  231.                                 $em->persist($authorEntity);
  232.                             }
  233.                             $em->flush();
  234.                         }
  235.                         $totalAuthors=$em->getRepository(Author::class)->findAll();
  236.                     }
  237.                 }
  238.             }  catch (TransportExceptionInterface $e){
  239.             }
  240.         }
  241.         if($this->getUser() && $this->getUser()->getCategories()->getValues()) {
  242.             $user $this->getUser();
  243.             $categories $user->getCategories()->getValues();
  244.             $categoriesNames = [];
  245.             foreach ($categories as $category) {
  246.                 $categoriesNames[] = $category->getName();
  247.             }
  248.             $query=$this->getDoctrine()->getRepository(Author::class)->getAuthorsByCategories($categoriesNames);
  249.         }else{
  250.             $query $em->getRepository(Author::class)->getAllAuthors();
  251.         }
  252.         $authors $paginator->paginate($query$page$perpage);
  253.         $total $authors->getTotalItemCount();
  254.         $pages = (int) ceil($total $perpage);
  255.         return $this->render('refonte/researchers/view-researchers.html.twig',
  256.             [
  257.                 'authors'=>$authors,
  258.                 'total'=>$total,
  259.                 'page'=>$page,
  260.                 'pages'=>$pages
  261.             ]);
  262.     }
  263. }