<?php
# ---------------------------------------------------------------------
# truc is a tool for requirement and use case tracking
# Copyright (C) 2006 ASDIS - http://sf.net/projects/truc
#
# (rth) Initial truc version based on rth
#       Copyright (C) 2005 George Holbrook - rth@lists.sourceforge.net
#
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
#----------------------------------------------------------------------

include"./api/include_api.php";
auth_authenticate_user();


$page				= basename(__FILE__);
$s_test_details		= session_get_properties("test");
$test_id			= $s_test_details['test_id'];

$project_properties	= session_get_project_properties();
$s_user				= session_get_user_properties();
$username			= $s_user['username'];
$test_name			= test_get_name( $test_id );
$comments			= session_validate_form_get_field('comments');
$doc_type			= session_validate_form_get_field('doc_type');

if( isset( $_POST['manual_test_id'] ) && $_POST['manual_test_id'] != "" ) {

	$manual_test_id = $_POST['manual_test_id']; //session_validate_form_get_field('manual_test_id');
}
else {
	$manual_test_id		= $_GET['manual_test_id'];
}

$redirect_page		= "test_detail_page.php?test_id=$test_id&tab=2";
$redirect_on_error	= "test_add_doc_version_page.php?test_id=$test_id&manual_test_id=$manual_test_id";

session_validate_form_set($_POST, $redirect_page);


# Make sure the user entered a file name
$file_temp_name = $_FILES['uploadfile_required']['tmp_name'];
$file_name		= $_FILES['uploadfile_required']['name'];
$file_size		= $_FILES['uploadfile_required']['size'];


# Make sure the user filled in the file name.
if( !isset( $file_temp_name ) || !is_uploaded_file( $file_temp_name) ) {

	error_report_show($redirect_on_error, REQUIRED_FIELD_MISSING);
}

if( $file_size != '0' && is_uploaded_file($file_temp_name) ) {

	//print"manual_test_id = $manual_test_id<br>";

	file_add_supporting_test_doc_version($file_temp_name, $file_name, $test_id, $manual_test_id, $comments, $doc_type );

}
else{
	error_report_show($redirect_on_error, FAILED_FILE_UPLOAD);
}

html_print_operation_successful( 'file_upload_page', $redirect_page );
?>