<?php
/*
*  appel des fonctions pour remplir les select du formulaire Sosa (recherches des individus liés à un patronyme)
*  en fonction des sélections successives
*/

	session_start();
	
	$phase= $_GET['phase'];
	$id_sosa = $_GET['id_sosa'];
	$family = $_GET['famille'];
	$patronyme=$_GET['patronyme'];
	$sosa = $_GET['sosa'];
	$DS= DIRECTORY_SEPARATOR;
	$ROOT=dirname(dirname(dirname(dirname(__FILE__))));
	$BASE_URL= dirname(dirname(dirname(dirname($_SERVER['SCRIPT_NAME'])))); 
	require $ROOT.$DS.'webroot'.$DS.'includes'.$DS.'lib_inc.php';
	
	$name_family = $_SESSION['t_familles'] [$family]['nom_famille'];
	
	if ($phase == 0) {
	// phase 0 : demande de confirmation avant 	
		$message = "</br><form>".$patronyme." ".$sosa."</br>".display_lng_text('SOSA_CHGT')."</br>".$name_family."</br></br>";
		$message .= "<input type='button'  value='Annuler' onClick='AnnulerSosaUpdate();' >&nbsp;&nbsp;";
		$message .= "<input type='button'  value='Confirmer' onClick='ConfirmerSosaUpdate(\"".$BASE_URL."\",".$family.",".$id_sosa.");' ></br>";
	//	$message .= " ConfirmerSosaUpdate(\"".$ROOT."\",".$family.",".$id_sosa.")"
		$response= $message."$".utf8_encode(display_lng_text('SOSA_ANNUL'))."$".utf8_encode(display_lng_text('SOSA_CONFIRM'));
		echo $response;
	}

	else {
	// phase 1 : modification confirmée	
	
	
	require $ROOT.$DS.'core'.$DS.'Router.php';	
	require $ROOT.$DS.'core'.$DS.'Request.php';
	require $ROOT.$DS.'core'.$DS.'Controller.php';
	require $ROOT.$DS.'core'.$DS.'Model.php'; 
	

	require $ROOT.$DS.'config'.$DS.'conf.php';

	require_once($ROOT.$DS.'model'.$DS.'Familles.php');
	$Familles = new Familles();
	
	//mise blanc des numéro sosa existant
	
		$table = 'oxg_individuals';
		//$this->Familles->table = $table;

		$sosa=$_GET['id_sosa'];
		$Familles->table = $table;
		$conditions .= "id_family=".$family;
$_SESSION['ind_affiche']=1;	
		$result_upd = $Familles->update(array('sosa'=>'-1'),$conditions);
echo "nbre modifié à -1 = ".$result_upd;
	
	//maj du sosa n° 1
		$conditions  = "id_individual=".$sosa;
		$conditions .= " and id_family=".$family;
		$result_upd = $Familles->update(array('sosa'=>'1'),$conditions);
		echo "nbre modifié à 1= ".$result_upd;
	//maj des nouveaux numéro sosa
		update_sosa ($Familles,$family,$sosa, '1');
		$_SESSION['t_stats_accueil'] = array();
		$_SESSION['b_init'] = 0;
		
		
	} // fin dif else branche phase=1


	
	function update_sosa($Familles,$family,$id_ind,$id_sosa) 	{
		// $DS= DIRECTORY_SEPARATOR;
		// $ROOT=dirname(dirname(dirname(dirname(__FILE__))));
		// require_once($ROOT.$DS.'model'.$DS.'Familles.php');
		// $Familles = new Familles();
//$_SESSION['ind_affiche']=1;			
		$table = 'oxg_individuals';
		//$this->Familles->table = $table;
		$Familles->table = $table;
		$conditions  = "id_individual=".$id_ind;
		$conditions .= " and id_family=".$family;
		
		$people=$Familles->find(array(
				'fields'  	=> 'id_father, id_mother',
				'conditions' => $conditions
		));
		foreach($people as $k => $row) {
					
			//$data = new stdClass();
			$sosa_father = ($id_sosa*2);
			$sosa_mother = ($id_sosa*2)+1;
			$id_father = $row->id_father;
			$id_mother = $row->id_mother;
			if ($id_father!=-1) { 
$_SESSION['ind_affiche']=1;				
				//$data->sosa  = $sosa_father;
				$conditions  = "id_individual=".$id_father." and sosa=-1";
				$conditions .= " and id_family=".$family;
				
				$result_upd  = $Familles->update(array('sosa'=>$sosa_father),$conditions);
				echo "nbre modifié (".$sosa_father.")= ".$result_upd;
			}
			
		//	$result = mysql_query( "UPDATE oxg_individuals SET sosa=$sosa_father WHERE id_individual=$id_father and sosa=-1 $sReqFam");
			if ($id_mother!=-1) {
				//$data->sosa = $sosa_mother;

				$conditions = "id_individual=".$id_mother." and sosa=-1";
				$conditions .= " and id_family=".$family;
				
				$result_upd = $Familles->update(array('sosa'=>$sosa_mother),$conditions);
				echo "nbre modifié (".$sosa_mother.")= ".$result_upd;
			}	
				
			//	$result = mysql_query( "UPDATE oxg_individuals SET sosa=$sosa_mother WHERE id_individual=$id_mother and sosa=-1 $sReqFam");
			if ($id_father!=-1) update_sosa ($Familles,$family,$id_father, $sosa_father);
			if ($id_mother!=-1) update_sosa ($Familles,$family,$id_mother, $sosa_mother);
		}
	}