Advertisement
vdp

Untitled

vdp
Jun 26th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.92 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. require APPPATH . '/libraries/REST_Controller.php';
  4. class History extends REST_Controller{
  5.     function __construct($config = 'rest') {
  6.       parent::__construct($config);
  7.       $this->load->database();
  8.       $this->load->model('GMapsModel');
  9.       $this->load->model('HistoryModel');
  10.       $this->load->model('EmailModel');
  11.       $this->load->model('SMSModel');
  12.     }
  13.     public function api_post(){
  14.       $headers = $this->input->request_headers();
  15.       if (array_key_exists('Authorization', $headers) && !empty($headers['Authorization'])) {
  16.           $decodedToken = AUTHORIZATION::validateToken($headers['Authorization']);
  17.           if ($decodedToken != false) {
  18.             $date = date("d/m/Y");
  19.             $time = date("h:i:sa");
  20.             $id_user = $this->post('id_user');
  21.             $latitude = $this->post('latitude');
  22.             $longitude = $this->post('longitude');
  23.             $address = $this->post('address');
  24.             $type = $this->post('type');
  25.             $data = array(
  26.               'id_user'  => $id_user,
  27.               'latitude' => $latitude,
  28.               'longitude' => $longitude,
  29.               'address' => $address,
  30.               'type'    => $type
  31.             );
  32.             if($insert_id = $this->HistoryModel->addHistory($id_user,$data)){
  33.               $response['insert_id'] = $insert_id;
  34.               $response['error'] = false;
  35.               $response['message'] = "Add History Success";
  36.             }
  37.             $this->db->select('user1.name as name,user2.telephone as telephone_family,user2.email as email_family,user2.firebase_token as firebase_token_family');
  38.             $this->db->from('families');
  39.             $this->db->join('user as user1','user1.id_user = families.id_user');
  40.             $this->db->join('user as user2','user2.id_user = families.id_user_family');
  41.             $this->db->where('user1.id_user',$id_user);
  42.             $families = $this->db->get()->result();
  43.             $email = array();
  44.             $registrationIds = array();
  45.             $telephone = array();
  46.             foreach($families as $family){
  47.               $name = $family->name;
  48.               array_push($telephone,$family->telephone_family);
  49.               array_push($email,$family->email_family);
  50.               array_push($registrationIds,$family->firebase_token_family);
  51.             }
  52.             if($type == 1){
  53.               $hospital_data = $this->GMapsModel->getNearestHospital($latitude,$longitude);
  54.               $hospital_name = $hospital_data['name'];
  55.               $hospital_address = $hospital_data['formatted_address'];
  56.               $hospital_phone_number = $hospital_data['international_phone_number'];
  57.               $body = "At $address, Latitude : $latitude Longitude : $longitude. Nearest Hospital : $hospital_name";
  58.               $title = "$name have taken a fall";
  59.               $notif_result = $this->HistoryModel->sendNotification($registrationIds,$latitude,$longitude,$address,$body,$title);
  60.               if($notif_result){
  61.                 $body = 'html_code';
  62.               if($this->EmailModel->sendMail($email,$name,"$name have taken a fall",$body)){
  63.                   $body = "Elder Protect : $name has taken a fall at $date $time. Please check your elder's history on app for detail information.";
  64.                   foreach($telephone as $telephone){
  65.                     $formatted_telephone = substr_replace($telephone,'0',0,2);
  66.                     $status = $this->SMSModel->sendSMS($body,$formatted_telephone);
  67.                   }
  68.                   $response['SMS'] = $status;
  69.                 }
  70.               }
  71.             }else{
  72.               $body = "At $address, Latitude : $latitude Longitude : $longitude";
  73.               $title = "$name in need emergency assistance";
  74.               $notif_result = $this->HistoryModel->sendNotification($registrationIds,$latitude,$longitude,$address,$body,$title);
  75.               if($notif_result){
  76.                 $body = 'html_code';
  77.               }
  78.               if($this->EmailModel->sendMail($email,$name,"$name in need emergency assistance",$body)){
  79.                 $response['email'] = 'Email Sent';
  80.               }else{
  81.                 $response['email'] = 'Failed to send email';
  82.               }
  83.               $body = "Elder Protect : $name in need emergency assistance at $date, $time. Please check your elder's history on app for detail information.";
  84.               foreach($telephone as $telephone){
  85.                 $formatted_telephone = substr_replace($telephone,'0',0,2);
  86.                 $status = $this->SMSModel->sendSMS($body,$formatted_telephone);
  87.               }
  88.               $response['SMS'] = $status;
  89.             }
  90.             $response['Notification'] = $notif_result;
  91.             $this->set_response($response, REST_Controller::HTTP_OK);
  92.           }
  93.       }else{
  94.         $this->set_response("Unauthorised", REST_Controller::HTTP_UNAUTHORIZED);
  95.       }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement