<?php

	$sPage="rechercheLieuX";
	$sTitle="";

//---------------------------
// Fichier : recherche.php
// Auteur : Doudou
// Date creation : 12/08/2004 par Doudou
// Derniere modif : 26/09/2004 par Oxy-Gen
// Derniere modif : 19/01/2006 par Oxy-Gen : Prise en compte des familles
// Objet : recherche d'un individu//	
//---------------------------
// inclusion des variables programmes
	require("admin/§lib_inc.php");
	require("admin/§lib_inc2.php");
	require("§menu.php");
	require("§progress_bar.php");

// recup variable du formulaire
$nom = @$_GET['nom'];
$prenom = @$_GET['prenom'];
$sexe = @$_GET['sexe'];
$lieu = @$_GET['lieu'];

$start = @$_GET['start'];
$end = @$_GET['end'];
$choice = @$_GET['choice'];
if ($choice=="") {$choice="all";}
$limite = @$_GET['limite'];
$FAMILY = @$_GET['FAMILY'];
$COUNTRY=isset($_REQUEST["COUNTRY"])?$_REQUEST["COUNTRY"] : -1;
$ZONE=isset($_REQUEST["ZONE"])?$_REQUEST["ZONE"] : -1;
$CITY=isset($_REQUEST["CITY"])?$_REQUEST["CITY"] : -1;

if($COUNTRY!=-1 || $ZONE!=-1 || $CITY!=-1)
{
	$lieu="1";
}

if($FAMILY=="")
{
	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 
	
}
$nb_msg_par_page = @$_GET['nb_msg_par_page'];


// on determine le nombre de resultats vus par page

if (empty($nb_res_par_page))
	$nb_res_par_page = '20';

$limite_param=$limite;	
// on test le parametre de limite
if (empty($limite))
	$limite=0;
else
	$limite=intval($limite*$nb_res_par_page);

// on definit le nobmre de page avant les fleches << 1 2 3 >>
// mettre un chiffre pair
$nb_aff_page=9;

debut_html("Recherche d'un individu");
// affichage du babeau de critères
echo "<form action=\"\" method='get'>";
echo "<input type=hidden name=FAMILY value=\"$FAMILY\">";
echo "<input type=hidden name=COUNTRY value=\"$COUNTRY\">";
echo "<input type=hidden name=ZONE value=\"$ZONE\">";
echo "<input type=hidden name=CITY value=\"$CITY\">";
echo "<center>";
echo "<table border=1><tr><td>";
display_lng_text("SEARCH_LAST_NAME");
echo " : </td><td><input type=text name=nom value=\"$nom\"></td>\n<td>";
display_lng_text("SEARCH_FISRT_NAME");
echo " : </td><td><input type=text name=prenom value=\"$prenom\"></td>\n<td>";
display_lng_text("SEARCH_SEXE");
echo " : </td><td><select name=sexe>";
echo "<OPTION value=>";
display_lng_text("SEARCH_SEXE_NO");
echo "</OPTION>";
echo "<OPTION ";
if ($sexe=="h")
echo "SELECTED ";
echo "value=\"h\">";
display_lng_text("SEARCH_SEXE_MALE");
echo "</OPTION>";
echo "<OPTION ";
if ($sexe=="f")
echo "SELECTED ";
echo "value=\"f\">";
display_lng_text("SEARCH_SEXE_FEMALE");
echo "</OPTION>";
echo "</SELECT></td>\n</tr>";
echo "<tr>";
echo "<td>".decode_lng_text("GEDTAG_DATE")." : </td><td colspan='5' rowspan='1'>";
echo decode_lng_text("DETAIL_DATE_BEGIN")." = <input type=text name=start value=\"$start\" SIZE=4>&nbsp;&nbsp;&nbsp;&nbsp;".decode_lng_text("DETAIL_DATE_END")." = <input type=text name=end value=\"$end\" SIZE=4>";
echo "</td></tr>";

