<?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/fic_mque.inc.php");
include_once("../inc/func.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");
Archivelog(@$_SESSION["code_log"]);

if (isset($_GET['td']))
	$td = strip_tags(trim(@$_GET['td']));
else
{
	$td="";
	if ((strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_enr)) OR (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_del)))
		$td = strip_tags(trim(@$_POST['td']));
}
	
if (isset($_GET['cm']))
	$coderlpma = strip_tags(trim(@$_GET['cm']));
else
{
	$coderlpma="";
	if ((strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_enr)) OR (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_del)))
		$coderlpma = strip_tags(trim(@$_POST['cm']));
}

if (isset($_GET['iwm']))
	$idwmque = strip_tags(trim(@$_GET['iwm']));
else
{
	$idwmque=0;
	if ((strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_enr)) OR (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_del)))
		$idwmque = strip_tags(trim(@$_POST['iwm']));
}

$pbm_rlpma=FALSE;
$wmque_prop="";
$wmquen_nota="";
$codeuser=@$_SESSION["code_log"];
if (@$_SESSION["niv_log"]==3)
	$codeuser=@$_SESSION["code_age"];

if ($coderlpma <> "")
{
	$row = lit_enr_mque($coderlpma,TRUE);
	$totalwmque=nb_wmque_in_mque($coderlpma);
	if ($totalwmque==0)
		$td="add";
	if (is_array($row))
	{
		extract ($row);
		
		$totalwmque=nb_wmque_in_mque($coderlpma);
		if ($totalwmque==0)
			$td="add";
		$retourliste=FALSE;
		
		if ((strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_enr)) OR (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_del)))
		{
			$idwmque = strip_tags(trim(@$_POST["iwm"]));
			$prop = strip_tags(trim(@$_POST["prop"]));
			$code = strip_tags(trim(@$_POST["cm"]));
			$desc = strip_tags(trim(@$_POST["desc"]));
			$lien = strip_tags(trim(@$_POST["lien"]));
			$login = strip_tags(trim(@$_POST["login"]));
			$mdp = strip_tags(trim(@$_POST["mdp"]));
			$notaa = str_escape(trim(@$_POST["notaa"]));
			$notat = str_escape(trim(@$_POST["notat"]));
			$notap = str_escape(trim(@$_POST["notap"]));

			$rech="";
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);

			// Fiche lien Marque
			switch ($td)
			{
				case "add" :
					$rech = "INSERT INTO " .TBWMQUE;
					$rech .= " (wmque_codemque,wmque_desc,wmque_lien,wmque_login,wmque_mdp,wmque_notaa,wmque_notat,wmque_prop) VALUES";
					$rech .= " ('$code','$desc','$lien','$login','$mdp','$notaa','$notat','$prop')";
					break;
				case "edit" :
					if (@$_SESSION["niv_log"]>=5)
					{
						$rech = "UPDATE `" .TBWMQUE. "` SET `wmque_codemque`='".$code."', `wmque_desc`='".$desc."', `wmque_lien`='".$lien."',";
						$rech.= " `wmque_login`='".$login."', `wmque_mdp`='".$mdp."',";
						$rech.= " `wmque_notaa`='".$notaa."', `wmque_notat`='".$notat."', `wmque_prop`='".$prop."'";
						$rech.= "  WHERE `wmque_id` = '".$idwmque."'";
					}
					else
					{
						$rech = "UPDATE `" .TBWMQUE. "` SET `wmque_codemque`='".$code."', `wmque_desc`='".$desc."', `wmque_lien`='".$lien."',";
						$rech.= " `wmque_login`='".$login."', `wmque_mdp`='".$mdp."',";
						$rech.= " `wmque_notat`='".$notat."', `wmque_prop`='".$prop."'";
						$rech.= "  WHERE `wmque_id` = '".$idwmque."'";
					}
					break;
				case "del" :
					$rech = "DELETE FROM `" .TBWMQUE. "` WHERE `wmque_id` = '$idwmque'";
					break;
				default :
					break;
			}
			if ($rech<>"")
			{
				$rs = mysql_query($rech) or die(mysql_error());

				$rech="";
				// Notes privées
				switch ($td)
				{
					case "add" :
						$id=mysql_insert_id();
						$rech ="INSERT INTO " .TBWMQUEN. "(wmquen_idwmque,wmquen_codeu,wmquen_nota)";
						$rech.=" VALUES ('$id','$codeuser','$notap')";
						break;
					case "edit" :
						$rech = "SELECT * FROM `" .TBWMQUEN. "` WHERE `wmquen_idwmque` = '$idwmque' AND `wmquen_codeu` = '$codeuser'";
						$rs = mysql_query($rech) or die(mysql_error());
						if (mysql_num_rows($rs)>0)
						{
							$rech ="UPDATE `" .TBWMQUEN. "`";
							$rech.= " SET `wmquen_nota`='".$notap."'";
							$rech.= " WHERE (`wmquen_idwmque` = '".$idwmque."' AND `wmquen_codeu` = '".$codeuser."')";
						}
						else
						{
							$rech ="INSERT INTO " .TBWMQUEN. "(wmquen_idwmque,wmquen_codeu,wmquen_nota)";
							$rech.=" VALUES ('$idwmque','$codeuser','$notap')";
						}
						break;
					case "del" :
						$rech = "DELETE FROM `" .TBWMQUEN. "` WHERE `wmquen_idwmque` = '$idwmque'";
						break;
					default :
						break;
				}
				if ($rech<>"")
					$rs = mysql_query($rech) or die(mysql_error());
			}
			mysql_close($conn);
			$td="lst"; // On revient à la liste
			// On recalcule le total contacts
			$totalwmque=nb_wmque_in_mque($coderlpma);
		}
	}
	else
		$pbm_rlpma=TRUE;

	switch ($td)
	{
		case "add" :
			$titre = "CREATION LIEN";
			break;
		case "edit" :
		case "del" :
			if ($td=="edit")
				$titre = "EDITION LIEN";
			else
				$titre = "SUPPRESSION LIEN";
			$row=lit_enr_wmque_id_in_mque($idwmque,$coderlpma);
			if (is_array($row))
			{
				extract ($row);
				$row=lit_enr_WMqueNPriv($wmque_id,$codeuser);
				if (is_array($row))
					extract ($row);
			}
			else
				$pbm_rlpma=TRUE;
			break;
		case "lst" :
			$titre = "LISTE LIENS";
			break;
		default :
			$pbm_rlpma=TRUE;
			break;
	}
}
else
	$pbm_rlpma=TRUE;		

