src/Controller/BaseController.php line 604

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Allpublications;
  4. use App\Entity\Category;
  5. use App\Entity\Follow;
  6. use App\Entity\Journal;
  7. use App\Entity\Newsletter;
  8. use App\Entity\User;
  9. use App\Form\AccountType;
  10. use App\Form\AllPublicationCreateType;
  11. use App\Form\AllPublicationType;
  12. use App\Form\DisactivationType;
  13. use App\Form\PasswordChangeType;
  14. use App\Manager\PublicationManager;
  15. use App\Repository\AllPublicationRepository;
  16. use App\Repository\CategoryRepository;
  17. use App\Repository\UserRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use MongoDB\Driver\Manager;
  21. use Psr\Log\LoggerInterface;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. use Symfony\Component\HttpClient\HttpClient;
  25. use Symfony\Component\HttpFoundation\Cookie;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  30. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  31. /**
  32.  * @IsGranted("ROLE_USER")
  33.  * @Route("/{_locale}/profile/conference")
  34.  */
  35. class BaseController extends AbstractController
  36. {
  37.     /** @var EntityManagerInterface */
  38.     private $entityManager;
  39.      /**
  40.      * BaseController constructor.
  41.      * @param LoggerInterface $logger
  42.      */
  43.     /** @var LoggerInterface */
  44.     private $logger;
  45.     private $encoder;
  46.     public function __construct(LoggerInterface $logger,EntityManagerInterface $entityManagerUserPasswordEncoderInterface $encoder)
  47.     {
  48.         $this->logger $logger;
  49.         $this->entityManager $entityManager;
  50.         $this->encoder=$encoder;
  51.     }
  52.     /**
  53.      * @Route("/home", name="homepage__")
  54.      */
  55.     public function indexAction()
  56.     {
  57.         return $this->render('index.html.twig');
  58.     }
  59.     /**
  60.      * @Route("/author_search", name="author_search", methods={"GET"})
  61.      */
  62.     public function author_search(Request $request)
  63.     {
  64.         $emS $this->getDoctrine()->getManager();
  65.         $array_crossref=[];
  66.         $exist $emS->getRepository(Allpublications::class)->findBy(['user' => $this->getUser()]);
  67.         $firstname='';$lastname="";$userFirstName="";
  68.         $manyRequests=false;
  69.         if($this->getUser()){
  70.             $firstname=$this->getUser()->getFirstName();
  71.             $lastname=$this->getUser()->getLastName();
  72.             $userFirstName=$this->getUser()->getFirstName();
  73.         }else{
  74.             return  $this->redirectToRoute('registre_user');
  75.         }
  76.         $cookies $request->cookies;
  77.         $skipSuggestProfile$cookies->get('skip-suggest-profile');
  78.         if ((empty($exist) && $this->getUser()->getFullName()) || ($skipSuggestProfile && $skipSuggestProfile == 'true')) {
  79.             if (str_contains($firstname'.')) {
  80.                 $firstname str_replace('.'""$firstname);
  81.             }  if (str_contains($lastname'.')) {
  82.                 $lastname str_replace('.'""$lastname);
  83.             }
  84.             return $this->redirectToRoute('publication_index',['firstname'=>$firstname,"lastname"=>$lastname,'id'=>$this->getUser()->getId()]);
  85.         } else {
  86.             $auteur $userFirstName;
  87.             $test false;
  88.             if (strpos($request->getUri(), 'authorValue') !== false) {
  89.                 $test true;
  90.             }
  91.             $param urlencode($auteur); // str_replace(' ','+',$auteur);
  92.             $data null;
  93.             try {
  94.                 $rawData HttpClient::create()
  95.                     ->request('GET'"https://api.crossref.org/works?query.author=$param&rows=40");
  96.                 if($rawData->getStatusCode()=== 200) {
  97.                     $rawData $rawData->getContent();
  98.                     $data = \json_decode($rawData);
  99.                     if(isset($data->message->items)) {
  100.                         $articles=$data->message->items;
  101.                         foreach ($articles as $article){
  102.                             $auteurs=$article->author;
  103.                             foreach ($auteurs as $aut){
  104.                                 $family='';
  105.                                 $given='';
  106.                                 $name='';
  107.                                 if(isset($aut->family)){
  108.                                     $family=strtolower($aut->family);
  109.                                 }if (isset($aut->given)){
  110.                                     $given=strtolower($aut->given);
  111.                                 }
  112.                                 if($family && $given && ($family==strtolower($lastname) || $given==strtolower($firstname))){
  113.                                     $name=strtolower($given).' '.strtolower($family);
  114.                                 }elseif ($family && $family==strtolower($firstname) ){
  115.                                     $name=strtolower($family);
  116.                                 }elseif ($given && $given==strtolower($firstname)){
  117.                                     $name=strtolower($given);
  118.                                 }
  119.                                 if($name && !in_array($name,$array_crossref)) $array_crossref[]=$name;
  120.                             }
  121.                         }
  122.                     }
  123.                 } else{
  124.                     return $this->redirectToRoute("publication_index",['firstname'=>$firstname,"lastname"=>$lastname,'id'=>$this->getUser()->getId()]);
  125.                 }
  126.             } catch (TransportExceptionInterface $exception) {
  127.                 $this->addFlash('error''Ooops ! Something wrong happened. Please retry again or contact the support');
  128.                 $this->logger->critical($exception->getTraceAsString());
  129.             }
  130.             // set cookie to skip suggest profile true
  131.             $response = new Response();
  132.             $response->headers->setCookie(new Cookie('skip-suggest-profile''true'));
  133.             return $this->render('refonte/authors/author_search.html.twig', [
  134.                 'data' => $array_crossref
  135.             ]);
  136.         }
  137.     }
  138.     /**
  139.      * @Route("/save_url_social", name="save_url_social")
  140.      */
  141.     public function save_url_social(Request $request)
  142.     {
  143.         /** @var User $user */
  144.         $user=$this->getUser();
  145.      $facebook=$request->get('facebook');
  146.      $twitter=$request->get('twitter');
  147.      $linkeid=$request->get('linkedin');
  148.      $youtube=$request->get('youtube');
  149.      $instagram=$request->get('instagram');
  150.         if($facebook!=""){
  151.             $user->setFacebook($facebook);
  152.         }
  153.         if($twitter!=""){
  154.             $user->setTwitter($twitter);
  155.         }
  156.         if($linkeid!=""){
  157.             $user->setLinkedin($linkeid);
  158.         }
  159.         if($youtube!=""){
  160.             $user->setYoutube($youtube);
  161.         }
  162.         if($instagram!=""){
  163.             $user->setInstagram($instagram);
  164.         }
  165.         if($facebook!="" || $twitter!="" || $linkeid!="" || $youtube!="" || $instagram!=""){
  166.             $em=$this->getDoctrine()->getManager();
  167.             $em->persist($user);
  168.             $em->flush();
  169.             $this->addFlash('success','Your social media links have been successfully saved.');
  170.         }
  171.         return $this->redirectToRoute('author_search');
  172.     }
  173.     /**
  174.      * @Route("/allpublications" ,name="get_author_allpublication_profile",options = { "expose" = true })
  175.      *
  176.      */
  177.     public function callAllpubapi(Request $request,EntityManagerInterface $em){
  178.         $auteur $request->get('auteur');
  179.         $param=str_replace(' ','+',$auteur);
  180.         $data null;
  181.         $data_crossref=null;
  182.         $auteur $request->get('auteur');
  183.         $param  urlencode($auteur); // str_replace(' ','+',$auteur);
  184.         try {
  185.             $rawData HttpClient::create()
  186.                 ->request('GET'"https://api.crossref.org/works?query.author=$param&rows=40")
  187.                 ->getContent()
  188.             ;
  189.             $data = \json_decode($rawData);
  190.             if(isset($data->message->items)) {
  191.                 $articles=$data->message->items;
  192.                 foreach ($articles as $article){
  193.                     $auteurs=$article->author;
  194.                     foreach ($auteurs as $aut){
  195.                         $family='';
  196.                         $given='';
  197.                         $name='';
  198.                         if(isset($aut->family)){
  199.                             $family=strtolower($aut->family);
  200.                         }if (isset($aut->given)){
  201.                             $given=strtolower($aut->given);
  202.                         }
  203.                         if($family && $given ){
  204.                             $name=strtolower($given).' '.strtolower($family);
  205.                         }elseif ($family ){
  206.                             $name=strtolower($family);
  207.                         }elseif ($given ){
  208.                             $name=strtolower($given);
  209.                         }
  210.                     }
  211.                     if($auteur == $name){
  212.                         $data_crossref[]=$article;
  213.                     }
  214.                 }
  215.             }
  216.             if($data_crossref){
  217.                 foreach ($data_crossref as $item){
  218.                       $pub = new Allpublications();
  219.                       $check $em->getRepository(Allpublications::class)->findOneBy(['title' => $item->title[0]]);
  220.                       if(!$check) {
  221.                           $pub->setType($item->type);
  222.                           $pub->setUser($this->getUser());
  223.                           if (isset($item->title) && count($item->title) > 0) {
  224.                               $pub->setTitle($item->title[0]);
  225.                           }
  226.                           if (isset($item->volume)) {
  227.                               $pub->setVolume($item->volume);
  228.                           }
  229.                           if (isset($item->page)) {
  230.                               $pub->setPages($item->page);
  231.                           }
  232.                           if (isset($item->URL)) {
  233.                               $pub->setUrl($item->URL);
  234.                           }
  235.                           if (isset($item->issue)) {
  236.                               $pub->setIssue($item->issue);
  237.                           }
  238.                           if (isset($item->author)) {
  239.                               $authors = [];
  240.                               foreach ($item->author as $a) {
  241.                                   $family '';
  242.                                   $given '';
  243.                                   $name '';
  244.                                   if (isset($a->family)) {
  245.                                       $family strtolower($a->family);
  246.                                   }
  247.                                   if (isset($a->given)) {
  248.                                       $given strtolower($a->given);
  249.                                   }
  250.                                   if ($family && $given) {
  251.                                       $name strtolower($given) . ' ' strtolower($family);
  252.                                   } elseif ($family) {
  253.                                       $name strtolower($family);
  254.                                   } elseif ($given) {
  255.                                       $name strtolower($given);
  256.                                   }
  257.                                   if (!in_array($name$authors)) $authors[] = $name;
  258.                               }
  259.                               if ($authors$pub->setAuthors(implode(","$authors));
  260.                           }
  261.                           if (isset($item->reference)) {
  262.                               $references $item->reference;
  263.                               // parse object references to array
  264.                               $references json_decode(json_encode($references), true);
  265.                               if($references) {
  266.                                   foreach ($references as $ref) {
  267.                                       if (isset($ref['journal-title'])) {
  268.                                           $pub->setName($ref['journal-title']);
  269.                                       }
  270.                                   }
  271.                               }
  272.                               }elseif (isset($item->{'container-title'}) && count($item->{'container-title'}) > 0) {
  273.                               $references $item->{'container-title'}[0];
  274.                               $pub->setName($references);
  275.                           }
  276.                           if (isset($item->{'published-online'})) {
  277.                               $date json_decode(json_encode($item->{'published-online'}), true);
  278.                               if (isset($date['date-parts'][0][0])) {
  279.                                   $pub->setYear($date['date-parts'][0][0]);
  280.                               }
  281.                           }
  282.                           $em->persist($pub);
  283.                           $em->flush();
  284.                           }
  285.                       }
  286.             }
  287.         }catch (TransportExceptionInterface $exception){
  288.             $this->addFlash('error''Ooops ! Something wrong happened. Please retry again or contact the support');
  289.             $this->logger->critical($exception->getTraceAsString());
  290.         }
  291.         $emS $this->getDoctrine()->getManager();
  292.             $emS->flush();
  293.             $user=$this->getUser();
  294.           return $this->redirectToRoute('publication_index',['id'=>$user->getId(),'firstname'=>$user->getFirstName(),'lastname'=>$user->getLastName()]);
  295.     }
  296.     /**
  297.      * @Route("/all_publication/{id}", name="all_publication_delete", methods={"DELETE"}, requirements={"id":"\d+"})
  298.      */
  299.     public function delete(Request $requestAllpublications $publication): Response
  300.     {
  301.         $user=$this->getUser();
  302.             $entityManager $this->getDoctrine()->getManager();
  303.             $entityManager->remove($publication);
  304.             $entityManager->flush();
  305.         return $this->redirectToRoute('publication_index',['firstname'=>$user->getFirstName(),'lastname'=>$user->getLastName(),'id'=>$user->getId()]);
  306.     }
  307.     /**
  308.      * @Route("/add_follower/{firstname}_{lastname}_{id}/{author}/{exist}/{redirect}", name="add_follower", methods={"GET"}, defaults={"id" = 0,"firstname" = "","lastname" = ""})
  309.      */
  310.     public function add_follower(UserRepository $userRepository,Request $request)
  311.     {
  312.         $entityManager $this->getDoctrine()->getManager();
  313.         $auteur $request->get('author');
  314.         $exist$request->get('exist');
  315.         $searchValue$request->get('searchValue');
  316.         $route$request->get('redirect');
  317.         if($route=="get_authors_publication"){
  318.             $parameter="auteur";
  319.         }else{
  320.             $parameter="authorValue";
  321.         }
  322.         $currentUser=$this->getUser();
  323.         if($currentUser) {
  324.             $firstName=$request->get('firstname');
  325.             $lastName=$request->get('lastname');
  326.             $id=$request->get('id');
  327.             /** @var User $user */
  328.             $user $currentUser;
  329.             if ($exist == 0) {
  330.                         $following $entityManager->getRepository(Follow::class)->findBy(['following' => $auteur]);
  331.                         if($following) {
  332.                             $following $following[0];
  333.                             $following->addFollower($user);
  334.                             $entityManager->persist($following);
  335.                             $entityManager->persist($user);
  336.                             $entityManager->flush();
  337.                         }
  338.                         else{
  339.                             $follow = new Follow();
  340.                             $follow->setFollowing($auteur);
  341.                             $follow->addFollower($user);
  342.                             $entityManager->persist($user);
  343.                             $entityManager->flush();
  344.                             $this->addFlash('success''You have successfully followed the author');
  345.                         }
  346.                         return $this->redirectToRoute('get_authors_publication',["auteur"=>$auteur]);
  347.                     }
  348.                     else {
  349.                         $following $entityManager->getRepository(Follow::class)->findBy(['following' => $auteur]);
  350.                         if($following){
  351.                             $following=$following[0];
  352.                             $following->removeFollower($user);
  353.                             $entityManager->persist($following);
  354.                             $entityManager->remove($following);
  355.                             $entityManager->flush();
  356.                             // add flash message
  357.                             $this->addFlash('success''You have successfully unfollowed the author');
  358.                             if($route=="authors_list"){
  359.                                 return $this->redirectToRoute($route);
  360.                             }else{
  361.                                 return $this->redirectToRoute('get_authors_publication',["auteur"=>$auteur]);
  362.                             }
  363.                         }
  364.                     }
  365.         }
  366.         else {
  367.             return $this->redirectToRoute("registre_user");
  368.         }
  369.         return $this->redirectToRoute('get_authors',["authorValue"=>$searchValue]);
  370.     }
  371.     /**
  372.      * @IsGranted("ROLE_USER")
  373.      * @Route("/{firstname}_{lastname}_{id}/followers", name="followers_index", methods={"GET"})
  374.      */
  375.     public function followers_index(UserRepository $userRepository,Request $request)
  376.     {
  377.         $user=$this->getUser();
  378.         $firstname=$request->get('firstname');
  379.         $lastname=$request->get('lastname');
  380.         $id=$request->get('id');
  381.         $myProfile=false;
  382.         if($user==$this->getUser()) {
  383.             $myProfile true;
  384.         }
  385.         $entityManager $this->getDoctrine()->getManager();
  386.         $myFollowers=$entityManager->getRepository(Follow::class)->findAllFollowers($user->getFullName());
  387.         $allPublication=$entityManager->getRepository(Allpublications::class)->findAll();
  388.         $allFollow=$entityManager->getRepository(Follow::class)->findAll();
  389.         return $this->render("refonte/follow/followers_index.html.twig",[
  390.                 'exist'=>$myFollowers,
  391.                 'myFollowers'=>$myFollowers,
  392.                 'allPublication'=>$allPublication,
  393.                 'allFollow'=>$allFollow,
  394.                 'myProfile'=>$myProfile,
  395.             ]);
  396.     }
  397.     /**
  398.      * @IsGranted("ROLE_USER")
  399.      * @Route("/{firstname}_{lastname}_{id}/following", name="following_index", methods={"GET"})
  400.      */
  401.     public function following_index(UserRepository $userRepository,Request $request)
  402.     {
  403.         $user=$this->getUser();
  404.         $firstname=$request->get('firstname');
  405.         $lastname=$request->get('lastname');
  406.         $id=$request->get('id');
  407.         $myProfile=false;
  408.         if($user==$this->getUser()) {
  409.             $myProfile true;
  410.         }
  411.         $entityManager $this->getDoctrine()->getManager();
  412.         $exist=$entityManager->getRepository(Follow::class)->findAllFollowing($user);
  413.         $allPublication=$entityManager->getRepository(Allpublications::class)->findAll();
  414.         $allFollow=$entityManager->getRepository(Follow::class)->findAll();
  415.         return $this->render("follow/following_index.html.twig",[
  416.             'exist'=>$exist,
  417.             'allPublication'=>$allPublication,
  418.             'allFollow'=>$allFollow,
  419.             'myProfile'=>$myProfile,
  420.             'user'=>$user
  421.         ]);
  422.     }
  423.     /**
  424.      * @Route("/{firstname}.{lastname}.{id}/co_authors", name="co_authors_index", methods={"GET"})
  425.      */
  426.     public function co_authors_index(Request $request,UserRepository $userRepository,PublicationManager $publicationManager,PaginatorInterface $paginator){
  427.       /** @var User $user */
  428.         $user=$this->getUser();
  429.         $firstname=$request->get('firstname');
  430.         $lastname=$request->get('lastname');
  431.         if($firstname && $lastname || ($firstname || $lastname )){
  432.             $user=$userRepository->userFromConctact($firstname,$lastname,$user->getEmail());
  433.             if($user){
  434.                 $user=$user[0];
  435.             }
  436.         }
  437.         $coauthors$publicationManager->getAuthorsList($user);
  438.         return $this->render("refonte/Profile/co_authors.html.twig",
  439.             [
  440.                 'user'=>$user,
  441.                 "coauthors"=>$coauthors,
  442.                 'total'=>count($coauthors)
  443.         ]);
  444.     }
  445.     /**
  446.      * @Route("/co_authors/{author}", name="co_authors_list_author", methods={"GET"})
  447.      */
  448.     public function co_authors_list_author(Request $request,UserRepository $userRepository,PublicationManager $publicationManager){
  449.         $author_search=$request->get('author');
  450.         $authors$publicationManager->getAuthorsListForAuthor($author_search);
  451.         return $this->render("Profile/co_authors.html.twig",
  452.             [
  453.                 'author'=>$author_search,
  454.                 "authors"=>$authors
  455.             ]);
  456.     }
  457.     /**
  458.      * @Route("/settings", name="settings", methods={"GET"})
  459.      */
  460.     public function settings()
  461.     {
  462.         $user=$this->getUser();
  463.         if($user){
  464.             return $this->redirectToRoute("profile_settings",['firstname'=>$user->getFirstName(),'lastname'=>$user->getLastName()]);
  465.         }
  466.     }
  467.     /**
  468.      * @Route("/settings/{firstname}.{lastname}", name="profile_settings", methods={"GET"})
  469.      */
  470.     public function profile_settings(Request $request)
  471.     {
  472.        return $this->render('Profile/profile_settings.html.twig');
  473.     }
  474.     /**
  475.      * @Route("/profile/{firstname}.{lastname}.{id}", name="share_your_profile", methods={"GET"})
  476.      */
  477.     public function share_your_profile(Request $request,AllPublicationRepository $allPublicationRepository)
  478.     {
  479.         $firstname=$request->get('firstname');
  480.         $lastname=$request->get('lastname');
  481.         $id=$request->get('id');
  482.         $publications=null;
  483.         $countFollowers=0;$countFolling=0;
  484.         $em=$this->getDoctrine()->getManager();
  485.         $user=$em->getRepository(User::class)->find($id);
  486.         if($user) {
  487.             $myFollowers=$em->getRepository(Follow::class)->findAllFollowers($user->getFullName());
  488.             $myFollowing=$em->getRepository(Follow::class)->findAllFollowing($user);
  489.             $countFollowers=count($myFollowers);
  490.             $countFolling=count($myFollowing);
  491.         }
  492.         if($user  && $user->getFirstName()==$firstname && ($user->getLastName()==$lastname || $user->getFullName()==$lastname)){
  493.         $publications=$allPublicationRepository->allPublications($user);
  494.         }
  495.         else{
  496.             $user=null;
  497.         }
  498.         return $this->render("Profile/share_your_profile.html.twig",[
  499.          'firstname'=>$firstname,
  500.          'lastname'=>$lastname,
  501.          'id'=>$id,
  502.          'publications'=>$publications,
  503.           'user'=>$user,
  504.             'countFolling'=>$countFolling,
  505.             'countFollowers'=>$countFollowers
  506.         ]);
  507.     }
  508.     /**
  509.      * @Route("/journal-search", name="journalSearch", methods={"GET"})
  510.      */
  511.     public function journalSearch()
  512.     {
  513.         return $this->render("refonte/pages/journalSearch.html.twig");
  514.     }
  515.     /**
  516.      * @Route("/verified-journal", name="verified-journal", methods={"GET"})
  517.      */
  518.     public function verifiedJournal()
  519.     {
  520.         return $this->render("search/verifiedJournal.html.twig");
  521.     }
  522.     /**
  523.      * @Route("/faq", name="faq", methods={"GET"})
  524.      */
  525.     public function faq()
  526.     {
  527.         return $this->render("pages/faq.html.twig");
  528.     }
  529.     /**
  530.      * @Route("/newsletter", name="newsletter", methods={"post"})
  531.      */
  532.     public function newsletter(Request $request, \Swift_Mailer $mailer)
  533.     {
  534.         $em $this->getDoctrine()->getManager();
  535.         $email$request->request->get("email");
  536.         $formSubmitted=$request->request->get("newsletter");
  537.         $newsletter$em->getRepository(Newsletter::class)->findOneBy(["email"=> $email]);
  538.         if(!$newsletter && $formSubmitted && $email && $this->validateEmail($email)){
  539.             $nl = new Newsletter();
  540.             $nl->setEmail($email);
  541.             $nl->setStatus(1);
  542.             $nl->setLastSendDate(new \DateTime());
  543.             $em->persist($nl);
  544.             $em->flush();
  545.             $this->addFlash('success','You have successfully subscribed to our newsletter.');
  546.         }elseif ($newsletter && $formSubmitted){
  547.             $this->addFlash('warning','You are already subscribed to our newsletter.');
  548.         }else{
  549.             $this->addFlash('error','Ooops ! Something wrong happened. Please retry again or contact the support');
  550.         }
  551.         return $this->redirect("/");
  552.     }
  553.     /**
  554.      * @Route("/ads.txt", name="ads.txt",  methods={"GET"})
  555.      */
  556.     public function adsText()
  557.     {
  558.         $file = (\dirname(__DIR__2).'/public/assets/ads.txt');
  559.         $txt file_get_contents($file);
  560.         $response = new Response($txt);
  561.         $response->headers->set('Content-Type''text/plain');
  562.         $response->sendHeaders();
  563.         return $response;
  564.     }
  565.     /**
  566.      * @Route("/savefavoriteCategories", name="save_favorite_categories")
  567.      */
  568.     public function saveFavoriteCategories(CategoryRepository $categoryRepository,Request $request,EntityManagerInterface $em): Response
  569.     {
  570.         $categories=$request->get('selected-categories');
  571.         $favoriteCategories explode(','$categories);
  572.         $referer $request->headers->get('referer');
  573.         $user=$this->getUser();
  574.         if(!empty($categories)){
  575.             if(empty($user->getCategories()->getValues())) {
  576.                 foreach ($favoriteCategories as $c) {
  577.                     /** @var Category $category */
  578.                     $category $categoryRepository->find($c);
  579.                     $user->addCategory($category);
  580.                     $em->persist($user);
  581.                     $em->flush();
  582.                 }
  583.             }else{
  584.                 $oldCategories=$user->getCategories()->getValues();
  585.                 foreach ($oldCategories as $old){
  586.                     $user->removeCategory($old);
  587.                     $em->persist($user);
  588.                 }
  589.                 foreach ($favoriteCategories as $c) {
  590.                     /** @var Category $category */
  591.                     $category $categoryRepository->find($c);
  592.                     $user->addCategory($category);
  593.                     $em->persist($user);
  594.                 }
  595.                 $em->flush();
  596.             }
  597.         }
  598.         if($referer && str_contains($referer'categories_list')){
  599.             return $this->redirect($referer);
  600.         }
  601.         return $this->redirect('/');
  602.     }
  603.     /**
  604.      * @Route("/registration" , name="registration-user")
  605.      */
  606.     public function registreUser(){
  607.         $user $this->getUser();
  608.         if (!$user) return $this->redirectToRoute('registre_user');
  609.         return $this->redirect('/');
  610.     }
  611.     function validateEmail($email) {
  612.         // Use filter_var() to validate the email
  613.         if (filter_var($emailFILTER_VALIDATE_EMAIL)) {
  614.             return true// Email is valid
  615.         } else {
  616.             return false// Email is invalid
  617.         }
  618.     }
  619. }