<?php 

/* mailing_adherents.php
 * - Envoi de mails en masse
 * Copyright (c) 2003 Frédéric Jaqcuot
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 
	include("includes/config.inc.php"); 
	include(WEB_ROOT."includes/database.inc.php"); 
	include(WEB_ROOT."includes/functions.inc.php"); 
	include(WEB_ROOT."includes/lang.inc.php"); 
	include(WEB_ROOT."includes/session.inc.php"); 
	
	if ($_SESSION["logged_status"]==0) 
	{
		header("location: index.php");
		die();
	}
	if ($_SESSION["admin_status"]==0) 
	{
		header("location: voir_adherent.php");
		die();
	}
		
	$mailing_adh = array();
	$nomail_adh = array();
	if (isset($_POST["mailing_adh"]))
		while (list($key,$value)=each($_POST["mailing_adh"]))
			$mailing_adh[]=$value;

	$mailing_corps = "";
	if (isset($_POST["mailing_corps"]))
		$mailing_corps = stripslashes($_POST["mailing_corps"]);

	$mailing_objet = "";
	if (isset($_POST["mailing_objet"]))
		$mailing_objet = stripslashes($_POST["mailing_objet"]);

	$error_detected = "";
	
	$etape = 0;
	if (date("d") > "20")
		$moiscour=date("m")+1;
	else
		$moiscour=date("m");

	if (isset($_POST["mailing_go"]))
	{
		if ($mailing_objet=="")
			$error_detected .= "<LI>"._T("Veuillez indiquer un objet pour le message.")."</LI>";

		if ($mailing_corps=="")
			$error_detected .= "<LI>"._T("Veuillez saisir un message.")."</LI>";
			
		if (!isset($_POST["mailing_adh"]))
			$error_detected .= "<LI>"._T("Veuillez sélectionner au moins un adhérent.")."</LI>";

		if ($error_detected=="")
			$etape = 1;
	}	

	include("header.php");

	if ($etape==0)
	{
		
		if (isset($_GET["filtre_2"]))
			if (is_numeric($_GET["filtre_2"]))
				$_SESSION["filtre_adh_2"]=$_GET["filtre_2"];
	
		if (isset($_GET["filtre_3"]))
			if (is_numeric($_GET["filtre_3"]))
				$_SESSION["filtre_adh_3"]=$_GET["filtre_3"];
		if (isset($_GET["filtre"]))
			if (is_numeric($_GET["filtre"]))
				$_SESSION["filtre_adh"]=$_GET["filtre"];
	
		// Tri
	
		if (isset($_GET["tri"]))
			if (is_numeric($_GET["tri"]))
			{
				if ($_SESSION["tri_adh"]==$_GET["tri"])
					$_SESSION["tri_adh_sens"]=($_SESSION["tri_adh_sens"]+1)%2;
				else
				{
					$_SESSION["tri_adh"]=$_GET["tri"];
					$_SESSION["tri_adh_sens"]=0;
				}
			}
		
		if (isset($_GET["etiquettes"]))
		{
?> 
			<H1 class="titre"><?php echo _T("Génération de feuille d'emargement"); ?></H1>
<?php
		}
		else
		{	
?> 
			<H1 class="titre"><?php echo _T("Mailing"); ?></H1>
<?php
		}
		
		// Affichage des erreurs
		if ($error_detected!="")
		{
?>
		  	<DIV id="errorbox">
		  		<H1><?php echo _T("- ERREUR -"); ?></H1>
		  		<UL>
		  			<?php echo $error_detected; ?>
		  		</UL>
		  	</DIV>
<?php
		}

		// selection des adherents et application filtre / tri
			
		$requete[0] = "SELECT id_adh, nom_adh, prenom_adh, pseudo_adh, activite_adh,
			       libelle_statut, bool_exempt_adh, titre_adh, email_adh, bool_admin_adh, date_echeance
			       FROM ".PREFIX_DB."adherents, ".PREFIX_DB."statuts
			       WHERE ".PREFIX_DB."adherents.id_statut=".PREFIX_DB."statuts.id_statut ";
		$requete[1] = "SELECT count(id_adh)
			       FROM ".PREFIX_DB."adherents
			       WHERE 1=1 ";
									
		// filtre d'affichage des adherents activés/desactivés
		if ($_SESSION["filtre_adh_2"]=="1")
		{
			$requete[0] .= "AND ".PREFIX_DB."adherents.activite_adh='1' ";
			$requete[1] .= "AND ".PREFIX_DB."adherents.activite_adh='1' ";
		}
		if ($_SESSION["filtre_adh_2"]=="2")
		{
			$requete[0] .= "AND ".PREFIX_DB."adherents.activite_adh='0' ";
			$requete[1] .= "AND ".PREFIX_DB."adherents.activite_adh='0' ";
		}
	        // filtre par panier
        	if ($_SESSION["filtre_adh_3"]=="1")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.panier_adh != '-1' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.panier_adh != '-1' ";
	        }
        	elseif ($_SESSION["filtre_adh_3"]=="2")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.pain_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.pain_adh != '0' ";
	        }
        	elseif ($_SESSION["filtre_adh_3"]=="3")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.volaille_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.volaille_adh != '0' ";
	        }
         	elseif ($_SESSION["filtre_adh_3"]=="4")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.boeuf_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.boeuf_adh != '0' ";
	        }
         	elseif ($_SESSION["filtre_adh_3"]=="5")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.veau_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.veau_adh != '0' ";
	        }
         	elseif ($_SESSION["filtre_adh_3"]=="6")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.cochon_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.cochon_adh != '0' ";
	        }
 	      	elseif ($_SESSION["filtre_adh_3"]=="7")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.farine_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.farine_adh != '0' ";
	        }
        	elseif ($_SESSION["filtre_adh_3"]=="8")
	        {
                	$requete[0] .= "AND ".PREFIX_DB."adherents.chevre_adh != '0' ";
        	        $requete[1] .= "AND ".PREFIX_DB."adherents.chevre_adh != '0' ";
	        }

		// filtre d'affichage des adherents à jour
		if ($_SESSION["filtre_adh"]=="3")
		{
			$requete[0] .= "AND (date_echeance > ".$DB->DBDate(time())." OR bool_exempt_adh='1') ";
			$requete[1] .= "AND (date_echeance > ".$DB->DBDate(time())." OR bool_exempt_adh='1') ";
		}

		// filtre d'affichage des adherents retardataires
		if ($_SESSION["filtre_adh"]=="2")
		{
			$requete[0] .= "AND date_echeance < ".$DB->DBDate(time())." ";
			$requete[1] .= "AND date_echeance < ".$DB->DBDate(time())." ";
		}
	
		// filtre d'affichage des adherents bientot a echeance
		if ($_SESSION["filtre_adh"]=="1")
		{
			$requete[0] .= "AND date_echeance > ".$DB->DBDate(time())."
				        AND date_echeance < ".$DB->OffsetDate(30)." ";
			$requete[1] .= "AND date_echeance > ".$DB->DBDate(time())."
				        AND date_echeance < ".$DB->OffsetDate(30)." ";
		}
		
		// phase de tri	
		
		if ($_SESSION["tri_adh_sens"]=="0")
			$tri_adh_sens_txt="ASC";
		else
			$tri_adh_sens_txt="DESC";
	
		$requete[0] .= "ORDER BY ";
		
		// tri par pseudo
		if ($_SESSION["tri_adh"]=="1")
			$requete[0] .= "pseudo_adh ".$tri_adh_sens_txt.",";
			
		// tri par statut
		elseif ($_SESSION["tri_adh"]=="2")
			$requete[0] .= "priorite_statut ".$tri_adh_sens_txt.",";
	
		// tri par echeance
		elseif ($_SESSION["tri_adh"]=="3")
			$requete[0] .= "bool_exempt_adh ".$tri_adh_sens_txt.", date_echeance ".$tri_adh_sens_txt.",";
	
		// defaut : tri par nom, prenom
		$requete[0] .= "nom_adh ".$tri_adh_sens_txt.", prenom_adh ".$tri_adh_sens_txt; 
		
		$resultat = &$DB->Execute($requete[0]);
		$nbadh = &$DB->Execute($requete[1]);
?>
		<SCRIPT LANGUAGE="JavaScript">
		<!--
		var checked = 1; 	
		function check()
		{ 
			for (var i=0;i<document.mailing_form.elements.length;i++)
			{
				var e = document.mailing_form.elements[i];
				if(e.type == "checkbox")
				{
					e.checked = checked;
				}
			}
			checked = !checked;
		}
		-->
		</SCRIPT>
		<TABLE id="infoline" width="100%">
			<TR>
				<TD class="left"><?php echo $nbadh->fields[0]." "; if ($nbadh->fields[0]!=1) echo _T("adhérents"); else echo _T("adhérent"); ?></TD>
				<TD class="right">
					<DIV id="listfilter">
						<FORM action="mailing_adherents.php" method="get" name="filtre">
						 	<?php echo _T("Afficher :"); ?>&nbsp;
							<SELECT name="filtre" onChange="form.submit()">
								<OPTION value="0"<?php isSelected("0",$_SESSION["filtre_adh"]) ?>><?php echo _T("Tous les adhérents"); ?></OPTION>
								<OPTION value="3"<?php isSelected("3",$_SESSION["filtre_adh"]) ?>><?php echo _T("Les adhérents à jour"); ?></OPTION>
								<OPTION value="1"<?php isSelected("1",$_SESSION["filtre_adh"]) ?>><?php echo _T("Les échéances proches"); ?></OPTION>
								<OPTION value="2"<?php isSelected("2",$_SESSION["filtre_adh"]) ?>><?php echo _T("Les retardataires"); ?></OPTION>
							</SELECT>
							<SELECT name="filtre_2" onChange="form.submit()">
								<OPTION value="0"<?php isSelected("0",$_SESSION["filtre_adh_2"]) ?>><?php echo _T("Tous  les comptes"); ?></OPTION>
								<OPTION value="1"<?php isSelected("1",$_SESSION["filtre_adh_2"]) ?>><?php echo _T("Comptes actifs"); ?></OPTION>
								<OPTION value="2"<?php isSelected("2",$_SESSION["filtre_adh_2"]) ?>><?php echo _T("Comptes désactivés"); ?></OPTION>
							</SELECT>
				                       <SELECT name="filtre_3" onChange="form.submit()">
                                				<OPTION value="0"<?php isSelected("0",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Tous les paniers"); ?></OPTION>
				                                <OPTION value="1"<?php isSelected("1",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Legumes"); ?></OPTION>
				                                <OPTION value="2"<?php isSelected("2",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Pain"); ?></OPTION>
				                                <OPTION value="3"<?php isSelected("3",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Volaille"); ?></OPTION>
                                				<OPTION value="4"<?php isSelected("4",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Boeuf"); ?></OPTION>
                                				<OPTION value="4"<?php isSelected("4",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Veau"); ?></OPTION>
                                				<OPTION value="4"<?php isSelected("4",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Cochon"); ?></OPTION>
                                				<OPTION value="5"<?php isSelected("5",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Farine-Huile"); ?></OPTION>
				                                <OPTION value="6"<?php isSelected("6",$_SESSION["filtre_adh_3"]) ?>><?php echo _T("Fromage de chèvre"); ?></OPTION>
                        </SELECT>

							<INPUT type="submit" value="<?php echo _T("Filtrer"); ?>">
						</FORM>
					</DIV>
				</TD>
			</TR>
		</TABLE>
<?php
		if (isset($_GET["etiquettes"]))
		{
?>
	<FORM action="etiquettes_adherents.php" method="post" name="mailing_form" target="_blank">
<?php
		}
		else
		{
?>
						<FORM action="mailing_adherents.php" method="post" name="mailing_form">
<?php
		}
?>
						<table width="100%"> 
							<TR> 
							<TH class="listing" width="15">#</TH> 
				  			<TH class="listing left" width="250"> 
									<A href="mailing_adherents.php?tri=0" class="listing"><?php echo _T("Nom"); ?></A>
									<?php
										if ($_SESSION["tri_adh"]=="0")
											if ($_SESSION["tri_adh_sens"]=="0")
												echo "<IMG src=\"images/asc.png\" width=\"7\" height=\"7\" alt=\"\">";
											else 
												echo "<IMG src=\"images/desc.png\" width=\"7\" height=\"7\" alt=\"\">";
									?>
								</TH> 
								<TH class="listing left"> 
									<A href="mailing_adherents.php?tri=1" class="listing"><?php echo _T("E-Mail"); ?></A>
									<?php
										if ($_SESSION["tri_adh"]=="1")
											if ($_SESSION["tri_adh_sens"]=="0")
												echo "<IMG src=\"images/asc.png\" width=\"7\" height=\"7\" alt=\"\">";
											else 
												echo "<IMG src=\"images/desc.png\" width=\"7\" height=\"7\" alt=\"\">";
									?>
								</TH> 
								<TH class="listing left"> 
									<A href="mailing_adherents.php?tri=2" class="listing"><?php echo _T("Statut"); ?></A>
									<?php
										if ($_SESSION["tri_adh"]=="2")
											if ($_SESSION["tri_adh_sens"]=="0")
												echo "<IMG src=\"images/asc.png\" width=\"7\" height=\"7\" alt=\"\">";
											else 
												echo "<IMG src=\"images/desc.png\" width=\"7\" height=\"7\" alt=\"\">";
									?>
								</TH> 
								<TH class="listing left"> 
									<A href="mailing_adherents.php?tri=3" class="listing"><?php echo _T("Etat cotisations"); ?></A>
									<?php
										if ($_SESSION["tri_adh"]=="3")
											if ($_SESSION["tri_adh_sens"]=="0")
												echo "<IMG src=\"images/asc.png\" width=\"7\" height=\"7\" alt=\"\">";
											else 
												echo "<IMG src=\"images/desc.png\" width=\"7\" height=\"7\" alt=\"\">";
									?>
								</TH> 
								<TH width="55" class="listing">Actions</TH> 
							</tr> 
<?php 
		if ($resultat->EOF)
		{
?>	
							<tr>
								<td colspan="6" class="emptylist"><?php echo _T("aucun adhérent"); ?></td>
							</tr>
<?php
		}
		else while (!$resultat->EOF) 
		{ 
		// définition CSS pour adherent désactivé
		if ($resultat->fields[4]=="1")
			$row_class = "actif";
		else
			$row_class = "inactif";
			
		// temps d'adhésion
		if($resultat->fields[6])
		{
			$statut_cotis = _T("Exempt de cotisation");
			$row_class .= " cotis-exempt";
		}
		else
		{
			if ($resultat->fields[10]=="")
			{
				$statut_cotis = _T("N'a jamais cotisé");
				$row_class .= " cotis-never";
			}
			else
			{
				$date_fin = split("-",$resultat->fields[10]);
				$ts_date_fin = mktime(0,0,0,$date_fin[1],$date_fin[2],$date_fin[0]);
				$aujourdhui = time();
				
				$difference = intval(($ts_date_fin - $aujourdhui)/(3600*24));
				if ($difference==0)
				{
					$statut_cotis = _T("Dernier jour !");
					$row_class .= " cotis-lastday";
				}
				elseif ($difference<0)
				{
					$statut_cotis = _T("En retard de ").-$difference." "._T("jours")." ("._T("depuis le")." ".$date_fin[2]."/".$date_fin[1]."/".$date_fin[0].")";
					$row_class .= " cotis-late";
				}
				else
				{
					$statut_cotis = $difference." "._T("jours restants")." ("._T("fin le")." ".$date_fin[2]."/".$date_fin[1]."/".$date_fin[0].")";
					if ($difference < 30)
						$row_class .= " cotis-soon";
					else
						$row_class .= " cotis-ok";	
				}				
			}
		}
?>							 
							<TR> 
								<TD width="15" class="<?php echo $row_class; ?>" nowrap> 
									<INPUT type="checkbox" name="mailing_adh[]" value="<?php echo $resultat->fields[0] ?>" <?php if (in_array($resultat->fields[0],$mailing_adh)) echo "CHECKED"; ?>> 
								</TD> 
								<TD class="<?php echo $row_class; ?>" nowrap>
<?php
			if ($resultat->fields[7]=="1") {
?>
									<IMG src="images/icon-male.png" Alt="<?php echo _T("[H]"); ?>" align="middle" width="10" height="12">
<?php
			} else {
?>
									<IMG src="images/icon-female.png" Alt="<?php echo _T("[F]"); ?>" align="middle" width="9" height="12">
<?php
			}
?>
<?php
			if ($resultat->fields[9]=="1") {
?>
									<IMG src="images/icon-star.png" Alt="<?php echo _T("[admin]"); ?>" align="middle" width="12" height="13">
<?php
			}	else {
?>
									<IMG src="images/icon-empty.png" Alt="" align="middle" width="12" height="13">
<?php
			}
?>
									<A href="voir_adherent.php?id_adh=<?php echo $resultat->fields["id_adh"] ?>"><?php echo strtoupper($resultat->fields[1])." ".htmlentities($resultat->fields[2], ENT_QUOTES, "ISO-8859-15"); ?></A>
								</TD> 
								<TD class="<?php echo $row_class; ?>" nowrap> 
									<?php if ($resultat->fields[8]!="") echo "<A href=\"mailto:".htmlentities($resultat->fields[8], ENT_QUOTES, "ISO-8859-15")."\">".htmlentities($resultat->fields[8], ENT_QUOTES, "ISO-8859-15")."</A>"; ?>&nbsp; 
								</TD> 
								<TD class="<?php echo $row_class; ?>" nowrap><?php echo _T($resultat->fields[5]) ?></TD> 
								<TD class="<?php echo $row_class; ?>" nowrap><?php echo $statut_cotis ?></TD>
								<TD width="55" class="<?php echo $row_class; ?> center"> 
									<A href="ajouter_adherent.php?id_adh=<?php echo $resultat->fields[0] ?>"><IMG src="images/icon-edit.png" alt="<?php echo _T("[mod]"); ?>" border="0" width="12" height="13"></A>
									<A href="gestion_contributions.php?id_adh=<?php echo $resultat->fields[0] ?>"><IMG src="images/icon-money.png" alt="<?php echo _T("[$]"); ?>" border="0" width="13" height="13"></A>
									<A onClick="return confirm('<?php echo str_replace("\n","\\n",addslashes(_T("Voulez-vous vraiment supprimer cet adhérent de la base, ceci supprimera aussi l'historique de ses cotisations. Pour éviter cela vous pouvez simplement désactiver le compte.\n\nVoulez-vous tout de même supprimer cet adhérent ?"))); ?>')" href="gestion_adherents.php?sup=<?php echo $resultat->fields[0] ?>"><IMG src="images/icon-trash.png" alt="<?php echo _T("[sup]"); ?>" border="0" width="11" height="13"></A>
								</TD> 
							</TR> 
<?php 
			$resultat->MoveNext();
		} 
		$resultat->Close();
?>							 
						</TABLE>
						<A href="#" onClick="check()"><?php echo _T("[ Tout cocher / décocher ]"); ?></A>
						<BR>
						<BR>
<?php
		if (isset($_GET["etiquettes"]))
		{
?>
							<input type="radio" name="panier" value="legume" checked> Légume
							<input type="radio" name="panier" value="pain"> Pain
							<SELECT name="mois">
							<OPTION value="13"<?php isSelected("1",$moiscour) ?>><?php echo "Janvier"; ?></OPTION>
							<OPTION value="2"<?php isSelected("2",$moiscour) ?>><?php echo "Février"; ?></OPTION>
							<OPTION value="3"<?php isSelected("3",$moiscour) ?>><?php echo "Mars"; ?></OPTION>
							<OPTION value="4"<?php isSelected("4",$moiscour) ?>><?php echo "Avril"; ?></OPTION>
							<OPTION value="5"<?php isSelected("5",$moiscour) ?>><?php echo "Mai"; ?></OPTION>
							<OPTION value="6"<?php isSelected("6",$moiscour) ?>><?php echo "Juin"; ?></OPTION>
							<OPTION value="7"<?php isSelected("7",$moiscour) ?>><?php echo "Juillet"; ?></OPTION>
							<OPTION value="8"<?php isSelected("8",$moiscour) ?>><?php echo "Août"; ?></OPTION>
							<OPTION value="9"<?php isSelected("9",$moiscour) ?>><?php echo "Septembre"; ?></OPTION>
							<OPTION value="10"<?php isSelected("10",$moiscour) ?>><?php echo "Octobre"; ?></OPTION>
							<OPTION value="11"<?php isSelected("11",$moiscour) ?>><?php echo "Novembre"; ?></OPTION>
							<OPTION value="12"<?php isSelected("2",$moiscour) ?>><?php echo "Décembre"; ?></OPTION>
							</SELECT>
							<DIV align="center"><INPUT type="submit" value="<?php echo _T("Génération de feuille d'emargement"); ?>"></DIV>
<?php
		}
		else
		{
?>
						<DIV align="center">
						<TABLE border="0" id="input-table">
							<TR>
								<TH id="libelle"><?php echo _T("Objet :"); ?></TH>
							</TR>
							<TR>
								<TD><INPUT type="text" name="mailing_objet" value="<?php echo htmlentities($mailing_objet, ENT_QUOTES, "UTF-8"); ?>" size="80"></TD>
							</TR>
							<TR>
								<TH id="libelle"><?php echo _T("Message :"); ?></TH>
							</TR>
							<TR>
								<TD><TEXTAREA name="mailing_corps" cols="72" rows="15"><?php echo htmlentities($mailing_corps, ENT_QUOTES, "UTF-8"); ?></TEXTAREA></TD>
							</TR>
							<TR>
								<TH align="center">
									<BR>
									<INPUT type="submit" value="<?php echo _T("Prévisualiser"); ?>">
								</TH>
							</TR>
						</TABLE>
						</DIV>
<?php
		}
?>
						<INPUT type="hidden" name="mailing_go" value="1">
						</FORM>
<?php 
	}
	else
	{
		$confirm_detected="";
		
		// $mailing_corps = $_POST["mailing_corps"];
		// adhérents avec email
		$requete = "SELECT id_adh, nom_adh, prenom_adh, pseudo_adh, activite_adh,
				libelle_statut, bool_exempt_adh, titre_adh, email_adh, bool_admin_adh, date_echeance
				FROM ".PREFIX_DB."adherents, ".PREFIX_DB."statuts
	  				WHERE ".PREFIX_DB."adherents.id_statut=".PREFIX_DB."statuts.id_statut AND (";
		$where_clause = "";
		while(list($key,$value)=each($mailing_adh))
		{
			if ($where_clause!="")
				$where_clause .= " OR ";
			$where_clause .= "id_adh='".$value."'";
		}
		$requete .= $where_clause.") AND email_adh IS NOT NULL ORDER by nom_adh, prenom_adh;";
		// echo $requete;
		$resultat = &$DB->Execute($requete);
		if (isset($_POST["mailing_confirmed"]))
			$confirm_detected = _T("Pensez à contacter les adhérents ne disposant pas d'une adresse E-Mail par un autre moyen.");
?>
			<H1 class="titre"><?php echo _T("Mailing"); ?> <?php if (isset($_POST["mailing_confirmed"])) echo _T("effectué !"); else echo _T("(prévisualisation)"); ?></H1>
<?php
		// Affichage des erreurs
		if ($confirm_detected!="")
	  	echo "			<BR><DIV align=\"center\"><TABLE><TR><TD style=\"background: #DDFFDD; color: #FF0000\"><B><DIV align=\"center\">"._T("- ATTENTION -")."</DIV></B>" . $confirm_detected . "</TD></TR></TABLE></DIV>";
?>
			<BR>
			<B><?php echo _T("Destinataires du mailing :"); ?></B>
			<TABLE width="100%"> 
				<TR> 
	  				<TH class="listing left" width="250"><?php echo _T("Nom"); ?></TH> 
					<TH class="listing left"><?php echo _T("E-Mail"); ?></TH> 
					<TH class="listing left"> <?php echo _T("Statut"); ?></TH> 
					<TH class="listing left"><?php echo _T("Etat cotisations"); ?></TH> 
				</TR> 			
<?php		
		$num_mails = 0;
		$concatmail = "";
		if ($resultat->EOF)
		{
?>	
				<tr>
					<td colspan="4" bgcolor="#EEEEEE" align="center"><i><?php echo _T("aucun adhérent"); ?></i></td>
				</tr>
<?php
		}
		else while (!$resultat->EOF) 
		{
			if (isset($_POST["mailing_confirmed"]))
			{
				mail ($resultat->fields[8], $mailing_objet, $mailing_corps,"From: ".PREF_EMAIL_NOM." <".PREF_EMAIL.">\r\nContent-Type: text/plain; charset=utf8\r\nContent-Transfer-Encoding: 8bit\r\nMime-Version: 1.0");
				$concatmail = $concatmail . " " . $resultat->fields[8];
				$num_mails++;
			}		

			// définition CSS pour adherent désactivé
			if ($resultat->fields[4]=="1")
				$activity_class = "";
			else
				$activity_class = " class=\"inactif\"";
				
			// temps d'adhésion
			if($resultat->fields[6])
			{
				$statut_cotis = _T("Exempt de cotisation");
				$color = "#DDFFDD";
			}
			else
			{
				if ($resultat->fields[10]=="")
				{
					$statut_cotis = _T("N'a jamais cotisé");
					$color = "#EEEEEE";			
				}
				else
				{
					$date_fin = split("-",$resultat->fields[10]);
					$ts_date_fin = mktime(0,0,0,$date_fin[1],$date_fin[2],$date_fin[0]);
					$aujourdhui = time();
					
					$difference = intval(($ts_date_fin - $aujourdhui)/(3600*24));
					if ($difference==0)
					{
						$statut_cotis = _T("Dernier jour !");
						$color = "#FFDDDD";
					}
					elseif ($difference<0)
					{
						$statut_cotis = _T("En retard de ").-$difference." "._T("jours")." ("._T("depuis le")." ".$date_fin[2]."/".$date_fin[1]."/".$date_fin[0].")";
						$color = "#FFDDDD";
					}
					else
					{
						$statut_cotis = $difference." "._T("jours restants")." ("._T("fin le")." ".$date_fin[2]."/".$date_fin[1]."/".$date_fin[0].")";
						if ($difference < 30)
							$color = "#FFE9AB";
						else
							$color = "#DDFFDD";	
					}					
				}
			}
		
?>							 
				<tr> 
					<td bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>>
<?php
			if ($resultat->fields[7]=="1") {
?>
						<img src="images/icon-male.png" Alt="<?php echo _T("[H]"); ?>" align="middle" width="10" height="12">
<?php
			} else {
?>
						<img src="images/icon-female.png" Alt="<?php echo _T("[F]"); ?>" align="middle" width="9" height="12">
<?php
			}
?>
<?php
			if ($resultat->fields[9]=="1") {
?>
						<img src="images/icon-star.png" Alt="<?php echo _T("[admin]"); ?>" align="middle" width="12" height="13">
<?php
			}	else {
?>
						<img src="images/icon-empty.png" Alt="" align="middle" width="12" height="13">
<?php
			}
?>
						<a href="voir_adherent.php?id_adh=<?php echo $resultat->fields["id_adh"] ?>"><?php echo htmlentities(strtoupper($resultat->fields[1]), ENT_QUOTES)." ".htmlentities($resultat->fields[2], ENT_QUOTES) ?></a>
					</td> 
					<td bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>> 
						<?php if ($resultat->fields[8]!="") echo "<A href=\"mailto:".htmlentities($resultat->fields[8], ENT_QUOTES)."\">".htmlentities($resultat->fields[8], ENT_QUOTES)."</A>"; ?>&nbsp; 
					</td> 
					<td bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>>
						<?php echo _T($resultat->fields[5]) ?> 
					</td> 
					<td bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>> 
						<?php echo $statut_cotis ?>
					</td>
				</TR>

<?php	
			$resultat->MoveNext();
		}
		
		if (isset($_POST["mailing_confirmed"]))
			dblog(_T("Envoi d'un mailing intitulé :")." \"".$mailing_objet."\" - ".$num_mails." "._T("destinataires"), $concatmail."\n".$mailing_corps);
		
		$resultat->Close();
?>
			</TABLE>
			<DIV id="mailing_preview" align="center">
				<TABLE border="0">
				<TR><TH><?php echo _T("Objet :"); ?></TH></TR>
				<TR><TD><?php echo htmlentities($mailing_objet, ENT_QUOTES, "UTF-8"); ?></TD></TR>
				<TR><TH><?php echo _T("Message :"); ?></TH></TR>
				<TR><TD><?php echo nl2br(htmlentities($mailing_corps, ENT_QUOTES, "UTF-8")); ?></TD></TR>
				</TABLE>
			</DIV>
						<DIV align="center">
						<TABLE>
							<TR>
<?php
		if (!isset($_POST["mailing_confirmed"]))
		{
?>
								<TD>
									<FORM action="mailing_adherents.php" method="post">
<?php
			reset($mailing_adh);
			while(list($key,$value)=each($mailing_adh))
			{
				echo "<INPUT type=\"hidden\" name=\"mailing_adh[]\" value=\"".$value."\">";
			}
?>
										<INPUT type ="hidden" name="mailing_corps" value="<?php echo htmlentities($mailing_corps, ENT_QUOTES, "UTF-8"); ?>">
										<INPUT type ="hidden" name="mailing_objet" value="<?php echo htmlentities($mailing_objet, ENT_QUOTES, "UTF-8"); ?>">
										<INPUT type="submit" value="<?php echo _T("Retour"); ?>">&nbsp;&nbsp;&nbsp;
									</FORM>
								</TD>
								<TD>
									<FORM action="mailing_adherents.php" method="post">
<?php
			reset($mailing_adh);
			while(list($key,$value)=each($mailing_adh))
			{
				echo "<INPUT type=\"hidden\" name=\"mailing_adh[]\" value=\"".$value."\">";
			}
?>
										<INPUT type ="hidden" name="mailing_corps" value="<?php echo htmlentities($mailing_corps, ENT_QUOTES, "UTF-8"); ?>">
										<INPUT type ="hidden" name="mailing_objet" value="<?php echo htmlentities($mailing_objet, ENT_QUOTES, "UTF-8"); ?>">
										<INPUT type ="hidden" name="mailing_confirmed" value="1">
										<INPUT type ="hidden" name="mailing_go" value="1">
										&nbsp;&nbsp;&nbsp;<INPUT type="submit" value="<?php echo _T("Envoyer"); ?>">
									</FORM>
								</TD>
<?php
		}
		else
		{
?>
								<TD>
									<FORM action="gestion_adherents.php" method="post">
										<INPUT type="submit" value="<?php echo _T("Retour"); ?>">
									</FORM>
								</TD>
<?php
		}
?>								
							<TR>
						</TABLE>
						</DIV>
						<BR>
			<B><?php echo _T("Adhérents non joignables par email :"); ?></B>
			<TABLE width="100%"> 
				<TR> 
	  				<TH class="listing left" width="250"><?php echo _T("Nom"); ?></TH> 
					<TH class="listing left"><?php echo _T("Coordonnées"); ?></TH> 
					<TH class="listing left"> <?php echo _T("Statut"); ?></TH> 
					<TH class="listing left"><?php echo _T("Etat cotisations"); ?></TH> 
				</TR> 			
<?php
		// adhérents sans email
		$requete = "SELECT id_adh, nom_adh, prenom_adh, adresse_adh, activite_adh,
				libelle_statut, bool_exempt_adh, titre_adh, cp_adh, bool_admin_adh, date_echeance,
				ville_adh, tel_adh, gsm_adh, msn_adh, icq_adh, pays_adh, jabber_adh, adresse2_adh
				FROM ".PREFIX_DB."adherents, ".PREFIX_DB."statuts
			       	WHERE ".PREFIX_DB."adherents.id_statut=".PREFIX_DB."statuts.id_statut AND (";
		$requete .= $where_clause.") AND email_adh IS NULL ORDER by nom_adh, prenom_adh;";
		// echo $requete;
		$resultat = &$DB->Execute($requete);

		if ($resultat->EOF)
		{
?>	
							<tr>
								<td colspan="4" bgcolor="#EEEEEE" align="center"><i><?php echo _T("aucun adhérent"); ?></i></td>
							</tr>
<?php
		}
		else while (!$resultat->EOF) 
			{
				// définition CSS pour adherent désactivé
				if ($resultat->fields[4]=="1")
					$activity_class = "";
				else
					$activity_class = " class=\"inactif\"";
					
				// temps d'adhésion
				if($resultat->fields[6])
				{
					$statut_cotis = _T("Exempt de cotisation");
					$color = "#DDFFDD";
				}
				else
				{
					if ($resultat->fields[10]=="")
					{
						$statut_cotis = _T("N'a jamais cotisé");
						$color = "#EEEEEE";			
					}
					else
					{
						$date_fin = split("-",$resultat->fields[10]);
						$ts_date_fin = mktime(0,0,0,$date_fin[1],$date_fin[2],$date_fin[0]);
						$aujourdhui = time();
						
						$difference = intval(($ts_date_fin - $aujourdhui)/(3600*24));
						if ($difference==0)
						{
							$statut_cotis = _T("Dernier jour !");
							$color = "#FFDDDD";
						}
						elseif ($difference<0)
						{
							$statut_cotis = _T("En retard de ").-$difference." "._T("jours")." ("._T("depuis le")." ".$date_fin[2]."/".$date_fin[1]."/".$date_fin[0].")";
							$color = "#FFDDDD";
						}
						else
						{
							$statut_cotis = $difference." "._T("jours restants")." ("._T("fin le")." ".$date_fin[2]."/".$date_fin[1]."/".$date_fin[0].")";
							if ($difference < 30)
								$color = "#FFE9AB";
							else
								$color = "#DDFFDD";	
						}					
					}
				}
			
?>							 
							<tr> 
								<td valign="top" bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>>
<?php
				if ($resultat->fields[7]=="1") {
?>
									<img src="images/icon-male.png" Alt="<?php echo _T("[H]"); ?>" align="middle" width="10" height="12">
<?php
				} else {
?>
									<img src="images/icon-female.png" Alt="<?php echo _T("[F]"); ?>" align="middle" width="9" height="12">
<?php
				}
?>
<?php
				if ($resultat->fields[9]=="1") {
?>
									<img src="images/icon-star.png" Alt="<?php echo _T("[admin]"); ?>" align="middle" width="12" height="13">
<?php
				}	else {
?>
									<img src="images/icon-empty.png" Alt="" align="middle" width="12" height="13">
<?php
				}
?>
									<a href="voir_adherent.php?id_adh=<?php echo $resultat->fields["id_adh"] ?>"><?php echo htmlentities(strtoupper($resultat->fields[1]), ENT_QUOTES)." ".htmlentities($resultat->fields[2], ENT_QUOTES); ?></a>
								</td> 
<?php
				$coord_adh = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
				$adresse_adh = "";
				if ($resultat->fields[3]!="")
					$adresse_adh .= htmlentities($resultat->fields[3], ENT_QUOTES);
				if ($resultat->fields[8]!="") 
				{	
					if ($adresse_adh!="")
						$adresse_adh .= "<BR>";
					$adresse_adh .= htmlentities($resultat->fields[8], ENT_QUOTES);
				}
				if ($resultat->fields[11]!="") 
				{	
					if ($adresse_adh!="")
						$adresse_adh .= "<BR>";
					$adresse_adh .= htmlentities($resultat->fields[11], ENT_QUOTES);
				}
				if ($resultat->fields[16]!="") 
				{	
					if ($adresse_adh!="")
						$adresse_adh .= "<BR>";
					$adresse_adh .= htmlentities($resultat->fields[16], ENT_QUOTES);
				}
				if ($resultat->fields[18]!="") 
				{	
					if ($adresse_adh!="")
						$adresse_adh .= "<BR>";
					$adresse_adh .= htmlentities($resultat->fields[18], ENT_QUOTES);
				}
				if ($adresse_adh!="")
					$coord_adh .= "<tr><td width=\"10\" valign=\"top\"><B>".str_replace(" ","&nbsp;",_T("Adresse :"))."</B>&nbsp;</td><td>".$adresse_adh."</td></tr>";
				if ($resultat->fields[12]!="") 
					$coord_adh .= "<tr><td style=\"padding-right: 1px;\"><B>".str_replace(" ","&nbsp;",_T("Tel :"))."</B>&nbsp;</td><td>".htmlentities($resultat->fields[12], ENT_QUOTES)."</td></tr>";
				if ($resultat->fields[13]!="") 
					$coord_adh .= "<tr><td><B>".str_replace(" ","&nbsp;",_T("GSM :"))."</B>&nbsp;</td><td>".htmlentities($resultat->fields[13], ENT_QUOTES)."</td></tr>";
				if ($resultat->fields[15]!="") 
					$coord_adh .= "<tr><td><B>".str_replace(" ","&nbsp;",_T("ICQ :"))."</B>&nbsp;</td><td>".htmlentities($resultat->fields[15], ENT_QUOTES)."</td></tr>";
				if ($resultat->fields[17]!="") 
					$coord_adh .= "<tr><td><B>".str_replace(" ","&nbsp;",_T("Jabber :"))."</B>&nbsp;</td><td>".htmlentities($resultat->fields[17], ENT_QUOTES)."</td></tr>";
				if ($resultat->fields[14]!="") 
					$coord_adh .= "<tr><td><B>".str_replace(" ","&nbsp;",_T("MSN :"))."</B>&nbsp;</td><td>".htmlentities($resultat->fields[14], ENT_QUOTES)."</td></tr>";
				$coord_adh .= "</table>";
?>
								<td valign="top" bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>"><?php echo $coord_adh; ?></td> 
								<td valign="top" bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>><?php echo _T($resultat->fields[5]) ?></td> 
								<td valign="top" bgcolor="<?php echo $color ?>"<?php echo $activity_class ?>><?php echo $statut_cotis ?></td>
							</TR>

<?php	
				$nomail_adh[]=$resultat->fields[0];
				$resultat->MoveNext();
			} 
			$resultat->Close();
			

?>
						</TABLE>
						<BR>
						<DIV align="center">
						<TABLE>
							<TR>
<?php
		if (!isset($_POST["mailing_confirmed"]))
		{
?>
								<TD>
									<FORM action="mailing_adherents.php" method="post">
<?php
			reset($mailing_adh);
			while(list($key,$value)=each($mailing_adh))
			{
				echo "<INPUT type=\"hidden\" name=\"mailing_adh[]\" value=\"".$value."\">";
			}
?>
										<INPUT type ="hidden" name="mailing_corps" value="<?php echo htmlentities($mailing_corps, ENT_QUOTES, "UTF-8"); ?>">
										<INPUT type ="hidden" name="mailing_objet" value="<?php echo htmlentities($mailing_objet, ENT_QUOTES, "UTF-8"); ?>">
										<INPUT type="submit" value="<?php echo _T("Retour"); ?>">&nbsp;&nbsp;&nbsp;
									</FORM>
								</TD>
<?php
		}
		else
		{
?>
								<TD>
									<FORM action="gestion_adherents.php" method="post">
										<INPUT type="submit" value="<?php echo _T("Retour"); ?>">
									</FORM>
								</TD>
<?php
		}
?>								
								<TD>
								<FORM action="etiquettes_adherents.php" method="post" target="_blank">
<?php
		reset($nomail_adh);
		while(list($key,$value)=each($nomail_adh))
		{
			echo "<INPUT type=\"hidden\" name=\"mailing_adh[]\" value=\"".$value."\">";
		}
?>
										&nbsp;&nbsp;&nbsp;<INPUT type="submit" value="<?php echo _T("Génération de feuille d'emargement du mois"); ?>">
									</FORM>

								</TD>
							<TR>
						</TABLE>
						</DIV>
<?php
	}
	include("footer.php"); 
?>
