<?
/*
 * 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/users.php - version 1.0
*/

$nAdminOnlyPg = 1;

// 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("Users");

// Setup header
$nColumnARRAY = array();
$nColumnARRAY["#Checkbox#"] = array("","","","vertical-align:middle;","{ \"sWidth\":\"31\", \"bSortable\":false }");
$nColumnARRAY[_("Username")] = array("","","","","");
$nColumnARRAY[_("Access Level")] = array("","","","","");
$nColumnARRAY[_("Last Login")] = 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": [[ 1, "asc" ]],
			"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;">
	<?=nGetUsers()?>
</form>

<?
function nGetUsers(){
	global $DBName,$Link,$nParam,$nPluginListInclARRAY,$nColumnARRAY;
	
	$Query = "SELECT * FROM nEditor";
	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"];
			$nUser = $Row["nUser"];
			$nPass = $Row["nPass"];
			$nAdmin = $Row["nAdmin"];
			
			if($nUser=="salc_service"){ continue; }
			
			// Format data
			if($nAdmin){ $nAdmin = "<strong>"._("Administrator")."</strong>"; } else { $nAdmin = _("Editor"); }
			$nLoginDT = $Row["nLastLoginDate"];
			if($nLoginDT && $nLoginDT!="0000-00-00 00:00:00"){
				$nLoginDTARRAY = explode(" ",$nLoginDT);
				$nDateDiff = nGetDateDiffDays($nLoginDTARRAY[0]);
				switch($nDateDiff){
					case 0: $nLoginDT = _("Today").", ".date("g:i A",strtotime($nLoginDT)); break;
					case 1: $nLoginDT = _("Yesterday").", ".date("g:i A",strtotime($nLoginDT)); break;
					default: $nLoginDT = nDateToDisplay($nLoginDT)." - ".date("g:i A",strtotime($nLoginDT));
				}
			} else {
				$nLoginDT = "<em class=\"caption\">"._("never")."</em>";
			}
			if(!$nUser){ $nUser="<em class=\"caption\">"._("unnamed")."</em>"; }
			
			// Setup row (data, width, align, class, style)
			$nColumnARRAY = array();
			$nColumnARRAY["#Checkbox#"] = array("<input type=\"checkbox\" name=\"item_$nCurrentRecord\" value=\"$nID\" onClick=\"updateItemText();\">","","","","");
			$nColumnARRAY[_("Username")] = array("<a href=\"user_modify.php?usr=$nID\">$nUser</a>","","","","");
			$nColumnARRAY[_("Access Level")] = array("$nAdmin","","","","");
			$nColumnARRAY[_("Last Login")] = array("$nLoginDT","","","","");
			
			// 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 Users 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;
}
?>
<?
// disconnect from mysql
mysql_close($Link);
?>