src/Controller/BioMedWasteController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BioMedWaste;
  4. use App\Form\BioMedWasteType;
  5. use App\Repository\BioMedWasteRepository;
  6. use App\Service\Pagination;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\{RequestResponse};
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Nzo\UrlEncryptorBundle\Annotations\ParamDecryptor;
  12. #[Route(path'/bio-med-waste')]
  13. class BioMedWasteController extends AbstractController
  14. {
  15.     #[Route(path'/'name'bio_med_waste_index'methods: ['GET'])]
  16.     #[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')")]
  17.     public function index(Request $requestBioMedWasteRepository $repoPagination $pagination): Response
  18.     {
  19.         $query $repo->findByDepartment($this->getDepartment());
  20.         $wastes $pagination->paginate($query$request$this->getParameter('items_per_page'));
  21.         return $this->render('bio_med_waste/index.html.twig', [
  22.             'wastes' => $wastes,
  23.             'last_page' => $pagination->lastPage($wastes)
  24.         ]);
  25.     }
  26.     #[Route(path'/new'name'bio_med_waste_new'methods: ['GET''POST'])]
  27.     public function new(Request $requestBioMedWasteRepository $bmwRepo): Response
  28.     {
  29.         $bioMedWaste = new BioMedWaste();
  30.         $bioMedWaste
  31.             ->setDepartment($this->getDepartment())
  32.             ->setHospital($this->getUser()->getHospital());
  33.         $form $this->createForm(BioMedWasteType::class, $bioMedWaste);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $bmwRepo->save($bioMedWastetrue);
  37.             $this->addFlash(
  38.                 'success',
  39.                 'New Bio-medical Waste created successfully!'
  40.             );
  41.             return $this->redirectToRoute('bio_med_waste_index', [], Response::HTTP_SEE_OTHER);
  42.         }
  43.         return $this->renderForm('bio_med_waste/new.html.twig', [
  44.             'waste' => $bioMedWaste,
  45.             'form' => $form
  46.         ]);
  47.     }
  48.     #[Route(path'/dispose/{id}'name'bio_med_waste_dispose'methods: ['GET'])]
  49.     #[ParamDecryptor(["id"])]
  50.     public function dispose(BioMedWaste $bioMedWasteBioMedWasteRepository $bmwRepo): Response
  51.     {
  52.         if ($bioMedWaste->getIsDisposed() === true) {
  53.             $this->addFlash(
  54.                 'danger',
  55.                 'Bio-medical Waste already disposed!'
  56.             );
  57.         } else {
  58.             $bioMedWaste->setIsDisposed(true);
  59.             $bmwRepo->save($bioMedWastetrue);
  60.             $this->addFlash(
  61.                 'success',
  62.                 'Bio-medical Waste disposed successfully!'
  63.             );
  64.         }
  65.         return $this->redirectToRoute('bio_med_waste_index', [], Response::HTTP_SEE_OTHER);
  66.     }
  67.     private function getDepartment()
  68.     {
  69.         return match (true) {
  70.             $this->isGranted('ROLE_LAB_TECH') => 'Lab',
  71.             $this->isGranted('ROLE_RAD') => 'Radiology',
  72.             $this->isGranted('ROLE_WARD') => 'General Ward',
  73.             $this->isGranted('ROLE_CHILD_WARD') => 'Child Care Ward',
  74.             $this->isGranted('ROLE_WOMAN_WARD') => 'Woman Care Ward',
  75.             $this->isGranted('ROLE_ICU_WARD') => 'ICU Ward',
  76.             $this->isGranted('ROLE_SPW_WARD') => 'Special Ward',
  77.             $this->isGranted('ROLE_SURG_WARD') => 'Surgical Ward',
  78.             $this->isGranted('ROLE_OT_WARD') => 'OT Ward',
  79.             $this->isGranted('ROLE_DENT_WARD') => 'Dental Ward',
  80.             default => 'Pharmacy'
  81.         };
  82.     }
  83. }