From 9de710bdd6ae84545f834abf9649845e44da1484 Mon Sep 17 00:00:00 2001
From: user <user@imta.fr>
Date: Wed, 26 Mar 2025 04:53:05 +0100
Subject: [PATCH] git pull

---
 .gitignore                                 |  2 +-
 src/Controller/AdminController.php         | 21 ++++---
 src/Controller/PurchaseProofController.php |  4 +-
 src/Entity/Item.php                        |  4 +-
 src/Entity/PurchaseProof.php               |  4 +-
 src/Entity/Wishlist.php                    | 23 ++++----
 src/Repository/WishlistRepository.php      |  6 +-
 templates/admin/dashboard.html.twig        |  5 +-
 templates/admin/users.html.twig            | 66 ++++++++++++++++++++++
 9 files changed, 106 insertions(+), 29 deletions(-)
 create mode 100644 templates/admin/users.html.twig

diff --git a/.gitignore b/.gitignore
index 69f2383d..65da7121 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,4 +9,4 @@ vendor/
 .idea/
 docker/server
 migrations/
-public/uploads
\ No newline at end of file
+public/uploads
diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php
index a4477ee4..d6856fb9 100644
--- a/src/Controller/AdminController.php
+++ b/src/Controller/AdminController.php
@@ -8,7 +8,9 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Attribute\Route;
 use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 
+use App\Entity\User;
 use App\Entity\Item;
 use App\Entity\Wishlist;
 use Twig\TokenParser\UseTokenParser;
@@ -55,31 +57,36 @@ final class AdminController extends AbstractController
         ]);
     }
 
+   
+
     #[Route('/admin/user/{id}/lock', name: 'admin_user_lock', methods: ['POST'])]
-    public function lockUser(User $user, EntityManagerInterface $entityManager): Response
+    public function lockUser(User $user, EntityManagerInterface $entityManager): RedirectResponse
     {
         $user->setIsLocked(true);
         $entityManager->flush();
 
-        return $this->redirectToRoute('admin_users');
+        $this->addFlash('success', "User has been locked.");
+        return $this->redirectToRoute('admin_dashboard');
     }
 
     #[Route('/admin/user/{id}/unlock', name: 'admin_user_unlock', methods: ['POST'])]
-    public function unlockUser(User $user, EntityManagerInterface $entityManager): Response
+    public function unlockUser(User $user, EntityManagerInterface $entityManager): RedirectResponse
     {
         $user->setIsLocked(false);
         $entityManager->flush();
 
-        return $this->redirectToRoute('admin_users');
+        $this->addFlash('success', "User has been unlocked.");
+        return $this->redirectToRoute('admin_dashboard');
     }
 
     #[Route('/admin/user/{id}/delete', name: 'admin_user_delete', methods: ['POST'])]
-    public function deleteUser(User $user, EntityManagerInterface $entityManager): Response
+    public function deleteUser(User $user, EntityManagerInterface $entityManager): RedirectResponse
     {
-        $entityManager->remove($user);
+        $entityManager->remove(object: $user);
         $entityManager->flush();
 
-        return $this->redirectToRoute('admin_users');
+        $this->addFlash('success', "User has been deleted.");
+        return $this->redirectToRoute('admin_dashboard');
     }
 
 }
diff --git a/src/Controller/PurchaseProofController.php b/src/Controller/PurchaseProofController.php
index 935dc5f3..40a13dd1 100644
--- a/src/Controller/PurchaseProofController.php
+++ b/src/Controller/PurchaseProofController.php
@@ -23,8 +23,8 @@ class PurchaseProofController extends AbstractController
         }
 
         $purchaseProof = new PurchaseProof();
-        $purchaseProof->setItem($item);
-
+        $purchaseProof->setItem(item: $item);
+        
         $form = $this->createForm(PurchaseProofType::class, $purchaseProof);
         $form->handleRequest($request);
 
diff --git a/src/Entity/Item.php b/src/Entity/Item.php
index 3ebfe22e..a56313aa 100644
--- a/src/Entity/Item.php
+++ b/src/Entity/Item.php
@@ -39,10 +39,10 @@ class Item
     #[ORM\Column]
     private ?float $price = null;
 
-    #[ORM\OneToOne(mappedBy: 'item', targetEntity: PurchaseProof::class, cascade: ['persist', 'remove'])]
+    #[ORM\OneToOne(mappedBy: 'item', targetEntity: PurchaseProof::class, cascade: ['remove'])]
     private ?PurchaseProof $purchaseProof = null;
 
-    #[ORM\ManyToOne(inversedBy: 'items')]
+    #[ORM\ManyToOne(inversedBy: 'items', cascade: ['remove'])]
     #[ORM\JoinColumn(nullable: false)]
     private ?Wishlist $wishlist = null;
 
diff --git a/src/Entity/PurchaseProof.php b/src/Entity/PurchaseProof.php
index 3667d913..f8638c43 100644
--- a/src/Entity/PurchaseProof.php
+++ b/src/Entity/PurchaseProof.php
@@ -23,8 +23,8 @@ class PurchaseProof
     #[ORM\JoinColumn(nullable: false)]
     private ?Item $item = null;
 
-    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'purchaseProofs')]
-    #[ORM\JoinColumn(nullable: false)]
+    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'purchaseProofs',cascade: ['remove'])]
+    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
     private ?User $buyer = null;
 
     public function getId(): ?int
diff --git a/src/Entity/Wishlist.php b/src/Entity/Wishlist.php
index 60d02f6b..871bfe5b 100644
--- a/src/Entity/Wishlist.php
+++ b/src/Entity/Wishlist.php
@@ -29,11 +29,13 @@ class Wishlist
     /**
      * @var Collection<int, Item>
      */
-    #[ORM\OneToMany(targetEntity: Item::class, mappedBy: 'wishlist', orphanRemoval: true)]
+    #[ORM\OneToMany(targetEntity: Item::class, mappedBy: 'wishlist', orphanRemoval: true, cascade: ['remove'])]
+    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
+
     private Collection $items;
 
-    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'wishlists')]
-    #[ORM\JoinColumn(nullable: false)]
+    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'wishlists', cascade: ['remove'] )]
+    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
     private ?User $owner = null;
 
     public function getOwner(): ?User
@@ -150,14 +152,15 @@ class Wishlist
     }
 
 
-    public function wishlistTotalPrice() {
-        $itemsArray = (($this->items->toArray())) ;
-        
-        $total =  0  ; 
+    public function wishlistTotalPrice(): float
+    {
+        $itemsArray = $this->items->toArray(); // Pas besoin des doubles parenthèses
+        $total = 0;
+    
         for ($i = 0; $i < count($itemsArray); $i++) {
-            $total += $itemsArray[i]->getPrice() ; 
+            $total += $itemsArray[$i]->getPrice(); // Utilisation correcte des crochets
         }
-
-        return $total ; 
+    
+        return $total;
     }
 }
diff --git a/src/Repository/WishlistRepository.php b/src/Repository/WishlistRepository.php
index 637aa1d3..3b71436d 100644
--- a/src/Repository/WishlistRepository.php
+++ b/src/Repository/WishlistRepository.php
@@ -58,8 +58,8 @@ class WishlistRepository extends ServiceEntityRepository
                 usort($rankings, callback: function($a, $b) {return $a['total'] - $b['total'];});
             } else {
                 for ($i = 0; $i < sizeof($rankings) ; $i++ ) {
-                    if ($rankings[i]['total'] < $total ) {
-                        $rankings[i] = ['wishlist' => $wishlist, 'total' => $total] ; 
+                    if ($rankings[$i]['total'] < $total ) {
+                        $rankings[$i] = ['wishlist' => $wishlist, 'total' => $total] ; 
                     }
                 } 
 
@@ -68,7 +68,7 @@ class WishlistRepository extends ServiceEntityRepository
         }
         $result = array() ; 
         for ($i =  0 ; $i < sizeof($rankings) ; $i++ ) {
-            $result[] = $rankings[i]['wishlist'] ; 
+            $result[] = $rankings[$i]['wishlist'] ; 
         }
         return $result;
     }
