src/Form/CaseType.php line 169

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\{
  4.     Cases,
  5.     ClinicalPattern,
  6.     DiseaseCategory,
  7.     District,
  8.     DoctorCategory,
  9.     Hospital,
  10.     LabTest,
  11.     Ncd,
  12.     Patient,
  13.     ProvisionalDiagnosis,
  14.     ProvisionalExamination,
  15.     State,
  16.     Symptom,
  17.     Taluka,
  18.     User,
  19.     Ward,
  20. };
  21. use Doctrine\ORM\{EntityRepositoryEntityManagerInterface};
  22. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  23. use Symfony\Component\Form\ChoiceList\ChoiceList;
  24. use Symfony\Component\Form\Extension\Core\Type\{ChoiceTypeDateTypeEmailTypeTextareaTypeHiddenType};
  25. use Symfony\Component\Form\{AbstractTypeFormBuilderInterfaceFormEventFormEventsFormInterface};
  26. use Symfony\Component\OptionsResolver\OptionsResolver;
  27. use Symfony\Component\Validator\Constraints\NotBlank;
  28. use Symfony\Component\Validator\Constraints\{GreaterThanOrEqualLessThanOrEqual};
  29. class CaseType extends AbstractType
  30. {
  31.     public function __construct(private EntityManagerInterface $em)
  32.     {
  33.     }
  34.     public function buildForm(FormBuilderInterface $builder, array $options)
  35.     {
  36.         $hospital $options['hospital'];
  37.         $builder
  38.             ->add('patientCategory'ChoiceType::class, [
  39.                 'choices' => [
  40.                     'Appointment for follow up' => 'Appointment for follow up',
  41.                     'Delivery case' => 'Delivery case',
  42.                     'Dental case' => 'Dental case',
  43.                     'Emergency case' => 'Emergency case',
  44.                     'Laboratory case' => 'Laboratory case',
  45.                     'Medicolegal case' => 'Medicolegal case',
  46.                     'New born child case' => 'New born child case',
  47.                     'New OPD patient for check up' => 'New OPD patient for check up',
  48.                     'Outbreak patient' => 'Outbreak patient',
  49.                     'Physiotherapy case' => 'Physiotherapy case',
  50.                     'Radiology case' => 'Radiology case',
  51.                     'Referral in Case' => 'Referral in Case',
  52.                     'Registration & appointment' => 'Registration & appointment'
  53.                 ],
  54.                 'placeholder' => 'Select Category',
  55.                 'label' => 'Patient Category'
  56.             ]);
  57.         $builder
  58.             ->add('abha_number'HiddenType::class, array(
  59.                 'required' => true,
  60.                 'data' => ''
  61.             ));
  62.         if (!$builder->getData()->getId()) {
  63.             $builder->add('patient'EntityType::class, [
  64.                 'class' => Patient::class,
  65.                 'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('p')
  66.                     ->where('p.isAlive = true'),
  67.                 'choice_attr' => ChoiceList::attr($this, function (?Patient $patient) {
  68.                     return $patient ? ['data-photo' => $patient->getphoto(), 'data-abha' => $patient->getHealthIdNumber()] : [];
  69.                 }),
  70.                 'placeholder' => 'Select Patient'
  71.             ]);
  72.         }
  73.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
  74.             $form $event->getForm();
  75.             $data $event->getData();
  76.             $diseaseCategory $data->getDiseaseCategory();
  77.             $this->addElements($form$data$diseaseCategory$options['role']);
  78.         });
  79.         $diseaseCatRepo $this->em->getRepository(DiseaseCategory::class);
  80.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($diseaseCatRepo$options) {
  81.             $form $event->getForm();
  82.             $case $event->getData();
  83.             $caseObject null;
  84.             if (is_array($case)) {
  85.                 $diseaseCategory $diseaseCatRepo->find($case['diseaseCategory']);
  86.             } elseif ($case instanceof Cases) {
  87.                 $diseaseCategory $case->getDiseaseCategory();
  88.                 $caseObject $case;
  89.             } else {
  90.                 $diseaseCategory null;
  91.             }
  92.             $this->addElements($form$caseObject$diseaseCategory$options['role']);
  93.         });
  94.         if ($options['role'] == 'recep') {
  95.             $builder
  96.                 ->add('department'EntityType::class, [
  97.                     'label' => 'Department',
  98.                     'mapped' => false,
  99.                     'class' => DoctorCategory::class,
  100.                     'placeholder' => 'Select Department'
  101.                 ])
  102.                 ->add('doctor'EntityType::class, [
  103.                     'class' => User::class,
  104.                     'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('u')
  105.                         ->innerJoin('u.profile''sp')
  106.                         ->where("u.roles LIKE '%ROLE_DOC%' OR u.roles LIKE '%ROLE_CHO%' OR u.roles LIKE '%ROLE_AYUSH%'")
  107.                         ->andWhere('sp.hospital = :hospital')
  108.                         ->setParameter('hospital'$hospital),
  109.                     'placeholder' => 'Select Doctor'
  110.                 ]);
  111.         } else {
  112.             $builder
  113.                 ->add('familyHistory'null, ['label' => 'Family History'])
  114.                 ->add('medicalHistory'null, ['label' => 'Medical History'])
  115.                 ->add('medicineHistory'null, ['label' => 'Medicine History'])
  116.                 ->add('currentDiseaseHistory'null, ['label' => 'History of present Illness'])
  117.                 ->add('confirmedDiagnosis'null, ['label' => 'Other Confirmed Diagnosis'])
  118.                 ->add('temparature')
  119.                 ->add('cns'null, ['label' => 'CNS'])
  120.                 ->add('endrocine'null, ['label' => 'Endocrine'])
  121.                 ->add('excretory')
  122.                 ->add('pulse'null, ['label' => 'Pulse'])
  123.                 ->add('bpUpper'null, ['label' => 'BP''required' => true'constraints' => [new NotBlank()]])
  124.                 ->add('bpLower'null, ['constraints' => [new NotBlank()]])
  125.                 ->add('respiration'null, ['required' => true'constraints' => [new NotBlank()]])
  126.                 ->add('auscultation')
  127.                 ->add('localExamination'null, ['label' => 'Local Examination'])
  128.                 ->add('icd10CauseGroup'null, [
  129.                     'label' => 'Major Cause Group as per ICD 10',
  130.                     'placeholder' => 'Select Major Cause Group'
  131.                 ])
  132.                 ->add('icd10CauseSubGroup'null, [
  133.                     'label' => 'Major Cause Sub-group as per ICD 10',
  134.                     'placeholder' => 'Select Major Cause Sub-group'
  135.                 ])
  136.                 ->add('icd10Disease'null, [
  137.                     'placeholder' => 'Select Confirmed Diagnosis',
  138.                     'label' => 'Confirmed Diagnosis as per ICD 10'
  139.                 ])
  140.                 ->add('isLab'null, [
  141.                     'label' => 'Laboratory Tests Required'
  142.                 ])
  143.                 ->add('isMedicine'null, [
  144.                     'label' => 'Medicines Required'
  145.                 ])
  146.                 ->add('isWard'null, [
  147.                     'label' => 'Admit to Ward'
  148.                 ])
  149.                 ->add('ward'EntityType::class, [
  150.                     'class' => Ward::class,
  151.                     'label' => 'Ward Type',
  152.                     'placeholder' => 'Select Ward Type',
  153.                     'mapped' => false,
  154.                     'required' => false,
  155.                     'data' => $builder->getData()->getId() && $builder->getData()->getWardBed() ? $builder->getData()->getWardBed()->getWard() : null,
  156.                     'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('w')
  157.                         ->where('w.hospital = :hospital')
  158.                         ->setParameter('hospital'$hospital)
  159.                 ])
  160.                 ->add('isOpinion'null, ['label' => 'Opinion Required'])
  161.                 ->add('isReferral'null, ['label' => 'Refer To'])
  162.                 ->add('ref_type'ChoiceType::class, [
  163.                     'choices' => [
  164.                         'Other Doctor' => '0',
  165.                         'Other Center' => '1',
  166.                     ],
  167.                     'expanded' => true,
  168.                     'mapped' => false,
  169.                     'data' => '0'
  170.                 ])
  171.                 ->add('ref_state'EntityType::class, [
  172.                     'label' => 'State',
  173.                     'mapped' => false,
  174.                     'required' => false,
  175.                     'class' => State::class,
  176.                     'placeholder' => 'Select State'
  177.                 ])
  178.                 ->add('ref_district'EntityType::class, [
  179.                     'label' => 'District',
  180.                     'mapped' => false,
  181.                     'required' => false,
  182.                     'class' => District::class,
  183.                     'placeholder' => 'Select District'
  184.                 ])
  185.                 ->add('ref_taluka'EntityType::class, [
  186.                     'label' => 'Taluka',
  187.                     'mapped' => false,
  188.                     'required' => false,
  189.                     'class' => Taluka::class,
  190.                     'placeholder' => 'Select Taluka'
  191.                 ])
  192.                 ->add('ref_center_type'ChoiceType::class, [
  193.                     'choices' => [
  194.                         'PHC' => 'PHC',
  195.                         'CHC' => 'CHC',
  196.                         'SDHC' => 'SDHC',
  197.                         'DHC' => 'DHC',
  198.                         'MC' => 'MC'
  199.                     ],
  200.                     'label' => 'Center Type',
  201.                     'placeholder' => 'Select Center Type',
  202.                     'required' => false,
  203.                     'mapped' => false
  204.                 ])
  205.                 ->add('ref_center'EntityType::class, [
  206.                     'label' => 'Center',
  207.                     'mapped' => false,
  208.                     'required' => false,
  209.                     'class' => Hospital::class,
  210.                     'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('h')
  211.                         ->where('h.id <> :id')
  212.                         ->setParameter('id'$hospital->getId()),
  213.                     'placeholder' => 'Select Center'
  214.                 ])
  215.                 ->add('ref_doc_cat'EntityType::class, [
  216.                     'label' => 'Department',
  217.                     'mapped' => false,
  218.                     'required' => false,
  219.                     'class' => DoctorCategory::class,
  220.                     'placeholder' => 'Select Department'
  221.                 ])
  222.                 ->add('ref_doctor'EntityType::class, [
  223.                     'label' => 'Doctor',
  224.                     'mapped' => false,
  225.                     'required' => false,
  226.                     'class' => User::class,
  227.                     'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('u')
  228.                         ->where("u.roles LIKE '%ROLE_DOC%' OR u.roles LIKE '%ROLE_CHO%' OR u.roles LIKE '%ROLE_AYUSH%'"),
  229.                     'placeholder' => 'Select Doctor'
  230.                 ])
  231.                 ->add('ref_email'EmailType::class, ['label' => 'Email''required' => false'mapped' => false])
  232.                 ->add('ref_note'TextareaType::class, ['label' => 'Notes''required' => false'mapped' => false])
  233.                 ->add('pharmacyPatientNotes'null, ['label' => 'General Notes for Patient'])
  234.                 ->add('pharmacyStaffNotes'null, ['label' => 'General Notes for Pharmacy Staff'])
  235.                 ->add('wardStaffNotes'null, ['label' => 'General Notes for Ward Staff'])
  236.                 ->add('isClosed'ChoiceType::class, [
  237.                     'label' => '',
  238.                     'choices' => [
  239.                         'Follow-up' => '0',
  240.                         'Closure' => '1',
  241.                     ],
  242.                     'expanded' => true
  243.                 ])
  244.                 ->add(
  245.                     'followUpDate',
  246.                     DateType::class,
  247.                     [
  248.                         'label' => 'Follow Up Date',
  249.                         'widget' => 'single_text',
  250.                         'format' => 'yyyy-MM-dd',
  251.                         'html5' => false,
  252.                         'constraints' => [
  253.                             new GreaterThanOrEqual([
  254.                                 'value' => new \DateTime(date('Y-m-d')),
  255.                                 'message' => 'Please enter valid date.',
  256.                             ])
  257.                         ],
  258.                     ]
  259.                 )
  260.                 ->add('closureReason'ChoiceType::class, [
  261.                     'label' => 'Reason for Closure',
  262.                     'placeholder' => 'Select Reason for Closure',
  263.                     'choices' => [
  264.                         'Treatment completed' => 'Treatment completed',
  265.                         'Voluntary closure by patient' => 'Voluntary closure by patient',
  266.                         'Case expired for no activity' => 'Case expired for no activity',
  267.                         'Death of patient' => 'Death of patient',
  268.                         'Referred' => 'Referred',
  269.                         'Uncurable' => 'Uncurable'
  270.                     ]
  271.                 ]);
  272.             $symptomData = [];
  273.             $ncdData = [];
  274.             $clinicalPatternData = [];
  275.             $provisionalExaminationData = [];
  276.             $provisionalDiagnosisData = [];
  277.             $case $builder->getData();
  278.             foreach ($case->getSymptoms() as $caseSymptom) {
  279.                 $symptomData[] = $caseSymptom->getSymptom();
  280.             }
  281.             foreach ($case->getNcds() as $caseNcd) {
  282.                 $ncdData[] = $caseNcd->getNcd();
  283.             }
  284.             foreach ($case->getClinicalPatterns() as $caseClinicalPattern) {
  285.                 $clinicalPatternData[] = $caseClinicalPattern->getClinicalPattern();
  286.             }
  287.             foreach ($case->getProvisionalExaminations() as $caseProvisionalExamination) {
  288.                 $provisionalExaminationData[] = $caseProvisionalExamination->getProvisionalExamination();
  289.             }
  290.             foreach ($case->getProvisionalDiagnosis() as $caseProvisionalDiagnosis) {
  291.                 $provisionalDiagnosisData[] = $caseProvisionalDiagnosis->getProvisionalDiagnosis();
  292.             }
  293.             $builder
  294.                 ->add('ncds'EntityType::class, [
  295.                     'label' => 'Non-communicable Diseases',
  296.                     'multiple' => true,
  297.                     'required' => false,
  298.                     'mapped' => false,
  299.                     'class' => Ncd::class,
  300.                     'data' => $ncdData
  301.                 ])
  302.                 ->add('clinical_patterns'EntityType::class, [
  303.                     'label' => 'Clinical Patterns',
  304.                     'multiple' => true,
  305.                     'required' => false,
  306.                     'mapped' => false,
  307.                     'class' => ClinicalPattern::class,
  308.                     'data' => $clinicalPatternData
  309.                 ])
  310.                 ->add('provisional_examinations'EntityType::class, [
  311.                     'label' => 'General Examinations',
  312.                     'multiple' => true,
  313.                     'mapped' => false,
  314.                     'class' => ProvisionalExamination::class,
  315.                     'data' => $provisionalExaminationData,
  316.                     'constraints' => [new NotBlank()]
  317.                 ])
  318.                 ->add('provisional_diagnosis'EntityType::class, [
  319.                     'label' => 'Provisional Diagnosis',
  320.                     'multiple' => true,
  321.                     'mapped' => false,
  322.                     'class' => ProvisionalDiagnosis::class,
  323.                     'data' => $provisionalDiagnosisData,
  324.                     'constraints' => [new NotBlank()]
  325.                 ])
  326.                 ->add('lab_tests'EntityType::class, [
  327.                     'label' => 'Select Tests',
  328.                     'multiple' => true,
  329.                     'mapped' => false,
  330.                     'required' => false,
  331.                     'class' => LabTest::class,
  332.                     'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('l')
  333.                         ->where('l.isGeneral = false AND l.isUserDefined = false')
  334.                         ->orderBy('l.name')
  335.                 ]);
  336.         }
  337.     }
  338.     protected function addElements(FormInterface $form, ?Cases $case, ?DiseaseCategory $diseaseCategory$role)
  339.     {
  340.         $symptomData = [];
  341.         if ($case) {
  342.             foreach ($case->getSymptoms() as $caseSymptom) {
  343.                 $symptomData[] = $caseSymptom->getSymptom();
  344.             }
  345.         }
  346.         $symptoms = [];
  347.         if ($diseaseCategory) {
  348.             $symptoms $diseaseCategory->getSymptoms();
  349.         }
  350.         if ($role == 'doc') {
  351.             $form
  352.                 ->add('diseaseCategory'EntityType::class, [
  353.                     'label' => 'Disease Category',
  354.                     'required' => true,
  355.                     'data' => $diseaseCategory,
  356.                     'class' => DiseaseCategory::class,
  357.                     'placeholder' => 'Select Disease Category',
  358.                     'constraints' => [new NotBlank()]
  359.                 ])
  360.                 ->add('symptoms'EntityType::class, [
  361.                     'multiple' => true,
  362.                     'mapped' => false,
  363.                     'required' => true,
  364.                     'class' => Symptom::class,
  365.                     'choices' => $symptoms,
  366.                     'data' => $symptomData,
  367.                     'constraints' => [new NotBlank()]
  368.                 ]);
  369.         } else {
  370.             $form
  371.                 ->add('diseaseCategory'EntityType::class, [
  372.                     'label' => 'Disease Category',
  373.                     'required' => false,
  374.                     'data' => $diseaseCategory,
  375.                     'class' => DiseaseCategory::class,
  376.                     'placeholder' => 'Select Disease Category'
  377.                 ])
  378.                 ->add('symptoms'EntityType::class, [
  379.                     'multiple' => true,
  380.                     'mapped' => false,
  381.                     'required' => false,
  382.                     'class' => Symptom::class,
  383.                     'choices' => $symptoms,
  384.                     'data' => $symptomData
  385.                 ]);
  386.         }
  387.     }
  388.     public function configureOptions(OptionsResolver $resolver)
  389.     {
  390.         $resolver->setDefaults([
  391.             'data_class' => Cases::class,
  392.             'translation_domain' => false,
  393.             'csrf_protection' => false
  394.         ]);
  395.         $resolver->setRequired(['role''hospital']);
  396.     }
  397. }