<?php

function listusers(){
	
	global $db; 
	
	$mode=SGET('mode');
	$key=SGET('key');
	
	$page=isset($_GET['page'])?$_GET['page']+0:0;
	
	
	$user=userinfo();
	$myuserid=$user['userid']+0;
	$gsid=$user['gsid']+0;
			
	if (!isset($user['groups']['accounts'])) die('<div class="section">You cannot manage user accounts</div>');
	
	if ($mode!='embed'){

?>
<div class="section">
<div class="listbar">

	<form class="listsearch" onsubmit="_inline_lookupuser(gid('userkey'));return false;" style="position:relative;">
		<div class="listsearch_">
			<input onfocus="document.hotspot=this;" id="userkey" class="img-mg" onkeyup="_inline_lookupuser(this);" autocomplete="off">
			<img src="imgs/inpback.gif" class="inpback" onclick="inpbackspace('userkey');_inline_lookupuser(gid('userkey'));">
		</div>
		<input type="image" src="imgs/mg.gif" class="searchsubmit" value=".">
		<?makehelp('userlistlookup','listviewlookup',1);?>
	</form>

	<div style="padding-top:10px;">
	<a class="recadder" onclick="addtab('user_new','<img src=&quot;imgs/t.gif&quot; class=&quot;ico-user&quot;><?tr('list_user_add_tab');?>','newuser');"> <img src="imgs/t.gif" class="img-addrec"><?tr('list_user_add');?></a>
	</div>
</div>

<div id="userlist">
<?		
	}
	
	$params=array($gsid);

	$query="select * from ".TABLENAME_USERS." where gsid=? ";
	if ($key!='') {
		$query.=" and (login like ? or dispname like ?) ";
		array_push($params,"$key%","%$key%");
	}
	$rs=sql_prep($query,$db,$params);
	$count=sql_affected_rows($db,$rs);

	$pagelead=0;
	$perpage=20;
	$dperpage=$perpage;
	
	$maxpage=ceil($count/$perpage)-1;
	if ($maxpage<0) $maxpage=0;
	if ($page<0) $page=0;
	if ($page>$maxpage) $page=$maxpage;
	$start=$perpage*$page;
	if ($page>0) {$start-=$pagelead;$dperpage+=$pagelead;if ($start<0) $start=0;}

	$pager='';
	if ($maxpage>0){
		ob_start();
?>
<div class="listpager">
<a href=# class="hovlink" onclick="ajxpgn('userlist',document.appsettings.codepage+'?cmd=slv_core__users&page=<?echo $page-1;?>&mode=embed&key='+encodeHTML(gid('userkey').value));return false;"><img src="imgs/t.gif" class="img-pageleft">Prev</a>
&nbsp;
<a class="pageskipper" onclick="var pagenum=sprompt('Go to page:',<?echo $page+1;?>);if (pagenum==null||parseInt(pagenum,0)!=pagenum) return false;ajxpgn('userlist',document.appsettings.codepage+'?cmd=slv_core__users&key='+encodeHTML(gid('userkey').value)+'&page='+(pagenum-1)+'&mode=embed');return false;"><?echo $page+1;?></a> of <?echo $maxpage+1;?>
&nbsp;
<a href=# class="hovlink" onclick="ajxpgn('userlist',document.appsettings.codepage+'?cmd=slv_core__users&page=<?echo $page+1;?>&mode=embed&key='+encodeHTML(gid('userkey').value));return false;">Next<img src="imgs/t.gif" class="img-pageright"></a>
</div>
<?		
		$pager=ob_get_clean();
	}
	
	echo $pager;
	
	$query.=" order by userid=? desc, virtualuser, login limit $start,$dperpage ";	
	array_push($params,$myuserid);

	$rs=sql_prep($query,$db,$params);

	
	$pageleadidx=0;
	
	while ($myrow=sql_fetch_array($rs)){
		$userid=$myrow['userid'];
		$login=$myrow['login'];
		$dispname=htmlspecialchars($myrow['dispname']);
		$virtual=$myrow['virtualuser'];
		
		$usertitle="$login"; //change this if needed
		
		$dbusertitle=noapos(htmlspecialchars(htmlspecialchars($usertitle)));
		$groupnames=$myrow['groupnames'];
		$hash=substr(md5($groupnames),0,6);
		if ($virtual) $hash='ffffff';
		
		if ($pagelead!=0&&$pageleadidx==$pagelead&&$page>0){
?>

<div style="font-size:8px;color:#333333;border-bottom:dotted 1px #444444;text-align:center;">PAGE <?echo $page+1;?></div>
<?			
		}
		
?>
<div class="listitem" style="<?if ($pageleadidx<$pagelead&&$page>0) echo 'opacity:0.6;';?>border-left:solid 3px #<?echo $hash;?>;padding-left:5px;"><a onclick="showuser(<?echo $userid;?>,'<?echo $dbusertitle;?>');"><?echo htmlspecialchars($usertitle);?><br><?echo htmlspecialchars($usertitle!=$dispname?$dispname:'');?></a></div>
<?		
		$pageleadidx++;

	}//while
	
	echo $pager;
	
	if ($mode!='embed'){
?>
</div>
</div>

<script>
gid('tooltitle').innerHTML='<a><?tr('list_users');?></a>';
ajxjs(<?jsflag('showuser');?>,'users_js.php');
ajxjs(<?jsflag('setaccountpass');?>,'accounts.js');
</script>
<?	
	}//embed mode

}

