<?php

function listreports(){
	$user=userinfo();
	$gsid=$user['gsid']+0;
	$groupnames=$user['groups'];
	$key=SGET('key');
	$mode=SGET('mode');
	
	global $db;
	global $lang;
	global $deflang;
	
	$query="select * from ".TABLENAME_REPORTS." where (gsid=? or gsid=0) ";
	$params=array($gsid);
	
	if ($key!='') {
		$query.=" and (reportgroup_$lang like ? or reportname_$lang like ?) ";
		array_push($params,'%'.$key.'%','%'.$key.'%');
	}
	
	$query.=" order by reportgroup_$lang,reportname_$lang";
	
	$rs=sql_prep($query,$db,$params);
	$found=0;
	
	header('listviewtitle:'.tabtitle(_tr('icon_reports')));
	header('listviewflag:'._jsflag('showreport'));
	header('listviewjs:reports.js');
		
	if ($mode!='embed'){
?>
<div class="section">

<div class="listbar">
	<form class="listsearch" onsubmit="_inline_lookupreport(gid('reportkey'));return false;" style="position:relative;">
	<div class="listsearch_">
		<input onfocus="document.hotspot=this;" id="reportkey" class="img-mg" onkeyup="_inline_lookupreport(this);" autocomplete="off">
		<img src="imgs/inpback.gif" class="inpback" onclick="inpbackspace('reportkey');_inline_lookupreport(gid('reportkey'));">
	</div>
	<input type="image" src="imgs/mg.gif" class="searchsubmit" value=".">
	<?makehelp('reportlistlookup','listviewlookup',1);?>
	</form>
</div>
<div id="reportlist">
<?
}
	$lastgroup='';
	
	while ($myrow=sql_fetch_assoc($rs)){
		$reportkey=$myrow['reportkey'];
		$reportname=$myrow['reportname_'.$lang];
		if ($reportname=='') $reportname=$myrow['reportname_'.$deflang];
		$reportgroup=$myrow['reportgroup_'.$lang];
		$reportfunc=$myrow['reportfunc'];
		$dbreportname=noapos(htmlspecialchars(htmlspecialchars($reportname)));
		$reportgroupnames=explode('|',$myrow['reportgroupnames']);
		$test=array_intersect($reportgroupnames,array_keys($groupnames));
		if (count($test)<1) continue; //&&count($reportgroupnames)==0
		$found=1;
		
		if ($lastgroup!=$reportgroup){
?>
<div class="sectionheader"><?echo $reportgroup;?></div>
<?			
			$lastgroup=$reportgroup;
		}
?>
	<div class="listitem">
		<a onclick="<?echo $reportfunc;?>reloadtab('rpt<?echo $reportkey;?>','<img src=&quot;imgs/t.gif&quot; class=&quot;ico-report&quot;><?echo $dbreportname;?>','rpt<?echo $reportkey;?>',(self.rptreload_<?echo $reportkey;?>?rptreload_<?echo $reportkey;?>:null));addtab('rpt<?echo $reportkey;?>','<img src=&quot;imgs/t.gif&quot; class=&quot;ico-report&quot;><?echo $dbreportname;?>','rpt<?echo $reportkey;?>',(self.rptinit_<?echo $reportkey;?>?rptinit_<?echo $reportkey;?>:null));"><?echo htmlspecialchars($reportname);?></a>
	</div>
<?		
	}//while
	
	if (!$found&&$key==''){
?>
	<em style="color:#666666;">You cannot see any reports.</em>
<?		
	}
	
	if ($mode!='embed'){
?>
</div>
</div>
<?/*
<script>
gid('tooltitle').innerHTML='<a><?tr('icon_reports');?></a>';
ajxjs(self.showreport,'reports.js');
</script>
<?
*/
	}

}
