<?php
/**

	Send Notifications to Multiple users

**/
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FI_SendNotification as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");

$mode 			= $_GET['mode'];
$note_title 	= $_POST['note_title'];
$note_body  	= $_POST['note_body'];
$noteReceiver  	= $_POST['noteReceiver'];

if ($mode == 'sendNotification'){
	foreach($noteReceiver as $rcvr){
		$queryRef = "select ifnull(max(ifnull(cast(notification_reference as unsigned),0)),0)+1 from notification where notification_createduser = '".$rcvr."' ";
		$resRef = mysql_query($queryRef, $connection);
		$rowRef = mysql_fetch_row($resRef);
		$ref	= $rowRef[0];
			//echo $queryRef;	echo "<br>";
		$queryins = "insert into notification (notification_type, notification_description, notification_reference, notification_createduser, notification_date, notification_checked, createdby_user)
					values 
					(".GetSQLValueString($note_title,'text').", ".GetSQLValueString($note_body,'text').", '$ref', '$rcvr', now(), 0, '".$_SESSION['useraccount']."') ";
		//echo $queryins;	echo "<br>";		
		$resins = mysql_query($queryins, $connection);
	}
	
}

echo "<html><head></head><body>";
echo "<form name='sendnotif' id='sendnotif' method='POST' action='Form_SendNotifications.php'>";
echo "<input type='hidden' name='message' id='message' value='Notification Sent!'>";
echo "</form>";
echo "<script> ";
echo " document.sendnotif.submit(); ";
echo "</script>";
echo "</body></html>";
exit;
?>