<?php

	include "library.php";

	$cxn = start_code();

    $cxn_common = db_common_login();


	$pathroot = "/home/academi1/public_html/melbourneparaplanning/workflow";

	$resource_group_id = val( $_GET, "resourcegroupid" );
	
	
	$_SESSION['resource_group_id'] = $resource_group_id;
	
	
			/* get variables */

//	if (val( $_SESSION, 'id' ) != "")
//		$id = $_SESSION['id'];
//	else
//	{
//		$id = val( $_POST, "id" );
//
//		$_SESSION['id'] = $id;
//	}
	

		/* start the page  */

	$query = "SELECT * FROM resource_groups WHERE id = ".$resource_group_id;			

	if (! query_has_rows( $query, $cxn ))
	{
		page_header( "", $cxn, $cxn_common );

		$_SESSION['id'] = "";
				
		echo black( "<br>Resource group ID not found<br><br>" );
		
		echo show_home_link();
	}
	else
	{

		page_header( "Resource Files", $cxn, $cxn_common );

        echo "<script type='text/javascript'>";
        
        echo "  function uploadFile()";
        echo "  {";
        echo "      var fd = new FormData();";
        echo "      fd.append('file', document.getElementById('file').files[0]);";
        echo "      if (document.getElementById('file').value == '')";
        echo "          alert ('Select a file to upload by clicking on the \'Browse\' / \'Choose file\' button then click \'Upload\'');";
        echo "      else";
        echo "      {";
        echo "          var xhr = new XMLHttpRequest();";
        echo "          xhr.upload.addEventListener('progress', uploadProgress, false);";
        echo "          xhr.addEventListener('load', uploadComplete, false);";
        echo "          xhr.addEventListener('error', uploadFailed, false);";
        echo "          xhr.addEventListener('abort', uploadCanceled, false);";
        echo "          xhr.open('POST', 'upload_resource_file.php');";
        echo "          xhr.send(fd);";
        echo "      }";
        echo "  }";
        
        echo "  function uploadProgress(evt)";
        echo "  {";
        echo "      if (evt.lengthComputable)";
        echo "      {";
        echo "          var percentComplete = Math.round((evt.loaded / evt.total) * 100);";
        echo "          document.getElementById('progressbar').value = percentComplete;";
        echo "      }";
        echo "      else ";
        echo "      {";
        
        //          document.getElementById('progressbar').innerHTML = 'unable to compute';
        
        echo "      }";
        echo "  }";
        
        echo "      function uploadComplete(evt)";
        echo "      {";
        echo "              alert( 'File upload complete. If you attempted to upload a file that already exists it has not been overwritten. You must rename the old file first.' );";
        echo "              window.location.href = 'manage_resources.php?resourcegroupid=".$resource_group_id."';";
        echo "      }";
        
        echo "      function uploadFailed(evt)"; 
        echo "      {";
        echo "          alert('There was an error attempting to upload the file.');";
        echo "      }";
        
        echo "      function uploadCanceled(evt)";
        echo "      {";
        echo "          alert('The upload has been canceled by the user or the browser dropped the connection.');";
        echo "      }";
        
        echo "</script>";
            


		$result = run_query( $query, $cxn );

		$data = get_row( $result );


		$random_file_number = $data['random_file_number'];
	
			/* retrieve existing files */
	
		$query = "SELECT * FROM resource_file WHERE resource_group_id = ".$resource_group_id;
	
		$result = run_query( $query, $cxn );
	
		if ($random_file_number == "" || query_num_rows($result) == 0)
			echo red( "No files currently stored" );
	
		echo "<br><table><tr>";
	

		$result = run_query( $query, $cxn );
	
		for ($i=0;$i <  query_num_rows($result);$i++)
		{
			$data = get_row( $result );
	
			$file_path = "/resource_files/resource_group_".$resource_group_id."_".$random_file_number."/".$data['filename'];
	
			$modified_datetime = date( "d M Y H:i:s", filemtime( $pathroot.$file_path ) );
	
			echo "<tr><td align='left'><a href='https://melbourneparaplanning.com/workflow".$file_path."'>".black( $data['filename'] )."</a></td><td>&nbsp;".black( $modified_datetime )."<td>";
			
			echo "&nbsp;&nbsp;&nbsp;".show_link( "rename", "get_new_filename.php", "resourcegroupid", $resource_group_id, "resourcefileid", $data['id'] );

			echo "</td></tr>";			
	
		}
	
		echo "</tr><tr>";
	
		echo "</tr></table><br><br><br><br>";
		
		echo "<br>";
		echo "<br>";
		echo "<br>";
		echo "<br>";
		echo "<br>";
		echo "<br>";
		
	
//		echo "<br>".blue( "Upload a new file. Please click on 'Choose File' then click on 'Upload'" )."<br><br><br>";
	
		echo "<table>";
	
		echo "<form name='form' enctype='multipart/form-data' action='".url_encode( "upload_resource_file.php" )."' method='POST'>";

        echo "<tr><td valign='center'>".black( "<b>Select file</b>" )."</td><td valign='center'><input type='file' name='file' id='file' size='60'></td></tr>";

        echo "<tr><td>&nbsp;</td></tr>";

        echo "<tr><td><progress id='progressbar' value='0' max='100'></progress></td></tr>";
        
        echo "<tr><td>&nbsp;</td></tr>";

		echo "<tr><td><div onclick='uploadFile()' class='greenbuttondesktop' style='line-height: 25px; width: 90;' align='center'>Upload</div></td></tr>";			

		echo "<tr>";
	

		form_end();
	
		echo "<br>".red( "NOTES: <br><br>1. Please do not attempt to upload files that have a percent symbol % in the file name. Change the name of the file before upload.<br><br>2. If you attempt to upload a file that already exists no action will occur. You must rename the old file first to upload a new version." )."<br><br>";

	}
	
		
?>