$affliste=TRUE;
if (((@$_SESSION["niv_log"]==3) OR (@$_SESSION["niv_log"]==4)) AND ($cmque_prop=="*"))
	$affliste=FALSE;
?>
<!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; ?> : Liens Web Marque</title>

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

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

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<script language="javascript">
<!--
var tab_champ=new Array(
<?PHP
if ($affliste==TRUE)
{
?>
	"nom:prop ; format:liste ; req:Y ; txt:Type de contact ; vmn:1 ; stok:frmok ; stbad:frmbad",
<?PHP } else { ?>
	"nom:prop ; format:txt ; req:Y ; txt:Type de contact ; stok:frmok ; stbad:frmbad",
<?PHP } ?>
	"nom:desc; format:txt ; req:Y ; txt:<?PHP echo html_entity_decode($txt_chp_lib); ?>; lmx:50; stok:frmok ; stbad:frmbad",
	"nom:lien; format:url_pref ; req:Y ; txt:Lien; lmx:200; stok:frmok ; stbad:frmbad",
	"nom:login ; format:txt ; req:N ; txt:Login ; lmx:40 ; stok:frmok ; stbad:frmbad",
	"nom:mdp ; format:txt ; req:N ; txt:<?PHP echo html_entity_decode($txt_chp_mdp); ?> ; lmx:40 ; stok:frmok ; stbad:frmbad",
<?PHP if (@$_SESSION["niv_log"] >= 5)
{ 
?>
	"nom:notaa; format:txt ; req:N ; txt:<?PHP echo html_entity_decode($txt_chp_notes); ?> Administration; stok:frmok ; stbad:frmbad",
<?PHP
}
?>
	"nom:notat; format:txt ; req:N ; txt:<?PHP echo html_entity_decode($txt_chp_notes); ?> Agence; stok:frmok ; stbad:frmbad");
//-->
</script>

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

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

<!-- Scripts Javascript pour vérifications des champs -->
<script language="JavaScript" src="../jscript/verif_field.js"></script>

</head>
<?PHP if ($td=="del") { ?>
<body onLoad="verif_wmque('1','wmque','<?PHP echo $totalwmque; ?>');warndel();metReadonly();">
<?PHP }

if (($td=="add") OR ($td=="edit") OR ($td=="lst")){ ?>
<body onLoad="verif_wmque('1','wmque','<?PHP echo $totalwmque; ?>');metFocus();">
<?PHP }