echo "<tr><td>".decode_lng_text("INDEX_EVENT")." :</td>";
echo "<td colspan='4' rowspan='1'>";
if ($choice=='birth') {$check='checked';} else {$check='';}
echo "<input type='radio' name='choice' value='birth' $check>&nbsp;".decode_lng_text("GEDTAG_BIRT")."&nbsp;&nbsp;&nbsp;";
if ($choice=='wedding') {$check='checked';} else {$check='';}
echo "<input type='radio' name='choice' value='wedding' $check>&nbsp;".decode_lng_text("GEDTAG_MARR")."&nbsp;&nbsp;&nbsp;";
if ($choice=='death') {$check='checked';} else {$check='';}
echo "<input type='radio' name='choice' value='death' $check>&nbsp;".decode_lng_text("GEDTAG_DEAT")."&nbsp;&nbsp;&nbsp;";
if ($choice=='other') {$check='checked';} else {$check='';}
echo "<input type='radio' name='choice' value='other' $check>&nbsp;".decode_lng_text("PATRO_OTHER")."&nbsp;&nbsp;&nbsp;";
if ($choice=='all') {$check='checked';} else {$check='';}
echo "<input type='radio' name='choice' value='all' $check>&nbsp;".decode_lng_text("IDX_LIEUX_ALL")."&nbsp;&nbsp;";
echo "</td>";
echo "<td><center><input type=submit value=".decode_lng_text('PRIVATE_ENTER')."></center></td>";
echo "</tr></table></form>";
echo "</center>";


// preparation de la requete
$req_select = "SELECT oxg_individuals.id_family, oxg_individuals.id_individual, oxg_individuals.id_patronym, oxg_individuals.first_name, oxg_individuals.sex";
$req_select = $req_select.", oxg_patronyms.id_patronym, oxg_patronyms.libelle";
	if (!empty($lieu) or !empty($year) or !empty($start) or !empty($end))
	{
		$req_select = $req_select.", oxg_events2.id_family, oxg_events2.id_individu, oxg_events2.id_city, oxg_events2.type, oxg_events2.devnt";
		$req_select = $req_select.", oxg_cities.id_city, oxg_cities.libelle";
	}
//$req_select = "SELECT *";
$req_select = $req_select." FROM oxg_individuals, oxg_patronyms ";
	if (!empty($lieu) or !empty($year) or !empty($start) or !empty($end))
	{
		$req_select = $req_select." ,oxg_events2, oxg_cities ";
	}
$flag_where=1;

