<?PHP
	require_once ("../classes/xpm/smtp.php");

/********************************************************/
/* Fonctions de MAJ des textes types pour les emailings */
/********************************************************/

// Mise à jour d'un texte en fonction de codes de champs

function parse_txtmail($typedest,$codedest,$typeexp,$codeexp,$texte)
{

 	// Balayage de l'expéditeur
 	switch ($typeexp)
	{
		// Administrateur
		case "adm" :
					break;
		// Agence
		case "age" :
					$row = lit_enr_age($codedest);
					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$codedest."'";
					break;
					
		// Centre de gestion
		case "cdg" :
					$row = lit_enr_cdg();
					$rech2 = "SELECT * FROM `" .TBCG;
					break;
					
		default : break;
	}


	$txt = $texte;

	if ($typeexp <> "adm")
	{
		extract($row);
	
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs2 = mysql_query($rech2)or die(mysql_error());
		$num = mysql_num_fields($rs2);
		$i = 0;
		for ($i = 0; $i < $num; $i++) 
		{
			$nom = mysql_field_name($rs2, $i);
			$nom_rech = "%".$nom."%";
			if (mysql_field_type($rs2, $i) <> "date")
				$txt = eregi_replace($nom_rech,$row[$i],$txt);
			else
				$txt = eregi_replace($nom_rech,formatedate($row[$i]),$txt);
			
		}
		mysql_free_result($rs2);
		mysql_close($conn2);
	}

 	// Balayage du destinataire
 	switch ($typedest)
	{
		// Agence
		case "age" :
					$row = lit_enr_age($codedest);
					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$codedest."'";
					break;
					
		// Client
		case "cli" :
					$row = lit_enr_cli($codedest);
					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$codedest."'";
					break;
					
		// Opérateur
		case "ope" :
					$row = lit_enr_ope($codedest);
					$rech2 = "SELECT * FROM `" .TBOPE. "` WHERE `ope_code` = '".$codedest."'";
					break;
					
		// Technicien
		case "tec" :
					$row = lit_enr_tec($codedest);
					$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '".$codedest."'";
					break;
					
		default : break;
	}


	extract($row);
	
	$txt = $texte;

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$num = mysql_num_fields($rs2);
	$i = 0;
	for ($i = 0; $i < $num; $i++) 
	{
		$nom = mysql_field_name($rs2, $i);
		$nom_rech = "%".$nom."%";
		if (mysql_field_type($rs2, $i) <> "date")
			$txt = eregi_replace($nom_rech,$row[$i],$txt);
		else
			$txt = eregi_replace($nom_rech,formatedate($row[$i]),$txt);
		
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $txt;
}


// Ajout d'un pied au texte d'email

function add_pied_txtmail()
{
	// Récupération données pied email
	
	$row = lit_enr_texte("MAIL_PIED");
	extract ($row);
	
	return $txt_texte;
}

// Création du corps de l'email

function cre_corps_mail($typedest,$codedest,$typeexp,$codeexp,$texte)
{
	$txtcorps = "";

	// Création du texte de l'email
	$txtcorps = parse_txtmail($typedest,$codedest,$typeexp,$codeexp,$texte);
	$txtcorps .= "<br>".add_pied_txtmail();

	return $txtcorps;

}


// Envoi des emails


// $typdest : cli, tec, age, ope
// $codedest : code du destinataire
// $typeexp : type de l'expéditeur (administrateur, cdg, agence)
// $codeexp : code de l'expéditeur (administrateur, cdg, agence)
// $codetxt : code du texte type utilisé
// $titre : titre du message
function envoi_emailing($typedest,$typemail,$codedest,$typeexp,$codeexp,$txt,$sujet)
{
	global
		$admin_name,
		$admin_mail,
		$smtp_ip,
		$smtp_port;

	$mail = new SMTP;
	$mail->Delivery('local');
	$mail->Priority('high');

	// Destinataires
	
	switch ($typedest) // Analyse du type de destinataire
	{
		// Agence
		case "age" :
					$row = lit_enr_age($codedest);
					extract ($row);
					for ($i = 0; $i < 4; $i++)
					{
						if ($typemail[$i] == 1)
						{
							switch ($i)
							{
								case 0 :
									$mail->AddTo($age_email);
									break;
								case 1 :
									if ($age_email <> $age_emaild)
										$mail->AddTo($age_emaild);
									break;
								case 2 :
									if ($age_email <> $age_emailc)
										$mail->AddTo($age_emailc);
									break;
								case 3 :
									if ($age_email <> $age_emailt)
										$mail->AddTo($age_emailt);
									break;
								default :
									break;
							}
						}
					}
					break;
					
		// Client
		case "cli" :
					$row = lit_enr_cli($codedest);
					extract ($row);

					if ($typemail[0] == 1)
						$mail->AddTo($cli_email);

					// Récupération des contacts
					$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);

					$rexist = "SELECT DISTINCT ccli_email FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '$cli_code'";
					$rs2 = mysql_query($rexist) or die(mysql_error());
					if (mysql_num_rows($rs2) <> 0)
					{
						while ($resultat = mysql_fetch_array($rs2)) 
						{
							extract($resultat);

							for ($i = 1; $i < 4; $i++)
							{
								if ($typemail[$i] == 1)
								{
									switch ($i)
									{
										case 1 :
											if ($ccli_emaild == 1)
											{
												if ($cli_email <> $ccli_email)
													$mail->AddTo($ccli_email);
											}
											break;
										case 2 :
											if ($ccli_emailc == 1)
											{
												if ($cli_email <> $ccli_email)
													$mail->AddTo($ccli_email);
											}
											break;
										case 3 :
											if ($ccli_emailt == 1)
											{
												if ($cli_email <> $ccli_email)
													$mail->AddTo($ccli_email);
											}
											break;
										default :
											break;
									}
								}
							}
						}
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;
					
		// Opérateur
		case "ope" :
					$row = lit_enr_ope($codedest);
					extract ($row);
					$mail->AddTo($ope_email);
					break;
					
		// Technicien
		case "tec" :
					$row = lit_enr_tec($codedest);
					extract ($row);
					$mail->AddTo($tec_email);
					break;
					
		default : break;
	}

	// Entête de l'email

	$nom="";
	$email="";
	
	switch ($typeexp) // Analyse du type de l'expéditeur
	{
		// Administrateur
		case "adm" :
					$nom = $admin_name;
					$email = $admin_mail;
					break;
					
		// Agence
		case "age" :
					$row = lit_enr_age($codeexp);
					extract ($row);
					$nom=$age_nom;
					$email=$age_email;
					break;
					
		// Centre de gestion
		case "cdg" :
					$row = lit_enr_cdg();
					extract($row);
					$nom=$cge_nom;
					$email=$cge_email;
					break;

		default : break;
	}
					
	//Expédié par
	$mail->From($email, $nom);

	//Réponse automatique à...
	$mail->AddHeader('Reply-To', $email);

	//Gestion des retours d'erreurs
	$mail->AddHeader('Return-Path', $admin_mail);
	$mail->AddHeader('Errors-To', $admin_mail);

	// Corps de l'email
	
	$txtcorps = "";	
	$txtcorps = cre_corps_mail($typedest,$codedest,$typeexp,$codeexp,$txt);

	$mail->Html($txtcorps);
	$sent = $mail->Send($sujet);
	
	return $sent;
}

?>