src/Controller/JournalController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Comment;
  4. use App\Entity\Contribution;
  5. use App\Entity\DataThomsonMeta;
  6. use App\Entity\Journal;
  7. use App\Entity\Report;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. class JournalController extends AbstractController
  16. {
  17.     /** @var \Swift_Mailer */
  18.     private $mailer;
  19.     /**
  20.      * JournalController constructor.
  21.      * @param \Swift_Mailer $mailer
  22.      */
  23.     private $serializer;
  24.     public function __construct(\Swift_Mailer $mailer,SerializerInterface $serializer)
  25.     {
  26.         $this->mailer $mailer;
  27.         $this->serializer $serializer;
  28.     }
  29.     /**
  30.      * @Route("/journal/{id}", name="journal_show", methods={"GET"})
  31.      */
  32.     public function showJournal($id,EntityManagerInterface $em): Response
  33.     {
  34.         $user=$this->getUser();
  35.         if(!$user){
  36.            $this->addFlash('info''You must be logged in to access this page');
  37.             return $this->redirectToRoute('registre_user');
  38.         }
  39.         $journalCategories=[];
  40.         $samePublisher=[];
  41.         $category="";
  42.         $sameAreas=[];
  43.         $sameQuartile=[];
  44.         $articlesDoaj=[];
  45.         $articles=[];
  46.         /** @var Journal $journal */
  47.         $journal $em->getRepository(Journal::class)->find($id);
  48.         if(!$journal){
  49.           return $this->redirectToRoute('data_search_by_several_values', ['searchTerm' => 'Journal''searchValue' => $id]);
  50.         }
  51.         $publisher=$journal->getPublisher();
  52.         if($publisher) {
  53.             $samePublisher $em->getRepository(Journal::class)->samePublisher($journal);
  54.         }
  55.         $area=$journal->getArea();
  56.         if(str_contains($area,';')){
  57.             $areas explode(";"$area);
  58.             $areas array_map('trim'$areas);
  59. //            $sameAreas = $em->getRepository(Journal::class)->sameArea($journal,$areas);
  60.             $sameAreas $em->getRepository(Journal::class)->sameCategoryName($journal,$journal->getCategoryName());
  61.         }
  62.         // check if the journal has a quartile and different from '%-%' with str_contains
  63.         if($journal->getQuartile() && !str_contains($journal->getQuartile(),'-')){
  64.             $sameQuartile $em->getRepository(Journal::class)->sameQuartile($journal);
  65.         }
  66.         $similatTitle $em->getRepository(Journal::class)->similarTitle($journal);
  67.         // get articles from doaj api Based on eissn
  68.         $term $journal->getEissn(); // eissn MUST be dynamic $journal->getEissn() or 2287-187X to test
  69.         $occurrences substr_count($term"-");
  70.        if($occurrences==1) {
  71.            $doaj_api 'https://doaj.org/api/search/articles/'.$term.'?sort=last_updated:desc';
  72.            try {
  73.                $json_data file_get_contents($doaj_api);
  74.                $response_data json_decode($json_data);
  75.                if(isset($response_data->results)) {
  76.                    $articlesDoaj $response_data->results;
  77.                    foreach ($articlesDoaj as $article) {
  78.                        if (in_array($term$article->bibjson->journal->issns)) {
  79.                            $articles[] = $article;
  80.                        }
  81.                    }
  82.                }
  83.            } catch (\Exception $e) {}
  84.        }
  85.        // journal comments not deleted
  86.         $comments $em->getRepository(Comment::class)->findBy(['journal'=>$journal,'deleted_at'=>null],['created_at'=>'DESC']);
  87.         return $this->render('refonte/journal/show.html.twig', [
  88.             'journal' => $journal,
  89.             'articles' => $articles,
  90.             'samePublisher' => $samePublisher,
  91.             'sameCategory' => $sameAreas,
  92.             'sameQuartile' => $sameQuartile,
  93.             'similarTitle' => $similatTitle,
  94.             'category' => $category,
  95.             'publisher'=> $publisher,
  96.             'journalCategories' => $journalCategories,
  97.             'comments' => $comments,
  98.         ]);
  99.     }
  100.      /////////////////////////////////
  101. /**
  102.  * @Route("API/journal/{id}", name="journal_show_api", methods={"GET"})
  103.  */
  104.     public function showJournalAPI($idEntityManagerInterface $emSerializerInterface $serializer)
  105.     {
  106.         // Retrieve the journal from the database using the EntityManager
  107.         $journal $em->getRepository(Journal::class)->getJournalById($id);
  108.         // If the journal is not found, redirect to a specific API route
  109.         if (!$journal) {
  110.             return $this->redirectToRoute('data_search_by_several_values', ['searchTerm' => 'Journal''searchValue' => $id]);
  111.         }
  112.         $publisher $journal['publisher'];
  113.         // Prepare the data to be serialized
  114.         $data = [
  115.             'journal' => $journal,
  116.             'category' => $journal['category'],
  117.             'publisher' => $publisher,
  118.         ];
  119.         // Serialize the data to JSON
  120.         $json $serializer->serialize($data'json');
  121.         return new JsonResponse(json_decode($json));
  122.     }
  123.     /**
  124.      * @Route("/contribute-journal", name="contribute-journal")
  125.      */
  126.     public function contributeJournal(Request $request,EntityManagerInterface $em){
  127.         $ok=true;
  128.          $text="";
  129.         $data=$request->get('data');
  130.         if($data) {
  131.             foreach ($data as $key => &$value) {
  132.                 $value str_replace(["\t""\n""\r""&"], ''$value);
  133.             }
  134.         $data array_map('trim'$data);
  135.         $idJournal=$request->get('journal');
  136.         $user=$this->getUser();
  137.         /** @var Journal $journal */
  138.         $journal $em->getRepository(Journal::class)->find($idJournal);
  139.         /*if((isset($data['sjr']) && !is_numeric($data['sjr'])) || (isset($data['rank']) && !is_numeric($data['rank'])) || (isset($data['citationCount']) && !is_numeric($data['citationCount']))){
  140.             $ok=false;
  141.             $text="-  SJR , Rank , Citation Count must be set to a numeric value <br> ";
  142.         }*/
  143.         if(isset($data['quartile'])){
  144.             $cases=["quartile 1""quartile 2""quartile 3""quartile 4""q1""q2""q3""q4"];
  145.             $casesUp array_map('ucfirst'$cases);
  146.             if(!in_array(strtolower($data['quartile']),$cases)){
  147.                 $ok=false;
  148.                 $text=$text."- The Quartile must have one of these values : ".implode(" , "$casesUp).".";
  149.             }
  150.         }
  151.         if(isset($data['openAccess'])){
  152.             $cases=["yes""no"];
  153.             $casesUp array_map('ucfirst'$cases);
  154.             if(!in_array(strtolower($data['openAccess']),$cases)){
  155.                 $ok=false;
  156.                 $text=$text."- Open Access must have one of these values : ".implode(" , "$casesUp).".";
  157.             }
  158.         }
  159.         if($ok){
  160.             $text="Thank you for your contribution, the admins will receive the data and check ";
  161.             $body $this->renderView('mails/contribution.html.twig', array('journal' => $journal,'user'=>$user,'data'=>$data));
  162.             $message = (new \Swift_Message())
  163.                 ->setSubject("Request for contribution for the journal ".$journal->getTitle())
  164.                 ->setFrom([$user->getUsername()])
  165. //                        ->setTo(($user->getUsername()))
  166.                 ->setTo(trim("researchguiderg@gmail.com"))
  167.                 ->setBody($body'text/html');
  168.             $this->mailer->send($message);
  169.             $contribution=new Contribution();
  170.             $contribution->setJournal($journal);
  171.             $contribution->setAuthor($user);
  172.             $contribution->setData($data);
  173.             $em->persist($contribution);
  174.             $em->flush();
  175.         }
  176.         }
  177.         return new JsonResponse([
  178.             "ok"=>$ok,
  179.             "text"=>$text,
  180.         ]);
  181.     }
  182.     /**
  183.      * @Route("/report_journal/{journalId}", name="report_journal")
  184.      */
  185.     public function report_journal(Request $request,$journalId,EntityManagerInterface $em){
  186.         $report=new Report();
  187.         /** @var Journal $journal */
  188.         $journal=$em->getRepository(Journal::class)->find($journalId);
  189.         $report->setJournal($journal);
  190.         $report->setReporter($this->getUser());
  191.         $report->setReason($request->get('reason'));
  192.         $report->setCreatedAt(new \DateTime());
  193.         $em->persist($report);
  194.         $em->flush();
  195.         // add flush message
  196.         $this->addFlash('success''Your report has been sent to the admins, thank you for your contribution');
  197.         return $this->redirectToRoute('journal_show', ['id' => $journalId]);
  198.     }
  199.     /**
  200.      * @Route("/deleteComment/{commentId}/{journalId}", name="deleteComment")
  201.      */
  202.     public function deleteComment(Request $request,$commentId,$journalId,EntityManagerInterface $em){
  203.         /** @var Comment $comment */
  204.         $comment=$em->getRepository(Comment::class)->find($commentId);
  205.          if($comment->getPublisher()==$this->getUser()){
  206.              $comment->setDeletedAt(new \DateTime());
  207.             $em->persist($comment);
  208.             $em->flush();
  209.          }
  210.          $this->addFlash('success''Your comment has been removed successfully');
  211.         return $this->redirectToRoute('journal_show', ['id' => $journalId]);
  212.     }
  213.     /**
  214.      * @Route("/latestJournals", name="latestJournals")
  215.      */
  216.     public function latest_journals(EntityManagerInterface $em){
  217.         $journals $em->getRepository(Journal::class)->latestJournals();
  218.         shuffle($journals);
  219.         $journals array_slice($journals09);
  220.         $eissns=[];
  221.         $articles=[];
  222.         foreach ($journals as $journal){
  223.             $eissns[]=[
  224.               'eissn' => $journal->getEissn(),
  225.                'journal'=> $journal->getId()
  226.             ];
  227.         }
  228.         foreach ($eissns as $term) {
  229.             $occurrences substr_count($term['eissn'], "-");
  230.             if ($occurrences == 1) {
  231.                 $doaj_api 'https://doaj.org/api/search/articles/' $term['eissn'] . '?sort=last_updated:desc';
  232.                 try {
  233.                     $json_data file_get_contents($doaj_api);
  234.                     $response_data json_decode($json_data);
  235.                     if (isset($response_data->results)) {
  236.                         $articlesDoaj $response_data->results;
  237.                         foreach ($articlesDoaj as $article) {
  238.                             if (in_array($term['eissn'], $article->bibjson->journal->issns)) {
  239.                                 $articles[$term['journal']][] = $article;
  240.                             }
  241.                         }
  242.                     }
  243.                 } catch (\Exception $e) {
  244.                 }
  245.             }
  246.         }
  247.          if($articles){
  248.              $articles array_slice($articles09,true);
  249.          }
  250.         return $this->render('journal/latest_journals.html.twig', [
  251.             'journals' => $journals,
  252.             'articles' => $articles,
  253.         ]);
  254.     }
  255.   
  256. }