Ajout du formulaire de modification des profil

pull/16/head
Corentin RICHARD 10 months ago
parent b10d389d95
commit 8c89c8c8a1

@ -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);
$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->redirectToRoute('profil_show', ['id' => $profil->getId()]);
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');
}
}

@ -0,0 +1,29 @@
<?php
namespace App\Form;
use App\Entity\Profil;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ProfilType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
->add('description')
// ->add('password')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Profil::class,
]);
}
}

@ -0,0 +1,30 @@
{# templates/profil/edit.html.twig #}
{% extends 'base.html.twig' %}
{% block title %}Edit Profile{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('styles/app.css') }}">
<link rel="stylesheet" href="{{ asset('public/css/components/profil.css') }}">
{% endblock %}
{% block body %}
<div class="profile-container">
<h1>Edit Profile</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary follow-button">Save</button>
{{ form_end(form) }}
<form method="post" action="{{ path('profil_delete', {id: profil.id}) }}" onsubmit="return confirm('Are you sure you want to delete this profile?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ profil.id) }}">
<button class="btn btn-danger follow-button">Delete Profile</button>
</form>
</div>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('scripts/scripts.js') }}"></script>
{% endblock %}

@ -17,10 +17,14 @@
<h1>{{ profil.name }}</h1>
<br>
<p>{{ profil.description }}</p>
</div>
</div>
<div class="horizontal-layout">
{% if selfFlag %}
<a href="{{ path('profil_edit', {id: profil.id}) }}" class="follow-button">Edit</a>
{% elseif connected %}
{% if followFlag %}
<a href="{{ path('profil_unfollow', {id: profil.id}) }}" class="follow-button">Unfollow</a>
{% else %}

Loading…
Cancel
Save