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

/****************************************************************/
/* Récupération des destinataires en fonction type destinataire */
/****************************************************************/

session_start(); 
sleep(1);
if (array_key_exists("tdest", $_REQUEST))
{
	$tomorrow = 0;//60*60*24; // en nb de secondes
	header("Cache-Control: max-age=$tomorrow");
	print_plain();
}
else
{
	print "Usage : $_SERVER[PHP_SELF]?tdest=un-type-destinataire";
}

function print_plain()
{
	global
		$_SESSION;
	header("Content-type: text/html; charset=windows-1252");
	$types = get_types($_REQUEST["tdest"]);
	$result = array();
	if (count($types)>0)
	{
		foreach ($types as $row)
		{
			switch ($_REQUEST["tdest"])
			{
				case 1 :
					$result[] = $row["tec_code"]."=".$row["tec_nom"]." ".$row["tec_prenom"]." (".$row["tec_codeage"].")";
					break;
				case 2 :
					$result[] = $row["age_code"]."=".$row["age_resp_nom"]." ".$row["age_resp_prenom"]." (".$row["age_code"].")";
					break;
				case 3 :
					$result[] = $row["tec_code"]."=".$row["tec_nom"]." ".$row["tec_prenom"]." (".$row["tec_codest"].")";
					break;
				case 4 :
					$result[] = $row["st_code"]."=".$row["st_resp_nom"]." ".$row["st_resp_prenom"]." (".$row["st_code"].")";
					break;
				case 5 :
					$result[] = "CGE=Centre de gestion";
					break;
			}
		}
	}
	else
	{
		if ($_REQUEST["tdest"]==-1)
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			switch (@$_SESSION["niv_log"])
			{
				case 3 :
					$rech = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '".@$_SESSION["code_log"]."'";
					$rs = mysql_query($rech)or die(mysql_error());
					if ($row = mysql_fetch_array($rs))
						$result[] = $row["tec_code"]."=".$row["tec_nom"]." ".$row["tec_prenom"]." (".$row["tec_codeage"].")";
					break;
				case 4 :
					$rech = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '".@$_SESSION["code_log"]."'";
					$rs = mysql_query($rech)or die(mysql_error());
					if ($row = mysql_fetch_array($rs))
						$result[] = $row["age_code"]."=".$row["age_resp_nom"]." ".$row["age_resp_prenom"]." (".$row["age_code"].")";
					break;
				case 5 :
					$result[] = "CGE=Centre de gestion";
					break;
				case 10 :
					$rech = "SELECT * FROM `" .TBST. "` WHERE `st_code` = '".@$_SESSION["code_log"]."'";
					$rs = mysql_query($rech)or die(mysql_error());
					if ($row = mysql_fetch_array($rs))
						$result[] = $row["st_code"]."=".$row["st_resp_nom"]." ".$row["st_resp_prenom"]." (".$row["st_code"].")";
					break;
				case 11 :
					$rech = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '".@$_SESSION["code_log"]."'";
					$rs = mysql_query($rech)or die(mysql_error());
					if ($row = mysql_fetch_array($rs))
						$result[] = $row["tec_code"]."=".$row["tec_nom"]." ".$row["tec_prenom"]." (".$row["tec_codest"].")";
					break;
				default :
					$result[] = @$_SESSION["code_log"]."=".@$_SESSION["code_log"];
					break;
			}
			mysql_free_result($rs);
			mysql_close($conn);
		}
	}
	print implode(";", $result);
}

function get_types($tdest)
{
	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_txt.inc.php");
	// On se connecte a la base
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	// On recupere les enregistrements
	$rech="";
	$result = array();
	switch ($tdest)
	{
		case 1 :
			$rech = "SELECT * FROM ".TBTEC;
			switch (@$_SESSION["niv_log"])
			{
				case 3 :
				case 11 :
					$rech.= " WHERE tec_code='".@$_SESSION["code_log"]."'";
					break;
				case 4 :
					$rech.= " WHERE tec_codeage='".@$_SESSION["code_log"]."'";
					break;
				default :
					$rech.= " WHERE tec_codeage<>''";
					break;
			}
			$rech.= " AND tec_out=0 ORDER BY tec_nom";
			break;
		case 2 :
			$rech = "SELECT * FROM ".TBAGE." WHERE age_ouvert=1 ORDER BY age_nom";
			break;
		case 3 :
			$rech = "SELECT * FROM ".TBTEC;
			if (@$_SESSION["niv_log"]<>10)
				$rech.= " WHERE tec_codest<>''";
			else
				$rech.= " WHERE tec_codest='".@$_SESSION["code_log"]."'";
			$rech.= " AND tec_out=0 ORDER BY tec_nom";
			break;
		case 4 :
			$rech = "SELECT * FROM ".TBST." WHERE st_ouvert=1 ORDER BY st_nom";
			break;
	}
	if ($rech<>"")
	{
		$rs = mysql_query($rech) or die(mysql_error());
		// On cree le resultat
		while($row = mysql_fetch_array($rs))
		{
			$result[] = $row;
		}
	}
	else
	{
		if ($tdest==5)
			$result[] = "CGE=Centre de gestion";
	}
	mysql_close();
	return $result;
}

?>