<?php

##############################################################################
# cartons_jaunes.php3                                                                #
#                                                                            #
# Copyright ViralGames SARL 2000                                             #
##############################################################################


include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_tables.php3");
include($BIB."bib_webmaster_forums.php3");
include($BIB."bib_cartons.php3");
include($BIB."bib_mailing.php3");

# pas de timeout possible qql soit l'action effectuée
set_time_limit($TIME_LIMIT);

# le script continue une fois qu'il est lancé qql soit l'état de la connection après
ignore_user_abort(true);

$current_european_date = date("Y-m-d H:i:s", my_local_time());

?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Envoyer un carton à la base</div>
<br>

<?php

/*---------------------------------------------------------------*/
# Envois d'un message
/**------------------------------------------------------------*/

if($submit=="envoyer")
{
	
	$error = "";
	$count = 0;
	//selon les optin demandés (par defaut on mail tous les non isoff)
	$addquery = "";
	if($Destinataires == "acceptNews")
		$addquery = " AND acceptNews=1";
	if($Destinataires == "acceptPartenaires")
		$addquery = " AND acceptPartenaires=1";
	if($Destinataires == "allOptin")
		$addquery = " AND (acceptNews=1 OR acceptPartenaires=1)";

	$fromPlayerRow = db_fetch_assoc(main_db_query("SELECT * FROM Players WHERE NickName='".trim($Expediteur)."'"));
	if($fromPlayerRow[Id] > 0)
	{
		//NickName existe
		$FromId = $fromPlayerRow[Id];		
	}
	else
	{
		//NickName n'existe pas
		$error .= C_LEPSEUDO." - ".$Expediteur." - ".C_NEXISTEPAS." !<br>";
	}
	
			
	if(!isset($TYPE_CARTON[$Color]))
	{
		$error .= "Vous n'avez pas sélectionné de couleur de cartons<br>";
	}
	
	if(strlen(trim($Objet))< 1)
	{
		$error .= "Vous n'avez pas saisi d'objet<br>";
	}
	if(strlen(trim($Text))< 1)
	{
		$error .= "Vous n'avez pas saisi de texte<br>";
	}
	
	if (empty($error))
	{
		# on formatte le texte pour les caractères dur genre ',",\ et NULL (à voir pour la virgule)
		$Text = nl2br(htmlentities($Text));
		
		# nl2br pas fiable
		$Text = ereg_replace("\n", "", $Text); 
		$Text = ereg_replace("\r", "", $Text); 
		
		$toMembers = main_db_query("SELECT Members.*,Players.NickName,Players.Id AS PlayerId FROM Members LEFT JOIN Players ON Members.Id=Players.MemberId WHERE IsOff=0 ".$addquery);
		while($toMember = db_fetch_assoc($toMembers))
		{
			$ToPId = $toMember["PlayerId"];
			
			# on met le postit dans la base
			main_db_query("INSERT INTO Messagerie (DateEnvoi,Objet,FromPId,ToPId,Text,Color) VALUES ('".$current_european_date."','".$Objet."','".$FromId."','".$ToPId."','".$Text."','".$TYPE_CARTON[$Color]."')");
	
			# on incremente le nb de cartons non lus
			//main_db_query("UPDATE Players SET NbNewCartons=".get_messages_non_lus($ToPId)." WHERE Id=".$ToPId);
			
			# On envoit un mail si l'optin Carton est activée
			if( $toMember['OptinCarton'] != 0 )
			{
				#ici on envoie un mail de confirmation d'inscription
				$subject="Vous avez reçu un carton sur ".$GAME_NAME." !";
				$msg = loadTemplate("../Mailing/template_optinCarton.html");
								
				$msg = replaceTag("__LOGIN__", $toMembers[NickName], $msg);
				$msg = replaceTag("__EXPEDITEUR__", $toMembers[NickName], $msg);
				$msg = replaceTag("__GAMEURL__", $SERVER_URL, $msg);
			
				generic_mail($toMember[Email], $subject, $msg,
						"From: ".$GAME_NAME." <".$FROM_FIELD.">\n" . 
						"Reply-To: ".$REPLY_TO_FIELD."\n" . 
						"X-Mailer: PHP/" . phpversion()."\n".
						"MIME-Version: 1.0\n".
						"Content-Type: text/html; charset=iso-8859-1"."\n"
				);

			}
			$count++;
		}
		
		# on remet le text à rien
		$ToNickName = $Objet = $Text="";
		
		
	}
}


