<?
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/tables/system_log.php - version 1.0
*/

$nAdminOnlyPg = 0;

// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

// bring in other required files
require("../../inc/config.php");
require("../inc/lang.php");
require("../inc/plugins.php");
require("../inc/pluginutil.php");
require("../inc/pageutil.php");
require("../inc/calendar.php");

// Get plugin inclusion points for list, and insert them in header
$nPluginListInclARRAY = nGetListPluginInclusions("SystemLog");

// Setup header
$nColumnARRAY = array();
//$nColumnARRAY["#Checkbox#"] = array("","","","vertical-align:middle;","{ \"sWidth\":\"31\", \"bSortable\":false }");
$nColumnARRAY[_("Date")] = array("","","","","{ \"sType\": 'datetime' }");
$nColumnARRAY[_("User")] = array("","","","","");
$nColumnARRAY[_("Description")] = array("","","","","");

// Insert plugin inclusion points for list in header
$nColumnARRAY = nInsertPluginHdrIncl($nPluginListInclARRAY,$nColumnARRAY);
$nHeaderDataTableJS = nBuildHeaderDataTableJS($nColumnARRAY);
?>
<script>
	$(function() {
	    // Initialize tables (if more than one per page, give it a second class for customization)
	    $('.dataTable').dataTable({
	        "sPaginationType": "full_numbers",
	        "bJQueryUI": true,
			"bStateSave": true,
			"aaSorting": [[ 0, "desc" ]],
			"aoColumns": [
				<?=$nHeaderDataTableJS;?>
			],
			"iDisplayLength": 20,
			"aLengthMenu": [[20, 50, 100, 200, -1], [20, 50, 100, 200, "All"]],
			"fnDrawCallback":function(){
				// Hide toolbar if showing less records than page-able
				if(this.fnSettings().fnRecordsTotal() <= this.fnSettings()._iDisplayLength){
					$('.dataTables_wrapper div.fg-toolbar').hide();
					$('.dataTables_wrapper').addClass("topBorder bottomBorder");
				} else {
					$('.dataTables_wrapper div.fg-toolbar').show();
				}
			}
	    });
	});
</script>

<form action="javascript:;" method="post" name="nListForm" id="nListForm" style="margin:0px;">
	<?=nGetSystemLog()?>
</form>

