diff --git a/src/Controller/StaffController.php b/src/Controller/StaffController.php index eeb3468..b407f3f 100644 --- a/src/Controller/StaffController.php +++ b/src/Controller/StaffController.php @@ -8,7 +8,9 @@ use App\Entity\StaffNote; use App\Entity\Supervision; use App\Entity\User; use App\Entity\UserCase; +use App\Factory\MessageFactory; use App\Form\StaffNoteFormType; +use App\Form\SupervisorStaffNoteFormType; use App\Libs\Breadcrumb; use App\Libs\NavList; use Doctrine\ORM\EntityManagerInterface; @@ -69,10 +71,14 @@ class StaffController extends AbstractController #[Route('/staff/my-cases', name:'app_staff_my_cases')] public function staffMyCases(#[CurrentUser()] User $user): Response { - $this->denyAccessUnlessGranted('IS_FULLY_AUTHENTICATED'); + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); + $sup = $this->entityManager->getRepository(Supervision::class)->findOneBy(['worker' => $user]); + $ucs = $this->entityManager->getRepository(UserCase::class)->findBy(['user' => $user]); $cases = []; $this->navLinks['staff_dashboard'] = 'nav-link text-dark'; @@ -89,6 +95,7 @@ class StaffController extends AbstractController [ 'cases' => $cases, 'user' => $user, + 'supervisor' => $sup->getSupervisor(), 'breadcrumbs' => [ new Breadcrumb($this->generateUrl('app_staff_dashboard'), 'Staff Dashboard'), new Breadcrumb('', 'Staff Cases') @@ -103,7 +110,9 @@ class StaffController extends AbstractController #[Route('/staff/{staffId}', name: 'app_staff_cases')] public function staffCases(string $staffId, #[CurrentUser()] User $user): Response { - $this->denyAccessUnlessGranted(['ROLE_ADMIN', 'ROLE_CASE_MANAGER']); + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); @@ -137,19 +146,23 @@ class StaffController extends AbstractController #[Route('/staff/{staffId}/case/{caseId}/list-notes', name: 'app_staff_list_notes')] public function staffListNotes(string $staffId, string $caseId, #[CurrentUser()] User $user): Response { - $this->denyAccessUnlessGranted('IS_FULLY_AUTHENTICATED'); + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); + $isWorker = ($staffId == $user->getId()->toString()); $staff = $this->entityManager->getRepository(User::class)->find($staffId); $case = $this->entityManager->getRepository(MemberCase::class)->find($caseId); - $staffNotes = $this->entityManager->getRepository(StaffNote::class)->findBy(['memberCase' => $case]); + $staffNotes = $this->entityManager->getRepository(StaffNote::class)->getOrderedNotes($case); return $this->render( 'internal/staff/notes/list-notes.html.twig', array_merge( $this->navLinks, [ + 'isWorker' => $isWorker, 'staffId' => $staffId, 'staff' => $staff, 'case' => $case, @@ -175,7 +188,9 @@ class StaffController extends AbstractController #[Route('/staff/add-note/{caseId}', name: 'app_staff_add_note')] public function addNote(string $caseId, #[CurrentUser()] User $user, Request $request): Response { - $this->denyAccessUnlessGranted('IS_FULLY_AUTHENTICATED'); + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); @@ -190,10 +205,24 @@ class StaffController extends AbstractController $note = $form->getData(); $note->setMemberCase($case); + /** @var Supervision $sup */ + $sup = $this->entityManager->getRepository(Supervision::class)->findOneBy(['worker' => $user]); + + $msg = MessageFactory::createStaffing($user, $sup->getSupervisor()); + + $this->entityManager->persist($msg); $this->entityManager->persist($note); $this->entityManager->flush(); - return $this->redirectToRoute('app_staff_list_notes', ['staffId' => $user->getId()->toHex(), 'caseId' => $caseId]); + $this->addFlash('info', 'Staff note added'); + + return $this->redirectToRoute( + 'app_staff_list_notes', + [ + 'staffId' => $user->getId()->toString(), + 'caseId' => $caseId + ] + ); } return $this->render( @@ -218,15 +247,21 @@ class StaffController extends AbstractController #[Route('/staff/edit-note/{noteId}', name: 'app_staff_edit_note')] public function editNote(string $noteId, #[CurrentUser()] User $user, Request $request): Response { - $this->denyAccessUnlessGranted('IS_FULLY_AUTHENTICATED'); + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); $note = $this->entityManager->getRepository(StaffNote::class)->find($noteId); $case = $note->getMemberCase(); - $form = $this->createForm(StaffNoteFormType::class, $note); + if ($note->supervisorSignDateTime || $note->workerSignDateTime) { + $this->addFlash('error', 'This note has already been signed'); + return $this->redirectToRoute('app_staff_view_note', ['noteId' => $noteId]); + } + $form = $this->createForm(StaffNoteFormType::class, $note); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -234,9 +269,157 @@ class StaffController extends AbstractController $this->addFlash('info', 'Staff notes updated'); - $this->redirectToRoute('app_staff_list_note', ['staffId' => $user->getId()->toHex(), 'caseId' => $case->getId()->toHex()]); + $this->redirectToRoute('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]); } - return new Response(); + return $this->render( + 'internal/staff/notes/edit-note.html.twig', + array_merge( + $this->navLinks, + [ + 'form' => $form, + 'note' => $note, + 'breadcrumbs' => [ + new Breadcrumb($this->generateUrl('app_staff_my_cases'), 'My Cases'), + new Breadcrumb($this->generateUrl('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]), 'Case Notes'), + new Breadcrumb('', 'Edit Note'), + ], + 'notifications' => $this->msgs, + 'notificationCount' => $this->notificationCount, + ] + ) + ); + } + + #[Route('/staff/sign-my-note/{noteId}', name: 'app_staff_sign_my_note')] + public function signNote(string $noteId, #[CurrentUser()] User $user, Request $request): Response + { + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } + $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); + $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); + + $note = $this->entityManager->getRepository(StaffNote::class)->find($noteId); + $case = $note->getMemberCase(); + + if (!$note->getSupervisorSignDateTime()) { + $this->addFlash('error', 'Your supervisor has not signed this note'); + + return $this->redirectToRoute('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]); + } + + $form = $this->createForm(StaffNoteFormType::class, $note); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $note->setWorkerSignDateTime(new \DateTimeImmutable()); + + $this->entityManager->flush(); + + $this->addFlash('info', 'Staff notes signed and saved'); + + $this->redirectToRoute('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]); + } + + return $this->render( + 'internal/staff/notes/sign-note.html.twig', + array_merge( + $this->navLinks, + [ + 'form' => $form, + 'note' => $note, + 'breadcrumbs' => [ + new Breadcrumb($this->generateUrl('app_staff_my_cases'), 'My Cases'), + new Breadcrumb($this->generateUrl('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]), 'Case Notes'), + new Breadcrumb('', 'Sign Note'), + ], + 'notifications' => $this->msgs, + 'notificationCount' => $this->notificationCount, + ] + ) + ); + } + + #[Route('/staff/sup-sign-note/{noteId}', name: 'app_staff_sign_worker_note')] + public function supSignNote(string $noteId, #[CurrentUser()] User $user, Request $request): Response + { + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } + $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); + $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); + + $note = $this->entityManager->getRepository(StaffNote::class)->find($noteId); + $case = $note->getMemberCase(); + + $form = $this->createForm(SupervisorStaffNoteFormType::class, $note); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $note->setSupervisorSignDateTime(new \DateTimeImmutable()); + + /** @var UserCase $uc */ + $uc = $this->entityManager->getRepository(UserCase::class)->findOneBy(['case' => $case]); + + $msg = MessageFactory::createSupervisorSignStaffNote($user, $uc->getUser(), $case); + + $this->entityManager->persist($msg); + $this->entityManager->flush(); + + $this->addFlash('info', 'Staff notes signed and saved'); + + $this->redirectToRoute('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]); + } + + return $this->render( + 'internal/staff/notes/sup-sign-note.html.twig', + array_merge( + $this->navLinks, + [ + 'form' => $form, + 'note' => $note, + 'breadcrumbs' => [ + new Breadcrumb($this->generateUrl('app_staff_my_cases'), 'Staff Dashboard'), + new Breadcrumb($this->generateUrl('app_staff_cases', ['staffId' => $user->getId()->toString()]), 'Cases'), + new Breadcrumb($this->generateUrl('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $case->getId()->toString()]), 'Case Notes'), + new Breadcrumb('', 'Sign Note'), + ], + 'notifications' => $this->msgs, + 'notificationCount' => $this->notificationCount, + ] + ) + ); + } + + #[Route('/staff/view/{noteId}', name: 'app_staff_view_note')] + public function viewNote(string $noteId, #[CurrentUser()] User $user): Response + { + if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { + return $this->redirectToRoute('app_login'); + } + $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); + $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); + + $note = $this->entityManager->getRepository(StaffNote::class)->find($noteId); + + return $this->render( + 'internal/staff/notes/view-note.html.twig', + array_merge( + $this->navLinks, + [ + 'note' => $note, + 'breadcrumbs' => [ + new Breadcrumb($this->generateUrl('app_staff_my_cases'), 'My Cases'), + new Breadcrumb($this->generateUrl('app_staff_list_notes', ['staffId' => $user->getId()->toString(), 'caseId' => $note->getMemberCase()->getId()->toString()]), 'Case Notes'), + new Breadcrumb('', 'View Note'), + ], + 'notifications' => $this->msgs, + 'notificationCount' => $this->notificationCount, + ] + ) + ); } }