<?php



function get_exp_message_nickname($msgId)
{
	$un_message = db_fetch_assoc(main_db_query("SELECT * FROM Messagerie WHERE Id=".$msgId));
	$from = getplayer2($un_message["FromPId"]);	
	//$Text = "\n\n\n/--Message du ".date("m/d/Y H:i", strtotime($un_message["DateEnvoi"]))."--/\n".ereg_replace("<br />", "\n", $un_message["Text"]); 
	return array($from["NickName"],"RE:".$un_message["Objet"],$un_message["Id"]);
}

/*---------------------------------------------------------------*/
# Compte le nombre de message non lus
/**------------------------------------------------------------*/
function get_messages_non_lus($playerId)
{
	global $TYPE_CARTON;
	
	$nonlus = db_fetch_assoc(main_db_query("SELECT count(*) AS total FROM Messagerie WHERE ToPId=".$playerId." AND Etat=".$TYPE_CARTON["Non_lu"]));
	return $nonlus["total"];
}


/*---------------------------------------------------------------*/
# Voir le message
/**------------------------------------------------------------*/
function view_message($playerId,$msgId,$sess)
{
	global $TYPE_CARTON;
	
	
	$un_message = db_fetch_assoc(main_db_query("SELECT * FROM Messagerie WHERE ToPId=".$playerId." AND Id=".$msgId));
	
	//On passe le message a l'etat "lu"
	if($un_message["Etat"] == $TYPE_CARTON["Non_lu"])
		main_db_query("UPDATE Messagerie SET Etat=".$TYPE_CARTON["Lu"]." WHERE ToPId=".$playerId." AND Id=".$msgId);
	
	if($un_message["Id"] < 0)
	{
		$msg = "";
		exit();
	}
	else
	{
		$msg = $un_message["Text"];
		$objet = $un_message["Objet"];
		$msg = $un_message["Text"];
		$color = $un_message["Color"];
		$from = getplayer2($un_message["FromPId"]);		
	}
	
	echo "<table style='padding:2px;border:1px solid #005F3F;width:300px;height:200px;' bgcolor=".$color." ><tr><td valign=top>";	
	echo "<b>De : </b>".$from["NickName"]."</b>, ";
	echo "le </b>".date("m/d/Y à H:i", strtotime($un_message["DateEnvoi"]));	
	echo "<br><b>Objet : </b>".$un_message["Objet"];
	
	echo "<br><br><div style='width:300px;height:125px;margin-bottom: 0px;overflow: auto;'>";
	echo $un_message["Text"];	
	echo "<br></div>";
	echo "</td></tr><tr><td>";
	echo "<tr><td align=right><a href='".$sess->link3($CARTONS_JAUNES_PAGE,"delId=".$un_message["Id"])."' class=smallDarklink>Supprimer</a> ";
	echo "- <a href='".$sess->link3($CARTONS_JAUNES_PAGE,"ansId=".$un_message["Id"],"POST")."' class=smallDarklink>Répondre</a> ";
	echo "- <a href='".$sess->link3($CARTONS_JAUNES_PAGE,"msgId=0","")."' class=smallDarklink>Fermer</a></td></tr>";
	echo "</table>";
	
}

/*---------------------------------------------------------------*/
# Suppression d'un message
/**------------------------------------------------------------*/
function delete_message($playerId,$msgId)
{
	$un_message = db_fetch_assoc(main_db_query("SELECT * FROM Messagerie WHERE ToPId=".$playerId." AND Id=".$msgId));
	
	if($un_message["Id"] > 0)
		main_db_query("DELETE FROM Messagerie WHERE ToPId=".$playerId." AND Id=".$msgId);	

}
?>