if ( !empty($nom) or !empty($prenom) or !empty($sexe) or !empty($lieu) or !empty($year) or !empty($start) or !empty($end) )
{
	if (empty($nom)) {$nom='*';}
	//if (empty($lieu)) {$lieu='*';}
	// si on a le nom en critere
	//if (empty($nom)) {$nom='*';}
	if (!empty($nom))
		{
		if (strpos($nom, "*")!==false) {$nom = str_replace("*", "%", $nom);}
		$test = "oxg_patronyms.libelle LIKE \"$nom\"";
		// on va rechercher le nom dans la table 
		$req_select = test_where($req_select);
		$req_select = $req_select." oxg_patronyms.id_patronym=oxg_individuals.id_patronym and $test";
		}
	
	// si on a le prenom en critere
	if (!empty($prenom))
		{
		if (strpos($prenom, "*")!==false) {$prenom = str_replace('*', '%', $prenom);}
		$test = " oxg_individuals.first_name LIKE \"%$prenom%\"";
		// on va rechercher le prenom dans la table 
		$req_select = test_where($req_select);
		$req_select = $req_select." ".$test;
		}
	  
	// si on a le sexe en critere
	if (!empty($sexe))
		{
		if ($sexe=="h")
			$sex=0;
		else
			$sex=1;	
		// on va rechercher le prenom dans la table 
		$req_select = test_where($req_select);
		$req_select = $req_select." oxg_individuals.sex=\"$sex\"";
		}
	
	// si on a le lieu en critere
	//if (empty($lieu)) {$lieu='*';}
	if (!empty($lieu))
	{
//		if (strpos($lieu, "*")!==false) {$lieu = str_replace('*', '%', $lieu);}
//		$test = " oxg_cities.libelle LIKE \"$lieu\"";
		if($CITY!=-1)
		{
			$test = " oxg_cities.id_city in ($CITY) and oxg_cities.id_zone in ($ZONE) and oxg_cities.id_country in ($COUNTRY)";
		}
		else if($ZONE!=-1)
		{
			$test = " oxg_cities.id_zone in ($ZONE) and oxg_cities.id_country in ($COUNTRY)";
		}
		else if($COUNTRY!=-1)
		{
			$test = "  oxg_cities.id_country in ($COUNTRY)";
		}
		// on va rechercher le nom du lieux dans la table 
		$req_select = test_where($req_select);
		$req_select = $req_select." oxg_events2.id_individu=oxg_individuals.id_individual and oxg_events2.id_city=oxg_cities.id_city and $test";
		if($FAMILY!=-1)
		{
			$req_select = $req_select." and oxg_events2.id_family=$FAMILY";
		}
		//$req_select = $req_select." oxg_events2.id_city=\"$row[0]\" and oxg_events2.id_father=id_individual";
		if ($choice=='birth') {$req_select = $req_select." and oxg_events2.type=51";}
		if ($choice=='wedding') {$req_select = $req_select." and oxg_events2.type=115";}
		if ($choice=='death') {$req_select = $req_select." and oxg_events2.type=77";}
		//if ($choice=='other') {$req_select = $req_select." and oxg_events2.type=22";}
		if ($choice=='other') {$req_select = $req_select." and oxg_events2.type<>'51' and oxg_events2.type<>'115' and oxg_events2.type<>'77'";}
	}

	// si on a une date en critere
	if (!empty($year) or !empty($start) or !empty($end))
	{
		if (empty($lieu))
			{
			$req_select = test_where($req_select);
			$req_select = $req_select." oxg_events2.id_individu=oxg_individuals.id_individual";
			if($FAMILY!=-1)
				{
					$req_select = $req_select." and oxg_events2.id_family=$FAMILY";
				}
			if ($choice=='birth') {$req_select = $req_select." and oxg_events2.type='51'";}
			if ($choice=='wedding') {$req_select = $req_select." and oxg_events2.type='115'";}
			if ($choice=='death') {$req_select = $req_select." and oxg_events2.type='77'";}
			if ($choice=='other') {$req_select = $req_select." and oxg_events2.type<>'51' and oxg_events2.type<>'115' and oxg_events2.type<>'77'";}
			}
//		if ($year<>"") {$req_select = $req_select." and left(oxg_events2.devnt,4)=$year";}
		if ($start<>"") {$req_select = $req_select." and left(oxg_events2.devnt,4)>=$start";}
		if ($end<>"") {$req_select = $req_select." and left(oxg_events2.devnt,4)+1>1 and left(oxg_events2.devnt,4)<=$end";}
	}	
}
	
//Affichage du résultat
if ($flag_where)
{
	display_lng_text("SEARCH_NO_CRITERIA");
}
else
{
	// on recupere la page + les parametres
	$lien = $_SERVER['REQUEST_URI'];
	// on retire le parametre limit
	$pos = strpos($lien,'&limite=');
	//if ($pos!=='0')
	//	$lien = substr($lien,0,$pos);
	
	if($FAMILY!=-1)
	{
		$req_select = test_where($req_select);
		$req_select = $req_select." oxg_individuals.id_family=$FAMILY";
	}
	$req_select = $req_select." GROUP by oxg_individuals.id_individual";
	$req_select = $req_select." ORDER by oxg_patronyms.libelle_ord, oxg_individuals.first_name ASC";
	$result2 = mysql_query($req_select);
	$nb_res_max = mysql_num_rows($result2);
	// on test si on affiche la limite de resulats
	if ($limite_param!=-1) { $req_select = $req_select." LIMIT $limite, $nb_res_par_page"; }
	//echo "execution de la requete : $req_select<br>\n";
	$result = mysql_query($req_select);
	$nb_res = mysql_num_rows($result);
	if ($nb_res>0)
	{
		$limiteplus = intval($limite+$nb_res_par_page);
		if ($limiteplus>$nb_res_max)
			$limiteplus=$nb_res_max;
		$limitemoins = $limite+1;
		echo "<hr>\n<b> $nb_res_max ";
		display_lng_text("SEARCH_FOUND_RESULTS");
		echo "  !</b> \n";
		if (($limite_param!=-1) && ($nb_res_max>$nb_res_par_page))
		{
			// lien pour ne pas mettre de limites de résultats
			echo "<a href=\"$lien&FAMILY=$FAMILY&limite=-1\">";
			display_lng_text("SEARCH_NO_LIMITS");
			echo "</a><br>";
			
			display_lng_text("SEARCH_RESULTS_NUMBERS");
			echo " $limitemoins => $limiteplus<br>\n";
			display_lng_text("SEARCH_PAGE_NEXT");
			aff_numeros_pages($nb_res_par_page,$nb_aff_page,$nb_res_max,$lien,$limite_param);
		}
	
	}
	else	
	{
		echo "<br>\n";
		display_lng_text("SEARCH_NO_RESULTS");
		echo "<br>\n";
	}
	$cpt=1;	
	echo "<table border=0>";
	while($row = mysql_fetch_array($result))
	{
		echo "<tr><td>";
		display_lng_text("SEARCH_FICHE");		
		if ($row['sex']==1) { $flag="female.png"; }
		if ($row['sex']==0) { $flag="male.png"; }
		echo "  <IMG SRC=$flag> ";
		echo affiche_lien_individu_lieux($row['id_family'], $row['id_individual'], true, true);
		echo " </td><td> ";
		//affiche_lien_arbre($row['id_family'], $row['id_individual']);
		echo "</td></tr>\n";
		$cpt++;
		
		flush();
	}

	
	echo "</table><hr>";
	// on reaffiche les lignes des pages suivantes si besoin
	if (($limite_param!=-1) && ($nb_res_max>$nb_res_par_page))
	{
		// lien pour ne pas mettre de limites de résultats
		echo "<a href=\"$lien&FAMILY=$FAMILY&limite=-1\">";
		display_lng_text("SEARCH_NO_LIMITS");
		echo "</a><br>";
		
		display_lng_text("SEARCH_PAGE_NEXT");			
		aff_numeros_pages($nb_res_par_page,$nb_aff_page,$nb_res_max,$lien,$limite_param);
	}
}	  

