<?php

function rptactionlog(){
	global $db;
	global $codepage;
	
	$user=userinfo();
	$gsid=$user['gsid']+0;

	$now=time();
	
	$day=date('j',$now);
	$mon=date('n',$now);
	$year=date('Y',$now);

	global $paytypes;
	global $paymethods;
	global $lang;
	
	//override date stamp
	$ds=explode('-',GETSTR('date'));
	if (count($ds)==3){
		$day=$ds[2];
		$mon=$ds[1];
		$year=$ds[0];
		$now=mktime(10,10,10,$mon,$day,$year);	
	}
?>
<div class="section">
<?
	//// Report Header
	
	$query="select * from ".TABLENAME_REPORTS." where reportkey='actionlog' and (gsid=$gsid or gsid=0) ";
	$rs=sql_query($query,$db);
	$myrow=sql_fetch_assoc($rs);
?>
<div class="sectiontitle" style="margin-bottom:0;"><?echo htmlspecialchars($myrow['reportname_'.$lang]);?></div>
<div class="infobox"><?echo htmlspecialchars($myrow['reportdesc_'.$lang]);?></div>
<?	
	////
	
	$key=GETSTR('key');
	$opairs=GETSTR('pairs');
	$pairs=explode(';',$opairs);

	$start=mktime(0,0,0,$mon,$day,$year);
	$end=mktime(23,59,59,$mon,$day,$year);
	
	// $prevday=date('Y-n-j',$start-3600);
	// $nextday=date('Y-n-j',$end+3600);

	$query="select * from ".TABLENAME_ACTIONLOG." left join ".TABLENAME_USERS." on ".TABLENAME_ACTIONLOG.".userid=".TABLENAME_USERS.".userid where ".TABLENAME_ACTIONLOG.".gsid=$gsid ";

	if ($key!=''||$opairs!='') {
		$query.=" and alogid!=0 and logmessage!='' ";
		if ($key!='') $query.=" and (logmessage like '%$key%' or login like '$key%' or rawobj like '%$key%' or logname like '%$key%') ";
		foreach ($pairs as $pair){
			$parts=explode('=',$pair);
			$k=trim($parts[0]);
			$v=trim($parts[1]);
			if ($k==''||$v=='') continue;
			$query.=" and rawobj like '%\"$k\":\"$v\"%' ";
		
		}

	}  else {
		$query.=" and logmessage!='' and logdate>='$start' and logdate<='$end' ";
		
		$q="select logdate from ".TABLENAME_ACTIONLOG." where gsid=$gsid and logmessage!='' and logdate<'$start' order by logdate desc limit 1";
		$rs=sql_query($q,$db);
		if ($myrow=sql_fetch_array($rs)) $prevday=date('Y-n-j',$myrow['logdate']);

		$q="select logdate from ".TABLENAME_ACTIONLOG." where gsid=$gsid and logmessage!='' and logdate>'$end' order by logdate limit 1";
		$rs=sql_query($q,$db);
		if ($myrow=sql_fetch_array($rs)) $nextday=date('Y-n-j',$myrow['logdate']);
				
	}

	$query.=" order by logdate desc,alogid desc";
	
	$rs=sql_query($query,$db);

?>

<form onsubmit="reloadtab('rptactionlog',null,'rptactionlog&key='+encodeHTML(gid('actionlog_key').value)+'&pairs='+encodeHTML(gid('actionlog_pairs').value),null,null,{persist:true});return false;">
Search: <input class="inp" style="width:30%;" id="actionlog_key" placeholder="Keyword in Action" value="<?echo stripslashes($key);?>"> 
	<input class="inp" style="width:30%;" id="actionlog_pairs" placeholder="Advanced Pattern" value="<?echo stripslashes($opairs);?>"> <input class="button" type="submit" value="Go">
</form>

<div style="padding:20px 30px;<?if ($key!=''||$opairs!='') echo 'display:none;';?>">
<?if (isset($prevday)){?>
<a onclick="reloadtab('rptactionlog',null,'rptactionlog&date=<?echo $prevday;?>',null,null,{persist:true});">&laquo;</a> 
<?}?>
&nbsp; &nbsp; <a class="hovlink" onclick="pickdate(gid('rptactionlogdate'));"><?echo date('M j, Y',$now);?></a><input type="hidden" id="rptactionlogdate" value="<?echo date('Y-n-j',$now);?>" onchange="reloadtab('rptactionlog',null,'rptactionlog&date='+this.value,null,null,{persist:true});">
&nbsp; &nbsp;
<?if (isset($nextday)){?>
<a onclick="reloadtab('rptactionlog',null,'rptactionlog&date=<?echo $nextday;?>',null,null,{persist:true});">&raquo;</a>
<?}?>
</div>
<?
makelookup('rptactionlogdate',1);
?>

<div class="stable">
<table cellpadding="4">
<tr>
	<td style="padding-right:20px;"><b>Time</b></td><td><b>User</b></td><td><b>Action</b></td><td><b>Extra</b></td>
</tr>
<?
	while ($myrow=sql_fetch_array($rs)){
		$username=htmlspecialchars($myrow['login']);
		if ($username=='') $username='<span style="color:#ee6666;">'.htmlspecialchars($myrow['logname']).'</span>';
		$logdate=$myrow['logdate'];
		$dlogdate=date('H:i:s',$logdate);
		if ($key!=''||$opairs!='') $dlogdate=date('Y-n-j H:i:s',$logdate);
		$logmessage=$myrow['logmessage'];
		$extra='';
		$obj=json_decode($myrow['rawobj'],1);
		foreach ($obj as $k=>$v) $extra.="; $k=".htmlspecialchars($v);
		$extra=trim($extra,'; ');
?>
<tr>
<td valign="top"><?echo $dlogdate;?></td>
<td valign="top"><?echo $username;?></td><td><?echo htmlspecialchars($logmessage);?></td>
<td valign="top"><?echo $extra;?></td>
</tr>
<?
	
	}//while
?>
</table>
</div>

</div>
<?
	
}