<?php
$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");
include("../inc/pageutil.php");
include("../inc/paginator.php");
include("../inc/calendar.php");

$nShowPerPg = 999;

// Get passed-in vars
if(isset($_GET["pg"])){ $nPage = $_GET["pg"]; } else { $nPage = ""; }
if(!$nPage && isset($_POST["pg"])){ $nPage = $_POST["pg"]; }
if(isset($_GET["srt"])){ $nSort = $_GET["srt"]; } else { $nSort = ""; }
if(isset($_GET["ord"])){ $nSortOrder = $_GET["ord"]; } else { $nSortOrder = ""; }
if(isset($_GET["spp"])){ $nShowPerPgOverride = $_GET["spp"]; } else { $nShowPerPgOverride = ""; }
if($nShowPerPgOverride){ $nShowPerPg = $nShowPerPgOverride; }

if(isset($_GET["t"])){ $nType = $_GET["t"]; } else { $nType = ""; }

// Setup SQL query for displayed listing
if(!$nSort){ $nSort="nPath"; }  // default sort
if(!$nSortOrder){ $nSortOrder="ASC"; }  // default sort order
switch($nType){
	case "PDF": $nWhereARRAY[] = "nType='pdf'"; break;
	case "Word": $nWhereARRAY[] = "nType='doc' OR nType='docx'"; break;
	case "Excel": $nWhereARRAY[] = "nType='xls' OR nType='xlsx'"; break;
	case "Other": $nWhereARRAY[] = "nType!='pdf' AND nType!='doc' AND nType!='docx' AND nType!='xls' AND nType!='xlsx'"; break;
}

$nWhereSQL = "";
if(isset($nWhereARRAY)){ $nWhereSQL = "WHERE ".implode(" AND ",$nWhereARRAY); } else { $nWhereSQL = ""; }
$nOrderBySQL = "ORDER BY $nSort $nSortOrder";
$nListingSQL = "SELECT * From nFile $nWhereSQL $nOrderBySQL";

if($nType){ $nPassThru = "?nType='$nType'"; } else { $nPassThru = ""; }
$nDivToUpdate = "ajaxFiles";
$nPageInDiv = "list/files.php";
$nCurrQueryStr = $_SERVER['QUERY_STRING'];
if($nCurrQueryStr){ $nFullPageInDiv = $nPageInDiv."?".$nCurrQueryStr; } else { $nFullPageInDiv = $nPageInDiv; }
$nPagingText = nGetPaging($nListingSQL,$nPage,$nShowPerPg,$nPassThru,$nDivToUpdate,$nPageInDiv);
if($nPassThru){ $nPassThruQry="?".$nPassThru; $nPassThru="&".$nPassThru; }
// for extrenal refresh requests
print "<script>refreshFiles = function(){ loadContent('".$nDivToUpdate."','".$nFullPageInDiv."'); }</script>";
?>


<table cellpadding="0" cellspacing="0" class="datatable">
	<tbody><tr>
		<?$nSortFld="nType"; if($nSortFld==$nSort){ if($nSortOrder=="ASC"){ $nLink="?srt=$nSortFld&ord=DESC$nPassThru";$nSortClass=" class=\"asc\""; } else { $nLink="?srt=$nSortFld&ord=ASC$nPassThru";$nSortClass=" class=\"desc\""; } } else { $nLink="?srt=$nSortFld&ord=ASC$nPassThru";$nSortClass=""; }?>
		<th style="width:25px;"><a href="javascript:loadContent('<?php echo $nDivToUpdate?>','<?php echo $nPageInDiv.$nLink?>');"<?php echo $nSortClass;?>><?php echo _("Type"); ?></a></th>
		<?$nSortFld="nPath"; if($nSortFld==$nSort){ if($nSortOrder=="ASC"){ $nLink="?srt=$nSortFld&ord=DESC$nPassThru";$nSortClass=" class=\"asc\""; } else { $nLink="?srt=$nSortFld&ord=ASC$nPassThru";$nSortClass=" class=\"desc\""; } } else { $nLink="?srt=$nSortFld&ord=ASC$nPassThru";$nSortClass=""; }?>
		<th><a href="javascript:loadContent('<?php echo $nDivToUpdate?>','<?php echo $nPageInDiv.$nLink?>');"<?php echo $nSortClass;?>><?php echo _("File Name"); ?></a></th>
		<th width="80" style="text-align:center;"><?php echo _("Filesize"); ?></th>
		<th style="width:96px;"><?php echo _("Actions"); ?></th>
	</tr>
	<?php echo nGetDocList();?>
</tbody></table>



<?php
function nGetDocList(){
	global $DBName,$Link,$nListingSQL;
	global $nShowPerPg,$nTotalRec,$nStartRec,$nEndRec,$nTotalPg;
	if(!$nListingSQL){ return ""; }
	$nCurrRec = 0;
	$nTable = "";
	$nRowClass = "";
	mysql_select_db($DBName); $Result = mysql_query($nListingSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		if($nCurrRec>=$nStartRec && $nCurrRec<=$nEndRec && $nCurrRec<=$nTotalRec){
			$nID = $Row["nID"];
			$nPath = $Row["nPath"];
			$nType = $Row["nType"];
			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,'.','');
					}
				}
			}
			if($nRowClass=="second"){ $nRowClass="first"; } else { $nRowClass="second"; }
			$nDeleteText = _("Delete");
			$nDeleteItemText = _("Delete Document");
			$nTable.= <<<CODE
			<tr id="row$nCurrRec" class="$nRowClass" onMouseOver="$('#row$nCurrRec').addClass('rowhilite');" onMouseOut="$('#row$nCurrRec').removeClass('rowhilite');">
				<td>$nIconImg</td>
				<td><a href="../$nPath" target="_blank"><strong style="font-size:smaller;">$nFilename</strong></a></td>
				<td align="center" style="text-align: center;">$nFilesizeText $nFilesizeByteText</td>
				<td>
					<div class="actions_menu" style="margin:6px 0;">
						<ul>
							<li><a href="dialog/file_delete.php?id=$nID" title="$nDeleteItemText" class="modal edit">$nDeleteText</a></li>
						</ul>
					</div>
				</td>
			</tr>
CODE;
		}
	}
	if(!$nTable){ $nTable = "<table width=\"100%\"><tr id=\"$nRowClass\"><td height=\"200\" align=\"center\" class=\"caption\" colspan=\"5\"><em>"._("No data available to display.")."</em></td></tr></table>"; }
	return $nTable;
}

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;
}
?>
<?php require("../../inc/db_close.php"); ?>
