diff --git a/src/Controller/ProfilController.php b/src/Controller/ProfilController.php index 05b482a..f2d69c7 100644 --- a/src/Controller/ProfilController.php +++ b/src/Controller/ProfilController.php @@ -3,10 +3,13 @@ namespace App\Controller; use App\Entity\Profil; +use App\Form\ProfilType; use Doctrine\ORM\EntityManager; +use SebastianBergmann\Environment\Console; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\HttpFoundation\Request; class ProfilController extends AbstractController { @@ -19,13 +22,17 @@ class ProfilController extends AbstractController #[Route('/profil/{id}',name:'profil_show', requirements: ['page' => '\d+'])] public function profil(int $id): Response { + $connected = $this->isGranted('ROLE_USER'); + // $connected = $this->isGranted('ROLE_USER') != false; + $profil = $this->mgr->find(Profil::class, $id); $posts = $profil->getPosts(); return $this->render('profil/index.html.twig', [ 'profil' => $profil, 'posts' => $posts, 'followFlag' => $profil->getFollowers()->contains($this->getUser()), - 'selfFlag' => $profil != $this->getUser() + 'selfFlag' => $profil == $this->getUser(), + 'connected' => $connected ]); } @@ -83,14 +90,53 @@ class ProfilController extends AbstractController ]); } - #[Route('/profil/new', name: 'profil_new')] - public function new(): Response + // #[Route('/profil/new', name: 'profil_new')] + // public function new(): Response + // { + // $profil = new Profil(); + + // return $this->redirectToRoute('profil_show', ['id' => $profil->getId()]); + // } + + #[Route('/profil/{id}/edit',name:'profil_edit', requirements: ['page'=> '\d'])] + public function editProfil(int $id,Request $request): Response { - $profil = new Profil(); + $profil = $this->mgr->find(Profil::class, $id); + + $form = $this->createForm(ProfilType::class, $profil); - return $this->redirectToRoute('profil_show', ['id' => $profil->getId()]); + $form->handleRequest($request); + if( $form->isSubmitted() && $form->isValid() ) { + $form->getData(); + $this->mgr->persist($profil); + $this->mgr->flush(); + + return $this->redirectToRoute('profil_show', ['id' => $id]); + } + + return $this->render('profil/edit.html.twig', [ + 'form'=> $form, + 'profil' => $profil, + ]); } + #[Route('/profil/{id}/delete', name: 'profil_delete', methods: ['POST'], requirements: ['id' => '\d+'])] + public function delete(int $id, Request $request): Response + { + $profil = $this->mgr->find(Profil::class, $id); + + if (!$profil) { + throw $this->createNotFoundException('The profile does not exist'); + } + + if ($this->isCsrfTokenValid('delete'.$profil->getId(), $request->request->get('_token'))) { + $this->mgr->remove($profil); + $this->mgr->flush(); + $this->addFlash('success', 'Profile deleted successfully'); + } + + return $this->redirectToRoute('app_login'); + } } diff --git a/src/Form/ProfilType.php b/src/Form/ProfilType.php new file mode 100644 index 0000000..d6d5f2e --- /dev/null +++ b/src/Form/ProfilType.php @@ -0,0 +1,29 @@ +add('name') + ->add('description') + // ->add('password') + + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Profil::class, + ]); + } +} diff --git a/templates/profil/edit.html.twig b/templates/profil/edit.html.twig new file mode 100644 index 0000000..997fd08 --- /dev/null +++ b/templates/profil/edit.html.twig @@ -0,0 +1,30 @@ +{# templates/profil/edit.html.twig #} + +{% extends 'base.html.twig' %} + +{% block title %}Edit Profile{% endblock %} + +{% block stylesheets %} + + +{% endblock %} + +{% block body %} +
+

Edit Profile

+ + {{ form_start(form) }} + {{ form_widget(form) }} + + {{ form_end(form) }} + +
+ + +
+
+{% endblock %} + +{% block javascripts %} + +{% endblock %} \ No newline at end of file diff --git a/templates/profil/index.html.twig b/templates/profil/index.html.twig index 3dc23d0..7e9aca1 100644 --- a/templates/profil/index.html.twig +++ b/templates/profil/index.html.twig @@ -17,10 +17,14 @@

{{ profil.name }}


{{ profil.description }}

+
+ {% if selfFlag %} + Edit + {% elseif connected %} {% if followFlag %} Unfollow {% else %}