src/Controller/UserProjetsController.php line 37
<?phpnamespace App\Controller;use App\Entity\Notes;use App\Entity\UserProjets;use App\Entity\Commentaires;use App\Form\UserProjetsType;use App\Form\CommentairesType;use App\Repository\ProjetsRepository;use Doctrine\ORM\EntityManagerInterface;use App\Repository\UserProjetsRepository;use Symfony\Component\Validator\Validation;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Validator\Constraints\Url;use Symfony\Component\Validator\Constraints\Regex;use Symfony\Component\Security\Core\User\UserInterface;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;#[Route('/user/projets')]class UserProjetsController extends AbstractController{#[Route('/', name: 'app_user_projets_index', methods: ['GET'])]public function index(UserProjetsRepository $userProjetsRepository): Response{return $this->render('user_projets/index.html.twig', ['user_projets' => $userProjetsRepository->findAll(),]);}#[Route('/en_cours', name: 'app_user_projets_in_progress', methods: ['GET'])]public function inProgress(UserProjetsRepository $userProjetsRepository): Response{return $this->render('user_projets/list_projects_by.html.twig', ['title' => 'Projets en cours','projects' => $userProjetsRepository->findBy(['Completed' => false]),]);}#[Route('/finish', name: 'app_user_projets_finish', methods: ['GET'])]public function finish(UserProjetsRepository $userProjetsRepository): Response{return $this->render('user_projets/list_projects_by.html.twig', ['title' => 'Projets terminĂ©s','projects' => $userProjetsRepository->findBy(['Completed' => true]),]);}#[Route('/new', name: 'app_user_projets_new', methods: ['GET', 'POST'])]public function new(Request $request, UserProjetsRepository $userProjetsRepository, ProjetsRepository $projetsRepository, UserInterface $user, EntityManagerInterface $entityManager): Response{$userProjet = new UserProjets();$projectId = $request->request->get('project');$projet = $projetsRepository->findBy(['id' => $projectId]);$userProjet->setUser($user);$userProjet->setProjet($projet[0]);if ($this->isCsrfTokenValid('new' . $projet[0]->getId(), $request->request->get('_token'))) {$entityManager->persist($userProjet);$entityManager->flush();}return $this->redirectToRoute('app_user_projet_show', ['id' => $userProjet->getId()], Response::HTTP_SEE_OTHER);// $form = $this->createForm(UserProjetsType::class, $userProjet);// $form->handleRequest($request);// if ($form->isSubmitted() && $form->isValid()) {// $userProjet->setUser($user);// $userProjet->setProjet($form->get('projet')->getData());// $userProjetsRepository->save($userProjet, true);// return $this->redirectToRoute('app_user_projets_index', [], Response::HTTP_SEE_OTHER);// }// return $this->render('user_projets/new.html.twig', [// 'user_projet' => $userProjet,// 'form' => $form,// ]);// return $this->renderForm('user_projets/new.html.twig', [// 'user_projet' => $userProjet,// ]);}#[Route('/{id}', name: 'app_user_projet_show', methods: ['GET', 'POST'])]public function show(Request $request, UserProjets $userProjet, ProjetsRepository $projetsRepository, UserProjetsRepository $userProjetsRepository, EntityManagerInterface $entityManager): Response{//if project is not completed return to project pageif (!$userProjet->isCompleted() && $userProjet->getUser() !== $this->getUser()) {return $this->redirectToRoute('app_projets_show', ['id' => $userProjet->getProjet()->getId()], Response::HTTP_SEE_OTHER);}$commentaire = new Commentaires();$note = new Notes();$form = $this->createForm(CommentairesType::class, $commentaire);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$nbNote = $request->request->get('rating');$commentaire->setUser($this->getUser());if ($nbNote) {$note->setRating($nbNote);$commentaire->addNote($note);}$userProjet->addCommentaire($commentaire);$userProjetsRepository->save($userProjet, true);// $entityManager->persist($commentaire);// $entityManager->flush();}$project = $projetsRepository->findOneBy(['id' => $userProjet->getProjet()->getId()]);$allProjects = $userProjetsRepository->findBy(['projet' => $project]);$isMine = false;if ($this->getUser() === $userProjet->getUser()) {$isMine = true;};$commentaires = $userProjet->getCommentairesJS();// dd($commentaires);return $this->render('user_projets/show.html.twig', ['userProject' => $userProjet,'projet' => $project,'isMine' => $isMine,'allProjects' => $allProjects,'form' => $form,'comments' => $commentaires,'ratings' => $userProjet->getAverageRatings()[0],'nbRatings' => $userProjet->getAverageRatings()[1],]);}#[Security("is_granted('ROLE_USER') and user === userProjet.getUser()")]#[Route('/{id}/edit', name: 'app_user_projets_edit', methods: ['GET', 'POST'])]public function edit(Request $request, UserProjets $userProjet, UserProjetsRepository $userProjetsRepository): Response{$form = $this->createForm(UserProjetsType::class, $userProjet);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$userProjetsRepository->save($userProjet, true);return $this->redirectToRoute('app_user_projets_index', [], Response::HTTP_SEE_OTHER);}return $this->render('user_projets/edit.html.twig', ['userProject' => $userProjet,'form' => $form,]);}#[Security("is_granted('ROLE_USER') and user === userProjet.getUser()")]#[Route('/end/{id}', name: 'app_user_projets_end', methods: ['POST', 'GET'])]public function end(Request $request, UserInterface $user, UserProjets $userProjet, UserProjetsRepository $userProjetsRepository): Response{if ($this->isCsrfTokenValid('end' . $userProjet->getId(), $request->request->get('_token'))) {$userProjet->setCompleted(true);$userProjet->setFinishedAt(new \DateTimeImmutable());$githubLink = $request->request->get('github');$validator = Validation::createValidator();$violations = $validator->validate($githubLink, [new Url(['protocols' => ['http', 'https']]),new Regex(['pattern' => '/github\.io/','message' => 'Le lien doit ĂȘtre un lien GitHub valide.',]),]);if (count($violations) === 0) {$userProjet->setGithub($githubLink);$userProjetsRepository->save($userProjet, true);} else {// Le lien GitHub n'est pas valide, traitez l'erreur}}return $this->redirectToRoute('app_user_projet_show', ['id' => $userProjet->getId()], Response::HTTP_SEE_OTHER);}#[Security("is_granted('ROLE_USER') and user === userProjet.getUser()")]#[Route('/delete/{id}', name: 'app_user_projets_delete', methods: ['POST', 'GET'])]public function delete(Request $request, UserInterface $user, UserProjets $userProjet, UserProjetsRepository $userProjetsRepository): Response{if ($this->isCsrfTokenValid('delete' . $userProjet->getId(), $request->request->get('_token'))) {$userProjetsRepository->remove($userProjet, true);}return $this->redirectToRoute('app_projets_show', ['id' => $userProjet->getProjet()->getId()], Response::HTTP_SEE_OTHER);}}