<?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
****************************************************************************************/

session_start(); 
include_once("../inc/config.inc.php");
include_once("../inc/func_log.inc.php");
if (CheckURLLog(TRUE)==FALSE) header("Location: ../index.php");
if (CheckNivLog("5-9")==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");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = strip_tags(trim(@$_POST["titre"]));
$td = strip_tags(trim(@$_POST["td"]));

$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$desc = str_majuscules(strip_tags(str_escape(trim(@$_POST["desc"]))));
$img = strip_tags(trim(@$_POST["image"]));

$retourliste = TRUE;

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

switch ($td)
{
	// Ajout de la fiche Statut
	case "add" :
		$rech = "SELECT * FROM " .TBSTA. " WHERE statut_code = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rs = mysql_query("INSERT INTO " .TBSTA. "(statut_code,statut_desc,statut_img,statut_delok) VALUES ('$code','$desc','$img','1')") or die(mysql_error());
			$msg = "Fiche ajoutée";			
		}
		else
		{
			$msg = "Ce code existe déjà !";
			$retourliste = FALSE;
		}
		break;
	
	// Modification de la fiche Statut
	case "edit" :
		$rech = "UPDATE `" .TBSTA. "` SET `statut_desc`='".$desc."' WHERE `statut_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
		$msg = "Fiche modifiée";			
		break;

	// Suppression de la fiche Statut
	case "del" :
		// Vérifie si utilisé dans une fiche intervention
		$rexist = "SELECT * FROM `" .TBINT. "` WHERE `int_codestatut` = '$code'";
		$rs = mysql_query($rexist) or die(mysql_error());
		if (mysql_num_rows($rs) <> 0)
		{
			$msg = "Ce statut est utilisée dans une fiche intervention !";			
			$retourliste = FALSE;
		}
		else
		{
			// Vérifie si on peut supprimer ce règlement
			$rexist = "SELECT * FROM `" .TBSTA. "` WHERE `statut_code` = '$code' AND `statut_delok` = '1'";
			$rs = mysql_query($rexist) or die(mysql_error());
			
			if (mysql_num_rows($rs) <> 0)
			{
				// SUPPRESSION de la fiche Statut
				$rech = "DELETE FROM `" .TBSTA. "` WHERE `statut_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());
				$msg = "Fiche supprimée";			
			}
			else
			{
				$msg = "Ce statut ne peut pas être supprimé !";			
				$retourliste = FALSE;
			}
		}
		break;

	default :
		break;
}
mysql_close($conn);
?>
<!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 : Fiche Statut</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>

</head>
</body>

<?php 
include_once("../inc/entete.inc.php"); 
?>

<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>
<?PHP if ($retourliste == FALSE) { ?>
	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"><a href="statut_lst.php"><img src="../img/liste.gif" width="33" height="34" border="0" align="absmiddle"></a> <a href="statut_lst.php" class="annot_gras_it">Retour &agrave; la liste</a></div></td>
		<td width="7%">&nbsp;</td>
		<td width="43%"><a href="index.php"><em><img src="../img/index.gif" width="48" height="48" border="0" align="absmiddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
<?PHP } else { ?>
	<script type="text/javascript">
	window.setTimeout("location=('statut_lst.php');",2000)
	</script>
	<p align="center" class="annot_std_it">Retour à la liste dans 2 secondes...</p>
<?PHP }
include_once("pied.php")
?>

