<?php
    include("connection.php");
    include("frames_header.php");
?>

<SCRIPT>
	function update(obj){
	    if(obj.checked){
		    document.updatew = window.open("users-modif.php?mode=add&user="+obj.id+"&grant="+obj.name+"&id="+obj.value+"","","toolbar=no,scrollbars=yes,resizable=yes,height=400,width=500");
	    }else{
		    document.updatew = window.open("users-modif.php?mode=rem&user="+obj.id+"&grant="+obj.name+"&id="+obj.value+"","","toolbar=no,scrollbars=yes,resizable=yes,height=400,width=500");
		}
		//window.location.reload();
	}
</SCRIPT>


<em>Valid ANISEED users:</em><br><br>
<center>
<table width="99%" border="0" cellspacing="0" cellpadding="0" background="pictures/barre.jpg">
	<tr bgcolor="#990000"> 
		<td align="center"><strong>User name</strong></td>
		<td align="center"><strong>Team</strong></td>
		<td align="center">&nbsp;</td>
        <td background="pictures/barre-red.jpg" align="center"><strong>granted roles</strong></td>
	</tr>

<?php
/* Function qui étant donné un nom d'user renvoie ses roles */

function giveMeRoles($dbuser)
{
global $c;

$a=0;
$temp=(array) null;


/*Etape1: récup de l'oid de l'user en cours*/
$r1="select \"usesysid\" from \"pg_user\" where \"usename\"='".$dbuser."'";
$s1=pg_query($c,$r1) or die("<strong>Error</strong> ".pg_last_error($c));
$row=pg_fetch_array($s1);
$oid=$row["usesysid"];



/*Etape2: recup des groupes ou se trouve l'oid*/
$r2="select \"groname\",\"grolist\" from \"pg_group\"";
$s2=pg_query($c,$r2) or die("<strong>Error</strong> ".pg_last_error($c));
while ($row=pg_fetch_array($s2))
{
$curlist=str_replace("{","",$row["grolist"]);
$curlist=str_replace("}","",$curlist);
$oidlist=explode(",",$curlist);

if (in_array($oid,$oidlist)) $temp[$a++]=$row["groname"];


}


return $temp;
}
?>

<?php

	$requete="select distinct a.\"usename\" as \"GRANTEE\",b.\"AUTHOR_ID\",b.\"TEAM\" from \"pg_user\" a, \"AUTHOR\" b where upper(\"usename\")=upper(\"LOGIN\") order by a.\"usename\"";


		$resultset = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

		$count=0;
		while($row=pg_fetch_array($resultset)){
		    ++$count;
		    $dbuser=$row["GRANTEE"];
		    print '<tr><td valign="top"><hr color="#871F1F" size="1"></td> <td valign="top"><hr color="#871F1F" size="1"></td> <td valign="top"><hr color="#871F1F" size="1"></td> <td valign="top"><hr color="#871F1F" size="1"></td></tr>';
		    print '<tr><td>&nbsp;<a target="_blank" href="user-remove.php?name='.$row["GRANTEE"].'"><img src="pictures/skull2.gif" border="0"></a>&nbsp;&nbsp;&nbsp;&nbsp;<strong>'.$row["GRANTEE"].'</strong></td>';
			print '<td><strong>'.$row["TEAM"].'</strong></td>';
			
	

			$a=0;
			$temp=(array) null;

			/*Appel a la procédure*/
			$temp=giveMeRoles($dbuser);

		
			if(in_array("ANNOTATOR_ANISEED",$temp))
			    print '<td><input type="checkbox" disabled checked><strong>Annotator</strong></td>';
			else{
			    print '<td><input type="checkbox" disabled><strong>Annotator</strong></td>';
			}
			
			
			if(in_array("CURATOR_ANISEED",$temp)){
			    print '<td><input id="'.$row["GRANTEE"].'" name="ANISEED_CURATOR" type="checkbox" value="'.$row["AUTHOR_ID"].'" onClick="javascript: self.update(this);" checked><strong>Curator</strong></td></tr>';
			}else{
			    print '<td><input id="'.$row["GRANTEE"].'" name="ANISEED_CURATOR" type="checkbox" value="'.$row["AUTHOR_ID"].'" onClick="javascript: self.update(this);"><strong>Curator</strong></td></tr>';
			}
			
		}
        if($count<=0){
		    print"<br><center><strong>No match found.<br>Try to login first.</strong></center>";
		}
		
		print '</table>';
		
		if($count>0){
		    print "<br><center><strong>$count users found</strong></center>";
		}
?>

</center>

<?php
    include("frames_footer.php");
?>