cmtracker/src/Controller/MemberController.php

186 lines
8.3 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Member;
use App\Entity\User;
use App\Entity\MemberCase;
use App\Form\MemberFormType;
use App\Libs\Breadcrumb;
use App\Libs\NavList;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
class MemberController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private array $navLinks = [],
) {
$this->navLinks = NavList::LIST;
}
#[Route('/list-members/{id}', name: 'app_case_members')]
public function listMembers(Request $request, #[CurrentUser()] User $user, string $id): Response
{
$case = $this->entityManager->getRepository(MemberCase::class)->find($id);
$members = $this->entityManager->getRepository(Member::class)->getCaseMembersByName($case);
return $this->render(
'internal/cases/members/list-members.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
new Breadcrumb($this->generateUrl('app_case_list'), 'List Cases'),
new Breadcrumb($this->generateUrl('app_case_members', ['id' => $id]), 'List Members'),
],
'notifications' => $user->retrieveUnreadNotifications(),
'case' => $case,
'members' => $members,
]
)
);
}
#[Route('/add-member/{id}', name: 'app_case_add_member')]
public function addMember(Request $request, #[CurrentUser()] User $user, string $id): Response
{
/** @var MemberCase $case */
$case = $this->entityManager->getRepository(MemberCase::class)->find($id);
$member = new Member();
$form = $this->createForm(MemberFormType::class, $member);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$member->setCaseId($case)
->setFirstName($form->get('firstName')->getData())
->setLastName($form->get('lastName')->getData())
->setDob($form->get('dob')->getData())
->setGender($form->get('gender')->getData())
->setRace($form->get('race')->getData())
->setLanguage($form->get('language')->getData())
->setPersonalId($form->get('personalId')->getData())
->setEmergencyContact($form->get('emergencyContact')->getData())
->setEmail($form->get('email')->getData())
->setAddress($form->get('address')->getData())
->setCity($form->get('city')->getData())
->setState($form->get('state')->getData())
->setZip($form->get('zip')->getData())
->setPhone($form->get('phone')->getData())
->setRelationship($form->get('relationship')->getData())
->setDayPhone($form->get('dayPhone')->getData())
->setEveningPhone($form->get('eveningPhone')->getData())
->setCellPhone($form->get('cellPhone')->getData())
->setSchool($form->get('school')->getData())
->setMaritalStatus($form->get('maritalStatus')->getData())
->setChild($form->get('isChild')->getData())
->setParent($form->get('isParent')->getData())
->setAdultChild($form->get('isAdultChild')->getData())
->setLegalGuardian($form->get('isLegalGuardian')->getData())
->setParentsLiveTogether($form->get('parentsLiveTogether')->getData())
->setDcsApproved($form->get('dcsApproved')->getData())
;
$this->entityManager->persist($member);
$this->entityManager->flush();
$this->addFlash(
'success',
'Member added successfully'
);
return $this->redirectToRoute('app_case_members', ['id' => $case->getId()]);
}
return $this->render(
'internal/cases/members/add-member.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
new Breadcrumb($this->generateUrl('app_case_list'), 'List Cases'),
new Breadcrumb($this->generateUrl('app_case_members', ['id' => $id]), 'List Members'),
new Breadcrumb($this->generateUrl('app_case_add_member', ['id' => $id]), 'Add Member'),
],
'notifications' => $user->retrieveUnreadNotifications(),
'case' => $case,
'form' => $form->createView(),
]
)
);
}
#[Route('/case/{caseId}/edit-member/{memberId}', name: 'app_case_edit_member')]
public function editMember(Request $request, #[CurrentUser()] User $user, string $caseId, string $memberId): Response
{
$member = $this->entityManager->getRepository(Member::class)->find($memberId);
$form = $this->createForm(MemberFormType::class, $member);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$member->setFirstName($form->get('firstName')->getData())
->setLastName($form->get('lastName')->getData())
->setDob($form->get('dob')->getData())
->setGender($form->get('gender')->getData())
->setRace($form->get('race')->getData())
->setLanguage($form->get('language')->getData())
->setPersonalId($form->get('personalId')->getData())
->setEmergencyContact($form->get('emergencyContact')->getData())
->setEmail($form->get('email')->getData())
->setAddress($form->get('address')->getData())
->setCity($form->get('city')->getData())
->setState($form->get('state')->getData())
->setZip($form->get('zip')->getData())
->setPhone($form->get('phone')->getData())
->setRelationship($form->get('relationship')->getData())
->setDayPhone($form->get('dayPhone')->getData())
->setEveningPhone($form->get('eveningPhone')->getData())
->setCellPhone($form->get('cellPhone')->getData())
->setSchool($form->get('school')->getData())
->setMaritalStatus($form->get('maritalStatus')->getData())
->setChild($form->get('isChild')->getData())
->setParent($form->get('isParent')->getData())
->setAdultChild($form->get('isAdultChild')->getData())
->setLegalGuardian($form->get('isLegalGuardian')->getData())
->setParentsLiveTogether($form->get('parentsLiveTogether')->getData())
->setDcsApproved($form->get('dcsApproved')->getData())
;
$this->entityManager->flush();
$this->addFlash(
'success',
'Member updated successfully'
);
return $this->redirectToRoute('app_case_members', ['id' => $caseId]);
}
return $this->render(
'internal/cases/members/edit-member.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
new Breadcrumb($this->generateUrl('app_case_list'), 'List Cases'),
new Breadcrumb($this->generateUrl('app_case_members', ['id' => $caseId]), 'List Members'),
new Breadcrumb($this->generateUrl('app_case_edit_member', ['caseId' => $caseId, 'memberId' => $memberId]), 'Edit Member'),
],
'notifications' => $user->retrieveUnreadNotifications(),
'member' => $member,
'form' => $form->createView(),
'caseId' => $caseId,
]
)
);
}
}