src/Controller/UserProjetsController.php line 37

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notes;
  4. use App\Entity\UserProjets;
  5. use App\Entity\Commentaires;
  6. use App\Form\UserProjetsType;
  7. use App\Form\CommentairesType;
  8. use App\Repository\ProjetsRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Repository\UserProjetsRepository;
  11. use Symfony\Component\Validator\Validation;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Validator\Constraints\Url;
  16. use Symfony\Component\Validator\Constraints\Regex;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. #[Route('/user/projets')]
  22. class UserProjetsController extends AbstractController
  23. {
  24.     #[Route('/'name'app_user_projets_index'methods: ['GET'])]
  25.     public function index(UserProjetsRepository $userProjetsRepository): Response
  26.     {
  27.         return $this->render('user_projets/index.html.twig', [
  28.             'user_projets' => $userProjetsRepository->findAll(),
  29.         ]);
  30.     }
  31.     #[Route('/en_cours'name'app_user_projets_in_progress'methods: ['GET'])]
  32.     public function inProgress(UserProjetsRepository $userProjetsRepository): Response
  33.     {
  34.         return $this->render('user_projets/list_projects_by.html.twig', [
  35.             'title' => 'Projets en cours',
  36.             'projects' => $userProjetsRepository->findBy(['Completed' => false]),
  37.         ]);
  38.     }
  39.     #[Route('/finish'name'app_user_projets_finish'methods: ['GET'])]
  40.     public function finish(UserProjetsRepository $userProjetsRepository): Response
  41.     {
  42.         return $this->render('user_projets/list_projects_by.html.twig', [
  43.             'title' => 'Projets terminĂ©s',
  44.             'projects' => $userProjetsRepository->findBy(['Completed' => true]),
  45.         ]);
  46.     }
  47.     #[Route('/new'name'app_user_projets_new'methods: ['GET''POST'])]
  48.     public function new(Request $requestUserProjetsRepository $userProjetsRepositoryProjetsRepository $projetsRepositoryUserInterface $userEntityManagerInterface $entityManager): Response
  49.     {
  50.         $userProjet = new UserProjets();
  51.         $projectId $request->request->get('project');
  52.         $projet $projetsRepository->findBy(['id' => $projectId]);
  53.         $userProjet->setUser($user);
  54.         $userProjet->setProjet($projet[0]);
  55.         if ($this->isCsrfTokenValid('new' $projet[0]->getId(), $request->request->get('_token'))) {
  56.             $entityManager->persist($userProjet);
  57.             $entityManager->flush();
  58.         }
  59.         return $this->redirectToRoute('app_user_projet_show', ['id' => $userProjet->getId()], Response::HTTP_SEE_OTHER);
  60.         // $form = $this->createForm(UserProjetsType::class, $userProjet);
  61.         // $form->handleRequest($request);
  62.         // if ($form->isSubmitted() && $form->isValid()) {
  63.         //     $userProjet->setUser($user);
  64.         //     $userProjet->setProjet($form->get('projet')->getData());
  65.         //     $userProjetsRepository->save($userProjet, true);
  66.         //     return $this->redirectToRoute('app_user_projets_index', [], Response::HTTP_SEE_OTHER);
  67.         // }
  68.         // return $this->render('user_projets/new.html.twig', [
  69.         //     'user_projet' => $userProjet,
  70.         //     'form' => $form,
  71.         // ]);
  72.         // return $this->renderForm('user_projets/new.html.twig', [
  73.         //     'user_projet' => $userProjet,
  74.         // ]);
  75.     }
  76.     #[Route('/{id}'name'app_user_projet_show'methods: ['GET''POST'])]
  77.     public function show(Request $requestUserProjets $userProjetProjetsRepository $projetsRepositoryUserProjetsRepository $userProjetsRepositoryEntityManagerInterface $entityManager): Response
  78.     {
  79.         //if project is not completed return to project page
  80.         if (!$userProjet->isCompleted() && $userProjet->getUser() !== $this->getUser()) {
  81.             return $this->redirectToRoute('app_projets_show', ['id' => $userProjet->getProjet()->getId()], Response::HTTP_SEE_OTHER);
  82.         }
  83.         $commentaire = new Commentaires();
  84.         $note = new Notes();
  85.         $form $this->createForm(CommentairesType::class, $commentaire);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted() && $form->isValid()) {
  88.             $nbNote $request->request->get('rating');
  89.             $commentaire->setUser($this->getUser());
  90.             if ($nbNote) {
  91.                 $note->setRating($nbNote);
  92.                 $commentaire->addNote($note);
  93.             }
  94.             $userProjet->addCommentaire($commentaire);
  95.             $userProjetsRepository->save($userProjettrue);
  96.             // $entityManager->persist($commentaire);
  97.             // $entityManager->flush();
  98.         }
  99.         $project $projetsRepository->findOneBy(['id' => $userProjet->getProjet()->getId()]);
  100.         $allProjects $userProjetsRepository->findBy(['projet' => $project]);
  101.         $isMine false;
  102.         if ($this->getUser() === $userProjet->getUser()) {
  103.             $isMine true;
  104.         };
  105.         $commentaires $userProjet->getCommentairesJS();
  106.         // dd($commentaires);
  107.         return $this->render('user_projets/show.html.twig', [
  108.             'userProject' => $userProjet,
  109.             'projet' => $project,
  110.             'isMine' => $isMine,
  111.             'allProjects' => $allProjects,
  112.             'form' => $form,
  113.             'comments' => $commentaires,
  114.             'ratings' => $userProjet->getAverageRatings()[0],
  115.             'nbRatings' => $userProjet->getAverageRatings()[1],
  116.         ]);
  117.     }
  118.     #[Security("is_granted('ROLE_USER') and user === userProjet.getUser()")]
  119.     #[Route('/{id}/edit'name'app_user_projets_edit'methods: ['GET''POST'])]
  120.     public function edit(Request $requestUserProjets $userProjetUserProjetsRepository $userProjetsRepository): Response
  121.     {
  122.         $form $this->createForm(UserProjetsType::class, $userProjet);
  123.         $form->handleRequest($request);
  124.         if ($form->isSubmitted() && $form->isValid()) {
  125.             $userProjetsRepository->save($userProjettrue);
  126.             return $this->redirectToRoute('app_user_projets_index', [], Response::HTTP_SEE_OTHER);
  127.         }
  128.         return $this->render('user_projets/edit.html.twig', [
  129.             'userProject' => $userProjet,
  130.             'form' => $form,
  131.         ]);
  132.     }
  133.     #[Security("is_granted('ROLE_USER') and user === userProjet.getUser()")]
  134.     #[Route('/end/{id}'name'app_user_projets_end'methods: ['POST''GET'])]
  135.     public function end(Request $requestUserInterface $userUserProjets $userProjetUserProjetsRepository $userProjetsRepository): Response
  136.     {
  137.         if ($this->isCsrfTokenValid('end' $userProjet->getId(), $request->request->get('_token'))) {
  138.             $userProjet->setCompleted(true);
  139.             $userProjet->setFinishedAt(new \DateTimeImmutable());
  140.             $githubLink $request->request->get('github');
  141.             $validator Validation::createValidator();
  142.             $violations $validator->validate($githubLink, [
  143.                 new Url(['protocols' => ['http''https']]),
  144.                 new Regex([
  145.                     'pattern' => '/github\.io/',
  146.                     'message' => 'Le lien doit ĂȘtre un lien GitHub valide.',
  147.                 ]),
  148.             ]);
  149.             if (count($violations) === 0) {
  150.                 $userProjet->setGithub($githubLink);
  151.                 $userProjetsRepository->save($userProjettrue);
  152.             } else {
  153.                 // Le lien GitHub n'est pas valide, traitez l'erreur
  154.             }
  155.         }
  156.         return $this->redirectToRoute('app_user_projet_show', ['id' => $userProjet->getId()], Response::HTTP_SEE_OTHER);
  157.     }
  158.     #[Security("is_granted('ROLE_USER') and user === userProjet.getUser()")]
  159.     #[Route('/delete/{id}'name'app_user_projets_delete'methods: ['POST''GET'])]
  160.     public function delete(Request $requestUserInterface $userUserProjets $userProjetUserProjetsRepository $userProjetsRepository): Response
  161.     {
  162.         if ($this->isCsrfTokenValid('delete' $userProjet->getId(), $request->request->get('_token'))) {
  163.             $userProjetsRepository->remove($userProjettrue);
  164.         }
  165.         return $this->redirectToRoute('app_projets_show', ['id' => $userProjet->getProjet()->getId()], Response::HTTP_SEE_OTHER);
  166.     }
  167. }