<?PHP

/*****************************************************************/
/* Fonctions de MAJ des textes types pour les générations de PDF */
/*****************************************************************/

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

function parse_txt($type,$code,$texte)
{
	$txt = $texte;

 	// Balayage de 1er niveau
 	switch ($type)
	{
		// Agence
		case "age" :
					$row = lit_enr_age($code);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$code."'";
					break;
					
		// Centre de gestion
		case "cdg" :
					$row = lit_enr_cdg();

					$txt = ereg_replace("%cp_cge%",Retournecp($row["cge_ville"]),$txt);
					$txt = ereg_replace("%ville_cge%",Retourneville($row["cge_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCG;
					break;
					
		// Client
		case "cli" :
					$row = lit_enr_cli($code);

					$txt = ereg_replace("%cp_cli%",Retournecp($row["cli_ville"]),$txt);
					$txt = ereg_replace("%ville_cli%",Retourneville($row["cli_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$code."'";
					break;
					
		// Contrat
		case "con" :
					$row = lit_enr_cocl($code);
					extract($row);
					$rech2 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '".$code."'";
					break;
					
		// Intervention
		case "int" :
					$row = lit_enr_int($code);
					$rech2 = "SELECT * FROM `" .TBINT. "` WHERE `int_code` = '".$code."'";
					break;
					
		// Opérateur
		case "ope" :
					$row = lit_enr_ope($code);

					$txt = ereg_replace("%cp_ope%",Retournecp($row["ope_ville"]),$txt);
					$txt = ereg_replace("%ville_ope%",Retourneville($row["ope_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBOPE. "` WHERE `ope_code` = '".$code."'";
					break;
					
		// Technicien
		case "tec" :
					$row = lit_enr_tec($code);

					$txt = ereg_replace("%cp_tec%",Retournecp($row["tec_ville"]),$txt);
					$txt = ereg_replace("%ville_tec%",Retourneville($row["tec_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '".$code."'";
					break;
					
		default : break;
	}

	$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 pour les champs complémentaires
	switch ($type)
	{
		// Contrat
		case "con" :
					// Balayage des champs client
					$row = lit_enr_cli($cocl_codecli);
					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$cocl_codecli."'";
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($cocl_codeage);

					$txt = ereg_replace("%cp_age%",Retournecp($row["age_ville"]),$txt);
					$txt = ereg_replace("%ville_age%",Retourneville($row["age_ville"]),$txt);

					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$cocl_codeage."'";
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs centre de gestion
					$row = lit_enr_cdg();
					$rech2 = "SELECT * FROM `" .TBCDG;
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs périodicités
					$row = lit_enr_per($cocl_codecope);

					$rech2 = "SELECT * FROM `" .TBPER. "` WHERE `cope_code` = '".$cocl_codecope."'";
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs quotas d'heures

					$txt = ereg_replace("%heures_cocl%",sec_heure($cocl_nbs),$txt);

					break;
					
		// Intervention
		case "int" :
					// Balayage des champs client
					$row = lit_enr_cli($int_codecli);
					$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '".$int_codecli."'";
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs agence
					$row = lit_enr_age($int_codeage);
					$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".$int_codeage."'";
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					// Balayage des champs centre de gestion
					$row = lit_enr_cdg();
					$rech2 = "SELECT * FROM `" .TBCDG;
					$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."%";
						$txt = eregi_replace($nom_rech,$row[$i],$txt);
					}
					mysql_free_result($rs2);
					mysql_close($conn2);

					break;
					
		default : break;
	}

	return $txt;
}