if(!isset($Expediteur))
	$Expediteur = $TAB_AUTHORS[0]["NickName"];
	
if($error)
	echo "<div class='error'>".$error."</div>";

if($count > 0)
	echo "<div class='error'>".$count." cartons envoyés</div>";
		
	

$addqueryacceptNews = " AND acceptNews=1";	
$addqueryacceptPartenaires = " AND acceptPartenaires=1";	
$addqueryallOptin = " AND (acceptNews=1 OR acceptPartenaires=1)";
$memberAll = db_fetch_assoc(main_db_query("SELECT COUNT(*) AS Total FROM Members WHERE IsOff=0"));
$memberAcceptNews = db_fetch_assoc(main_db_query("SELECT COUNT(*) AS Total FROM Members WHERE IsOff=0".$addqueryacceptNews));
$memberAcceptPartenaires = db_fetch_assoc(main_db_query("SELECT COUNT(*) AS Total FROM Members WHERE IsOff=0".$addqueryacceptPartenaires));
$memberAllOtpin = db_fetch_assoc(main_db_query("SELECT COUNT(*) AS Total FROM Members WHERE IsOff=0".$addqueryallOptin));
	
?> 
<form>
<table class=smalldark>
	
	<form name="envoyer" method="post">
	
	<tr>	
		<td valign=top>		
			<a name="POST">
			<b>Expediteur :</b> 
			<br><input type="text" class=inputBg2 name="Expediteur" value="<?php echo $Expediteur;?>" />
			<br>
			<a name="POST">
			<b>Destinataire :</b> 
			<br>
			<select class=inputBg2 name="Destinataires" >
				<option value="all" <?php if($Destinataires=="all") echo "selected"?>>Tous (<?php echo $memberAll["Total"];?>)</option>
				<option value="acceptNews" <?php if($Destinataires=="acceptNews") echo "selected"?>>Optin News (<?php echo $memberAcceptNews["Total"];?>)</option>
				<option value="acceptPartenaires" <?php if($Destinataires=="acceptPartenaires") echo "selected"?>>OptIn Partenaires (<?php echo $memberAcceptPartenaires["Total"];?>)</option>
				<option value="allOptin" <?php if($Destinataires=="allOptin") echo "selected"?>>OptIn News et Partenaires (<?php echo $memberAllOtpin["Total"];?>)</option>
			</select>
			<br>
			<b>Objet :</b> 
			<br><input type="text" class=inputBg2 name="Objet" maxlength="100" size="50" value="<?php echo $Objet; ?>">
			<br>
			<b>Type de carton :</b> 
			<br><input type="radio" name="Color" value="Jaune" <?php if($Color=="Jaune") echo "selected"?> onClick="document.getElementById('textCarton').style.backgroundColor='<?php echo $TYPE_CARTON["Jaune"];?>'"> Jaune 
			<br><input type="radio" name="Color" value="Rouge" <?php if($Color=="Rouge") echo "selected"?> onClick="document.getElementById('textCarton').style.backgroundColor='<?php echo $TYPE_CARTON["Rouge"];?>'"> Rouge 
			<br><input type="radio" name="Color" value="Rose" <?php if($Color=="Rose") echo "selected"?> onClick="document.getElementById('textCarton').style.backgroundColor='<?php echo $TYPE_CARTON["Rose"];?>'"> Rose

			<br>
			<b>Texte :</b><br>
			<textarea name="Text"class=inputBg2  cols=60 rows=10 id="textCarton"><?php echo $Text?></textarea><br>
			
		</td>
	</tr>
	<tr>
		<td align=center>						
			<input align=absmiddle type=submit name=submit value="envoyer">
		</td>
	</tr>
	</form>
	
	</table>
</form>