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

Version 0.9.7

Copyright (C) 2006-2007  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");

include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_cli.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"])) == "Enregistrer") OR (strip_tags(trim(@$_POST["val"])) == "Supprimer"))
		$td = strip_tags(trim(@$_POST['td']));
}
	
if (isset($_GET['cc']))
	$codecli = strip_tags(trim(@$_GET['cc']));
else
{
	$codecli="";
	if ((strip_tags(trim(@$_POST["val"])) == "Enregistrer") OR (strip_tags(trim(@$_POST["val"])) == "Supprimer"))
		$codecli = strip_tags(trim(@$_POST['cc']));
}

if (isset($_GET['icc']))
	$idccli = strip_tags(trim(@$_GET['icc']));
else
{
	$idccli=0;
	if ((strip_tags(trim(@$_POST["val"])) == "Enregistrer") OR (strip_tags(trim(@$_POST["val"])) == "Supprimer"))
		$idccli = strip_tags(trim(@$_POST['icc']));
}

$pbm_cli=FALSE;
$totalccli=0;

if ($codecli <> "")
{
	$row = lit_enr_cli($codecli);
	if (is_array($row))
	{
		extract ($row);
		
		$totalccli=nb_ccli_in_cli($codecli);
		if ($totalccli==0)
			$td="add";
		$retourliste=FALSE;
		
		if ((strip_tags(trim(@$_POST["val"])) == "Enregistrer") OR (strip_tags(trim(@$_POST["val"])) == "Supprimer"))
		{
			$idccli = strip_tags(trim(@$_POST["icc"]));
			$code = strip_tags(trim(@$_POST["cc"]));
			$civil = str_majuscules(strip_tags(trim(@$_POST["civil"])));
			$prenom = ucwords(strtolower(str_escape(trim(@$_POST["prenom"]))));
			$nom = str_majuscules(str_escape(trim(@$_POST["nom"])));
			$fonction = str_majuscules(strip_tags(trim(@$_POST["fonction"])));
			$telf = strip_tags(trim(@$_POST["telf"]));
			$fax = strip_tags(trim(@$_POST["fax"]));
			$telp = strip_tags(trim(@$_POST["telp"]));
			$email = strtolower(strip_tags(trim(@$_POST["email"])));
			$mess = strtolower(strip_tags(trim(@$_POST["mess"])));
			$nota = str_escape(trim(@$_POST["nota"]));
			$mailc = 0;
			if (strtolower(strip_tags(trim(@$_POST["cpta"]))) == "cpta")
				$mailc = 1;
			$maild = 0;
			if (strtolower(strip_tags(trim(@$_POST["dir"]))) == "dir")
				$maild = 1;
			$mailt = 0;
			if (strtolower(strip_tags(trim(@$_POST["tec"]))) == "tec")
				$mailt = 1;

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

			switch ($td)
			{
				case "add" :
					$rech = "INSERT INTO " .TBCCLI;
					$rech .= " (ccli_datecrea,ccli_datemod,ccli_codecli,ccli_codefonc,ccli_civilite,ccli_prenom,ccli_nom,ccli_telf,ccli_fax,ccli_telp,ccli_email,ccli_mess,ccli_nota,ccli_mailc,ccli_maild,ccli_mailt) VALUES";
					$rech .= " ('$datea','$datea','$code','$fonction','$civil','$prenom','$nom','$telf','$fax','$telp','$email','$mess','$nota','$mailc','$maild','$mailt')";
					break;
				case "edit" :
					if (@$_SESSION["niv_log"]>=5)
					{
						$rech = "UPDATE `" .TBCCLI. "` SET `ccli_datemod`='".$datea."', `ccli_codecli`='".$code."', `ccli_codefonc`='".$fonction."',";
						$rech.= " `ccli_civilite`='".$civil."', `ccli_prenom`='".$prenom."', `ccli_nom`='".$nom."', `ccli_telf`='".$telf."', ";
						$rech.= " `ccli_fax`='".$fax."', `ccli_telp`='".$telp."', `ccli_email`='".$email."', `ccli_mess`='".$mess."', ";
						$rech.= " `ccli_nota`='".$nota."', `ccli_maild`='".$maild."', `ccli_mailc`='".$mailc."', `ccli_mailt`='".$mailt."'";
						$rech.= "  WHERE `ccli_id` = '".$idccli."'";
					}
					else
					{
						$rech = "UPDATE `" .TBCCLI. "` SET `ccli_datemod`='".$datea."', `ccli_codecli`='".$code."', `ccli_codefonc`='".$fonction."',";
						$rech.= " `ccli_civilite`='".$civil."', `ccli_prenom`='".$prenom."', `ccli_nom`='".$nom."', `ccli_telf`='".$telf."', ";
						$rech.= " `ccli_fax`='".$fax."', `ccli_telp`='".$telp."', `ccli_email`='".$email."', `ccli_mess`='".$mess."', ";
						$rech.= " `ccli_maild`='".$maild."', `ccli_mailc`='".$mailc."', `ccli_mailt`='".$mailt."'";
						$rech.= "  WHERE `ccli_id` = '".$idccli."'";
					}
					break;
				case "del" :
					$rech = "DELETE FROM `" .TBCCLI. "` WHERE `ccli_id` = '$idccli'";
					break;
				default :
					break;
			}
			if ($rech<>"")
			{
				$rs = mysql_query($rech) or die(mysql_error());
				$retourliste=TRUE;
			}
			mysql_close($conn);
		}
		else
		{
			switch ($td)
			{
				case "add" :
					$titre = "CREATION CONTACT";
					break;
				case "edit" :
				case "del" :
					if ($td=="edit")
						$titre = "EDITION CONTACT";
					else
						$titre = "SUPPRESSION CONTACT";
					$row=lit_enr_ccli_id_in_cli($idccli,$codecli);
					if (is_array($row))
						extract ($row);
					else
						$pbm_cli=TRUE;
					break;
				case "lst" :
					$titre = "LISTE CONTACTS";
					break;
				default :
					$pbm_cli=TRUE;
					break;
			}
		}
	}
	else
		$pbm_cli=TRUE;
}
else
	$pbm_cli=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 - ESPACE PRIVE : Contact Client</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(
	"nom:civil ; format:liste ; req:Y ; txt:Civilité ; vmn:1 ; stok:frmok ; stbad:frmbad",
	"nom:prenom; format:txt ; req:Y ; txt:Prénom; lmx:50; stok:frmok ; stbad:frmbad",
	"nom:nom; format:txt ; req:Y ; txt:Nom; lmx:50; stok:frmok ; stbad:frmbad",
	"nom:fonction ; format:liste ; req:Y ; txt:Fonction ; vmn:1; stok:frmok ; stbad:frmbad",
	"nom:telf; format:tel ; req:Y ; txt:Téléphone fixe; stok:frmok ; stbad:frmbad",
	"nom:fax; format:tel ; req:N ; txt:Télécopieur; stok:frmok ; stbad:frmbad",
	"nom:telp; format:telp ; req:N ; txt:Téléphone portable; stok:frmok ; stbad:frmbad",
	"nom:email; format:mail ; req:N ; txt:Email; stok:frmok ; stbad:frmbad",
	"nom:mess; format:mail ; req:N ; txt:Messagerie; stok:frmok ; stbad:frmbad",
<?PHP if (@$_SESSION["niv_log"] >= 5) { ?>
	"nom:nota; format:txt ; req:N ; txt:Notes; stok:frmok ; stbad:frmbad",
<?PHP } ?>
	"nom:mess; format:mail ; req:N ; txt:Messagerie; 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>

</head>
<?PHP if ($td=="del") { ?>
<body onLoad="warndel();metReadonly();">
<?PHP }

if (($td=="add") OR ($td=="edit") OR ($td=="lst")){ ?>
<body onLoad="metFocus();">
<?PHP }

if ($pbm_cli==FALSE)
{
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center"><span class="titre_gras">Pour le client : <?php echo $cli_ste; ?> </span></p>
<table width="890"  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.gif" width="10" height="11" alt="Champ obligatoire"> : Saisie obligatoire) </span></div></td>
	<?PHP } else { ?>
	&nbsp;
	<?PHP } ?>
	<td width="33%"><div align="right">
    <input name="quit" type="button" class="bton_fmarr_std" id="quit" value="Quitter" style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;" onClick="javascript:; window.opener.location.reload(); self.close('fscomp');">
	<?PHP if (($totalccli>0) AND ($td<>"lst")) { ?>
     <input name="lst" type="button" class="bton_fmarr_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=('ccli_maj.php?cc=<?PHP echo $codecli; ?>&td=lst');">
    <?PHP } ?>
	</div>
	</td>
</tr>
</table>
<?PHP
if ($retourliste==FALSE)
{
	switch ($td)
	{
		case "add" :
		case "edit" :
		case "del" :
			if ($td=="del")
			{
?>
<form name="ccli_maj" method="post" action="ccli_maj.php">
<?PHP 		} else { ?>
<form name="ccli_maj" method="post" action="ccli_maj.php" onSubmit="javascript:; return (VForm(this,tab_champ,'',''));">
<?PHP 		} ?>
<input name="icc" type="hidden" id="icc" value="<?PHP echo $ccli_id ?>">
<input name="cc" type="hidden" id="cc" value="<?PHP echo $codecli ?>">
<input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
  <table width="890"  border="0">
    <tr>
      <td width="20%" class="rubfrm" id="f_civil"><div align="right">Civilit&eacute;</div></td>
      <td width="84%"><select name="civil" size="1" id="civil">
          <?PHP
				echo '<option>-- Choisir une civilité --</option>';

				// Création de la liste des civilités
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rs = mysql_query("SELECT * FROM ".TBCIV." ORDER BY civ_desc");
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					if ($ccli_civilite == $civ_code)
					{
						echo '<option value="'.$civ_code.'" selected>'.$civ_desc.'</option>';
					}
					else
					{
						echo '<option value="'.$civ_code.'">'.$civ_desc.'</option>';
					}
				}
				mysql_free_result($rs);
				mysql_close($conn);
				?>
        </select>
          <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
	</td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_prenom"><div align="right">Pr&eacute;nom</div></td>
      <td><input name="prenom" type="text" id="prenom" value="<?PHP echo $ccli_prenom ?>" size="50" maxlength="50"  onChange="javascript:this.value=ucWords(this.value,true);">
          <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire"></td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_nom"><div align="right">Nom</div></td>
      <td><input name="nom" type="text" id="nom" value="<?PHP echo $ccli_nom ?>" size="50" maxlength="50" onChange="javascript:this.value=this.value.toUpperCase();">
          <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire"></td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_func"><div align="right">Fonction</div></td>
      <td width="84%">
	 <select name="fonction" size="1" id="fonction">
          <?PHP
				echo '<option>-- Choisir une fonction --</option>';

				// Création de la liste des fonctions
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rs = mysql_query("SELECT * FROM ".TBFONC." ORDER BY fonc_desc");
				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					if ($ccli_codefonc == $fonc_code)
					{
						echo '<option value="'.$fonc_code.'" selected>'.$fonc_desc.'</option>';
					}
					else
					{
						echo '<option value="'.$fonc_code.'">'.$fonc_desc.'</option>';
					}
				}
				mysql_free_result($rs);
				mysql_close($conn);
				?>
        </select>
          <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire">
		</td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_telf"><div align="right">T&eacute;l. Fixe </div></td>
      <td><input name="telf" type="text" id="telf" value="<?PHP echo $ccli_telf ?>" size="20" maxlength="20">
          <img src="../img/oblig.gif" width="10" height="11" alt="Champ obligatoire"></td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_fax"><div align="right">T&eacute;l&eacute;copieur</div></td>
      <td><input name="fax" type="text" id="fax" value="<?PHP echo $ccli_fax ?>" size="20" maxlength="20"></td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_telp"><div align="right">Portable</div></td>
      <td><input name="telp" type="text" id="telp" value="<?PHP echo $ccli_telp ?>" size="20" maxlength="20">
      </td>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_email"><div align="right">Email</div></td>
      <td><input name="email" type="text" id="email" value="<?PHP echo $ccli_email ?>" size="50" maxlength="50" onChange="javascript:this.value=this.value.toLowerCase();">
		<?PHP if ($td == "edit") { ?>
      	<a href="mailto:<?PHP echo $ccli_email ?>"><img src="../img/mail.png" width="22" height="22" title="Envoyer e-mail" border ="0" align="absmiddle"></a></td>
		<?PHP } ?>
    </tr>
    <tr>
      <td width="20%" class="rubfrm" id="f_mess"><div align="right">Messagerie</div></td>
      <td><input name="mess" type="text" id="mess" value="<?PHP echo $ccli_mess ?>" size="50" maxlength="50" onChange="javascript:this.value=this.value.toLowerCase();"></td>
    </tr>
    <tr>
      <td class="rubfrm" id="f_mess"><div align="right">Accepte les emails... </div></td>
      <td>
	  <?PHP if ($ccli_mailc == "0") {
	  ?>
        <input type="checkbox" name="cpta" style="background : #FFFFFF;" value="cpta">Comptabilité 
	  <?PHP } else { ?>
        <input type="checkbox" name="cpta" style="background : #FFFFFF;" checked value="cpta">Comptabilité 
	  <?PHP }
		if ($ccli_maild == "0") {
	  ?>
        <input type="checkbox" name="dir" style="background : #FFFFFF;" value="dir">Direction 
	  <?PHP } else { ?>
        <input type="checkbox" name="dir" style="background : #FFFFFF;" checked value="dir">Direction 
	  <?PHP }
		if ($ccli_mailt == "0") {
	  ?>
        <input type="checkbox" name="tec" style="background : #FFFFFF;" value="tec">Technique 
	  <?PHP } else { ?>
        <input type="checkbox" name="tec" style="background : #FFFFFF;" checked value="tec">Technique 
	  <?PHP } ?>
	    <br>
        <span class="annot_std_it">(Vous pouvez choisir plusieurs possibilit&eacute;s pour recevoir les emails...ou aucune)</span></td>
    </tr>
<?PHP if (@$_SESSION["niv_log"] >= 5)
{ 
?>
    <tr>
      <td width="20%" class="rubfrm" id="f_nota"><div align="right">Notes</div></td>
      <td><textarea name="nota" cols="50" id="nota"><?PHP echo $ccli_nota ?></textarea></td>
    </tr>
<?PHP } ?>
  </table>
  <p align="center">
    <?PHP if ($td == "add") { ?>
    <input name="RAZ" type="reset" class="bton_fmarr_std" id="RAZ" value="R&eacute;initialiser">
    <input name="val" type="submit" class="bton_fmarr_std" id="val" value="Enregistrer">
	<?PHP }
	
	if ($td == "edit") { ?>
    <input name="RAZ" type="reset" class="bton_fmarr_std" id="RAZ" value="R&eacute;initialiser">
    <input name="val" type="submit" class="bton_fmarr_std" id="val" value="Enregistrer">
	<br>
	<br>
    <input name="add" type="button" class="bton_fmarr_std" id="add" value="Saisir un nouveau contact" onClick="javascript:; window.location=('ccli_maj.php?cc=<?PHP echo $codecli; ?>&td=add');">
    <?PHP }

	if ($td == "del") { ?>
    <input name="val" type="submit" class="bton_fmarr_std" id="val" value="Supprimer">
    <?PHP } ?>
  </p>
</form>
<?PHP
		break;
	case "lst" :
		// Création de la liste des contacts clients
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM `" .TBCCLI. "` WHERE `ccli_codecli` = '".$codecli."' ORDER BY 'ccli_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 contacts clients
			echo "<th>";
			echo "Nom, Prénom";
			echo "</th>\n";
			echo "<th>";
			echo "Fonction";
			echo "</th>\n";
			echo "<th>";
			echo "T&eacute;l&eacute;phone";
			echo "</th>\n";
			echo "<th>";
			echo "Email";
			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 $row["ccli_nom"]." ".$row["ccli_prenom"];
				echo "</td>\n";
				echo "<td>\n";
				$txtfonc="&nbsp;";
				$rech2 = "SELECT * FROM `" .TBFONC. "` WHERE `fonc_code` = '".$row["ccli_codefonc"]."'";
				$rs2 = mysql_query($rech2)or die(mysql_error());
				if (mysql_num_rows($rs2) <> 0)
				{
					if ($row2 = mysql_fetch_array($rs2))
						$txtfonc=$row2["fonc_desc"];
				 }
				mysql_free_result($rs2);
				echo $txtfonc;
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo $row["ccli_telf"];
				echo "</td>\n";
				echo "<td align=\"center\">\n";
				echo "<a href=\"mailto:".$row["ccli_email"]."\">".$row["ccli_email"]."</a>";
				echo "</td>\n";
		
				echo "<td align=\"center\">\n";
				$lst_maj=TRUE;
				echo "<a href=\"ccli_maj.php?td=edit&cc=".$row["ccli_codecli"]."&icc=".$row["ccli_id"]."\"><img src=\"../img/edit.png\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
				$lst_del=TRUE;
				echo " <a href=\"ccli_maj.php?td=del&cc=".$row["ccli_codecli"]."&icc=".$row["ccli_id"]."\"><img src=\"../img/del.gif\" width=\"".$hl_vign."\" height=\"".$hl_vign."\" border=\"0\" align=\"absmiddle\"></a>";
				echo "</tr>\n";
			
				$j++;
			}
			
			echo "</table>\n";
			// Fin affichage table liste contacts clients
			$taille_piedla="";
			include_once("piedla.inc.php");
		?>
		<p align="center">
    	<input name="add" type="button" class="bton_fmarr_std" id="add" value="Saisir un nouveau contact" onClick="javascript:; window.location=('ccli_maj.php?cc=<?PHP echo $codecli; ?>&td=add');">
		</p>
		<?PHP
		}
		break;
		
	default :
		break;
	}
}
else
{
?>
	<script type="text/javascript">
	window.location=('ccli_maj.php?cc=<?PHP echo $codecli; ?>&td=lst');
	</script>
<?PHP
}
}
else
{
?>
	<script type="text/javascript">
	self.close('fscomp');
	</script>
<?PHP
}
?>
</body>
</html>
