Skip to content

Instantly share code, notes, and snippets.

@iaditya
Created May 8, 2017 06:32
Show Gist options
  • Save iaditya/253013fadef4b81c884bb6e835802821 to your computer and use it in GitHub Desktop.
Save iaditya/253013fadef4b81c884bb6e835802821 to your computer and use it in GitHub Desktop.
public function addCaregiver($patient, $p)
{
if ($p == 1) {
//debug("hh"); exit;
$caregiver1_email = $patient->primary_contact_email ? $patient->primary_contact_email : null;
$caregiver1_mobile = $patient->primary_contact_phone1 ? $patient->primary_contact_phone1 : null;
$caregiver1_name = $patient->primary_contact_name ? $patient->primary_contact_name : null;
$data['caregiver_type'] = 0;
}
if ($p == 2) {
//debug("YY"); exit;
$caregiver1_email = $patient->second_contact_email ? $patient->second_contact_email : null;
$caregiver1_mobile = $patient->second_contact_phone1 ? $patient->second_contact_phone1 : null;
$caregiver1_name = $patient->second_contact_name ? $patient->second_contact_name : null;
$data['caregiver_type'] = 1;
}
$patient_name = $patient->user->first_name;
$data['patient']['user']['not_email'] = 0;
if ($caregiver1_email == null) {
$data['patient']['user']['not_email'] = 1;
} else {
$data['patient']['user']['email'] = $caregiver1_email;
}
// for new user
$data['patient_id'] = $patient->id;
$data['active'] = 0;
$data['patient']['user']['first_name'] = $caregiver1_name;
$data['patient']['user']['last_name'] = " ";
$data['patient']['user']['mobile'] = $caregiver1_mobile;
$data['patient']['patient_uuid'] = Text::uuid();
$data['patient']['user']['role'] = 'Patient';
//$data['patient']['caregiver_label'] = 1;
$data['patient']['user']['username'] = !empty($data['patient']['user']['email']) ? $data['patient']['user']['email'] : $data['patient']['patient_uuid'];
if (!empty($data['patient']['user']['email']))
$data['patient']['email'] = $data['patient']['user']['email'];
$data['patient']['user']['password'] = $data['patient']['user']['mobile'];
$template = "patientAddCareGiver";
if ($data['patient']['user']['not_email']) {
$template = "patientAddCareGiverNoEmail";
$data['patient']['user']['active'] = 1;
$data['patient']['user']['email_enable'] = 0;
} else {
$data['patient']['user']['active'] = 0;
$tokenExpiration = Configure::read('Token.expiration');
$expires = new DateTime();
$expires->modify("+ $tokenExpiration secs");
$data['patient']['user']['token_expires'] = $expires;
$data['patient']['user']['token'] = str_replace('-', '', Text::uuid());
}
//dob, city, postal code, district, state
//debug($data); exit;
$caregiver = $this->Patients->Caregivers->newEntity();
$caregiver = $this->Patients->Caregivers->patchEntity($caregiver, $data, [
'associated' => ['Patients' => ['associated' => ['Users']]
],
]);
//debug($caregiver);
if ($this->Patients->Caregivers->save($caregiver, array('validate' => 'only'))) {
// no provider approval
if ($p == 1) {
$patient->primary_caregiver_active = 1;
}
if ($p == 2) {
$patient->second_caregiver_active = 1;
}
//$this->Patients->save($patient);
$today = Time::now()->toDateString();
$patient_scale_entities = $this->Patients->PatientScales->findByPatientId($patient->id)
->where(['PatientScales.completed' => 0])
->andWhere(['Date(PatientScales.validity_date) >=' => $today]);
// updating patientScale Table
if($patient_scale_entities->count() > 0) {
foreach ($patient_scale_entities as $patient_scale_entity) {
$patient_scale_entity->caregiver_patient_id = $caregiver->caregiver_patient_id;
}
}
// Notification
$recipientLists = $template;
$notificationManager = new NotificationManager();
$notificationManager->addRecipientList($recipientLists, [$caregiver->patient->user_id]);
$this->Notifier->notify([
'recipientLists' => $recipientLists,
'template' => $recipientLists,
'vars' => [
'name' => $patient_name,
'username' => $caregiver->patient->user->username,
'email' => $caregiver->patient->user->username,
'caregiver_name' => $caregiver->patient->user->first_name,
'caregiver_mobile' => $caregiver1_mobile,
],
], $caregiver->patient->user_id);
if ($data['patient']['user']['not_email']) {
//$this->Flash->success(__('No need for Caregiver validation.'));
} else {
$this->getMailer('CakeDC/Users.Users')
->send('validation', [$caregiver->patient->user, __d('Users', 'Your account validation link'), 'CakeDC/Users.caregiver_validation']);
}
} else {
$errors = $caregiver->errors();
//debug($errors); exit;
foreach ($errors as $key1 => $value1) {
foreach ($value1 as $key => $value) {
$this->Flash->error("$key is required.");
//$this->Flash->error(__('The patient could not be created. Please, try again '. $key1.":" .$value));
}
}
$this->Flash->error("Caregiver could not be saved. Login and try again.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment