From be580ac843f9d68358b929a79e5a11f145df8825 Mon Sep 17 00:00:00 2001
From: Ryan Prather <ryan@rkprather.com>
Date: Tue, 10 Dec 2024 12:20:59 -0500
Subject: [PATCH] Add flash messages and convert to use Breadcrumb class

---
 src/Controller/CaseController.php | 48 +++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 9 deletions(-)

diff --git a/src/Controller/CaseController.php b/src/Controller/CaseController.php
index 67265cb..6f99c1a 100644
--- a/src/Controller/CaseController.php
+++ b/src/Controller/CaseController.php
@@ -8,6 +8,7 @@ use App\Entity\User;
 use App\Entity\UserCase;
 use App\Form\MemberCaseFormType;
 use App\Form\UserCaseFormType;
+use App\Libs\Breadcrumb;
 use App\Libs\NavList;
 use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -41,7 +42,7 @@ class CaseController extends AbstractController
                 $this->navLinks,
                 [
                     'breadcrumbs' => [
-                        'List Cases'
+                        new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases')
                     ],
                     'notifications' => $user->retrieveUnreadNotifications(),
                     'cases' => $cases,
@@ -69,6 +70,8 @@ class CaseController extends AbstractController
             $this->entityManager->persist($case);
             $this->entityManager->flush();
 
+            $this->addFlash('success', 'Case added successfully');
+
             return $this->redirectToRoute('app_list_cases');
         }
 
@@ -79,8 +82,8 @@ class CaseController extends AbstractController
                 [
                     'title' => 'Add Case',
                     'breadcrumbs' => [
-                        'Case',
-                        'Add Case'
+                        new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases'),
+                        new Breadcrumb($this->generateUrl('app_add_case'), 'Add Case')
                     ],
                     'notifications' => $admin->retrieveUnreadNotifications(),
                     'form' => $form,
@@ -103,9 +106,10 @@ class CaseController extends AbstractController
         if ($form->isSubmitted() && $form->isValid()) {
             $case = $form->getData();
 
-            $this->entityManager->persist($case);
             $this->entityManager->flush();
 
+            $this->addFlash('success', 'Case updated successfully');
+
             return $this->redirectToRoute('app_list_cases');
         }
 
@@ -116,8 +120,8 @@ class CaseController extends AbstractController
                 [
                     'title' => 'Edit Case',
                     'breadcrumbs' => [
-                        'Case',
-                        'Edit Case'
+                        new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases'),
+                        new Breadcrumb($this->generateUrl('app_edit_case', ['id' => $id]), 'Edit Case')
                     ],
                     'notifications' => $admin->retrieveUnreadNotifications(),
                     'form' => $form,
@@ -136,15 +140,21 @@ class CaseController extends AbstractController
 
         $caseWorkers = $this->entityManager->getRepository(User::class)->getCaseWorkers();
         $case = $this->entityManager->getRepository(MemberCase::class)->find($id);
+
         $prevUc = $this->entityManager->getRepository(UserCase::class)->findBy(['memberCase' => $case]);
         $uc = new UserCase();
         $form = $this->createForm(UserCaseFormType::class, $uc);
         $form->handleRequest($request);
 
         if ($form->isSubmitted() && $form->isValid()) {
+            $user = $form->get('user')->getData();
             $uc = $form->getData();
             $uc->setMemberCase($case);
-            $uc->setUser($form->get('user')->getData());
+            $uc->setUser($user);
+
+            if(!$uc->checkLevel()) {
+                throw new Exception('');
+            }
 
             if (count($prevUc) > 0) {
                 $this->entityManager->remove($prevUc[0]);
@@ -154,6 +164,8 @@ class CaseController extends AbstractController
             $this->entityManager->persist($uc);
             $this->entityManager->flush();
 
+            $this->addFlash('success', 'Case assigned successfully');
+
             return $this->redirectToRoute('app_list_cases');
         }
 
@@ -164,8 +176,8 @@ class CaseController extends AbstractController
                 [
                     'title' => 'Assign Case',
                     'breadcrumbs' => [
-                        'Case',
-                        'Assign Case'
+                        new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases'),
+                        new Breadcrumb($this->generateUrl('app_assign_case', ['id' => $id]), 'Assign Case')
                     ],
                     'notifications' => $admin->retrieveUnreadNotifications(),
                     'form' => $form,
@@ -176,4 +188,22 @@ class CaseController extends AbstractController
             )
         );
     }
+
+    #[Route('/api/filter-cases-by-user', name: 'ajax_filter_cases_by_user')]
+    public function filterCasesByUser(Request $request): Response
+    {
+        /** @var User $user */
+        $user = $this->entityManager->getRepository(User::class)->find($request->query->get('userId'));
+        /** @var UserCase[] $ucs */
+        $ucs = $this->entityManager->getRepository(UserCase::class)->findBy(['user' => $user]);
+
+        $ret = [];
+        foreach ($ucs as $uc) {
+            $ret[] = $uc->getMemberCase();
+        }
+
+        dump($ret);
+        die;
+        return $this->json($ret);
+    }
 }