<?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 ss-traitant en fonction spécialité */
/***************************************************/

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

/** Liste des sous-traitant en fonction pays, dépertement, et spécialité **/

function print_plain()
{
	header("Content-type: text/html; charset=windows-1252");
	$gsp=$_REQUEST["sp"];
	$tab_sp=explode("_",$gsp);
	$str_sp="";
	for ($i=2;$i<count($tab_sp);$i++)
	{
		$str_sp.=$tab_sp[$i]."_";
	}
	$result = array();
	if ($str_sp<>"")
	{
		$str_sp = substr($str_sp,0,strlen($str_sp)-1);
		$sp = get_sp($tab_sp[0],$tab_sp[1],$str_sp);
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		foreach ($sp as $row)
		{
			$result[] = $row["st_code"]."=".$row["st_nom"];
		}
		mysql_close($conn2);
	}
	print implode(";", $result);
}

function get_sp($payscli,$depcli,$sp)
{
	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);
	$rech = "SELECT DISTINCT ".TBZOC.".zoc_code FROM ".TBZOC.", ".TBST;
	$rech.= " WHERE ".TBZOC.".zoc_codepays='".str_majuscules($payscli)."' AND ".TBZOC.".zoc_codedep='".$depcli."'";
	$rech.= " AND ".TBZOC.".zoc_code=".TBST.".st_code";
	$rech.= " ORDER BY ".TBST.".st_nom";
	$rs = mysql_query($rech) or die(mysql_error());
	// On cree le resultat
	$result = array();
	while ($row = mysql_fetch_array($rs))
	{
		$rech2 = "SELECT * FROM ".TBST." WHERE `st_ouvert` = '1'";
		$rech2.= " AND `st_code` = '".$row["zoc_code"]."'";
		if ($sp<>"*")
		{
			$tab_sp=explode("_",$sp);
	
			// *** Filtrage
			if (count(tab_sp)>0)
			{
				// WHERE existe ?
				if(!stristr($rech2,"WHERE "))
				 $rech2 .= " WHERE (";
				else
				 $rech2 .= " AND (";
					
				// Construction de la chaîne de recherche
				foreach ($tab_sp as $idx=>$value)
				{
					 $rech2 .= " st_spe LIKE '%".str_escape(trim($value))."%' OR";
				}	
				// Suppression du dernier 'OR'
				$rech2 = substr($rech2,0,strlen($rech2)-3);
				$rech2 .= ")";
			}
			// *** Fin de Filtrage
		}
		$rech2.= " ORDER BY st_nom";
	// print($rech2);
		$rs2 = mysql_query($rech2) or die(mysql_error());
		if (mysql_num_rows($rs2)>0)
		{
			if ($row2 = mysql_fetch_array($rs2))
			{
				$rech3= "SELECT * FROM `" .TBPW. "` WHERE `pw_code` = '".$row2["st_code"]."' AND `pw_actif` = '1'";
				$rs3 = mysql_query($rech3) or die(mysql_error());
				if (mysql_num_rows($rs3) <> 0)
					$result[] = $row2;
			}
		}
	}
	mysql_close();
	return $result;
}

?>