<?php

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);
}

$mainaction 			= $_POST['mainaction'];			if ($mainaction == '') $mainaction = $_GET['mainaction'];
$reminderid 			= $_POST['reminderid']; 		if ($reminderid == '') $reminderid = $_GET['reminderid'];
$reminderCreationDate 	= $_POST['reminderCreationDate'];
$reminderName 			= $_POST['reminderName'];
$reminderDesc 			= $_POST['reminderDesc'];
$reminderPeriod 		= $_POST['reminderPeriod'];
$reminderDate 			= $_POST['reminderDate'];
$reminderTime 			= $_POST['reminderTime'];
$reminderEmail 			= $_POST['reminderEmail'];
$reminderDay 			= $_POST['reminderDay'];

$querylog="SELECT FU_Reminder 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");

if ($reminderTime == '') $reminderTime = "00:00:00";	 
if ($reminderDate == '') $reminderDate = "NULL";
else $reminderDate = "'".$reminderDate." ".$reminderTime."'";

if ($reminderEmail == '')  	$reminderEmail  = 0;

	//insert into table users_reminder 
if ($mainaction == 'insertReminder' && $reminderid == ''){
	$query = "insert into users_reminder (reminder_creationDate, reminder_name, reminder_desc, reminder_period, reminder_Date, reminder_days, reminder_sendEmail, user_account) values 
			(".GetSQLValueString($reminderCreationDate,'text').",".GetSQLValueString($reminderName,'text').",".GetSQLValueString($reminderDesc,'text').",
			".GetSQLValueString($reminderPeriod,'text').",$reminderDate,".$reminderDay.",".$reminderEmail.", '".$_SESSION['useraccount']."') ";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
	$reminderid = mysql_insert_id();
}

	//update table users_reminder 
if ($mainaction == 'updateReminder' && $reminderid != ''){
	$query = "update users_reminder set reminder_creationDate = ".GetSQLValueString($reminderCreationDate,'text').", 
							reminder_name = ".GetSQLValueString($reminderName,'text').", 
							reminder_desc = ".GetSQLValueString($reminderDesc,'text').", 
							reminder_period = ".GetSQLValueString($reminderPeriod,'text').", 
							reminder_Date = ".$reminderDate.",
							reminder_days = ".$reminderDay.", 
							reminder_sendEmail = ".$reminderEmail."
			where reminder_id = $reminderid ";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
}

	//insert or update table users_reminderdt
if ($message == '' && $reminderid != '' && ($mainaction == 'insertReminder' || $mainaction == 'updateReminder')){
	$count = 0;
	$queryusers = "select Notification_Access.user_code from ".$_SESSION['dbLabel'].".Notification_Access as Notification_Access
			inner join ".$_SESSION['dbLabel'].".users as users on users.user_account = Notification_Access.user_code
			where notification_name = 'User_Reminder' and ifnull(view_access,0) = 1 
			order by Notification_Access.user_code; ";
	$resusers = mysql_query($queryusers, $connection);
	while ($rowusers = mysql_fetch_array($resusers)){
		$chkUserReminder = 'chkUserReminder'.$count;

		if ($$chkUserReminder == '') $$chkUserReminder = 0;
		$querychk = "select reminderdt_id from users_reminderdt where  reminderdt_userCode = '$rowusers[0]' and reminder_id = $reminderid";
		$reschk = mysql_query($querychk, $connection);
		$rowchk = mysql_fetch_row($reschk);
		$reminderdt_id = $rowchk[0];

		if ($reminderdt_id == ''){
			$querydt = "insert into users_reminderdt (reminder_id, reminderdt_userCode, reminderdt_userChecked) values 
						(".$reminderid.", '".$rowusers[0]."',".$$chkUserReminder." )";
			$resultdt = mysql_query($querydt, $connection);
		} else {
			$querydt = "update users_reminderdt set reminderdt_userCode = '".$rowusers[0]."', reminderdt_userChecked = ".$$chkUserReminder."
						where reminderdt_id = $reminderdt_id ";
			$resultdt = mysql_query($querydt, $connection);
		}

		$count++;	
	}
}

	//delete table users_reminder and users_reminderdt
if ($mainaction == 'deleteReminder' && $reminderid != ''){
	$querydt = "delete from users_reminderdt where reminder_id = $reminderid ";
	$resultdt = mysql_query($querydt, $connection);
	if ($resultdt){
		$query = "delete from users_reminder where reminder_id = $reminderid ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	} else $message = mysql_error();


}

if ($mainaction == 'deleteReminder')
	header("Location: ./Form_SearchReminders.php");
else
	echo "<form name='reminder' method='POST' action='Form_Reminder.php' >
		<input type='hidden' name='mainaction' value=''>
		<input type='hidden' name='reminderid' id='reminderid' value=''>
		<input type='hidden' name='message' value='$message' size='200'>
		</form>
		
		<script>
			document.reminder.submit();
		</script>";
exit;		
?>