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

$nSecureMe = 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/calendar.php");

// Get passed-in variables
if(isset($_GET["t"])){ $nType = $_GET["t"]; } else { $nType = ""; }
$nSearch = (isset($_GET["s"]) ? $_GET["s"] : NULL);
$nMode = (isset($_GET["m"]) ? $_GET["m"] : NULL); // r = regular, m = mini

// Search params
$nParam = "";
if($nParam){ $nWhereSQL="AND"; } else { $nWhereSQL="WHERE"; }
switch($nType){
	case "PDF": $nParam = "$nWhereSQL nType='pdf'"; break;
	case "Word": $nParam = "$nWhereSQL nType='doc' OR nType='docx'"; break;
	case "Excel": $nParam = "$nWhereSQL nType='xls' OR nType='xlsx'"; break;
	case "Other": $nParam = "$nWhereSQL nType!='pdf' AND nType!='doc' AND nType!='docx' AND nType!='xls' AND nType!='xlsx'"; break;
}

// Defaults
if(!$nMode){ $nMode = "r"; }

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

// Setup header
$nColumnARRAY = array();
//if($nMode=="r"){ $nColumnARRAY["#Checkbox#"] = array("","","","vertical-align:middle;","{ \"sWidth\":\"31\", \"bSortable\":false }"); }
if($nMode=="r"){ $nColumnARRAY["File ID"] = array("","","","text-align:center;","{ \"bVisible\":false }"); }
$nColumnARRAY["Type"] = array("","","","text-align:center;","");
$nColumnARRAY["File Name"] = array("","","","","");
$nColumnARRAY["Filesize"] = array("","","","","");
$nColumnARRAY["Actions"] = array("","","","","");

// Insert plugin inclusion points for list in header
$nColumnARRAY = nInsertPluginHdrIncl($nPluginListInclARRAY,$nColumnARRAY);
$nHeaderDataTableJS = nBuildHeaderDataTableJS($nColumnARRAY);

