<?php
namespace App\Controller;
use App\Entity\BioMedWaste;
use App\Form\BioMedWasteType;
use App\Repository\BioMedWasteRepository;
use App\Service\Pagination;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\Routing\Annotation\Route;
use Nzo\UrlEncryptorBundle\Annotations\ParamDecryptor;
#[Route(path: '/bio-med-waste')]
class BioMedWasteController extends AbstractController
{
#[Route(path: '/', name: 'bio_med_waste_index', methods: ['GET'])]
#[Security("is_granted('ROLE_LAB_TECH') or is_granted('ROLE_RAD') or is_granted('ROLE_PHARMA') or is_granted('ROLE_WARD') or is_granted('ROLE_CHILD_WARD') or is_granted('ROLE_WOMAN_WARD') or is_granted('ROLE_ICU_WARD') or is_granted('ROLE_SPW_WARD') or is_granted('ROLE_SURG_WARD') or is_granted('ROLE_OT_WARD') or is_granted('ROLE_DENT_WARD')")]
public function index(Request $request, BioMedWasteRepository $repo, Pagination $pagination): Response
{
$query = $repo->findByDepartment($this->getDepartment());
$wastes = $pagination->paginate($query, $request, $this->getParameter('items_per_page'));
return $this->render('bio_med_waste/index.html.twig', [
'wastes' => $wastes,
'last_page' => $pagination->lastPage($wastes)
]);
}
#[Route(path: '/new', name: 'bio_med_waste_new', methods: ['GET', 'POST'])]
public function new(Request $request, BioMedWasteRepository $bmwRepo): Response
{
$bioMedWaste = new BioMedWaste();
$bioMedWaste
->setDepartment($this->getDepartment())
->setHospital($this->getUser()->getHospital());
$form = $this->createForm(BioMedWasteType::class, $bioMedWaste);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bmwRepo->save($bioMedWaste, true);
$this->addFlash(
'success',
'New Bio-medical Waste created successfully!'
);
return $this->redirectToRoute('bio_med_waste_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bio_med_waste/new.html.twig', [
'waste' => $bioMedWaste,
'form' => $form
]);
}
#[Route(path: '/dispose/{id}', name: 'bio_med_waste_dispose', methods: ['GET'])]
#[ParamDecryptor(["id"])]
public function dispose(BioMedWaste $bioMedWaste, BioMedWasteRepository $bmwRepo): Response
{
if ($bioMedWaste->getIsDisposed() === true) {
$this->addFlash(
'danger',
'Bio-medical Waste already disposed!'
);
} else {
$bioMedWaste->setIsDisposed(true);
$bmwRepo->save($bioMedWaste, true);
$this->addFlash(
'success',
'Bio-medical Waste disposed successfully!'
);
}
return $this->redirectToRoute('bio_med_waste_index', [], Response::HTTP_SEE_OTHER);
}
private function getDepartment()
{
return match (true) {
$this->isGranted('ROLE_LAB_TECH') => 'Lab',
$this->isGranted('ROLE_RAD') => 'Radiology',
$this->isGranted('ROLE_WARD') => 'General Ward',
$this->isGranted('ROLE_CHILD_WARD') => 'Child Care Ward',
$this->isGranted('ROLE_WOMAN_WARD') => 'Woman Care Ward',
$this->isGranted('ROLE_ICU_WARD') => 'ICU Ward',
$this->isGranted('ROLE_SPW_WARD') => 'Special Ward',
$this->isGranted('ROLE_SURG_WARD') => 'Surgical Ward',
$this->isGranted('ROLE_OT_WARD') => 'OT Ward',
$this->isGranted('ROLE_DENT_WARD') => 'Dental Ward',
default => 'Pharmacy'
};
}
}