<?php
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

require_once("include/dbcommon.php");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 

$id_user = $_SESSION['id_user'];
$rs = DB::Query("SELECT count(*) count FROM recipients WHERE isRead  = 0 AND user_id = $id_user");
$data = $rs->fetchAssoc();
$count = $data['count'];

$message = '*';
$rs2 = DB::Query("SELECT count(*) count FROM recipients WHERE isNotified  = 0 AND user_id = $id_user order by dateAdd desc");
$data2 = $rs2->fetchAssoc();
$count2 = $data2['count'];

$rs2 = DB::Query("SELECT * FROM recipients WHERE isNotified  = 0 AND user_id = $id_user order by dateAdd desc");

if ( $count2 <> 0 ){ // There are messages to notify
	$data2 = $rs2->fetchAssoc();
	$id_recipients = $data2['id_recipients'];
	$data = array();
	$keyvalues = array();
	$data["isNotified"] = "1";
	$keyvalues["id_recipients"] = $id_recipients;
	DB::Update("recipients", $data, $keyvalues );

	$id_contact = $data2['contact_id'];
	$sql = <<<EOT
	SELECT
	contact.id_contact,
	contact.administrator_id,
	contact.isGroup,
	if(contact.isGroup = 1, contact.nameGroup, user.name) AS name
	FROM contact AS contact
	JOIN member AS member ON ( member.contact_id = contact.id_contact )
	JOIN user user ON ( member.user_id = user.id_user )
	WHERE (contact.isDelete = 0 and user.id_user <> $id_user and contact.id_contact = $id_contact)
EOT;
	$rs = DB::Query($sql);
	$data = $rs->fetchAssoc();
	$name = $data['name'];
	$message = "You have received a new message from: '$name'";
}

echo "$count;$message;";


?>

