Add flash messages and convert to use Breadcrumb class

This commit is contained in:
Ryan Prather 2024-12-10 12:20:59 -05:00
parent edbfe93a41
commit be580ac843

View File

@ -8,6 +8,7 @@ use App\Entity\User;
use App\Entity\UserCase; use App\Entity\UserCase;
use App\Form\MemberCaseFormType; use App\Form\MemberCaseFormType;
use App\Form\UserCaseFormType; use App\Form\UserCaseFormType;
use App\Libs\Breadcrumb;
use App\Libs\NavList; use App\Libs\NavList;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -41,7 +42,7 @@ class CaseController extends AbstractController
$this->navLinks, $this->navLinks,
[ [
'breadcrumbs' => [ 'breadcrumbs' => [
'List Cases' new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases')
], ],
'notifications' => $user->retrieveUnreadNotifications(), 'notifications' => $user->retrieveUnreadNotifications(),
'cases' => $cases, 'cases' => $cases,
@ -69,6 +70,8 @@ class CaseController extends AbstractController
$this->entityManager->persist($case); $this->entityManager->persist($case);
$this->entityManager->flush(); $this->entityManager->flush();
$this->addFlash('success', 'Case added successfully');
return $this->redirectToRoute('app_list_cases'); return $this->redirectToRoute('app_list_cases');
} }
@ -79,8 +82,8 @@ class CaseController extends AbstractController
[ [
'title' => 'Add Case', 'title' => 'Add Case',
'breadcrumbs' => [ 'breadcrumbs' => [
'Case', new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases'),
'Add Case' new Breadcrumb($this->generateUrl('app_add_case'), 'Add Case')
], ],
'notifications' => $admin->retrieveUnreadNotifications(), 'notifications' => $admin->retrieveUnreadNotifications(),
'form' => $form, 'form' => $form,
@ -103,9 +106,10 @@ class CaseController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$case = $form->getData(); $case = $form->getData();
$this->entityManager->persist($case);
$this->entityManager->flush(); $this->entityManager->flush();
$this->addFlash('success', 'Case updated successfully');
return $this->redirectToRoute('app_list_cases'); return $this->redirectToRoute('app_list_cases');
} }
@ -116,8 +120,8 @@ class CaseController extends AbstractController
[ [
'title' => 'Edit Case', 'title' => 'Edit Case',
'breadcrumbs' => [ 'breadcrumbs' => [
'Case', new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases'),
'Edit Case' new Breadcrumb($this->generateUrl('app_edit_case', ['id' => $id]), 'Edit Case')
], ],
'notifications' => $admin->retrieveUnreadNotifications(), 'notifications' => $admin->retrieveUnreadNotifications(),
'form' => $form, 'form' => $form,
@ -136,15 +140,21 @@ class CaseController extends AbstractController
$caseWorkers = $this->entityManager->getRepository(User::class)->getCaseWorkers(); $caseWorkers = $this->entityManager->getRepository(User::class)->getCaseWorkers();
$case = $this->entityManager->getRepository(MemberCase::class)->find($id); $case = $this->entityManager->getRepository(MemberCase::class)->find($id);
$prevUc = $this->entityManager->getRepository(UserCase::class)->findBy(['memberCase' => $case]); $prevUc = $this->entityManager->getRepository(UserCase::class)->findBy(['memberCase' => $case]);
$uc = new UserCase(); $uc = new UserCase();
$form = $this->createForm(UserCaseFormType::class, $uc); $form = $this->createForm(UserCaseFormType::class, $uc);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$user = $form->get('user')->getData();
$uc = $form->getData(); $uc = $form->getData();
$uc->setMemberCase($case); $uc->setMemberCase($case);
$uc->setUser($form->get('user')->getData()); $uc->setUser($user);
if(!$uc->checkLevel()) {
throw new Exception('');
}
if (count($prevUc) > 0) { if (count($prevUc) > 0) {
$this->entityManager->remove($prevUc[0]); $this->entityManager->remove($prevUc[0]);
@ -154,6 +164,8 @@ class CaseController extends AbstractController
$this->entityManager->persist($uc); $this->entityManager->persist($uc);
$this->entityManager->flush(); $this->entityManager->flush();
$this->addFlash('success', 'Case assigned successfully');
return $this->redirectToRoute('app_list_cases'); return $this->redirectToRoute('app_list_cases');
} }
@ -164,8 +176,8 @@ class CaseController extends AbstractController
[ [
'title' => 'Assign Case', 'title' => 'Assign Case',
'breadcrumbs' => [ 'breadcrumbs' => [
'Case', new Breadcrumb($this->generateUrl('app_list_cases'), 'List Cases'),
'Assign Case' new Breadcrumb($this->generateUrl('app_assign_case', ['id' => $id]), 'Assign Case')
], ],
'notifications' => $admin->retrieveUnreadNotifications(), 'notifications' => $admin->retrieveUnreadNotifications(),
'form' => $form, '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);
}
} }