Skip to content

Commit 0895347

Browse files
JPechbertyJPechberty
JPechberty
authored and
JPechberty
committed
Refact UI, TestUserController
1 parent a6949f7 commit 0895347

14 files changed

+609
-1
lines changed

src/Controller/TestUserController.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use App\Entity\User;
6+
use App\Repository\UserRepository;
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use Symfony\Component\Routing\Annotation\Route;
11+
use Symfony\Component\Validator\Validator\ValidatorInterface;
12+
13+
class TestUserController extends AbstractController
14+
{
15+
#[Route('/test/user', name: 'app_test_user')]
16+
public function index(ValidatorInterface $validator, EntityManagerInterface $entityManager): Response
17+
{
18+
19+
$user = new User();
20+
$user->setCreatedAt(new \DateTimeImmutable('now'));
21+
$user->setPrenom("");
22+
$user->setNom("");
23+
$user->setEmail("");
24+
$user->setTelephone("");
25+
$user->setAge(1);
26+
$errors = $validator->validate($user);
27+
28+
if(count($errors)>0){
29+
dd($errors);
30+
}
31+
$entityManager->persist($user);
32+
$entityManager->flush();
33+
34+
35+
dd($user);
36+
return $this->render('test_user/index.html.twig', [
37+
'controller_name' => 'TestUserController',
38+
]);
39+
}
40+
}

src/Controller/UserController.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use App\Entity\User;
6+
use App\Form\UserType;
7+
use App\Repository\UserRepository;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\Routing\Annotation\Route;
13+
14+
#[Route('/user')]
15+
class UserController extends AbstractController
16+
{
17+
#[Route('/', name: 'app_user_index', methods: ['GET'])]
18+
public function index(UserRepository $userRepository): Response
19+
{
20+
return $this->render('user/index.html.twig', [
21+
'users' => $userRepository->findAll(),
22+
]);
23+
}
24+
25+
#[Route('/new', name: 'app_user_new', methods: ['GET', 'POST'])]
26+
public function new(Request $request, EntityManagerInterface $entityManager): Response
27+
{
28+
$user = new User();
29+
$form = $this->createForm(UserType::class, $user);
30+
$form->handleRequest($request);
31+
32+
if ($form->isSubmitted() && $form->isValid()) {
33+
$entityManager->persist($user);
34+
$entityManager->flush();
35+
36+
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
37+
}
38+
39+
return $this->render('user/new.html.twig', [
40+
'user' => $user,
41+
'form' => $form,
42+
]);
43+
}
44+
45+
#[Route('/{id}', name: 'app_user_show', methods: ['GET'])]
46+
public function show(User $user): Response
47+
{
48+
return $this->render('user/show.html.twig', [
49+
'user' => $user,
50+
]);
51+
}
52+
53+
#[Route('/{id}/edit', name: 'app_user_edit', methods: ['GET', 'POST'])]
54+
public function edit(Request $request, User $user, EntityManagerInterface $entityManager): Response
55+
{
56+
$form = $this->createForm(UserType::class, $user);
57+
$form->handleRequest($request);
58+
59+
if ($form->isSubmitted() && $form->isValid()) {
60+
$entityManager->flush();
61+
62+
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
63+
}
64+
65+
return $this->render('user/edit.html.twig', [
66+
'user' => $user,
67+
'form' => $form,
68+
]);
69+
}
70+
71+
#[Route('/{id}', name: 'app_user_delete', methods: ['POST'])]
72+
public function delete(Request $request, User $user, EntityManagerInterface $entityManager): Response
73+
{
74+
if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
75+
$entityManager->remove($user);
76+
$entityManager->flush();
77+
}
78+
79+
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
80+
}
81+
}

src/Entity/User.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use App\Repository\UserRepository;
6+
use Doctrine\ORM\Mapping as ORM;
7+
use Symfony\Component\Validator\Constraints as Assert;
8+
9+
#[ORM\Entity(repositoryClass: UserRepository::class)]
10+
class User
11+
{
12+
#[ORM\Id]
13+
#[ORM\GeneratedValue]
14+
#[ORM\Column]
15+
private ?int $id = null;
16+
17+
#[ORM\Column]
18+
private ?\DateTimeImmutable $createdAt = null;
19+
20+
#[ORM\Column(length: 255)]
21+
#[Assert\NotNull(message:"Le prenom ne peut pas être null")]
22+
#[Assert\NotBlank(message:"Le prenom ne peut pas être vide")]
23+
#[Assert\Length(min:3,minMessage:"Le prenom doit etre plus long")]
24+
private ?string $prenom = null;
25+
26+
#[ORM\Column(length: 255)]
27+
private ?string $nom = null;
28+
29+
#[ORM\Column(length: 255, nullable: true)]
30+
private ?string $email = null;
31+
32+
#[ORM\Column(length: 255, nullable: true)]
33+
private ?string $telephone = null;
34+
35+
#[ORM\Column]
36+
private ?int $age = null;
37+
38+
public function getId(): ?int
39+
{
40+
return $this->id;
41+
}
42+
43+
public function getCreatedAt(): ?\DateTimeImmutable
44+
{
45+
return $this->createdAt;
46+
}
47+
48+
public function setCreatedAt(\DateTimeImmutable $createdAt): static
49+
{
50+
$this->createdAt = $createdAt;
51+
52+
return $this;
53+
}
54+
55+
public function getPrenom(): ?string
56+
{
57+
return $this->prenom;
58+
}
59+
60+
public function setPrenom(string $prenom): static
61+
{
62+
$this->prenom = $prenom;
63+
64+
return $this;
65+
}
66+
67+
public function getNom(): ?string
68+
{
69+
return $this->nom;
70+
}
71+
72+
public function setNom(string $nom): static
73+
{
74+
$this->nom = $nom;
75+
76+
return $this;
77+
}
78+
79+
public function getEmail(): ?string
80+
{
81+
return $this->email;
82+
}
83+
84+
public function setEmail(?string $email): static
85+
{
86+
$this->email = $email;
87+
88+
return $this;
89+
}
90+
91+
public function getTelephone(): ?string
92+
{
93+
return $this->telephone;
94+
}
95+
96+
public function setTelephone(?string $telephone): static
97+
{
98+
$this->telephone = $telephone;
99+
100+
return $this;
101+
}
102+
103+
public function getAge(): ?int
104+
{
105+
return $this->age;
106+
}
107+
108+
public function setAge(int $age): static
109+
{
110+
$this->age = $age;
111+
112+
return $this;
113+
}
114+
}

