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

$nSecureMe = 1;

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

// Includes
require("../inc/config.php");
require("inc/lang.php");
?>
<?php include("inc/nav/header.php"); ?>

<div id="titlediv">
    <div class="clearfix container" id="pattern">
        <div class="row">
            <div class="col_12">
                <ul class="breadcrumbs hor-list">
                    <li><a href="home.php"><?php echo _("Dashboard"); ?></a></li>
                    <li><a href="library_images.php"><?php echo _("Image Library"); ?></a></li>
                </ul>
                <h1><?php echo _("Document Library"); ?></h1>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
	$('#file_upload').uploadify({
		'uploader'  : 'assets/scripts/uploadify/uploadify.swf',
		'script'    : 'assets/scripts/uploadify/uploadify.php',
		'cancelImg' : 'assets/scripts/uploadify/cancel.png',
		'folder'    : '<?if(PHP_OS == 'WINNT'){ echo str_replace("\\","/","/////".$nSiteRootPath."usr/docs/"); } else { echo "../usr/docs"; }?>',
		'multi'     : true,
		'auto'      : true,
		//'queueSizeLimit' : 10, // for display purposes only
		'checkScript' : 'assets/scripts/uploadify/check.php',
		'onComplete' : function(event, ID, fileObj, response, data) {
			fileSave(fileObj.name,'');
		},
		'onAllComplete' : function() {
			//alert('all files uploaded');
			//refreshImages();
		}
	});
});
// Handle file saving
var fileCounter = 0;
fileSave = function(path,id,xType){
	var formData = "rid="+id+"&path="+path;
	var loadUrl = "process/doc_save.php";
	$('#fileCounter').html(loaderImgS).load(loadUrl, formData, function(response, status, xhr) {
		if(status!="error"){
			if(response == "1"){
				// additional processing, if needed
				fileCounter++;
				$('#fileCounter').html(fileCounter+" files uploaded.");
				nLoadAjaxFiles();
			} else { alert("There was an error saving the file: "+response); }
		} else {
			 alert("An error occurred while performing your request: " + xhr.status + " " + xhr.statusText);
		}
	});
	return false;
}
</script>

<div class="container" id="actualbody">
	
	<div class="row clearfix" style="padding:0; text-align:right;">
		<button onClick="$('#imgFormDiv').slideDown('slow');" class="icon" data-icon-primary="ui-icon-plus" style="margin-top:-60px;"><?php echo _("Add New Document"); ?></button>
	</div>
	<div class="row clearfix" id="imgFormDiv" style="display:none;">
	    <div class="widget clearfix">
			<div class="view-switcher">
			    <h2><?php echo _("Upload a new document"); ?></h2>
			</div>
			<div class="widget_inside">

				<div>
					<table style="width: auto;">
						<tr style="background-color:transparent;">
							<td style="vertical-align:bottom;"><?php echo _("Choose file(s) to upload:"); ?></td>
							<td><input id="file_upload" name="file_upload" class="file_upload" /></td>
							<td style="vertical-align:bottom;" id="fileCounter"></td>
						</tr>
					</table>
					<BR><BR>
					<a href="javascript:;" title="Close" onClick="$('#imgFormDiv').slideUp('fast');" class="button"><?php echo _("I'm finished uploading"); ?></a>
				</div>
				
	        </div>
	    </div>
	</div>
	
	<div class="row clearfix">
	    <div class="widget clearfix">
			<div class="view-switcher">
			    <h2><span id="ajaxFilesTitle"><?php echo _("All document types"); ?></span><a href="javascript:;">&darr;</a></h2>
			    <ul>
					<?=nGetImageLibraries();?>
			    </ul>
			</div>
			<div class="widget_inside">
	            <span id="notify" class="notification done" style="display:none;"></span>
				<div class="col_12" id="ajaxFiles"></div>
	        </div>
	    </div>
	</div>
    
</div>

<script>
// load external content
function nLoadAjaxFiles(){
	loadContentToID("#ajaxFiles","tables/files.php?t=all");
}
$(function(){
	nLoadAjaxFiles();
});
</script>

<?php include("inc/nav/footer.php") ?>
<?php
function nGetImageLibraries(){
	global $DBName, $Link;
	$nTable = "";
	$nTable .= <<<CODE
		<li><a href="javascript:;" onClick="loadTable('#ajaxFiles','tables/files.php?t=all','All document types');">All types</a></li>
		<li><a href="javascript:;" onClick="loadTable('#ajaxFiles','tables/files.php?t=PDF','PDF');">PDF</a></li>
		<li><a href="javascript:;" onClick="loadTable('#ajaxFiles','tables/files.php?t=Word','Word');">Word</a></li>
		<li><a href="javascript:;" onClick="loadTable('#ajaxFiles','tables/files.php?t=Excel','Excel');">Excel</a></li>
		<li><a href="javascript:;" onClick="loadTable('#ajaxFiles','tables/files.php?t=Other','Other');">Other</a></li>
CODE;
	return $nTable;
}
function nSanitizeFilename($nFilename){
	if(!$nFilename){ return; }
	$nBadCharsARRAY = array("amp;",",","!","@","#","$","%","^","&","*","(",")",":",";","/","\"","'","?","<",">","[","]","{","}","|","+","=","~","`");
	$nFilename = str_replace($nBadCharsARRAY,"",$nFilename);
	$nFilename = str_replace(" ","-",$nFilename);
	$nFilename = str_replace("--","-",$nFilename);
	$nFilename = strtolower($nFilename);
	return $nFilename;
}
function nImg_GetExtension($nImgFile){
	if(!$nImgFile){ return; }
	$nImgInfo = pathinfo($nImgFile);
	$nExt = strtolower($nImgInfo["extension"]);
	return $nExt;
}
?>
<?
// disconnect from mysql
mysql_close($Link);
?>
