<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Notification extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Notification_model');
        $this->load->library('form_validation');
        $this->userDetails = $this->session->userdata('isUserLoggedIn');
    }

    public function index()
    {
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'notification/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'notification/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'notification/';
            $config['first_url'] = base_url() . 'notification/';
        }

        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Notification_model->total_rows($q,$this->userDetails['Id']);
        $Notification = $this->Notification_model->get_limit_data($config['per_page'], $start, $q, $this->userDetails['Id']);

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data = array(
            'notification_data' => $Notification,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
        );

        $pageData = array(
            'header'=>array('pageTitle'=>'Notification'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('notification/notification_list',$pageData);
    }

    public function create() 
    {
        $data = array(
            'button' => 'Create',
            'action' => site_url('notification/create_action'),
        'id' => set_value('id'),
        'fsTitle' => set_value('fsTitle'),
	    'fsMessage' => set_value('fsMessage'),
	    'fiStatus' => set_value('fiStatus'),
	    'fdCreateDate' => set_value('fdCreateDate'),
	    'fdUpdateDate' => set_value('fdUpdateDate'),
	);

        $pageData = array(
            'header'=>array('pageTitle'=>'Notification create'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('notification/notification_form',$pageData);
    }
    
    public function create_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
                'fiHotelId' => $this->userDetails['Id'],
                'fsTitle' => $this->input->post('fsTitle',TRUE),
        		'fsMessage' => $this->input->post('fsMessage',TRUE),
        		'fdCreateDate' => date('Y-m-d H:i:s'),
        	); 

        /* 1 for normal broadcast message */
        $deviceLists = $this->db->select('fsToken')->from('token')->where('fiHotelId',$this->userDetails['Id'])->get();
        $deviceLists = $deviceLists->result_array();
        if(count($deviceLists) > 0){
            //print_r($deviceLists);
            $toeknLists = array();
            foreach($deviceLists as $singleToken){
                $toeknLists[] = $singleToken['fsToken'];
            }
            $response = $this->sendAndroidNotification(
                $toeknLists,
                $this->input->post('fsTitle',TRUE),
                $this->input->post('fsMessage',TRUE),
                array('fiNotifyId'=>1)
            );
            //print_r($response); die;
        }        

        // 1 End
        $this->Notification_model->insert($data);
        $this->session->set_flashdata('message', 'Create Record Success');
        redirect(site_url('notification'));
        }
    }

    /* 3 for normal broadcast message */
    function sendAndroidNotification($to,$title,$message,$other_data = array()){
        $url=FCM_SERVER_URL;
        $key=FCM_SERVER_KEY;

        $titleMsg = array(
            'fsTitle'=>$title,
            'fsMessage'=>$message
        );

        $data = array( 
            'body'=>array_merge($titleMsg,$other_data),
            'sound'=>'default',
            'content_available'=>true,
            'priority'=>'high'
        );         
        $notification = array('title' =>$title , 'text' => $message, 'sound' => 'default', 'badge' => '1');
        $fields = array('registration_ids' =>$to,'notification' => $notification,'data' =>$data);
        $headers = array(
            'Authorization:key=' . $key,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//new added
        $result = curl_exec($ch);
        return json_decode($result,true);
    } 
    // 3 for normal broadcast message */

    public function delete($id) 
    {
        $row = $this->Notification_model->get_by_id($id);

        if ($row) {
            $this->Notification_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('notification'));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('notification'));
        }
    }

    public function _rules() 
    {

    $this->form_validation->set_rules('fsTitle', 'fstitle', 'trim|required');
	$this->form_validation->set_rules('fsMessage', 'fsMessage', 'trim|required');
	$this->form_validation->set_rules('id', 'id', 'trim');
	$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }

}

/* End of file category.php */
/* Location: ./application/controllers/category.php */
/* Please DO NOT modify this information : */
/* Generated by Harviacode Codeigniter CRUD Generator 2020-03-13 09:27:44 */
/* http://harviacode.com */