<?php
###############################
# utils pour sport24
###############################

include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_all_config.php3");
include($BIB."bib_check_members.php3");


//on se connect au compte
if($_POST["connectMe"])
{
	$sess = open_page($sessid);
	
	$sess->memberid = $_POST["ConnecToPId"];
	$sess->origine = $ORIGINE;	
	
	$sess->save();	
	
	# on autorise qu'une seule session par joueur
	$sess->clear_similar_sessions();
	
	
	
	/*
	echo $sess->sessid." : ".$_POST["ConnecToPId"];	
	exit();
	*/
	$sess->redirect($MEMBER_PAGE,"sessid=".$sess->sessid);
	exit();
}


####################
# Start functions
####################
function showPlayer($req)
{
	global $SERVER_URL,$MEMBER_PAGE,$ORIGINE;
	
	$player_res = main_db_query($req);
	$player = db_fetch_array($player_res);
	
	$nb_fields = mysql_num_fields($player_res);
	
	echo "<table border=0><tr class=trTitle>";
	for($r=0;$r<$nb_fields;$r++)
	{
		echo "<td>";
		$fieldName = mysql_field_name($player_res,$r);
		echo $fieldName;
		
		if ($fieldName == "Password")
			$iPassword = $r;
		echo "</td>";
	}
	
	# on rajoute le classement
	echo "<td>Place</td>";
	
	echo "</tr><tr bgcolor=white>";
	for($r=0 ; $r < $nb_fields ; $r++)
	{
		echo "<td class=univers>";
		
		if ($iPassword != $r)
			echo $player[$r];
		echo "</td>";
	}
	
	# on rajoute le classement
	if($player[1])
		echo "<td class=univers>".rank($player)."</td>";
	else
		echo "<td class=univers></td>";
		
	echo "</tr></table>";
		
		
	?>
<!--
<table border=0 cellpadding=4 cellspacing=4 class=univers>
	<tr bgcolor="#00CC99">
		<td class=universBold>N°</td>
		<td class=universBold>Pseudo</td>
		<td class=universBold>Pass</td>
		<td class=universBold>Email</td>
		<td class=universBold>Off</td>
		<td class=universBold>Score</td>
		<td class=universBold>Place</td>
		<td class=universBold>Sx</td>
		<td class=universBold>Prenom</td>
		<td class=universBold>Nom</td>
		<td class=universBold>Adresse</td>
		<td class=universBold>CP</td>
		<td class=universBold>Ville</td>
		<td class=universBold>Pays</td>
		<td class=universBold>Tel</td>
	</tr>
	<tr bgcolor=white>
-->
<?php
/*
	echo "<td>". $player[PId]."</td>";
	echo "<td>". $player[NickName]."</td>";
	echo "<td>". $player[Password]."</td>";
	echo "<td>". $player[Email]."</td>";
	echo "<td>". $player[IsOff]."</td>";
	echo "<td>". $player[Score]."</td>";
	echo "<td>". rank($player) ."</td>";
	echo "<td>". $player[Gender]."</td>";
	echo "<td>". $player[FirstName]."</td>";
	echo "<td>". $player[Name]."</td>";
	echo "<td>". $player[Address]."</td>";
	echo "<td>". $player[Zip]."</td>";
	echo "<td>". $player[Town]."</td>";
	echo "<td>". $player[Country]."</td>";
	echo "<td>". $player[Phone];
	echo "</tr></table>";*/
	
	$theM = getmember2($player[0]);
	echo "<br>";
	echo ($theM['IsOff'] == 0) ? "<font color=#70C038>Ce joueur est On" : "<font color=red>Ce joueur est Off";
	echo "</font>";

	//Formulaire de connection
	?>
	<form method="post" target="_blank">
		<input type="hidden" name="connectMe" value="1">
		<input type="hidden" name="ConnecToPId" value="<?php echo $theM['Id'];?>">
		<input type="submit" name="submit" value="Se connecter à son compte"/>
	</form>
	<?php
	echo "<br>";

	return $player[PId];
}
####################
# End function
####################
?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Requêtes diverses</div>
<br>
<?php
if ($ok=="Send")
{
	# change le score d'un joueur
	if ($NewScore!="")
	{
		main_db_query("UPDATE Players SET Score='".$NewScore."' WHERE Id='".$PId."'");
		
		$player=getplayer2($PId);
		$NickName = $player[NickName];
	}

	# change l'email d'un joueur
	if ($NewEmail!="")
	{
		$NewEmail = trim($NewEmail);
		$member=getmember2($PId);
		$player=getplayer2($PId);
		
		echo changePlayerEmail($member['Email'],$NewEmail,$DEBUG);
		
		$NickName = $player[NickName];
	}
	
	
	# désinscrit un joueur via son PId
	if ($SetOff!="")
	{
		//debug( "ici - SetOff:".$SetOff." Erase:".$Erase);
		$member=getmember2($SetOff);
		$player=getplayer2($SetOff);

		if ($IsOff == "Off" or $IsOff == "On")
		{
			echo "Joueur offé";
			$NickName = $player[NickName];
			main_db_query("UPDATE Members SET IsOff='".(($IsOff == "Off") ? "1" : "0")."' WHERE Id='".$member[Id]."'");
		}
		elseif ($Erase == "Erase")
		{			
			$arand = $player[Id]."_RAZ";
			main_db_query("UPDATE Members SET Name='".$arand."', FirstName='".$arand."', Address='".$arand."', Email='".$arand."', Vip='".$arand."' WHERE Id='".$member[Id]."'");
			main_db_query("UPDATE Players SET NickName='".$arand."' WHERE Id='".$player[Id]."'");
			echo "Joueur supprimé : ".$arand." - Pid:".$player[Id]." - Mid:".$member[Id];
			$NickName = $arand;
		}
	}
	
	# donne les infos du joueurs via son pseudo
	if ($NickName!="")
	{
		$req = "SELECT *,Players.Id AS PId FROM Players ".
			"LEFT JOIN Members ON Members.Id=Players.MemberId ".
			"WHERE NickName='".$NickName."'";
		
		$PId = showPlayer($req);
		
	}
	
	# donne les infos du joueurs via son email
	if ($Email!="")
	{
		# on verifie que la personne existe dans Members et dans Players
		# car elle peut existe dans Members sans etre dans Players (pb d'intégrité)
		$test1 = db_fetch_array(main_db_query("SELECT Id FROM Members WHERE Email like '".$Email."'"));
		
		if ($test1)
		{
			$test2 = db_fetch_array(main_db_query("SELECT Id FROM Players WHERE MemberId='".$test1[Id]."'"));
			if ($test2)
			{
				$req = "SELECT *,Players.Id AS PId FROM Players ".
					"LEFT JOIN Members ON Members.Id=Players.MemberId ".
					"WHERE Email like '".$Email."'";
				$PId = showPlayer($req);
			}
			else
			{
				echo "Pb d'intégrité : le joueur existe en Members MId:".$test1[Id]." mais pas en Players<br>";
			}
		}
		else
		{
			echo "Joueur inconnu";
		}
	}
}

?>
<form>
Recherche par Pseudo
<input type=text name=NickName value="">
<input type=submit name=ok value=Send>
</form>

<form>
Recherche par Email :
<input type=text name=Email value="">
<input type=submit name=ok value=Send>
</form>

<form>
Changer l'état (Off) du joueur N° :
<input type=text name=SetOff value="<?php echo $PId ?>">
<input type=hidden name=ok value=Send>
<input type=submit name=IsOff value="0n">
<input type=submit name=IsOff value="Off">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<input type=submit name=Erase value="Erase">
</form>

<form>
Changer l'email du joueur N° :
<input type=text name=PId value="<?php echo $PId ?>">
<br>
Nouvel email : <input size=45 type=text name=NewEmail value="">
<br>
<input type=checkbox name=DEBUG value=1> DEBUG MODE
<br>
<input type=submit name=ok value=Send>
</form>

<form>
Changer le score du joueur N° :
<input type=text name=PId value="<?php echo $PId ?>">
<br>
Nouveau score : <input size=10 type=text name=NewScore value="">
<br>
<input type=submit name=ok value=Send>
</form>
</body>
</html>

