src/Controller/JournalController.php line 229

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.         $journalCategories=[];
  35.         $samePublisher=[];
  36.         $category="";
  37.         $sameAreas=[];
  38.         $sameQuartile=[];
  39.         $articlesDoaj=[];
  40.         $articles=[];
  41.         /** @var Journal $journal */
  42.         $journal $em->getRepository(Journal::class)->find($id);
  43.         if(!$journal){
  44.           return $this->redirectToRoute('data_search_by_several_values', ['searchTerm' => 'Journal''searchValue' => $id]);
  45.         }
  46.         $publisher=$journal->getPublisher();
  47.         if($publisher) {
  48.             $samePublisher $em->getRepository(Journal::class)->samePublisher($journal);
  49.         }
  50.         $area=$journal->getArea();
  51.         if(str_contains($area,';')){
  52.             $areas explode(";"$area);
  53.             $areas array_map('trim'$areas);
  54. //            $sameAreas = $em->getRepository(Journal::class)->sameArea($journal,$areas);
  55.             $sameAreas $em->getRepository(Journal::class)->sameCategoryName($journal,$journal->getCategoryName());
  56.         }
  57.         $sameQuartile $em->getRepository(Journal::class)->sameQuartile($journal);
  58.         $similatTitle $em->getRepository(Journal::class)->similarTitle($journal);
  59.         // get articles from doaj api Based on eissn
  60.         $term $journal->getEissn(); // eissn MUST be dynamic $journal->getEissn() or 2287-187X to test
  61.         $occurrences substr_count($term"-");
  62.        if($occurrences==1) {
  63.            $doaj_api 'https://doaj.org/api/search/articles/'.$term.'?sort=last_updated:desc';
  64.            try {
  65.                $json_data file_get_contents($doaj_api);
  66.                $response_data json_decode($json_data);
  67.                if(isset($response_data->results)) {
  68.                    $articlesDoaj $response_data->results;
  69.                    foreach ($articlesDoaj as $article) {
  70.                        if (in_array($term$article->bibjson->journal->issns)) {
  71.                            $articles[] = $article;
  72.                        }
  73.                    }
  74.                }
  75.            } catch (\Exception $e) {}
  76.        }
  77.        // journal comments not deleted
  78.         $comments $em->getRepository(Comment::class)->findBy(['journal'=>$journal,'deleted_at'=>null],['created_at'=>'DESC']);
  79.         return $this->render('journal/show.html.twig', [
  80.             'journal' => $journal,
  81.             'articles' => $articles,
  82.             'samePublisher' => $samePublisher,
  83.             'sameCategory' => $sameAreas,
  84.             'sameQuartile' => $sameQuartile,
  85.             'similarTitle' => $similatTitle,
  86.             'category' => $category,
  87.             'publisher'=> $publisher,
  88.             'journalCategories' => $journalCategories,
  89.             'comments' => $comments,
  90.         ]);
  91.     }
  92.      /////////////////////////////////
  93. /**
  94.  * @Route("API/journal/{id}", name="journal_show_api", methods={"GET"})
  95.  */
  96.     public function showJournalAPI($idEntityManagerInterface $emSerializerInterface $serializer)
  97.     {
  98.         // Retrieve the journal from the database using the EntityManager
  99.         $journal $em->getRepository(Journal::class)->getJournalById($id);
  100.         // If the journal is not found, redirect to a specific API route
  101.         if (!$journal) {
  102.             return $this->redirectToRoute('data_search_by_several_values', ['searchTerm' => 'Journal''searchValue' => $id]);
  103.         }
  104.         $publisher $journal['publisher'];
  105.         // Prepare the data to be serialized
  106.         $data = [
  107.             'journal' => $journal,
  108.             'category' => $journal['category'],
  109.             'publisher' => $publisher,
  110.         ];
  111.         // Serialize the data to JSON
  112.         $json $serializer->serialize($data'json');
  113.         return new JsonResponse(json_decode($json));
  114.     }
  115.     /**
  116.      * @Route("/contribute-journal", name="contribute-journal")
  117.      */
  118.     public function contributeJournal(Request $request,EntityManagerInterface $em){
  119.         $ok=true;
  120.          $text="";
  121.         $data=$request->get('data');
  122.         if($data) {
  123.             foreach ($data as $key => &$value) {
  124.                 $value str_replace(["\t""\n""\r""&"], ''$value);
  125.             }
  126.         $data array_map('trim'$data);
  127.         $idJournal=$request->get('journal');
  128.         $user=$this->getUser();
  129.         /** @var Journal $journal */
  130.         $journal $em->getRepository(Journal::class)->find($idJournal);
  131.         /*if((isset($data['sjr']) && !is_numeric($data['sjr'])) || (isset($data['rank']) && !is_numeric($data['rank'])) || (isset($data['citationCount']) && !is_numeric($data['citationCount']))){
  132.             $ok=false;
  133.             $text="-  SJR , Rank , Citation Count must be set to a numeric value <br> ";
  134.         }*/
  135.         if(isset($data['quartile'])){
  136.             $cases=["quartile 1""quartile 2""quartile 3""quartile 4""q1""q2""q3""q4"];
  137.             $casesUp array_map('ucfirst'$cases);
  138.             if(!in_array(strtolower($data['quartile']),$cases)){
  139.                 $ok=false;
  140.                 $text=$text."- The Quartile must have one of these values : ".implode(" , "$casesUp).".";
  141.             }
  142.         }
  143.         if(isset($data['openAccess'])){
  144.             $cases=["yes""no"];
  145.             $casesUp array_map('ucfirst'$cases);
  146.             if(!in_array(strtolower($data['openAccess']),$cases)){
  147.                 $ok=false;
  148.                 $text=$text."- Open Access must have one of these values : ".implode(" , "$casesUp).".";
  149.             }
  150.         }
  151.         if($ok){
  152.             $text="Thank you for your contribution, the admins will receive the data and check ";
  153.             $body $this->renderView('mails/contribution.html.twig', array('journal' => $journal,'user'=>$user,'data'=>$data));
  154.             $message = (new \Swift_Message())
  155.                 ->setSubject("Request for contribution for the journal ".$journal->getTitle())
  156.                 ->setFrom([$user->getUsername()])
  157. //                        ->setTo(($user->getUsername()))
  158.                 ->setTo(trim("researchguiderg@gmail.com"))
  159.                 ->setBody($body'text/html');
  160.             $this->mailer->send($message);
  161.             $contribution=new Contribution();
  162.             $contribution->setJournal($journal);
  163.             $contribution->setAuthor($user);
  164.             $contribution->setData($data);
  165.             $em->persist($contribution);
  166.             $em->flush();
  167.         }
  168.         }
  169.         return new JsonResponse([
  170.             "ok"=>$ok,
  171.             "text"=>$text,
  172.         ]);
  173.     }
  174.     /**
  175.      * @Route("/report_journal/{journalId}", name="report_journal")
  176.      */
  177.     public function report_journal(Request $request,$journalId,EntityManagerInterface $em){
  178.         $report=new Report();
  179.         /** @var Journal $journal */
  180.         $journal=$em->getRepository(Journal::class)->find($journalId);
  181.         $report->setJournal($journal);
  182.         $report->setReporter($this->getUser());
  183.         $report->setReason($request->get('reason'));
  184.         $em->persist($report);
  185.         $em->flush();
  186.         // add flush message
  187.         $this->addFlash('success''Your report has been sent to the admins, thank you for your contribution');
  188.         return $this->redirectToRoute('journal_show', ['id' => $journalId]);
  189.     }
  190.     /**
  191.      * @Route("/deleteComment/{commentId}/{journalId}", name="deleteComment")
  192.      */
  193.     public function deleteComment(Request $request,$commentId,$journalId,EntityManagerInterface $em){
  194.         /** @var Comment $comment */
  195.         $comment=$em->getRepository(Comment::class)->find($commentId);
  196.          if($comment->getPublisher()==$this->getUser()){
  197.              $comment->setDeletedAt(new \DateTime());
  198.             $em->persist($comment);
  199.             $em->flush();
  200.          }
  201.          $this->addFlash('success''Your message is deleted');
  202.         return $this->redirectToRoute('journal_show', ['id' => $journalId]);
  203.     }
  204.     /**
  205.      * @Route("/latestJournals", name="latestJournals")
  206.      */
  207.     public function latest_journals(EntityManagerInterface $em){
  208.         $journals $em->getRepository(Journal::class)->latestJournals();
  209.         // randomize the journals order to show different journals each time and get 6 journals
  210.         shuffle($journals);
  211.         $journals array_slice($journals09);
  212.         return $this->render('journal/latest_journals.html.twig', [
  213.             'journals' => $journals,
  214.         ]);
  215.     }
  216.   
  217. }