<?php if (!isset($_SESSION['FirstName'])) {
	session_start();
}
session_start();
//require_once ('includes/mysql_connect.php');
	include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
    include ('menudatabase.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php include('title.php'); ?>

<link href="css/main_style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/print_style.css" rel="stylesheet" type="text/css" media="print" />

<link rel="stylesheet" type="text/css" href="css/cssverticalmenu.css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style>
label, input { display:block; }
input.text { margin-bottom:12px; width:95%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
h1 { font-size: 1.2em; margin: .6em 0; }
div#users-contain { width: 350px; margin: 20px 0; }
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
.ui-dialog .ui-state-error { padding: .3em; }
.validateTips { border: 1px solid transparent; padding: 0.3em; }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<style type="text/css">
.file-icon{
	border: medium none;
    height: 30px;
    position: relative;
    top: 8px;
    width: 30px;
}
.gearToRight{
	float: right;
    margin-right: 15px;
}
</style>
</head>
<body>

<div id="wrapper">

<!-- start header -->
<?php 
include('includes/header.inc.php');

 ?>
<!-- end header-->


<!-- start page -->
<div id="page-container-nobg">
<div id="page-bg-top">
<!-- start content -->
<div id="content-one-column">
   
		<div class="post">
		  <div class="entry">
          <br/>
				<h1>Uploaded Files</h1>
           <br/>
           
</br>
  <script>
  $( function() {
 
    function addAlias() {
      	if( $('#p_alias').val() == ''  ){
      		$('.validatEr').html('Please provide alias.').css('color','red');
      		return false;
      	}else{
      		var p_userid    = $('#p_userid').val();                  
			var p_file_name = $('#p_file_name').val();
			var p_alias 	= $('#p_alias').val();                        
      		$.ajax({
      			url      : '/cmsadmin/storealias.php',
      			type     : 'POST',
      			dataType : 'json',
      			data     : {
      				userid : p_userid,
      				file_name : p_file_name,
      				alias : p_alias,
  				},
      		}).done(function(res) {
      			if( res.status == 1){
      				$('.validatEr').html(res.message).css('color','green');
      				setTimeout(function(){
  						dialog.dialog( "close" );
  						window.location.reload();
      				},2000);
      			} else {
      				$('.validatEr').html(res.message).css('color','red');
  					return false;
      			}
      		});
      		return false;
      	}
    }

    dialog = $( "#dialog-form" ).dialog({
      	autoOpen: false,
      	height: 280,
      	width: 350,
      	modal: true,
      	buttons: {
	        "Update Alias": addAlias,
	        Cancel: function() {
	          	dialog.dialog( "close" );
	        }
      	},
      	close: function() {
			$('#p_userid').val('');                  
			$('#p_file_name').val('');
			$('#p_alias').val('');           
      	}
    });
 
    $(".update_alias").on( "click", function() {
    	$('#p_userid').val( $(this).data('id') );
		$('#p_file_name').val( $(this).data('filename') );
		$('#p_alias').val( $(this).data('alias')  );
      	dialog.dialog( "open" );
    });
    $(".delete").on( "click", function() {
    	var result=confirm('Are you sure you wish to delete this file?');
    	if(result){
    	var p_userid=$(this).data('id') ;
		var file_name=$(this).data('filename') ;
		$.ajax({
      			url      : 'deletefile.php',
      			type     : 'POST',
      			dataType : 'json',
      			data     : {
      				userid : p_userid,
      				file_name : file_name,
      				
  				},
      		}).done(function(res) {
      			if(res.status == 1){ 
      				  $("#message").append(res.message);
				      setTimeout(function(){
				       location.reload(); 
				      }, 1000); 
				   }
      		});
      	}
    });
} );
  </script>

<div id="dialog-form" title="Update Alias">
  	<p class="validatEr"></p>
  	<form id="aliasForm" >
    	<fieldset>
    		<input type="hidden" name="user_id" value="" id="p_userid" />

	      	<label for="email">File Name</label>
	      	<input type="text" name="file" id="p_file_name" disabled value="" class="text ui-widget-content ui-corner-all">

	      	<label for="name">Alias</label>
	      	<input type="text" name="alias" id="p_alias" value="" class="text ui-widget-content ui-corner-all">

	      	<input type="submit" tabindex="-1" style="position:absolute;top:-1000px">
	    </fieldset>
  	</form>
</div>
<p class="successEr"></p>

<?php
if($_SESSION['adminkey']=="security"){
   echo '<center><i><br><br>';    
  
   if (($_SESSION['FirstName'] == "")|| ($_SESSION['adminkey'] !=="security")) {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}
	$_SESSION['selectVal']   = $_GET['searchName'];
	$_SESSION['selectField'] = $_GET['searchField'];
	//echo $_SESSION['selectField'];
	echo '<div style="float: right; margin-right: 36px; padding:10px">
           <input type="button" value="back to the list" onclick="javascript:window.history.back(); return false;">
           </div>';
    echo '<span id="message" style="color: green; font-size: 30px;"></span>';
    $userid          = $_GET["userid"];
	if(is_dir('../custom/users/'.$userid)){
		if ($handle = opendir('../custom/users/'.$userid)) {
			echo '<table  bordercolor="#000" style="text-align: left;
	    width: 100%;" cellspacing=0 cellpadding=4>';
		    while (false !== ($entry = readdir($handle))) {
			    if ($entry != "." && $entry != "..") {
			    	$icon      = '';
			    	$fileType  = explode('.', $entry);
			    	$extension = strtolower($fileType[1]);
			    	switch ($extension) {
			    		case 'jpg':
			    		case 'png':
			    		case 'jpeg':
			    		case 'ico':
			    			$icon = '/images/file-icon/image.png';
			    			break;

			    		case 'xls':
			    		case 'xlsx':
			    			$icon = '/images/file-icon/xls.png';
			    			break;

			    		case 'pdf':
			    			$icon = '/images/file-icon/pdf.png';
			    			break;

			    		case 'odt':
			    		case 'docx':
			    		case 'docm':
			    			$icon = '/images/file-icon/doc.png';
			    			break;

			    		case 'mp3':
			    		case 'mp4':
			    		case '3gp':
			    		case 'gif':
			    			$icon = '/images/file-icon/audio-video.ico';
			    			break;
			    		
			    		default:
			    			$icon = '/images/file-icon/generic.png';
			    			break;
			    	}
			    	?>
			    	<tr>
			    		<td>
			    		   
			    		   <a href="javascript:void(0);" class="delete" data-id="<?php echo $userid; ?>" data-filename="<?php echo $entry ?>">
		    				
		    				<img src="/images/cancel.gif" style="float: right; margin: 16px;">
	    					</a>
			    			<img class="file-icon" src="<?php echo $icon;?>"/>
		    				<a href="javascript:void(0);" class="update_alias" data-id="<?php echo $userid; ?>" data-alias="<?php echo ( isset($filenamearr[$entry])?$filenamearr[$entry]:'' ) ?>" data-filename="<?php echo $entry ?>">
		    					<img style="float:right;" class="file-icon gearToRight" src="/cmsadmin/images/gear-icon.png"/>
	    					</a>
	    					
		    				<a href="secure.php?file=<?php echo $entry;?>&id=<?php echo $userid; ?>" target="_blank">
		    					<?php echo ( isset($filenamearr[$entry])?$filenamearr[$entry]:$entry )  ?>	
	    					</a><br>
		    			</td>
	    			</tr>
			<?php }
			}
			
		    closedir($handle);
		}
	}else{
		echo "No documents available";
	}
} else {
	echo '<font color="red"><center>You are not authorized to view this page. <br>Please <a href="index.php"><font color="blue">login here</font></a>.</center></font>';

}

?>            
<br />

</div><!-- end entry-->

<br />

</div><!-- end post-->
    


<div style="clear: both;">&nbsp;</div>
<div id="page-bg-btm">
</div><!-- end page-bg-btm div -->
</div><!-- end page-bg-top div -->
</div><!-- end page-container -->



</div><!-- end wrapper div-->

<!-- end page -->


<!--footer-->



</body>
</html>