function affiche_lien_individu_lieux($id_family, $id_individual, $bAffichePatronyme=true, $bAfficheDate=false, $bAfficheSurnom=false)
{
	global	$bSosa, $bSessionOk;

	if($id_family!=-1)
	{
		$sReqFam="and id_family=$id_family";
	}
	else
	{
		$sReqFam="";
	}
	
	$sInd="";
	
	$sReq  = "select first_name, libelle ";
	if($bSosa==true)
	{
		$sReq  .= ", sosa ";
	}
	$sReq .= ", date_naissance, date_deces, contemporary ";
	$sReq .= " from oxg_individuals, oxg_patronyms ";
	$sReq .= "where id_individual='$id_individual' ";
	$sReq .= "and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ";
	$sReq .= $sReqFam;

	$result = mysql_query( $sReq ) or mysql_die();
	if($row = mysql_fetch_array($result));
	{
		$sInd.="<A HREF='§detail_individu.php?INDIVIDU=$id_individual&FAMILY=$id_family'>";
		if($bAffichePatronyme==true)
		{
			$sInd.="$row[libelle] ";
		}
		if($row['first_name']=="")
		{
			$sInd.="?";
		}
		else
		{
			$sInd.=$row['first_name'];
		}
		if($bAfficheSurnom==true)
		{
			$sInd.=display_nickname($id_individual, $sReqFam, "GIVN", "NICK", $row['sex']);
		}
		$sInd.="</A>";
		if($bSosa==true && $row['sosa']!='-1')
		{
			$sInd=$sInd." <strong><font color=red>$row[sosa]</font></strong>";
		}
		
		if($bAfficheDate==true && ($row['date_naissance']!="" || $row['date_deces']!=""))
		{
			$sInd.=" (";
			if($row['date_naissance']!="")
			{
				$sInd.=decode_date_eng($row['date_naissance']);
			}
			else
			{
				$sInd.="?";
			}
			if($row['date_deces']!="")
			{
				$sInd.=" - ";
				$sInd.=decode_date_eng($row["date_deces"]);
			}
			$sInd.=")";
		}
	}
	if ($row['first_name']=="" AND $row['libelle']=="")
	{
		return "?";
	}
	else
	{	
		if (($bSessionOk==false || $bSessionOk=="") and $row['contemporary']==0)
		{
			return decode_lng_text('PRIVATE_MSG');
		}
		else
		{
			return $sInd;
		}
	}
}

affiche_pied_page(true);
//echo $req_select;
?>

<html>
<script type="text/javascript">
document.getElementById('progress_bar').style.display="none"
</script>
</html>
