	<?php	
    // header('Access-Control-Allow-Origin: *.khandesh.site');
    @$persDb = $db_functions->get_person($main_person);
	// *** Use class to process person ***
// 	$pers_cls = New person_cls;
// 	$pers_cls->construct($persDb);
	
    $start_url= $humo_option["link01"].'/index.php?rin='.$persDb->pers_gedcomnumber;
	echo "<h2> <a href=" . $start_url . ">Close relatives of " . $persDb->pers_firstname . "</a></h2>" ; 
	
	?>
    
	<table class="humo index_table">
		<thead>
			<tr>
				<th>RIN</th>
				<th>NAME</th>
				<th>Date of Birth</th>
				<th>PHONE</th>
				<th>EMAIL</th>
				<th>PLACE</th>
				<th>ALIVE</th>
				<th>GENDER</th>
				<th>RELATION</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$sql ="SELECT 
          rel.pers_gedcomnumber
        , rel.pers_firstname 
        , rel.pers_lastname
        , rel.pers_birth_date
        , a.address_place
        , a.address_email
        , a.address_phone
        , rel.pers_sexe
        , rel.pers_alive
        , 'parent' as relation
        FROM (`humo_persons` self left join `humo_persons` rel
        ON self.pers_famc = rel.pers_indexnr
        AND rel.pers_famc !=  rel.pers_indexnr)
        LEFT JOIN humo_addresses a ON rel.pers_gedcomnumber = a.address_connect_id
        WHERE self.pers_gedcomnumber = ?
           ";
        
        $result = $dbh->prepare($sql); 
        $result->bindParam(1,$main_person,PDO::PARAM_STR);
        $result->execute();
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		    
		    $start_url=CMS_ROOTPATH.'/admin/index.php?page=editor&amp;tree=humo_'.'&amp;person='.$row['pers_gedcomnumber'];
		    
		    
			echo '<tr>
					<td><a href="'.$start_url.'">'.$row['pers_gedcomnumber'].'</a></td>
					<td>'.$row['pers_firstname'].' '.$row['pers_lastname'].'</td>
					<td>'.$row['pers_birth_date'].'</td>
					<td>'.$row['address_phone']. '</td>
					<td>'.$row['address_email'].'</td>
					<td>'.$row['address_place'].'</td>
					<td>'.$row['pers_alive'].'</td>
					<td>'.$row['pers_sexe'].'</td>
					<td>'.$row['relation'].'</td>
				</tr>';
		}
		?>
		
		<!-- siblings   --> 
		<!-- special case - 1. when parent is missing -->
		<?php
		$sql ="SELECT 
          rel.pers_gedcomnumber
        , rel.pers_firstname 
        , rel.pers_lastname
        ,  rel.pers_birth_date
        ,   a.address_place
        ,   a.address_email
        ,   a.address_phone
        , rel.pers_sexe
        , rel.pers_alive
        , 'sibling' as relation
         FROM (( humo_persons self LEFT OUTER JOIN humo_persons rel
           ON self.pers_famc = rel.pers_famc 
           and self.pers_famc != ''
           AND self.pers_gedcomnumber != rel.pers_gedcomnumber
           )
         LEFT JOIN humo_addresses a
           ON rel.pers_gedcomnumber = a.address_connect_id)
           WHERE self.pers_gedcomnumber = ?
           AND rel.pers_gedcomnumber != self.pers_gedcomnumber";
        
        $result = $dbh->prepare($sql); 
        $result->bindParam(1,$main_person,PDO::PARAM_STR);
        $result->execute();
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		    $start_url=CMS_ROOTPATH.'/admin/index.php?page=editor&amp;tree=humo_'.'&amp;person='.$row['pers_gedcomnumber'];
			echo '<tr>
					<td><a href="'.$start_url.'">'.$row['pers_gedcomnumber'].'</a></td>
					<td>'.$row['pers_firstname'].' '.$row['pers_lastname'].'</td>
					<td>'.$row['pers_birth_date'].'</td>
					<td>'.$row['address_phone'].'</td>
					<td>'.$row['address_email'].'</td>
					<td>'.$row['address_place'].'</td>
					<td>'.$row['pers_alive'].'</td>
					<td>'.$row['pers_sexe'].'</td>
					<td>'.$row['relation'].'</td>
				</tr>';
		}
		?>
		
		<!-- children   --> 
		<?php
		$sql ="SELECT 
          rel.pers_gedcomnumber
        , rel.pers_firstname 
        , rel.pers_lastname
        , rel.pers_birth_date
         ,   a.address_place
        ,   a.address_email
        ,   a.address_phone
        , rel.pers_sexe
        , rel.pers_alive
        , 'child' as relation
         FROM (`humo_persons` self left join `humo_persons` rel
                ON self.pers_indexnr = rel.pers_famc
                AND self.pers_gedcomnumber != rel.pers_gedcomnumber
                AND self.pers_famc != rel.pers_famc)
         LEFT JOIN humo_addresses a
           ON rel.pers_gedcomnumber = a.address_connect_id
           WHERE self.pers_gedcomnumber = ?";
        
        $result = $dbh->prepare($sql); 
        $result->bindParam(1,$main_person,PDO::PARAM_STR);
        $result->execute();
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		    $start_url=CMS_ROOTPATH.'/admin/index.php?page=editor&amp;tree=humo_'.'&amp;person='.$row['pers_gedcomnumber'];
			echo '<tr>
					<td><a href="'.$start_url.'">'.$row['pers_gedcomnumber'].'</a></td>
					<td>'.$row['pers_firstname'].' '.$row['pers_lastname'].'</td>
					<td>'.$row['pers_birth_date'].'</td>
					<td>'.$row['address_phone'].'</td>
					<td>'.$row['address_email'].'</td>
					<td>'.$row['address_place'].'</td>
					<td>'.$row['pers_alive'].'</td>
					<td>'.$row['pers_sexe'].'</td>
					<td>'.$row['relation'].'</td>
				</tr>';
		}
		?>
		
		<!-- paternal grandparents   --> 
		<?php
		$sql ="SELECT 
          rel.pers_gedcomnumber
        , rel.pers_firstname 
        , rel.pers_lastname
        , rel.pers_birth_date
        ,   a.address_place
        ,   a.address_email
        ,   a.address_phone
        , rel.pers_sexe
        , rel.pers_alive
        , 'grandparent' as relation
         FROM `humo_persons` rel RIGHT OUTER JOIN 
            (`humo_persons` self inner join  `humo_persons` inter
            on self.pers_famc = inter.pers_indexnr
            and inter.pers_famc !=  inter.pers_indexnr
            and inter.pers_sexe = 'M'
            and self.pers_gedcomnumber = ?)
            on inter.pers_famc = rel.pers_indexnr
            and rel.pers_famc !=  rel.pers_indexnr
         LEFT JOIN humo_addresses a
           ON rel.pers_gedcomnumber = a.address_connect_id";
        
        $result = $dbh->prepare($sql); 
        $result->bindParam(1,$main_person,PDO::PARAM_STR);
        $result->execute();
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		    $start_url=CMS_ROOTPATH.'/admin/index.php?page=editor&amp;tree=humo_'.'&amp;person='.$row['pers_gedcomnumber'];
			echo '<tr>
					<td><a href="'.$start_url.'">'.$row['pers_gedcomnumber'].'</a></td>
					<td>'.$row['pers_firstname'].' '.$row['pers_lastname'].'</td>
					<td>'.$row['pers_birth_date'].'</td>
					<td>'.$row['address_phone'].'</td>
					<td>'.$row['address_email'].'</td>
					<td>'.$row['address_place'].'</td>
					<td>'.$row['pers_alive'].'</td>
					<td>'.$row['pers_sexe'].'</td>
					<td>'.$row['relation'].'</td>
				</tr>';
		}
		?>
		
		<!-- maternal grandparents   --> 
		<?php
		$sql ="SELECT 
          rel.pers_gedcomnumber
        , rel.pers_firstname 
        , rel.pers_lastname
        , rel.pers_birth_date 
        ,   a.address_place
        ,   a.address_email
        ,   a.address_phone
        , rel.pers_sexe
        , rel.pers_alive
        , 'grandparent' as relation
         FROM `humo_persons` rel RIGHT OUTER JOIN 
            (`humo_persons` self inner join  `humo_persons` inter
            on self.pers_famc = inter.pers_indexnr
            and inter.pers_famc !=  inter.pers_indexnr
            and inter.pers_sexe = 'F'
            and self.pers_gedcomnumber = ?)
            on inter.pers_famc = rel.pers_indexnr
            and rel.pers_famc !=  rel.pers_indexnr
         LEFT JOIN humo_addresses a
           ON rel.pers_gedcomnumber = a.address_connect_id";
        
        $result = $dbh->prepare($sql); 
        $result->bindParam(1,$main_person,PDO::PARAM_STR);
        $result->execute();
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		    $start_url=CMS_ROOTPATH.'/admin/index.php?page=editor&amp;tree=humo_'.'&amp;person='.$row['pers_gedcomnumber'];
			echo '<tr>
					<td><a href="'.$start_url.'">'.$row['pers_gedcomnumber'].'</a></td>
					<td>'.$row['pers_firstname'].' '.$row['pers_lastname'].'</td>
					<td>'.$row['pers_birth_date'].'</td>
					<td>'.$row['address_phone'].'</td>
					<td>'.$row['address_email'].'</td>
					<td>'.$row['address_place'].'</td>
					<td>'.$row['pers_alive'].'</td>
					<td>'.$row['pers_sexe'].'</td>
					<td>'.$row['relation'].'</td>
				</tr>';
		}
		?>
		</tbody>
	</table> 
	<br/>