<?php

class notification_avidami {
	
	function notifier_save_avis($data_notif, $contact_log_id)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$tab_ami = array();
		
		$sql = "SELECT listeami_ami
			FROM avidami_liste_ami
			WHERE listeami_contact = '".$contact_log_id."'
			AND listeami_valide = '1'";
		$query_ami = $db->sql_query($sql);
		
		while($result_ami = $db->sql_fetchrow($query_ami))
		{
			$tab_ami[] = $result_ami["listeami_ami"];
		}
		
		$sql = "SELECT notificationtype_id 
			FROM notification_type
			WHERE notificationtype_label = 'avis'";
		$query = $db->sql_query($sql);
		$result = $db->sql_fetchrow($query);
		
		$type = $result["notificationtype_id"];
		
		foreach($tab_ami as $contact_id)
		{
			$sql = "INSERT INTO notification
				(notification_contact,
				notification_message,
				notification_link,
				notification_type)
				VALUE
				('".$contact_id."',
				'".serialize($data_notif)."',
				'points-interets/fiche.html?gplace_id=".$data_notif["pointinteret_gplaceid"]."',
				'".$type."')";
			$db->sql_query($sql);
		}
		
	}
	
	
	function soliciter_avis($data_notif, $demandeur, $tab_destinataire)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$sql = "SELECT notificationtype_id 
			FROM notification_type
			WHERE notificationtype_label = 'solicitation'";
		$query = $db->sql_query($sql);
		$result = $db->sql_fetchrow($query);
		
		$type = $result["notificationtype_id"];
		
		if(!empty($tab_destinataire))
		{
			$sql_insert = "INSERT INTO notification
				(notification_contact,
				notification_message,
				notification_link,
				notification_type)
				VALUES";
			
			$tag = "";
			
			foreach($tab_destinataire as $contact_id)
			{
				$sql_insert .= $tag."('".$contact_id."',
					'".serialize($data_notif)."',
					'',
					'".$type."')";
					
				$tag = ",";
			}
			
			$db->sql_query($sql_insert);
			
			$sql = "SELECT notificationtype_id 
				FROM notification_type
				WHERE notificationtype_label = 'solicitation_envoye'";
			$query = $db->sql_query($sql);
			$result = $db->sql_fetchrow($query);
			
			$type = $result["notificationtype_id"];
			
			$msg = array("etat"=>"solicitation_envoye");
			
			$sql_notif_go = "INSERT INTO notification
				(notification_contact,
				notification_message,
				notification_link,
				notification_type)
				VALUE
				('".$demandeur."',
				'".serialize($msg)."',
				'',
				'".$type."')";
			$db->sql_query($sql_notif_go);
			
			return true;
		}
		
		return false;
	}
	
	
	function get_unread_nb($contact_log_id)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$sql = "SELECT COUNT(notification_id) AS nb_notif
			FROM notification
			WHERE notification_contact = '".$db->sql_inject($contact_log_id)."'
			AND notification_read = '0'";
		$query = $db->sql_query($sql);
		$result = $db->sql_fetchrow($query);
		
		echo $result["nb_notif"];
	}
	
	
	function demande_ami_save($stalker, $victime)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$sql = "SELECT notificationtype_id 
			FROM notification_type
			WHERE notificationtype_label = 'demande_ami'";
		$query = $db->sql_query($sql);
		$result = $db->sql_fetchrow($query);
		
		$type = $result["notificationtype_id"];
		
		$sql = "SELECT contact_id, contact_nom, contact_prenom
			FROM contact
			WHERE contact_id = '".$db->sql_inject($stalker)."'";
		$query = $db->sql_query($sql);
		$result = $db->sql_fetchrow($query);
		
		$result["etat"]="no_reponse";
		
		$sql = "INSERT INTO notification 
			(notification_contact,
			notification_message,
			notification_type)
			VALUE
			('".$db->sql_inject($victime)."',
			'".serialize($result)."',
			'".$type."')";
		$db->sql_query($sql);
		
		return true;
	}
	
	
	function demande_ami_valide($stalker, $victime, $notification_demande)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		//On récupère les infos du destinataire de la demande
		$sql = "SELECT contact_id, contact_nom, contact_prenom
			FROM contact
			WHERE contact_id = '".$db->sql_inject($victime)."'";
		$query = $db->sql_query($sql);
		$result_victime = $db->sql_fetchrow($query);
		
		$result_victime["etat"]="repondu_ok";
		
		/*******************************************************/
		//On met à jour la notification du destinataire de la demande
		//pour qu'elle n'apparaisse plus comme "demande en attente"
		$sql = "SELECT contact_id, contact_nom, contact_prenom
			FROM contact
			WHERE contact_id = '".$db->sql_inject($stalker)."'";
		$query = $db->sql_query($sql);
		$result_stalker = $db->sql_fetchrow($query);
		
		$result_stalker["etat"]="repondu_ok";
		
		$sql = "UPDATE notification
			SET notification_message = '".serialize($result_stalker)."'
			WHERE notification_id = '".$db->sql_inject($notification_demande)."'";
		$db->sql_query($sql);
		/*************************************************************/
		
		//Ici, on notifie au demandeur que la demande est validé
		$sql = "SELECT notificationtype_id 
			FROM notification_type
			WHERE notificationtype_label = 'demande_ami_valide'";
		$query = $db->sql_query($sql);
		$result_type = $db->sql_fetchrow($query);
		
		$type = $result_type["notificationtype_id"];
		
		$sql = "INSERT notification 
			(notification_contact,
			notification_message,
			notification_type)
			VALUE
			('".$db->sql_inject($stalker)."',
			'".serialize($result_victime)."',
			'".$type."')";
		$db->sql_query($sql);
		
		return true;
	}
	
	
	function demande_ami_refus($stalker, $victime, $notification_demande)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		//On récupère les infos du destinataire de la demande
		$sql = "SELECT contact_id, contact_nom, contact_prenom
			FROM contact
			WHERE contact_id = '".$db->sql_inject($victime)."'";
		$query = $db->sql_query($sql);
		$result_victime = $db->sql_fetchrow($query);
		
		$result_victime["etat"]="repondu_no";
		
		/*******************************************************/
		//On met à jour la notification du destinataire de la demande
		//pour qu'elle n'apparaisse plus comme "demande en attente"
		$sql = "SELECT contact_id, contact_nom, contact_prenom
			FROM contact
			WHERE contact_id = '".$db->sql_inject($stalker)."'";
		$query = $db->sql_query($sql);
		$result_stalker = $db->sql_fetchrow($query);
		
		$result_stalker["etat"]="repondu_no";
		
		$sql = "UPDATE notification
			SET notification_message = '".serialize($result_stalker)."'
			WHERE notification_id = '".$db->sql_inject($notification_demande)."'";
		$db->sql_query($sql);
		/*************************************************************/
		
		//Ici, on notifie au demandeur que la demande est refusé
		$sql = "SELECT notificationtype_id 
			FROM notification_type
			WHERE notificationtype_label = 'demande_ami_refuse'";
		$query = $db->sql_query($sql);
		$result_type = $db->sql_fetchrow($query);
		
		$type = $result_type["notificationtype_id"];
		
		$sql = "INSERT notification 
			(notification_contact,
			notification_message,
			notification_type)
			VALUE
			('".$db->sql_inject($stalker)."',
			'".serialize($result_victime)."',
			'".$type."')";
		$db->sql_query($sql);
		
		return true;
	}
	
	
	function notification_commentaire($data_notif, $cible)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$sql = "SELECT notificationtype_id 
			FROM notification_type
			WHERE notificationtype_label = 'reponse_commentaire'";
		$query = $db->sql_query($sql);
		$result = $db->sql_fetchrow($query);
		
		$type = $result["notificationtype_id"];
		
		
		$sql = "INSERT INTO notification
			(notification_contact,
			notification_message,
			notification_link,
			notification_type)
			VALUE
			('".$cible."',
			'".serialize($data_notif)."',
			'points-interets/fiche.html?gplace_id=".$data_notif["gplace_id"]."',
			'".$type."')";
		$db->sql_query($sql);
		
		return true;
	}
	
	
}



?>