<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.9

Copyright (C) 2006-2009  Sylvain FATOME
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
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
include_once("../inc/config.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/lst.inc.php");
include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_rel.inc.php");
Archivelog(@$_SESSION["code_log"]);

$pbm_cli=FALSE;
$pbm_cocl = FALSE;

// Niveau <=2 ne peut que voir les sites des contrats
if (@$_SESSION["niv_log"] <= 2)
	$td = "view";

if (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_valider))
{
	$lstsites = "";
	if (count($_POST["scli_choisi"]) <> 0)
	{
		for ($i = 0; $i < count($_POST["scli_choisi"]); $i++)
		{
			$lstsites .= $_POST["scli_choisi"][$i]."-";
		}
	}
	if ($lstsites!="")
		$lstsites = substr($lstsites,0,strlen($lstsites)-1);
}
else
{
	$cc = strip_tags(trim($_GET['cc'])); // Récupération code Client
	$co = strip_tags(trim($_GET['co'])); // Récupération code Contrat
	$td = strip_tags(trim($_GET['td'])); // Récupération type demande

	if ($cc<>"")
	{
		// Contrôle qu'un client n'essaye pas de "hacker" un contrat
		if ((@$_SESSION["niv_log"] == 1) AND ($cc <> @$_SESSION["code_log"]))
			$pbm_cli = TRUE;

		$row = lit_enr_cli($cc,TRUE);
		if (is_array($row))
		{
			extract ($row);

			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
			switch(@$_SESSION["niv_log"])
			{
				case 3 :
				case 4 :
					if (client_age_ok($cli_code,@$_SESSION["code_age"],TRUE)==FALSE)
						$detail_ok = TRUE;
					break;
				default :
					break;
			}
		}
		else
			$pbm_cli = TRUE;
	}
	else
		$pbm_cli = TRUE;

	if (($td<>"edit") AND ($td<>"view") AND ($td<>"add"))
		$pbm_cocl=TRUE;

	if ($co <> "")
	{
		// Récupération des informations contrat client
		$row = lit_enr_cocl($co);
		if (is_array($row))
		{
			extract ($row);
			
			// Si contrat terminé, force le mode "view"
			if (formatedate($cocl_datesig) <> "")
				$td="view";

			if ($td <> "add")
			{
				// Contrôle qu'un client n'essaye pas de "hacker" un contrat
				if ((@$_SESSION["niv_log"] == 1) AND ($cocl_codecli <> @$_SESSION["code_log"]))
					$pbm_cocl = TRUE;
			// Contrôle qu'une agence n'essaye pas de "hacker" un contrat
				if (((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) AND ($cocl_codeage <> @$_SESSION["code_age"]))
					$pbm_cocl = TRUE;
			}
		}
		else
		{
			if (($td=="edit") OR ($td=="view"))
				$pbm_cocl=TRUE;
		}
	}
	else
		$pbm_cocl = TRUE;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Sites secondaires</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

</head>


<?PHP
if (($pbm_cli==FALSE) AND ($pbm_cocl==FALSE))
{
	if (strip_tags(trim(@$_POST["val"])) <> $but_valider) // Si le formulaire n'a pas été validé
	{
?>
	<body <?PHP if (($td=="add") OR ($td=="edit")) { ?>onLoad="javascript:; document.sites_lst.sel.value=valbouton(document.sites_lst);"<?PHP } ?>>
	<p align="center"><span class="titrepage">SITES SECONDAIRES</span></p>
	<p align="center">
	<?PHP
	if ($td=="add")
	{
	?>
		Merci de choisir les sites secondaires que vous souhaitez voir couvrir par le contrat en cours de cr&eacute;ation.
	<?PHP
	}
	
	if ($td=="edit")
	{
	?>
		Merci de choisir les sites secondaires que vous souhaitez voir couvrir par le contrat <?PHP echo $co; ?>.
	<?PHP
	}

	if (($td=="add") OR ($td=="edit"))
	{
	?>
		<br>
		<span class="annot_petit_it">(Si vous modifiez votre choix par rapport &agrave; celui en place, la liste des mat&eacute;riels/licences pr&eacute;c&eacute;demment g&eacute;n&eacute;r&eacute;e sera r&eacute;initialis&eacute;e)</span>
	<?PHP
	}
	
	if ($td=="view")
	{
	?>
		Sites secondaires couverts par le contrat <?PHP echo $co; ?>.
	<?PHP
	}
	?>
	</p>		
	
	<form name="sites_lst" method="post" action="sites_lst.php">
	<input name="cc" type="hidden" id="cc" value="<?PHP echo $cc ?>">
	<?PHP
	if ($td<>"view")
	{
		echo "<p align=\"center\">";
		echo "<br>";
		echo "<input type=\"button\" name=\"sel\" id=\"sel\" value=\"Tous\" class=\"bton_fgris_petit\" onclick=\"this.value=checkall(this.value,this.form);\">";
		echo "</p>";
	}
	
	// Création de la liste des sites secondaires
	
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_codecli`= '".$cc."' AND `scli_actif`=1 ORDER BY `scli_ste`";
	
	$rs = mysql_query($rech) or die(mysql_error());
	
	if (mysql_num_rows($rs) <> 0)
	{
		echo "<table class=\"MtableD\" border=\"0\" width=\"890\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
		 
		// Création de l'entête de la table "sites secondaires"
		echo "<th>";
		echo "Coordonn&eacute;es";
		echo "</th>\n";
		echo "<th>";
		echo "Tel/Fax";
		echo "</th>\n";
		echo "<th>";
		echo "Contact";
		echo "</th>\n";
		echo "<th>";
		echo "Créé le";
		echo "</th>\n";
		echo "<th>";
		if ($td<>"view")
			echo "Choisir";
		else
			echo "&nbsp;";
		echo "</th>\n";
		
		$j = 0;
		$lstsites = "";
	
		while ($row = mysql_fetch_assoc($rs)) 
		{         
			// Création de l'entête de ligne (TR)
			creetrlstdet($j);
			
			echo "<td>\n";
			echo htmlspecialchars($row["scli_ste"],ENT_NOQUOTES);
			echo "<br>";
			echo htmlspecialchars($row["scli_adr1"],ENT_NOQUOTES);
			echo "<br>";
			if ($row["scli_adr2"]<>"")
			{
				echo htmlspecialchars($row["scli_adr2"],ENT_NOQUOTES);
				echo "<br>";
			}
			echo Retournecp_openId($row["scli_ville"],FALSE)." ".Retourneville_openId($row["scli_ville"],FALSE)."<br>";
			echo "</td>\n";
			echo "<td align=\"center\">\n";
			echo "T.".$row["scli_telf"];
			if ($row["scli_fax"]<>"")
			{
				echo"<br>";
				echo "F.".$row["scli_fax"];
			}
			echo "<td align=\"center\">\n";
			echo htmlspecialchars($row["scli_rcivil"],ENT_NOQUOTES)." ".htmlspecialchars($row["scli_rprenom"],ENT_NOQUOTES)." ".htmlspecialchars($row["scli_rnom"],ENT_NOQUOTES);
			echo "<br>";
			echo "T.".$row["scli_rtelf"];
			if ($row["scli_rfax"]<>"")
			{
				echo "<br>";
				echo "F.".$row["scli_rfax"];
			}
			if ($row["scli_rtelp"]<>"")
			{
				echo "<br>";
				echo "P.".$row["scli_rtelp"];
			}
			echo "</td>\n";
			echo "<td align=\"center\">\n";
			echo formatedate($row["scli_datecrea"]);
			echo "</td>\n";
	
			echo "<td align=\"center\">\n";
			$choisi = FALSE;
			if ($_SESSION["scli_choisis"] <> "")
			{
				if (ereg($row["scli_id"],$_SESSION["scli_choisis"]))
					$choisi = TRUE;
			}
	
			echo "<input type=\"checkbox\" name=\"scli_choisi[]\" value=\"".$row["scli_id"]."\"";
			if($j%2 == 0)
				echo " class=\"TRalter1D\"";
			else
				echo " class=\"TRalter2D\"";
	
	
			if ($choisi == TRUE)
				echo " checked";
				
			if ($td=="view")
				echo " disabled";
			else
				echo " onclick=\"document.sites_lst.sel.value=valbouton(document.sites_lst);\"";
			echo ">";
			echo "</td>\n";
			echo "</tr>\n";
		
			$j++;
		}
		
		echo "</table>\n";
		// Fin affichage table liste sites
	}
	mysql_free_result($rs);
	mysql_close($conn);  
	?>
	<p align="center">
	<?PHP
	if ($td<>"view")
	{
	?>
		<input name="RAZ" type="button" class="bton_std" id="RAZ"  value="<?PHP echo $but_reinit; ?>" onClick="document.getElementById('sel').value=uncheckall(this.form);">
		<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_valider; ?>">
		<input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo $but_cancel; ?>" onClick="javascript:; self.close('fscomp');">
	<?PHP
	}
	else
	{
	?>
		<input name="annul" type="button" class="bton_std" id="annul" value="Fermer" onClick="javascript:; self.close('fscomp');">
	<?PHP
	}
	?>
	</p>
	</form>
	<?PHP
	}
	else
	{
	?>
<body>
<script type="text/javascript">
<!--
var valmat=window.opener.document.cocl_maj.matos.value;
<?PHP
$display="none";
if ((exist_rlic_actif($_POST["cc"]) == TRUE) OR (exist_rpdt_actif($_POST["cc"]) == TRUE))
	$display="block";
	
echo "var exsite='".$_SESSION["scli_choisis"]."';\n";
echo "var nouvsite='".$lstsites."';\n";
echo "var affbout='".$display."';\n";
$_SESSION["scli_choisis"] = $lstsites;
// On génère la variable qui va permettre de comparer
echo "var valtxtmat='".@$_SESSION["txtmatlst"]."';\n";
// On remet les valeurs générées à zéro car on repart sur un nouveau site
$_SESSION["txtmatlst"]='';
$_SESSION["rlp_choisis"] = '';
?>
// On affiche ou on efface les boutons de choix
var objet1 = window.opener.document.getElementById('boutmat');
if (objet1)
	objet1.style.display = affbout;
var objet2 = window.opener.document.getElementById('notamat');
if (objet2)
	objet2.style.display = affbout;
if (exsite!=nouvsite)
{
	var matos=update_txt_in_txt(valmat,valtxtmat,'');
	window.opener.document.cocl_maj.matos.value=matos;
	// S'il n'y a pas de bouton (pas de problème sur le site choisi), on laisse la possibilité de saisir du texte dans le textarea
	// en enlevant le readonly
	if (affbout=='none')
		window.opener.document.cocl_maj.matos.readOnly = false;
	else
		window.opener.document.cocl_maj.matos.readOnly = true;
}
// On affecte le site et on met à jour la zone d'affichage du nom du site
window.opener.document.cocl_maj.sitesec.value='<?PHP echo $_SESSION["scli_choisis"]; ?>';
if (nouvsite=="")
{
	window.opener.document.cocl_maj.sitep.checked=true;
}
self.close();
//-->
</script>
<?PHP
	}
}
else
{
?>
	<script type="text/javascript">
	<!--
	self.close();
	//-->
	</script>
<?PHP
}
?>
</body>
</html>