if ($pbm_rlpma==FALSE)
{
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center"><span class="titre_gras">Pour la marque : <?php echo $mque_ste; ?> </span></p>
<table width="100%"  border="0">
<tr>
	<td width="33%">&nbsp;</td>
	<td width="33%">
	<?PHP if (($td=="add") OR ($td=="edit")) { ?>
	<div align="center"><span class="annot_std_it">(<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"> : <?PHP echo $txt_div_saisoblg; ?>) </span></td>
	<?PHP } else { ?>
	&nbsp;
	<?PHP } ?>
	<td width="33%" align="right">
    <input name="quit" type="button" class="bton_std" id="quit" value="<?PHP echo $but_quit; ?>" style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;" onClick="javascript:; self.close('fscomp');">
	<?PHP if (($totalwmque>0) AND ($td<>"lst")) { ?>
     <input name="lst" type="button" class="bton_std" id="lst" value="Revenir &agrave; la liste" style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;" onClick="javascript:; window.location=('wmque_maj.php?cm=<?PHP echo $coderlpma; ?>&td=lst');">
    <?PHP } ?>
	</div>
	</td>
</tr>
</table>
<?PHP
if ($retourliste==FALSE)
{
	switch ($td)
	{
		case "add" :
		case "edit" :
		case "del" :
			if ($td=="del")
			{
?>
<form name="wmque_maj" method="post" action="wmque_maj.php">
<?PHP 		} else { ?>
<form name="wmque_maj" method="post" action="wmque_maj.php" onSubmit="javascript:; return (VForm(this,tab_champ,'','<?PHP echo $_SESSION["lng"]; ?>'));">
<?PHP 		} ?>
<input name="iwm" type="hidden" id="iwm" value="<?PHP echo $wmque_id ?>">
<input name="cm" type="hidden" id="cm" value="<?PHP echo $coderlpma ?>">
<input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
<table width="100%"  border="0">
<tr>
	<td width="160" class="rubfrm" id="f_prop" align="right">Type de lien</td>
	<td>
	<?PHP
	if ($affliste==TRUE)
	{
		echo "<select name=\"prop\" size=\"1\" id=\"prop\">";
		echo '<option>-- Choisir un type de lien --</option>';
		
		// Création de la liste des types de liens
		echo '<option value="*"';
		if ($wmque_prop == "*")
			echo ' selected';
		echo '>Public</option>';
		echo '<option value="'.$codeuser.'"';
		if ($wmque_prop <> "*")
		{
			if (($wmque_prop == $codeuser) OR (@$_SESSION["niv_log"]>=5))
				echo ' selected';
		}
		echo '>Privé</option>';
		echo "</select>";
		echo '<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">';
		echo "<br>";
		echo "<span class=\"annot_petit_it\">";
		echo "'Priv&eacute;' signifie que seul vous pourrez voir cette fiche.<br>Si vous passez ce lien en 'public', il le restera (sauf modification de l'administrateur).";
		echo "</span>";
	}
	else
	{
		if ($wmque_prop == "*")
			$nprop="Public";
		else
			$nprop="Privé";
		echo "<input name=\"prop\" type=\"hidden\" id=\"prop\" value=\"".$wmque_prop."\">";
		echo "<input name=\"nprop\" type=\"text\" id=\"nprop\" value=\"".$nprop."\" size=\"10\" readonly=\"1\">";
	}
	?>
	</td>
</tr>
<tr>
	<td width="160" class="rubfrm" id="f_desc" align="right"><?PHP echo $txt_chp_lib; ?></td>
	<td><input name="desc" type="text" id="desc" value="<?PHP echo $wmque_desc ?>" size="50" maxlength="50">
	  <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"></td>
</tr>
<tr>
	<td width="160" class="rubfrm" id="f_lien" align="right">Lien web</td>
	<td><input name="lien" type="text" id="lien" value="<?PHP echo $wmque_lien ?>" size="80" maxlength="200">
	  <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	<?PHP if ($td=="edit") echo "<a href=\"javascript:;\" onclick=\"flienexterne('".$wmque_lien."');\"><img src=\"../img/web_go.png\" border=\"0\" align=\"absmiddle\"></a>"; ?>
	  <br><span class="annot_petit_it">(Vous pouvez saisir sous la forme http://xxx.xxx.xxx ou  ftp://login:mdp@xxx.xxx.xxx)</span>
	</td>
</tr>
<tr>
	<td width="160" class="rubfrm" id="f_login" align="right">Login</td>
	<td><input name="login" type="text" id="login" value="<?PHP echo $wmque_login ?>" size="40" maxlength="40"></td>
</tr>
<tr>
	<td width="160" class="rubfrm" id="f_mdp" align="right">Mot de passe</td>
	<td><input name="mdp" type="text" id="mdp" value="<?PHP echo $wmque_mdp ?>" size="40" maxlength="40"></td>
</tr>
<?PHP if (@$_SESSION["niv_log"] >= 5)
{ 
?>
<tr>
	<td width="160" class="rubfrm" id="f_notaa" align="right">Notes Administration</td>
	<td><textarea name="notaa" id="notaa"><?PHP echo $wmque_notaa ?></textarea></td>
</tr>
<?PHP } ?>
<tr>
	<td width="160" class="rubfrm" id="f_notat" align="right">Notes publiques Agences<br><span class="annot_petit_it">(Saisies par l'administrateur ou<br>le centre de gestion)</span></td>
	<td><textarea name="notat" id="notat"><?PHP echo $wmque_notat ?></textarea></td>
</tr>
<tr>
	<td width="160" class="rubfrm" id="f_notap">
	<div align="right">Notes Personnelles<br><span class="annot_petit_it">(Pour les techniciens ce sont<br>celles de leur agence)</span></div>
	</td>
	<td>
		<textarea name="notap" rows="5" id="notap"><?PHP echo htmlspecialchars($wmquen_nota) ?></textarea>
		<span class="annot_petit_it">Vous seul pourrez voir ces notes</span>	</td>
</tr>
</table>
<p align="center">
<?PHP if ($td == "add") { ?>
<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo $but_reinit; ?>">
<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_enr; ?>">
<?PHP }

if ($td == "edit") { ?>
<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo $but_reinit; ?>">
<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_enr; ?>">
<br>
<br>
<input name="add" type="button" class="bton_std" id="add" value="Saisir un nouveau lien" onClick="javascript:; window.location=('wmque_maj.php?cm=<?PHP echo $coderlpma; ?>&td=add');">
<?PHP }

if ($td == "del") { ?>
<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_del; ?>">
<?PHP } ?>
</p>
</form>
<?PHP
		break;
	case "lst" :
		// Création de la liste des liens marques
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM `" .TBWMQUE. "` WHERE `wmque_codemque` = '".$coderlpma."' ORDER BY 'wmque_nom'";
	
		$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 liens marques
			echo "<th width=\"90%\">";
			echo "Descriptif";
			echo " <span class=\"annot_petit_it\">(cliquer pour ouvrir le lien)</span>";
			echo "</th>\n";
			echo "<th>";
			echo "Choisir";
			echo "</th>\n";
			
			$j = 0;
			
			while ($row = mysql_fetch_assoc($rs)) 
			{         
				// Création de l'entête de ligne (TR)
				creetrlstdet($j);
				
				echo "<td>\n";
				echo "<a href=\"javascript:;\" onclick=\"flienexterne('".$row["wmque_lien"]."');\">".$row["wmque_desc"]."</a>";
				echo "<div class=\"annot_petit_it\" align=\"right\">";
				if ($row["wmque_prop"]=="*")
					echo "Lien public";
				else
				{
					echo "Lien priv&eacute;";
					if  ($row["wmque_prop"]<>$codeuser) echo " (".$row["wmque_prop"].")";
				}
				echo "</div>";
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				$lst_maj=TRUE;
				echo "<a href=\"wmque_maj.php?td=edit&cm=".$row["wmque_codemque"]."&iwm=".$row["wmque_id"]."\"><img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\"></a>";
				$lst_del=TRUE;
				echo " <a href=\"wmque_maj.php?td=del&cm=".$row["wmque_codemque"]."&iwm=".$row["wmque_id"]."\"><img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\"></a>";
				echo "</tr>\n";
			
				$j++;
			}
			
			echo "</table>\n";
			// Fin affichage table liste contacts marques
			$taille_piedla="";
			include_once("piedla.inc.php");
		?>
		<p align="center">
    	<input name="add" type="button" class="bton_std" id="add" value="Saisir un nouveau lien" onClick="javascript:; window.location=('wmque_maj.php?cm=<?PHP echo $coderlpma; ?>&td=add');">
		</p>
		<?PHP
		}
		break;
		
	default :
		break;
	}
}
else
{
?>
	<script type="text/javascript">
	<!--
	window.location=('wmque_maj.php?cm=<?PHP echo $coderlpma; ?>&td=lst');
	//-->
	</script>
<?PHP
}
}
else
{
?>
	<script type="text/javascript">
	<!--
	self.close('fscomp');
	//-->
	</script>
<?PHP
}
?>
</body>
</html>