<?
function nGetSystemLog(){
	global $DBName,$Link,$nParam,$nPluginListInclARRAY,$nColumnARRAY;
	
	$Query = "SELECT * FROM nSystemEventLog ORDER BY nEventTime DESC LIMIT 500";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link); $nNumRec = mysql_num_rows($Result);
	
	$nTable = "";
	$nCurrentRecord = 1;
	
	if($nNumRec){
		// insert header and main listing table
		$nHeaderCode = nBuildHeaderHTML($nColumnARRAY);
		$nTable.="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"dataTable\">";
		$nTable.="<thead><tr class=\"listHdr\">$nHeaderCode</tr></thead>";
		
		while($Row = mysql_fetch_array($Result)){
			// Get data
			$nID = $Row["nID"];
			$nEditorUser = $Row["nEditorUser"];
			$nEventDesc = $Row["nEventDesc"];
			$nEventTime = $Row["nEventTime"];
			
			// Format data
			$nEventTime = $Row["nEventTime"];
			if($nEventTime && $nEventTime!="0000-00-00 00:00:00"){
				$nEventTimeARRAY = explode(" ",$nEventTime);
				$nDateDiff = nGetDateDiffDays($nEventTimeARRAY[0]);
				switch($nDateDiff){
					case 0: $nEventTimeText = _("Today").", ".date("g:i A",strtotime($nEventTime)); break;
					case 1: $nEventTimeText = _("Yesterday").", ".date("g:i A",strtotime($nEventTime)); break;
					default: $nEventTimeText = nDateToDisplay($nEventTime)." - ".date("g:i A",strtotime($nEventTime));
				}
			}
			if(!$nEditorUser){ $nEditorUser = "<em class=\"caption\">unknown</em>"; }
			// Replace record IDs with links, if available
			$nEventDesc = preg_replace_callback("/\[nUserID:(\d+)\]/","nGetUserLink",$nEventDesc);
			$nEventDesc = preg_replace_callback("/\[nPageID:(\d+)\]/","nGetPageLink",$nEventDesc);
			$nEventDesc = preg_replace_callback("/\[nSectionID:(\d+)\]/","nGetSectionLink",$nEventDesc);
			$nEventDesc = preg_replace_callback("/\[nCompID:(\d+)\]/","nGetComponentLink",$nEventDesc);
			
			// Setup row (data, width, align, class, style)
			$nColumnARRAY = array();
			//$nColumnARRAY["#Checkbox#"] = array("<input type=\"checkbox\" name=\"item_$nCurrentRecord\" value=\"$nID\" onClick=\"updateItemText();\">","","","","");
			$nColumnARRAY[_("Date")] = array("$nEventTimeText","","","","");
			$nColumnARRAY[_("User")] = array("$nEditorUser","","","","");
			$nColumnARRAY[_("Description")] = array("$nEventDesc","","","","");
			
			// Get plugin inclusion points for list, and insert them in row
			$nColumnARRAY = nInsertPluginRowIncl($nPluginListInclARRAY,$nColumnARRAY,$nID);
			$nRowCode = nBuildRowHTML($nColumnARRAY);
			
			// Insert row
			$nTable.="<tr id=\"row$nID\">$nRowCode</tr>";
			$nCurrentRecord++;
		}
		// Insert footer
		//$nTable.= "</table>".nGetActionFooter()."<BR><BR>";
	} else {
		$nTable.= "<div style=\"text-align:center; padding:50px 0;\"><i>"._("There are currently no Events to display.")."</i></div>";
	}
	return $nTable;
}
function nGetActionFooter(){
	$nFooter = "<div style=\"padding:8px;\">\n";
	$nFooter.= "	<strong>With <strong id=\"itemAmount\">0</strong> items selected:</strong> \n";
	$nFooter.= "	<input type=\"button\" name=\"nRemove\" id=\"nRemove\" value=\" "._("Remove")." \" onClick=\"editList('usr','remove');\" disabled> \n";
	$nFooter.= "	<input type=\"button\" name=\"nEdit\" id=\"nEdit\" value=\" "._("Edit")." \" onClick=\"editList('usr','edit');\" disabled>\n";
	$nFooter.= "</div>\n";
	return $nFooter;
}
function nGetUserLink($nPregMatchesARRAY){
	global $DBName, $Link;
	if(!$nPregMatchesARRAY){ return; } else { $nRecID = $nPregMatchesARRAY[1]; }
	if(!$nRecID){ return; }
	$nName = "";
	$Query = "SELECT nUser FROM nEditor WHERE nID=$nRecID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nName = $Row["nUser"];
	}
	if($nName){ $nName = "<a href=\"user_modify.php?usr=$nRecID\"><strong>$nName</strong></a>"; }
	return $nName;
}
function nGetPageLink($nPregMatchesARRAY){
	global $DBName, $Link;
	if(!$nPregMatchesARRAY){ return; } else { $nRecID = $nPregMatchesARRAY[1]; }
	if(!$nRecID){ return; }
	$nName = "";
	$Query = "SELECT nNavTitle FROM nPage WHERE nID=$nRecID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nName = $Row["nNavTitle"];
	}
	if($nName){ $nName = "<a href=\"page_edit.php?id=$nRecID\"><strong>$nName</strong></a>"; }
	return $nName;
}
function nGetSectionLink($nPregMatchesARRAY){
	global $DBName, $Link;
	if(!$nPregMatchesARRAY){ return; } else { $nRecID = $nPregMatchesARRAY[1]; }
	if(!$nRecID){ return; }
	$nName = "";
	$Query = "SELECT nName FROM nSection WHERE nID=$nRecID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nName = $Row["nName"];
	}
	if($nName){ $nName = "<a href=\"pages.php\"><strong>$nName</strong></a>"; }
	return $nName;
}
function nGetComponentLink($nPregMatchesARRAY){
	global $DBName, $Link;
	if(!$nPregMatchesARRAY){ return; } else { $nRecID = $nPregMatchesARRAY[1]; }
	if(!$nRecID){ return; }
	$nName = "";
	$Query = "SELECT nName FROM nComponent WHERE nID=$nRecID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nName = $Row["nName"];
	}
	if($nName){ $nName = "<strong>$nName</strong>"; }
	return $nName;
}
function nGetEditorNameFromID($nEditorID){
	global $DBName, $Link;
	if(!$nEditorID){ return; }
	$nUser = "";
	$Query = "SELECT nUser FROM nEditor WHERE nID=$nEditorID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nUser = $Row["nUser"];
	}
	return $nUser;
}
?>
<?
// disconnect from mysql
mysql_close($Link);
?>