diff --git a/templates/admin/dashboard.html.twig b/templates/admin/dashboard.html.twig
index 8c663bf6..ad56b4c4 100644
--- a/templates/admin/dashboard.html.twig
+++ b/templates/admin/dashboard.html.twig
@@ -39,7 +39,7 @@
                         {% for wishlist in topWishlists %}
                             <li class="list-group-item d-flex justify-content-between align-items-center">
                                 {{ wishlist.name }}
-                                <span class="badge bg-success rounded-pill">{{ wishlist.totalValue }} €</span>
+                                <span class="badge bg-success rounded-pill">{{ wishlist.wishlistTotalPrice() }} €</span>
                             </li>
                         {% endfor %}
                         </ul>
@@ -72,7 +72,8 @@
                             {% for user in users %}
                                 <tr>
                                     <td>{{ user.id }}</td>
-                                    <td>{{ user.username }}</td>
+                                    <td>{{ user.firstName }}</td>
+                                    <td>{{ user.lastName }}</td>
                                     <td>{{ user.email }}</td>
                                     <td>
                                         {% if user.isLocked %}
diff --git a/templates/admin/users.html.twig b/templates/admin/users.html.twig
new file mode 100644
index 00000000..f663e661
--- /dev/null
+++ b/templates/admin/users.html.twig
@@ -0,0 +1,66 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}User Management{% endblock %}
+
+{% block body %}
+    <header>
+        <div class="admin-icon"></div>
+        <h1><a href="#">User Management</a></h1>
+        <input type="text" placeholder="Search users…" class="search-bar">
+    </header>
+
+    <main>
+        <div class="container">
+            <section class="form-section">
+                <h1>User List</h1>
+
+                <table class="table">
+                    <thead>
+                    <tr>
+                        <th>Id</th>
+                        <th>First Name</th>
+                        <th>Last Name</th>
+                        <th>Email</th>
+                        <th>Is Locked</th>
+                        <th>Is Admin</th>
+                        <th>Actions</th>
+                    </tr>
+                    </thead>
+                    <tbody>
+                    {% for user in users %}
+                        <tr>
+                            <td>{{ user.id }}</td>
+                            <td>{{ user.firstName }}</td>
+                            <td>{{ user.lastName }}</td>
+                            <td>{{ user.email }}</td>
+                            <td>
+                                {% if user.isLocked %}
+                                    <span class="text-danger">Yes</span>
+                                {% else %}
+                                    <span class="text-success">No</span>
+                                {% endif %}
+                            </td>
+                            <td>
+                                {% if user.isAdmin %}
+                                    <span class="text-primary">Yes</span>
+                                {% else %}
+                                    <span class="text-secondary">No</span>
+                                {% endif %}
+                            </td>
+                            <td>
+                                <a href="{{ path('admin_user_lock', {'id': user.id}) }}" class="btn btn-danger btn-sm">Lock</a>
+                                <a href="{{ path('admin_user_unlock', {'id': user.id}) }}" class="btn btn-success btn-sm">Unlock</a>
+                                <a href="{{ path('admin_user_delete', {'id': user.id}) }}" class="btn btn-warning btn-sm">Delete</a>
+                            </td>
+                        </tr>
+                    {% else %}
+                        <tr>
+                            <td colspan="7">No users found</td>
+                        </tr>
+                    {% endfor %}
+                    </tbody>
+                </table>
+            </section>
+        </div>
+    </main>
+{% endblock %}
\ No newline at end of file
-- 
GitLab