<?php
include("config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_mailing.php3");
include($BIB."bib_tables.php3");

function getIdFromNickName($NickName)
{
	global $error;

	$player_row=main_db_query("SELECT Id FROM Players WHERE NickName='$NickName'");
	if (db_num_rows($player_row)==0)
	{
		$error= $error.$NickName.C_NEXISTEPAS." !<br>";
		$getIdFromNickName=0;
	}
	else
	{
		$tmp_row=db_fetch_array($player_row);
		$getIdFromNickName=$tmp_row[Id];
	}
	return $getIdFromNickName;
}

# debut de la presentation
$sess = open_page($sessid, 1);
if ($sess->memberid == -1) $sess->reinit("Error: invalid access to member page");

$member = getmember($sess->memberid);
$player = getplayer($sess->memberid);

if (isset($modif))
{
	$error="";
	unset($player_options_nicknames);
	
	# $r est l'indice sur les cases du formulaire
	# $t est l'indice du tableau a entrer ds la BD
	$r = $t = 1;
	while ($r<=$NB_PLAYER_OPTIONS)
	{
		$var_name = "Player".$r;
		if ($$var_name != "")
		{
			$player_options_ids[$t]=getIdFromNickName($$var_name);
			$t++;
		}
		$r++;
	}
	
	if ($error=="")
	{
		# on delete les anciennes options et on insert les nouvelles
		main_db_query("DELETE FROM PlayerOptions WHERE PlayerId='$player[Id]' AND OptionName > 0");
		
		for ($r=1;$r<=count($player_options_ids);$r++)
		{
			$player_option_rows=main_db_query("INSERT INTO PlayerOptions (PlayerId,OptionName,Value) VALUES ('$player[Id]','$r','".$player_options_ids[$r]."')");
		}
		$sess->redirect($HIGH_PAGE, "type_classement=1&error="."Opération réussie : votre liste d'amis a été modifiée");

	}
}
else
{
	# recup les nick des playeroptions du player
	$player_option_rows=main_db_query("SELECT NickName FROM PlayerOptions LEFT JOIN Players ON PlayerOptions.Value=Players.Id WHERE PlayerId='$player[Id]' AND OptionName > 0 ORDER BY OptionName ASC");
	
	# recup les NickName de chaque joueur de PlayerOptions
	for ($r=1;$r<=db_num_rows($player_option_rows);$r++)
	{
		$var_name="Player".$r;
		$tmp_row=db_fetch_array($player_option_rows);
		$$var_name=$tmp_row[NickName];
	}
}

/*------------ TAG HEADER ----*/
$header_title = "Kestufoot: jeu de pronostics gratuits sur le football, mes amis.";
$header_description = "Cette page vous permet de gérer vos amis du Kestufoot. Vous pouvez leur envoyer des messages sous forme de cartons de différentes couleurs (jaune, rouge, rose) et bloquer certains joueurs. Vous avez aussi la possibilité d’inviter de nouveaux amis et de les relancer à venir jouer avec vous.";
$header_keywords = "amis, cartons jaunes, cartons rouges, inviter des amis, relancer des amis, matchs de foot, pronostics gratuits, jeu de pronostics, jeu de foot.";
/*------------ /TAG HEADER ----*/

$page_courante = "Liste d'amis";

if($sess->memberid != -1)
{
	$sub_header='subh_connecte.inc';
}
else
{
	$sub_header='subh_deconnecte.inc';
} 
 
 
 include("header.inc");




include("mes-amis.inc");

include("footer.inc");
close_page($sess);
?>