src/Form/UserType.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Form;
4+
5+
use App\Entity\User;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
8+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
9+
use Symfony\Component\Form\Extension\Core\Type\TextType;
10+
use Symfony\Component\Form\FormBuilderInterface;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
13+
class UserType extends AbstractType
14+
{
15+
public function buildForm(FormBuilderInterface $builder, array $options): void
16+
{
17+
$builder
18+
->add('prenom',TextType::class,[])
19+
->add('nom',TextType::class,[])
20+
->add('email',TextareaType::class,[])
21+
->add('telephone',TextType::class,[])
22+
->add('age',IntegerType::class,[])
23+
;
24+
}
25+
26+
public function configureOptions(OptionsResolver $resolver): void
27+
{
28+
$resolver->setDefaults([
29+
'data_class' => User::class,
30+
]);
31+
}
32+
}

src/Repository/UserRepository.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Repository;
4+
5+
use App\Entity\User;
6+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Doctrine\Persistence\ManagerRegistry;
8+
9+
/**
10+
* @extends ServiceEntityRepository<User>
11+
*
12+
* @method User|null find($id, $lockMode = null, $lockVersion = null)
13+
* @method User|null findOneBy(array $criteria, array $orderBy = null)
14+
* @method User[] findAll()
15+
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
16+
*/
17+
class UserRepository extends ServiceEntityRepository
18+
{
19+
public function __construct(ManagerRegistry $registry)
20+
{
21+
parent::__construct($registry, User::class);
22+
}
23+
24+
// /**
25+
// * @return User[] Returns an array of User objects
26+
// */
27+
// public function findByExampleField($value): array
28+
// {
29+
// return $this->createQueryBuilder('u')
30+
// ->andWhere('u.exampleField = :val')
31+
// ->setParameter('val', $value)
32+
// ->orderBy('u.id', 'ASC')
33+
// ->setMaxResults(10)
34+
// ->getQuery()
35+
// ->getResult()
36+
// ;
37+
// }
38+
39+
// public function findOneBySomeField($value): ?User
40+
// {
41+
// return $this->createQueryBuilder('u')
42+
// ->andWhere('u.exampleField = :val')
43+
// ->setParameter('val', $value)
44+
// ->getQuery()
45+
// ->getOneOrNullResult()
46+
// ;
47+
// }
48+
}

templates/base.html.twig

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
{% endblock %}
1414
</head>
1515
<body>
16-
{% block body %}{% endblock %}
16+
<div class="container mt-3">
17+
{% block body %}{% endblock %}
18+
</div>
1719
</body>
1820
</html>

templates/test_user/index.html.twig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block title %}Hello TestUserController!{% endblock %}
4+
5+
{% block body %}
6+
<style>
7+
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
8+
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
9+
</style>
10+
11+
<div class="example-wrapper">
12+
<h1>Hello {{ controller_name }}! ✅</h1>
13+
14+
This friendly message is coming from:
15+
<ul>
16+
<li>Your controller at <code><a href="{{ '/Users/nours/Documents/Enseignement/2023-2024/BTS SIO 2/sf-secure-entities/src/Controller/TestUserController.php'|file_link(0) }}">src/Controller/TestUserController.php</a></code></li>
17+
<li>Your template at <code><a href="{{ '/Users/nours/Documents/Enseignement/2023-2024/BTS SIO 2/sf-secure-entities/templates/test_user/index.html.twig'|file_link(0) }}">templates/test_user/index.html.twig</a></code></li>
18+
</ul>
19+
</div>
20+
{% endblock %}

templates/user/_delete_form.html.twig

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<form method="post" action="{{ path('app_user_delete', {'id': user.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
2+
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ user.id) }}">
3+
<button class="btn btn-danger mt-1">Delete</button>
4+
</form>

templates/user/_form.html.twig

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ form_start(form) }}
2+
{{ form_widget(form) }}
3+
<button class="btn btn-success">{{ button_label|default('Save') }}</button>
4+
{{ form_end(form) }}

templates/user/edit.html.twig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block title %}Edit User{% endblock %}
4+
5+
{% block body %}
6+
<h1>Edit User</h1>
7+
8+
{{ include('user/_form.html.twig', {'button_label': 'Update'}) }}
9+
10+
<a href="{{ path('app_user_index') }}" class="btn btn-danger mt-1">{{ button_label|default('Back') }}</a>
11+
12+
13+
{{ include('user/_delete_form.html.twig') }}
14+
{% endblock %}

0 commit comments

Comments
 (0)