<?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__);
$action_page            = 'requirement_action.php';
$num                    = 0;
$s_project_properties   = session_get_project_properties();
$project_name           = $s_project_properties['project_name'];
$project_id 			= $s_project_properties['project_id'];

$s_user_properties		= session_get_user_properties();
$user_id				= $s_user_properties['user_id'];
$username				= $s_user_properties['username'];

$row_style				= '';

$display_options 	= session_set_display_options( "requirements", array_merge($_POST, $_GET) );
$s_tab 				= $display_options['tab'];

$s_properties 		= session_set_properties("requirements", $_GET);
$s_req_id			= $s_properties['req_id'];
$s_discussion_id	= $s_properties['discussion_id'];

$project_manager	= user_has_rights($project_id, $user_id, MANAGER);

html_window_title();
html_print_body();
html_page_title($project_name ." - ". lang_get("req_discussion_page"));
html_page_header( $db, $project_name );
html_print_menu();

requirement_menu_print($page);

error_report_check($_GET);

$row_discussion			= discussion_get_detail($s_discussion_id);
$req_id					= util_pad_id($row_discussion[DISC_REQ_ID]);
$discussion_author		= $row_discussion[DISC_AUTHOR];
$discussion_date		= $row_discussion[DISC_DATE];
$discussion_subject		= $row_discussion[DISC_SUBJECT];
$discussion_status		= $row_discussion[DISC_STATUS];
$discussion_discussion	= util_html_encode_string($row_discussion[DISC_DISCUSSION]);

$rows_requirement 	= requirement_get_detail($project_id, $req_id);
$row_requirement 	= $rows_requirement[0];
$req_name			= $row_requirement[REQ_FILENAME];

print"<br>\n";

print"<div align=center>";

# REQUIREMENT DETAILS
print"<table class=width100 rules=cols>\n";
print"<tr>\n";
html_tbl_print_header( lang_get('req_id') );
html_tbl_print_header( lang_get('req_name') );
print"</tr>\n";
print"<tr>\n";
print"<td width='33%' class=grid-data-c><a href='requirement_detail_page.php?req_id=$req_id'>$req_id</a></td>\n";
print"<td width='33%' class=grid-data-c>$req_name</td>\n";
print"</tr>\n";
print"</table>\n";

print"<br>\n";

# DISCUSSION DETAILS
print"<table class=width100 rules=all>\n";

print"<tr>\n";
print"<td class=grid-header-l width='25%' nowrap>".lang_get('author')."</td>\n";
print"<td class=grid-data-l width='25%'>$discussion_author</td>\n";
print"<td class=grid-header-l nowrap>".lang_get('created')."</td>\n";
print"<td class=grid-data-l>$discussion_date</td>\n";
print"</tr>\n";

print"<tr>\n";
print"<td class=grid-header-l width='25%' nowrap>".lang_get('subject')."</td>\n";
print"<td class=grid-data-l width='25%'>$discussion_subject</td>\n";
print"<td class=grid-header-l nowrap>".lang_get('status')."</td>\n";
print"<td class=grid-data-l>$discussion_status</td>\n";
print"</tr>\n";

print"<tr>\n";
print"<td class=grid-header-l valign=top nowrap>".lang_get('discussion')."</td>\n";
print"<td class=grid-data-l colspan=3>$discussion_discussion</td>\n";
print"</tr>\n";

print"</table>\n";

# CLOSE DISCUSSION
if( $discussion_status!=="CLOSED" && $project_manager ) {
	print"<br>\n";
	print"<form method=post action='requirement_discussion_close_page.php'>\n";
	print"<input type=hidden name=discussion_id value=$s_discussion_id >\n";
	print"<input type=hidden name=author value=$username >\n";
	print"<input type=submit name=submit_close_discussion value='".lang_get('close_discussion')."'>\n";
	print"</form>\n";
}

# POSTS
print"<form method=post action='requirement_discussion_action.php'>\n";
print"<input type=hidden name=discussion_id value=$s_discussion_id >\n";
print"<input type=hidden name=author value=$username >\n";

print"<h2>".lang_get("posts")."</h2>\n";

$rows_posts 	= discussion_get_posts( $s_discussion_id );

if( $rows_posts ) {
	print"<table class=width90 rules=all>\n";
	print"<tr>\n";
	print"<td class=grid-header-c>".lang_get("author")."</td>\n";
	print"<td class=grid-header-c>".lang_get("post")."</td>\n";
	print"</tr>\n";

	foreach($rows_posts as $row_post) {

		$post_author	= $row_post[POST_AUTHOR];
		$post_date		= $row_post[POST_DATE];
		$post_message	= util_html_encode_string($row_post[POST_MESSAGE]);

		$row_style = html_tbl_alternate_bgcolor($row_style);
		print"<tr class='$row_style'>\n";
		print"<td class=tbl-l width='20%' valign=top>\n";
		print"<p>$post_author<br><br>\n";
		print lang_get('posted')." $post_date</p>\n";
		print"</td>\n";
		print"<td class=tbl-l valign=top>$post_message</td>\n";
		print"</tr>\n";

	}

	print"</table>\n";
} else {
	print lang_get('no_posts');
}


# ADD POST
if( $discussion_status !== "CLOSED" ) {

	print"<br>\n";
	print"<br>\n";

	print"<span class='required'>*</span> <span class='print'>" . lang_get('must_complete_field') . "</span>\n";

	print"<table class=width60>\n";
	print"<tr>\n";
	print"<td>\n";
	print"<table class=inner>\n";
	print"<tr>\n";
	print"<td colspan=2><h4>". lang_get('add_new_post') ."</h4></td>\n";
	print"</tr>\n";
	print"<tr>\n";
	print"<td class=center>\n";

	# MESSAGE
	print"<tr>\n";
	print"<td valign=top><span class='required'>*</span></td>\n";
	print"<td align=left>\n";
	html_FCKeditor("new_post_required", 600, 200);
	print"</td>\n";
	print"</tr>\n";

	# SUBMIT BUTTON
	print"<tr>\n";
	print"<td colspan=2 align=center><input type='submit' name='submit_add_post' value='". lang_get('add') ."'></td>\n";
	print"</tr>\n";
	print"</table>\n";
	print"</td>\n";
	print"</tr>\n";
	print"</table>\n";
}

print"</form>\n";

print"</div>\n";

html_print_footer();
?>