<?php

	include "library.php";

	$cxn = start_code();
	
    $cxn_common = db_common_login();
	
	
	$pathroot = "/home/academi1/public_html/melbourneparaplanning/workflow";

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

	$task_id = val( $_SESSION, "task_id" );
		
	$task_file_id = val( $_SESSION, "task_file_id" );

	$new_filename = val( $_POST, "new_filename" );


	if ($new_filename == "")
	{
		echo red( "You must enter the new filename.<br><br>" );
		
		echo show_home_link();
	}
	else
	{
		$query = "SELECT random_file_number FROM task WHERE id = ".$task_id;

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

		$data = get_row( $result );
		
		$random_file_number = $data['random_file_number'];


		$query = "SELECT filename FROM task_file WHERE id = ".$task_file_id;

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

		$data = get_row( $result );
		
		$old_filename = $data['filename'];


		$old_filepath = $pathroot."/task_files/task_".$task_id."_".$random_file_number."/".$old_filename;

		$new_filepath = $pathroot."/task_files/task_".$task_id."_".$random_file_number."/".$new_filename;


//		$old_filepath = $pathroot."/resource_files/resource_group_".$resource_group_id."_".$random_file_number."/".$old_filename;
		
//		$new_filepath = $pathroot."/resource_files/resource_group_".$resource_group_id."_".$random_file_number."/".$new_filename;		
		
		
		if (rename( $old_filepath, $new_filepath ))
		{
			$query = "UPDATE task_file SET filename = '".$new_filename."' WHERE id = ".$task_file_id;
		
			$result = run_query( $query, $cxn );
				
			echo black( "File renamed successfully.<br><br>" );
		}
		else
		{
			echo red( "Error on renaming file.<br><br>" );
		}
			
		echo show_home_link();
	}

?>