<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require ('../credentials.php');
$con=mysqli_connect(HOST, USERNAME, PASSWORD, DBNAME)or die("cannot connect"); 

define('GOOGLE_API_KEY', 'AAAAt2LXVII:APA91bEBop0-4r6XcEmc6rIUyzpKHhxP2vLRE15v5gq9x6AFS7s1RnGmPZTMrm0xEvur_XjIMoWqXqmJXAhsjm5RQpMAZtzhUVgITEsyaixTVYQHAUXIIgRx0TPv8TSUkcb9bZTw1rvs');//Replace with your Key

$pushStatus = '0';






if($_REQUEST['level'] == "All") {$level = "level like '%'";} else {$level = "level='" .$_REQUEST['level']. "'";}
if($_REQUEST['sex'] == "All"){$sex = "sex like '%'";}else{$sex = "sex= '".$_REQUEST['sex']. "'";}
if($_REQUEST['department'] == "All"){$department = "department like '%'";}else{$department = "department='" .$_REQUEST['department']. "'";}
if($_REQUEST['faculty'] == "All"){$faculty = "faculty like '%'";}else{$faculty = "faculty= '".$_REQUEST['faculty']. "'";}


if(isset($_REQUEST['title'])) {
	//$id = $_REQUEST['news_id'];
	$gcmRegIds = array();
	$users_affected = array();
	$sql ="SELECT DISTINCT p.user_id AS user_id, p.gcm_device_token AS gcm_device_token  FROM parent_children pc JOIN parent p ON pc.parent_id=p.user_id ";
	$sql .=" JOIN student s ON pc.student_id = s.user_id"; 
	$sql .=" where ".$level." and ".$sex." and ".$department." and ".$faculty;

    
    $result = mysqli_query($con,$sql);
	
	$sql2 ="";
	
	while($row = mysqli_fetch_assoc($result)){
		
		array_push($gcmRegIds, $row["gcm_device_token"]);
		//$sql2 .= "insert into message_receiver(user_id,news_id) values('".$row["user_id"]."','".$id."'); " ;
	}
	//echo $sql2;
	//mysqli_multi_query($con,$sql2);
	
    $title = $_REQUEST['title'];
	 $body = $_REQUEST['body'];
	  $image = $_REQUEST['image']; 
	  $dateSent= date("Y-m-d h:i:sa");
    if(isset($gcmRegIds) && isset($title)) {
        $message = array('title' => $title,'body'=>$body,'image'=>$image,'date'=>$dateSent);
        $pushStatus = sendPushNotification($gcmRegIds, $message);
        echo $pushStatus ;
    }   
    
}

function sendPushNotification($registration_ids, $message) {
            // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';
		$fields = array(
            'registration_ids' => $registration_ids,
            'data' => $message,
        );
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();
 
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
      curl_setopt($ch,CURLOPT_SSLVERSION,6);
       // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        // Close connection
        curl_close($ch);
		return $result;
}
?>