if($nMode=="r"){
?>
<script>
	$(function() {
	    // Initialize tables (if more than one per page, give it a second class for customization)
	    $('#custData').dataTable({
	    	"bStateSave": true,
			"sPaginationType": "full_numbers",
	        "bJQueryUI": true,
			"aaSorting": [[ 2, "asc" ]],
			"aoColumns": [
				<?=$nHeaderDataTableJS;?>
			],
			"iDisplayLength": 20,
			<? if($nSearch){ ?>
			"bStateSave": false,
			"oSearch": {"sSearch":"<?=$nSearch;?>"},
			<? } ?>
			"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;">
	<?=nGetDocList()?>
</form>

<?
function nGetDocList() {
	global $DBName,$Link,$nParam,$nPluginListInclARRAY,$nColumnARRAY,$nMode;
	
	$Query = "SELECT * FROM nFile $nParam ORDER BY nPath ASC";
	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
		if($nMode=="r"){ $nTableClass = "dataTable"; } else { $nTableClass = "regular"; }
		$nHeaderCode = nBuildHeaderHTML($nColumnARRAY);
		$nTable.="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"custData\" class=\"$nTableClass\">";
		$nTable.="<thead><tr class=\"listHdr\">$nHeaderCode</tr></thead>";
		
		while($Row = mysql_fetch_array($Result)){
			// Get data
			$nID = $Row["nID"];
			$nPath = $Row["nPath"];
			$nType = $Row["nType"];
			
			// Format data
			switch($nType){
				case "pdf":
					$nIcon = "img/pdf.png"; break;
				case "doc":
				case "docx":
					$nIcon = "img/doc.png"; break;
				case "xls":
				case "xlsx":
					$nIcon = "img/xls.png"; break;
				case "txt":
					$nIcon = "img/txt.png"; break;
				case "pub":
					$nIcon = "img/pub.png"; break;
				case "jpg":
				case "jpeg":
				case "gif":
				case "bmp":
				case "png":
					$nIcon = "img/img.png"; break;
				case "html":
				case "htm":
				case "php":
				case "asp":
				case "xml":
					$nIcon = "img/web.png"; break;
				default:
					$nIcon = "img/other.png"; break;
			}
			$nFilename = nGetImageNameFromPath($nPath);
			if($nIcon){ $nIconImg = "<img src=\"$nIcon\" alt=\"$nType\" width=\"32\" width=\"32\" border=\"0\">"; }
			if(file_exists("../../$nPath")){
				$nFilesize = filesize("../../$nPath");
				if($nFilesize){
					$nFilesize = $nFilesize/1024;
					$nFilesizeByteText = "Kb";
					$nFilesizeText = number_format($nFilesize,0,'.','');
					if($nFilesize>=1000){
						$nFilesize = $nFilesize/1000;
						$nFilesizeByteText = "MB";
						$nFilesizeText = number_format($nFilesize,1,'.','');
					}
				}
			}
			
			// Setup row (data, width, align, class, style)
			$nColumnARRAY = array();
			//if($nMode=="r"){ $nColumnARRAY["#Checkbox#"] = array("<input type=\"checkbox\" name=\"item_$nCurrentRecord\" value=\"$nID\" onClick=\"updateItemText();\">","","","",""); }
			if($nMode=="r"){ $nColumnARRAY["File ID"] = array("$nID","","","",""); }
			$nColumnARRAY["Type"] = array("$nIconImg","","right","","text-align:center;");
			$nColumnARRAY["File Name"] = array("<a href=\"../$nPath\" target=\"_blank\">$nFilename</a>","","","","");
			$nColumnARRAY["Filesize"] = array("$nFilesizeText $nFilesizeByteText","","","","");
			$nColumnARRAY["Actions"] = array("<a href=\"dialog/file_delete.php?id=$nID\" class=\"modal\">Delete</a>","","","","");
			
			// 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>"; //if($nMode=="r"){ $nTable.= "</table>".nGetActionFooter()."<BR><BR>"; }
	} else {
		$nTable.= "<div style=\"text-align:center; padding:50px 0;\"><i>There are currently no Documents 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('ord','remove');\" disabled> \n";
	$nFooter.= "	<input type=\"button\" name=\"nEdit\" id=\"nEdit\" value=\" Edit \" onClick=\"editList('ord','edit');\" disabled>\n";
	$nFooter.= "</div>\n";
	return $nFooter;
}
function nGetImageNameFromPath($nPath){
	if(!$nPath){ return; }
	$nPathARRAY = explode("/",$nPath);
	$nImgName = $nPathARRAY[(count($nPathARRAY)-1)];
	return $nImgName;
}
function nGetSectionName($nRecID){
	global $DBName,$Link;
	$nSQL = "SELECT nName,nParentID FROM nSection WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nName = $Row["nName"];
		$nParentID = $Row["nParentID"];
	}
	if($nParentID){ $nParentName = nGetSectionName($nParentID)." &gt; "; } else { $nParentName = ""; }
	return "$nParentName$nName";
}
function nSectionExists($nSectionID){
	global $DBName,$Link;
	if(!$nSectionID){ return 0; }
	$nSQL = "SELECT nID FROM nSection WHERE nID=$nSectionID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nCount = mysql_num_rows($Result);
	return $nCount;
}
function nGetPageIDAccessARRAY($ValidUser){
	global $DBName,$Link;
	if(!$ValidUser){ return; }
	// Get user ID
	$nSQL = "SELECT nID FROM nEditor WHERE nUser='$ValidUser' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nEditorID = $Row["nID"];
	}
	$nDataARRAY = array();
	$nSQL = "SELECT nPageID FROM xEditorPage WHERE nEditorID=$nEditorID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nPageID"];
		$nDataARRAY[] = $nPageID;
	}
	return $nDataARRAY;
}
?>
<?
// disconnect from mysql
mysql_close($Link);
?>