<?php
namespace App\Form;
use App\Entity\{
Cases,
ClinicalPattern,
DiseaseCategory,
District,
DoctorCategory,
Hospital,
LabTest,
Ncd,
Patient,
ProvisionalDiagnosis,
ProvisionalExamination,
State,
Symptom,
Taluka,
User,
Ward,
};
use Doctrine\ORM\{EntityRepository, EntityManagerInterface};
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\Type\{ChoiceType, DateType, EmailType, TextareaType, HiddenType};
use Symfony\Component\Form\{AbstractType, FormBuilderInterface, FormEvent, FormEvents, FormInterface};
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\{GreaterThanOrEqual, LessThanOrEqual};
class CaseType extends AbstractType
{
public function __construct(private EntityManagerInterface $em)
{
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$hospital = $options['hospital'];
$builder
->add('patientCategory', ChoiceType::class, [
'choices' => [
'Appointment for follow up' => 'Appointment for follow up',
'Delivery case' => 'Delivery case',
'Dental case' => 'Dental case',
'Emergency case' => 'Emergency case',
'Laboratory case' => 'Laboratory case',
'Medicolegal case' => 'Medicolegal case',
'New born child case' => 'New born child case',
'New OPD patient for check up' => 'New OPD patient for check up',
'Outbreak patient' => 'Outbreak patient',
'Physiotherapy case' => 'Physiotherapy case',
'Radiology case' => 'Radiology case',
'Referral in Case' => 'Referral in Case',
'Registration & appointment' => 'Registration & appointment'
],
'placeholder' => 'Select Category',
'label' => 'Patient Category'
]);
$builder
->add('abha_number', HiddenType::class, array(
'required' => true,
'data' => ''
));
if (!$builder->getData()->getId()) {
$builder->add('patient', EntityType::class, [
'class' => Patient::class,
'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('p')
->where('p.isAlive = true'),
'choice_attr' => ChoiceList::attr($this, function (?Patient $patient) {
return $patient ? ['data-photo' => $patient->getphoto(), 'data-abha' => $patient->getHealthIdNumber()] : [];
}),
'placeholder' => 'Select Patient'
]);
}
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$form = $event->getForm();
$data = $event->getData();
$diseaseCategory = $data->getDiseaseCategory();
$this->addElements($form, $data, $diseaseCategory, $options['role']);
});
$diseaseCatRepo = $this->em->getRepository(DiseaseCategory::class);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($diseaseCatRepo, $options) {
$form = $event->getForm();
$case = $event->getData();
$caseObject = null;
if (is_array($case)) {
$diseaseCategory = $diseaseCatRepo->find($case['diseaseCategory']);
} elseif ($case instanceof Cases) {
$diseaseCategory = $case->getDiseaseCategory();
$caseObject = $case;
} else {
$diseaseCategory = null;
}
$this->addElements($form, $caseObject, $diseaseCategory, $options['role']);
});
if ($options['role'] == 'recep') {
$builder
->add('department', EntityType::class, [
'label' => 'Department',
'mapped' => false,
'class' => DoctorCategory::class,
'placeholder' => 'Select Department'
])
->add('doctor', EntityType::class, [
'class' => User::class,
'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('u')
->innerJoin('u.profile', 'sp')
->where("u.roles LIKE '%ROLE_DOC%' OR u.roles LIKE '%ROLE_CHO%' OR u.roles LIKE '%ROLE_AYUSH%'")
->andWhere('sp.hospital = :hospital')
->setParameter('hospital', $hospital),
'placeholder' => 'Select Doctor'
]);
} else {
$builder
->add('familyHistory', null, ['label' => 'Family History'])
->add('medicalHistory', null, ['label' => 'Medical History'])
->add('medicineHistory', null, ['label' => 'Medicine History'])
->add('currentDiseaseHistory', null, ['label' => 'History of present Illness'])
->add('confirmedDiagnosis', null, ['label' => 'Other Confirmed Diagnosis'])
->add('temparature')
->add('cns', null, ['label' => 'CNS'])
->add('endrocine', null, ['label' => 'Endocrine'])
->add('excretory')
->add('pulse', null, ['label' => 'Pulse'])
->add('bpUpper', null, ['label' => 'BP', 'required' => true, 'constraints' => [new NotBlank()]])
->add('bpLower', null, ['constraints' => [new NotBlank()]])
->add('respiration', null, ['required' => true, 'constraints' => [new NotBlank()]])
->add('auscultation')
->add('localExamination', null, ['label' => 'Local Examination'])
->add('icd10CauseGroup', null, [
'label' => 'Major Cause Group as per ICD 10',
'placeholder' => 'Select Major Cause Group'
])
->add('icd10CauseSubGroup', null, [
'label' => 'Major Cause Sub-group as per ICD 10',
'placeholder' => 'Select Major Cause Sub-group'
])
->add('icd10Disease', null, [
'placeholder' => 'Select Confirmed Diagnosis',
'label' => 'Confirmed Diagnosis as per ICD 10'
])
->add('isLab', null, [
'label' => 'Laboratory Tests Required'
])
->add('isMedicine', null, [
'label' => 'Medicines Required'
])
->add('isWard', null, [
'label' => 'Admit to Ward'
])
->add('ward', EntityType::class, [
'class' => Ward::class,
'label' => 'Ward Type',
'placeholder' => 'Select Ward Type',
'mapped' => false,
'required' => false,
'data' => $builder->getData()->getId() && $builder->getData()->getWardBed() ? $builder->getData()->getWardBed()->getWard() : null,
'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('w')
->where('w.hospital = :hospital')
->setParameter('hospital', $hospital)
])
->add('isOpinion', null, ['label' => 'Opinion Required'])
->add('isReferral', null, ['label' => 'Refer To'])
->add('ref_type', ChoiceType::class, [
'choices' => [
'Other Doctor' => '0',
'Other Center' => '1',
],
'expanded' => true,
'mapped' => false,
'data' => '0'
])
->add('ref_state', EntityType::class, [
'label' => 'State',
'mapped' => false,
'required' => false,
'class' => State::class,
'placeholder' => 'Select State'
])
->add('ref_district', EntityType::class, [
'label' => 'District',
'mapped' => false,
'required' => false,
'class' => District::class,
'placeholder' => 'Select District'
])
->add('ref_taluka', EntityType::class, [
'label' => 'Taluka',
'mapped' => false,
'required' => false,
'class' => Taluka::class,
'placeholder' => 'Select Taluka'
])
->add('ref_center_type', ChoiceType::class, [
'choices' => [
'PHC' => 'PHC',
'CHC' => 'CHC',
'SDHC' => 'SDHC',
'DHC' => 'DHC',
'MC' => 'MC'
],
'label' => 'Center Type',
'placeholder' => 'Select Center Type',
'required' => false,
'mapped' => false
])
->add('ref_center', EntityType::class, [
'label' => 'Center',
'mapped' => false,
'required' => false,
'class' => Hospital::class,
'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('h')
->where('h.id <> :id')
->setParameter('id', $hospital->getId()),
'placeholder' => 'Select Center'
])
->add('ref_doc_cat', EntityType::class, [
'label' => 'Department',
'mapped' => false,
'required' => false,
'class' => DoctorCategory::class,
'placeholder' => 'Select Department'
])
->add('ref_doctor', EntityType::class, [
'label' => 'Doctor',
'mapped' => false,
'required' => false,
'class' => User::class,
'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('u')
->where("u.roles LIKE '%ROLE_DOC%' OR u.roles LIKE '%ROLE_CHO%' OR u.roles LIKE '%ROLE_AYUSH%'"),
'placeholder' => 'Select Doctor'
])
->add('ref_email', EmailType::class, ['label' => 'Email', 'required' => false, 'mapped' => false])
->add('ref_note', TextareaType::class, ['label' => 'Notes', 'required' => false, 'mapped' => false])
->add('pharmacyPatientNotes', null, ['label' => 'General Notes for Patient'])
->add('pharmacyStaffNotes', null, ['label' => 'General Notes for Pharmacy Staff'])
->add('wardStaffNotes', null, ['label' => 'General Notes for Ward Staff'])
->add('isClosed', ChoiceType::class, [
'label' => '',
'choices' => [
'Follow-up' => '0',
'Closure' => '1',
],
'expanded' => true
])
->add(
'followUpDate',
DateType::class,
[
'label' => 'Follow Up Date',
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'html5' => false,
'constraints' => [
new GreaterThanOrEqual([
'value' => new \DateTime(date('Y-m-d')),
'message' => 'Please enter valid date.',
])
],
]
)
->add('closureReason', ChoiceType::class, [
'label' => 'Reason for Closure',
'placeholder' => 'Select Reason for Closure',
'choices' => [
'Treatment completed' => 'Treatment completed',
'Voluntary closure by patient' => 'Voluntary closure by patient',
'Case expired for no activity' => 'Case expired for no activity',
'Death of patient' => 'Death of patient',
'Referred' => 'Referred',
'Uncurable' => 'Uncurable'
]
]);
$symptomData = [];
$ncdData = [];
$clinicalPatternData = [];
$provisionalExaminationData = [];
$provisionalDiagnosisData = [];
$case = $builder->getData();
foreach ($case->getSymptoms() as $caseSymptom) {
$symptomData[] = $caseSymptom->getSymptom();
}
foreach ($case->getNcds() as $caseNcd) {
$ncdData[] = $caseNcd->getNcd();
}
foreach ($case->getClinicalPatterns() as $caseClinicalPattern) {
$clinicalPatternData[] = $caseClinicalPattern->getClinicalPattern();
}
foreach ($case->getProvisionalExaminations() as $caseProvisionalExamination) {
$provisionalExaminationData[] = $caseProvisionalExamination->getProvisionalExamination();
}
foreach ($case->getProvisionalDiagnosis() as $caseProvisionalDiagnosis) {
$provisionalDiagnosisData[] = $caseProvisionalDiagnosis->getProvisionalDiagnosis();
}
$builder
->add('ncds', EntityType::class, [
'label' => 'Non-communicable Diseases',
'multiple' => true,
'required' => false,
'mapped' => false,
'class' => Ncd::class,
'data' => $ncdData
])
->add('clinical_patterns', EntityType::class, [
'label' => 'Clinical Patterns',
'multiple' => true,
'required' => false,
'mapped' => false,
'class' => ClinicalPattern::class,
'data' => $clinicalPatternData
])
->add('provisional_examinations', EntityType::class, [
'label' => 'General Examinations',
'multiple' => true,
'mapped' => false,
'class' => ProvisionalExamination::class,
'data' => $provisionalExaminationData,
'constraints' => [new NotBlank()]
])
->add('provisional_diagnosis', EntityType::class, [
'label' => 'Provisional Diagnosis',
'multiple' => true,
'mapped' => false,
'class' => ProvisionalDiagnosis::class,
'data' => $provisionalDiagnosisData,
'constraints' => [new NotBlank()]
])
->add('lab_tests', EntityType::class, [
'label' => 'Select Tests',
'multiple' => true,
'mapped' => false,
'required' => false,
'class' => LabTest::class,
'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('l')
->where('l.isGeneral = false AND l.isUserDefined = false')
->orderBy('l.name')
]);
}
}
protected function addElements(FormInterface $form, ?Cases $case, ?DiseaseCategory $diseaseCategory, $role)
{
$symptomData = [];
if ($case) {
foreach ($case->getSymptoms() as $caseSymptom) {
$symptomData[] = $caseSymptom->getSymptom();
}
}
$symptoms = [];
if ($diseaseCategory) {
$symptoms = $diseaseCategory->getSymptoms();
}
if ($role == 'doc') {
$form
->add('diseaseCategory', EntityType::class, [
'label' => 'Disease Category',
'required' => true,
'data' => $diseaseCategory,
'class' => DiseaseCategory::class,
'placeholder' => 'Select Disease Category',
'constraints' => [new NotBlank()]
])
->add('symptoms', EntityType::class, [
'multiple' => true,
'mapped' => false,
'required' => true,
'class' => Symptom::class,
'choices' => $symptoms,
'data' => $symptomData,
'constraints' => [new NotBlank()]
]);
} else {
$form
->add('diseaseCategory', EntityType::class, [
'label' => 'Disease Category',
'required' => false,
'data' => $diseaseCategory,
'class' => DiseaseCategory::class,
'placeholder' => 'Select Disease Category'
])
->add('symptoms', EntityType::class, [
'multiple' => true,
'mapped' => false,
'required' => false,
'class' => Symptom::class,
'choices' => $symptoms,
'data' => $symptomData
]);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Cases::class,
'translation_domain' => false,
'csrf_protection' => false
]);
$resolver->setRequired(['role', 'hospital']);
}
}