Created
May 10, 2017 05:46
-
-
Save iaditya/0b1f95f425e2d49afc173435979a10ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function editCaregivers($id = null) | |
{ | |
if ($this->request->session()->read('Auth.User.role') == "Patient") { | |
$patient_user_id = $this->request->session()->read('Auth.User.id'); | |
$patient = $this->Patients->findByUserId($patient_user_id) | |
->contain(['Users', 'Facilities', 'PatientAddresses', 'Languages'])->first(); | |
} else { | |
$patient = $this->Patients->get($id, [ | |
'contain' => ['Facilities', 'Users', 'PatientAddresses', 'Languages'] | |
]); | |
} | |
$patient_user = $patient->user; | |
// returns associated caregivers | |
$caregiver1 = $this->Patients->Caregivers->findByPatientId($patient->id) | |
->where(['caregiver_type' => 0])->first(); // return Entity OR Null | |
$caregiver2 = $this->Patients->Caregivers->findByPatientId($patient->id) | |
->where(['caregiver_type' => 1])->first(); // return Entity OR Null | |
if ($this->request->is(['patch', 'post', 'put'])) { | |
$new_caregiver_id1 = $this->request->data['caregiver1_id'] ? $this->request->data['caregiver1_id'] : null; | |
$new_caregiver_id2 = $this->request->data['caregiver2_id'] ? $this->request->data['caregiver2_id'] : null; | |
$caregiver1_mobile = $this->request->data['primary_contact_phone1'] ? $this->request->data['primary_contact_phone1'] : null; | |
$caregiver2_mobile = $this->request->data['second_contact_phone1'] ? $this->request->data['second_contact_phone1'] : null; | |
$caregiver1_name = $this->request->data['primary_contact_name'] ? $this->request->data['primary_contact_name'] : null; | |
$caregiver2_name = $this->request->data['second_contact_name'] ? $this->request->data['second_contact_name'] : null; | |
$caregiver1_email = !empty($this->request->data['primary_contact_email']) ? $this->request->data['primary_contact_email'] : null; | |
$caregiver2_email = !empty($this->request->data['second_contact_email']) ? $this->request->data['second_contact_email'] : null; | |
// both email can not be same. | |
if ($caregiver1_email == $caregiver2_email) { | |
if (($caregiver1_email != null) || ($caregiver2_email != null)) { | |
$this->Flash->error(__('Both Caregiver must be different user. Check Email Value')); | |
return $this->redirect(['action' => 'editCaregivers']); | |
} | |
} | |
$patient = $this->Patients->patchEntity($patient, $this->request->data); | |
// primary caregiver | |
if ($patient->dirty('primary_contact_email') || $patient->dirty('primary_contact_phone1')) { | |
$today = Time::now()->toDateString(); | |
$patient_scale_entities = $this->Patients->PatientScales->findByPatientId($patient->id) | |
->where(['PatientScales.completed' => 0]) | |
->andWhere(['PatientScales.caregiver_patient_id IS NOT' => null]) | |
->andWhere(['Date(PatientScales.validity_date) >=' => $today]); | |
//debug("Dirty Primary"); | |
if ($new_caregiver_id1) { | |
// Found Existing user | |
$caregiver_patient = $this->Patients->get($new_caregiver_id1); | |
// send notification | |
$this->sendNotification($patient_user, $caregiver_patient->user_id); | |
$patient->primary_caregiver_active = 1; | |
// updating patientScale Table | |
if($patient_scale_entities->count() > 0) { | |
foreach ($patient_scale_entities as $patient_scale_entity) { | |
$patient_scale_entity->caregiver_patient_id = $new_caregiver_id1; | |
} | |
} | |
// create mapper Entity | |
if ($caregiver1) { | |
$caregiver1->caregiver_patient_id = $new_caregiver_id1; | |
$this->Patients->Caregivers->save($caregiver1); | |
} else { | |
$caregiver1 = $this->Patients->Caregivers->newEntity(); | |
$caregiver1->patient_id = $patient->id; | |
$caregiver1->caregiver_patient_id = $new_caregiver_id1; | |
$caregiver1->caregiver_type = 0; | |
//$caregiver1->active = 0; | |
// saving caregiver association. | |
$this->Patients->Caregivers->save($caregiver1); | |
} | |
} else { | |
if ($this->emailFound($caregiver1_email)) { | |
Log::debug("Email found for primary caregiver : " . $caregiver1_email); | |
// Found Existing user | |
$user = $this->Patients->Users->findByEmail($caregiver1_email)->first(); | |
$caregiver_patient = $this->Patients->findByUserId($user->id)->first(); | |
$new_caregiver_id1 = $caregiver_patient->id; | |
//send notification | |
$this->sendNotification($patient_user, $user->id); | |
$patient->primary_caregiver_active = 1; | |
// updating patientScale Table | |
if($patient_scale_entities->count() > 0) { | |
foreach ($patient_scale_entities as $patient_scale_entity) { | |
$patient_scale_entity->caregiver_patient_id = $new_caregiver_id1; | |
} | |
} | |
// create mapper Entity | |
if ($caregiver1) { | |
$caregiver1->caregiver_patient_id = $new_caregiver_id1; | |
$this->Patients->Caregivers->save($caregiver1); | |
} else { | |
$caregiver1 = $this->Patients->Caregivers->newEntity(); | |
$caregiver1->patient_id = $patient->id; | |
$caregiver1->caregiver_patient_id = $new_caregiver_id1; | |
$caregiver1->caregiver_type = 0; | |
//$caregiver1->active = 0; | |
// saving caregiver association. | |
$this->Patients->Caregivers->save($caregiver1); | |
} | |
} else { | |
if ($caregiver1) { | |
$this->Patients->Caregivers->delete($caregiver1); | |
} | |
if (($caregiver1_mobile != null) && ($caregiver1_name != null)) { | |
$this->addCaregiver($patient, 1); | |
// updating patientScale Table in addCaregiver function | |
} | |
/** | |
* if($patient->age >=18) { | |
* // Non -minor - provider-approval | |
* $patient->primary_caregiver_active = 0; | |
* //$this->Flash->success("Primary Caregiver will be activated after Doctor approval."); | |
* } else { | |
* // mobile and first_name are required. | |
* // minor - creating caregiver. | |
* if( ($caregiver1_mobile != null) && ($caregiver1_name != null ) ) { | |
* $this->addCaregiver($patient, 1); | |
* } | |
* } | |
*/ | |
} | |
} | |
$this->Flash->success("Primary Caregiver details has been updated."); | |
} | |
// secondary caregiver | |
if ($patient->dirty('second_contact_email') || $patient->dirty('second_contact_phone1')) { | |
//debug("Dirty Secondary"); | |
if ($new_caregiver_id2) { | |
// Found Existing user | |
$caregiver_patient = $this->Patients->get($new_caregiver_id2); | |
// send notification | |
$this->sendNotification($patient_user, $caregiver_patient->user_id); | |
$patient->second_caregiver_active = 1; | |
// create mapper Entity | |
if ($caregiver2) { | |
$caregiver2->caregiver_patient_id = $new_caregiver_id2; | |
$this->Patients->Caregivers->save($caregiver2); | |
} else { | |
$caregiver2 = $this->Patients->Caregivers->newEntity(); | |
$caregiver2->patient_id = $patient->id; | |
$caregiver2->caregiver_patient_id = $new_caregiver_id2; | |
$caregiver2->caregiver_type = 1; | |
//$caregiver1->active = 0; | |
// saving caregiver association. | |
$this->Patients->Caregivers->save($caregiver2); | |
} | |
} else { | |
if ($this->emailFound($caregiver2_email)) { | |
Log::debug("Email found for secondary caregiver : " . $caregiver2_email); | |
// Found Existing user | |
$user = $this->Patients->Users->findByEmail($caregiver2_email)->first(); | |
$caregiver_patient = $this->Patients->findByUserId($user->id)->first(); | |
$new_caregiver_id2 = $caregiver_patient->id; | |
//send notification | |
$this->sendNotification($patient_user, $user->id); | |
$patient->second_caregiver_active = 1; | |
// create mapper Entity | |
if ($caregiver2) { | |
$caregiver2->caregiver_patient_id = $new_caregiver_id2; | |
$this->Patients->Caregivers->save($caregiver2); | |
} else { | |
$caregiver2 = $this->Patients->Caregivers->newEntity(); | |
$caregiver2->patient_id = $patient->id; | |
$caregiver2->caregiver_patient_id = $new_caregiver_id2; | |
$caregiver2->caregiver_type = 1; | |
//$caregiver1->active = 0; | |
// saving caregiver association. | |
$this->Patients->Caregivers->save($caregiver2); | |
} | |
} else { | |
if ($caregiver2) { | |
$this->Patients->Caregivers->delete($caregiver2); | |
} | |
if (($caregiver2_mobile != null) && ($caregiver2_name != null)) { | |
$this->addCaregiver($patient, 2); | |
} | |
/** | |
* if (($patient->age) >= 18) { | |
* // Non -minor - provider-approval | |
* $patient->second_caregiver_active = 0; | |
* //$this->Flash->success("Secondary Caregiver will be activated after Doctor approval."); | |
* } else { | |
* // mobile and first_name are required. | |
* // minor - creating caregiver. | |
* if (($caregiver2_mobile != null) && ($caregiver2_name != null)) { | |
* $this->addCaregiver($patient, 2); | |
* } | |
* } | |
*/ | |
} | |
} | |
$this->Flash->success("Secondary Caregiver details has been updated."); | |
} | |
Log::debug("patient edit:" . $patient); | |
if ($this->Patients->save($patient)) { | |
//$this->Flash->success(__('The caregiver has been updated.')); | |
return $this->redirect(['action' => 'editCaregivers']); | |
} else { | |
$this->Flash->error(__('The caregiver could not be updated. Please, try again.')); | |
} | |
} | |
$this->set(compact('patient')); | |
$this->set('_serialize', ['patient']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment