Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- require APPPATH . '/libraries/REST_Controller.php';
- class History extends REST_Controller{
- function __construct($config = 'rest') {
- parent::__construct($config);
- $this->load->database();
- $this->load->model('GMapsModel');
- $this->load->model('HistoryModel');
- $this->load->model('EmailModel');
- $this->load->model('SMSModel');
- }
- public function api_post(){
- $headers = $this->input->request_headers();
- if (array_key_exists('Authorization', $headers) && !empty($headers['Authorization'])) {
- $decodedToken = AUTHORIZATION::validateToken($headers['Authorization']);
- if ($decodedToken != false) {
- $date = date("d/m/Y");
- $time = date("h:i:sa");
- $id_user = $this->post('id_user');
- $latitude = $this->post('latitude');
- $longitude = $this->post('longitude');
- $address = $this->post('address');
- $type = $this->post('type');
- $data = array(
- 'id_user' => $id_user,
- 'latitude' => $latitude,
- 'longitude' => $longitude,
- 'address' => $address,
- 'type' => $type
- );
- if($insert_id = $this->HistoryModel->addHistory($id_user,$data)){
- $response['insert_id'] = $insert_id;
- $response['error'] = false;
- $response['message'] = "Add History Success";
- }
- $this->db->select('user1.name as name,user2.telephone as telephone_family,user2.email as email_family,user2.firebase_token as firebase_token_family');
- $this->db->from('families');
- $this->db->join('user as user1','user1.id_user = families.id_user');
- $this->db->join('user as user2','user2.id_user = families.id_user_family');
- $this->db->where('user1.id_user',$id_user);
- $families = $this->db->get()->result();
- $email = array();
- $registrationIds = array();
- $telephone = array();
- foreach($families as $family){
- $name = $family->name;
- array_push($telephone,$family->telephone_family);
- array_push($email,$family->email_family);
- array_push($registrationIds,$family->firebase_token_family);
- }
- if($type == 1){
- $hospital_data = $this->GMapsModel->getNearestHospital($latitude,$longitude);
- $hospital_name = $hospital_data['name'];
- $hospital_address = $hospital_data['formatted_address'];
- $hospital_phone_number = $hospital_data['international_phone_number'];
- $body = "At $address, Latitude : $latitude Longitude : $longitude. Nearest Hospital : $hospital_name";
- $title = "$name have taken a fall";
- $notif_result = $this->HistoryModel->sendNotification($registrationIds,$latitude,$longitude,$address,$body,$title);
- if($notif_result){
- $body = 'html_code';
- if($this->EmailModel->sendMail($email,$name,"$name have taken a fall",$body)){
- $body = "Elder Protect : $name has taken a fall at $date $time. Please check your elder's history on app for detail information.";
- foreach($telephone as $telephone){
- $formatted_telephone = substr_replace($telephone,'0',0,2);
- $status = $this->SMSModel->sendSMS($body,$formatted_telephone);
- }
- $response['SMS'] = $status;
- }
- }
- }else{
- $body = "At $address, Latitude : $latitude Longitude : $longitude";
- $title = "$name in need emergency assistance";
- $notif_result = $this->HistoryModel->sendNotification($registrationIds,$latitude,$longitude,$address,$body,$title);
- if($notif_result){
- $body = 'html_code';
- }
- if($this->EmailModel->sendMail($email,$name,"$name in need emergency assistance",$body)){
- $response['email'] = 'Email Sent';
- }else{
- $response['email'] = 'Failed to send email';
- }
- $body = "Elder Protect : $name in need emergency assistance at $date, $time. Please check your elder's history on app for detail information.";
- foreach($telephone as $telephone){
- $formatted_telephone = substr_replace($telephone,'0',0,2);
- $status = $this->SMSModel->sendSMS($body,$formatted_telephone);
- }
- $response['SMS'] = $status;
- }
- $response['Notification'] = $notif_result;
- $this->set_response($response, REST_Controller::HTTP_OK);
- }
- }else{
- $this->set_response("Unauthorised", REST_Controller::HTTP_UNAUTHORIZED);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement