<?php
include( "func.inc" );

#### hidden_vars return string with hidden values #########################
function hidden_vars( $mode ){
	global $ver, $pid;
	$ret = "<input type=hidden name='mode' value='$mode'>";
	$ret .= "<input type=hidden name='ver' value='$ver'>";
	$ret .= "<input type=hidden name='pid' value='$pid'>";
	return $ret;
}

# open and select local database
# if( !open_db()) exit;

$t = getmsg( 15, $lang );
if( !head( $t[0], '', $xid, $xpar )) exit;
$mid = $xpar[ 'mid' ]; $e = $xpar[ 'xem' ];
$fn = $xpar[ 'xfn' ]; $ln = $xpar[ 'xln' ]; $dt = dt_de( $xpar[ 'xdt' ] );

echo "<table width=100%><tr><td width=10% valign=top>\n";
echo "<font $_fontbulk>\n";
echo "$t[1]:</td>\n<td><font $_fontbulk>\n";
echo "<a href='$PHP_SELF?xid=$xid'>";
if( empty( $opt )) $pic = 'more'; else $pic = 'bluebsm';
echo "<img src='/images/$pic.gif' border=0 height=12 width=12>\n";
echo "$t[2]</a><br>";
echo "<a href='$PHP_SELF?xid=$xid&opt=ini'>";
if( $opt == 'ini' ) $pic = 'more'; else $pic = 'bluebsm';
echo "<img src='/images/$pic.gif' border=0 height=12 width=12>\n";
echo "$t[3]</a><br>";
echo "<a href='$PHP_SELF?xid=$xid&opt=act'>";
if( $opt == 'act' ) $pic = 'more'; else $pic = 'bluebsm';
echo "<img src='/images/$pic.gif' border=0 height=12 width=12>\n";
echo "$t[4]</a><br>";
echo "<a href='$PHP_SELF?xid=$xid&opt=arc'>";
if( $opt == 'arc' ) $pic = 'more'; else $pic = 'bluebsm';
echo "<img src='/images/$pic.gif' border=0 height=12 width=12>\n";
echo "$t[5]</a><br>";
echo "<a href='$PHP_SELF?xid=$xid&opt=fin'>";
if( $opt == 'fin' ) $pic = 'more'; else $pic = 'bluebsm';
echo "<img src='/images/$pic.gif' border=0 height=12 width=12>\n";
echo "$t[6]</a></font></td>\n";
echo "<td align=right><font $_fontinpt>\n";
echo "<img src='/images/more.gif' alt='->'>&nbsp;";
echo "<a href='/setupform.phtml?xid=$xid'><b>$t[7]</b></a> &nbsp;\n";
$m = explode( '#', $_admin );
$m_no = count( $m );
for( $i = 0; $i < $m_no && $m[$i] != $mid; $i++ );
if( $i < $m_no ){
	echo "<br><img src='/images/more.gif' alt='->'>&nbsp;";
	echo "<a href='/otauth/param.phtml?xid=$xid'><b>$t[24]</b></a> &nbsp;\n";
}
echo "</font></td></tr></table>\n";

echo "<center>\n<table bgcolor='$_bg3' cellpadding=4>\n";
echo "<tr bgcolor='$_bg2'>\n<td>\n";
echo "<font $_fontbulk>\n";
echo "$t[8]</font></td>\n";
echo "<td valign='top'>\n";
echo "<font $_fontbulk>\n";
echo "$t[9]<br><font color='$_fg2'>";
echo "$t[10]</font> / <font color='$_fg4'>";
echo "$t[11]</font> ($t[12])</font></td>\n";
echo "<td valign='top' align='center'>\n";
echo "<font $_fontbulk>\n";
echo "$t[13]</font></td>\n";
echo "<td valign='top' align='center'>\n";
echo "<font $_fontbulk>\n";
echo "$t[14]</font></td>\n";
echo "<td valign='top' align='center'>\n";
echo "<font $_fontbulk>\n";
echo "$t[15]</font></td></tr>\n";
$is_finished = FALSE;

if( empty( $opt )){
	$opt = "( status = 'ini' or status = 'act')";
} else {
	$opt = "status = '$opt'";
}
# select a metatext record by memb_id
if( !$id = mysql_query( "select * from metatext where memb_id = $mid
	and $opt order by titlabb, vers_id desc" )) {
	echo "Error No. 3: ".mysql_errno().": ".mysql_error();
	exit;
}
# list all metatext records
while( $row = mysql_fetch_array( $id )) {

	# is the prev. listed text the prev. project version?
	$new_exists = ( $pid == $row[ "proj_id" ]) && ( --$ver == $row[ "vers_id" ] );
	$pid = $row[ "proj_id" ];
	$ver = $row[ "vers_id" ];
	$ti = $row[ "title" ];
	$ta = $row[ "titlabb" ];
	$dt = dt_de( $row[ "submit" ]);
	$st = $row[ "status" ];
	$ml = htmlentities( $row[ "maillst" ]);
	$rb = $row[ "ref_by" ];
	$lk = $row[ "link" ];
	$ds = add_xid( $row[ "descr" ], $xid );
	$st = st_de( $st ); 

	# get subproject
	if( $rb > 0 ) {
		if( !$rid = mysql_query( "select title, titlabb 
			from metatext where proj_id = $rb and $opt" )){
			echo "Error No. 31: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( !$row = mysql_fetch_array( $rid )){
			echo "Error No. 32: ".mysql_errno().": ".mysql_error();
			exit;
		}
		$rbs = "'".$row[ "titlabb" ]."': ".$row[ "title" ];
	}

	# check submitted base text  
	if( !$rid = mysql_query( "select proj_id from paragraph 
		where proj_id = $pid and vers_id = $ver" )) {
		echo "Error No. 4: ".mysql_errno().": ".mysql_error();
		exit;
	}

	# if no rows selected then no text was submitted
	$istxt = $row = mysql_fetch_array( $rid );

	####################################################
	# 1st col: project / vers_id / status / started at
	####################################################

	echo "<tr>\n<td valign='top'>\n";
	echo "<font $_fontbulk>\n";
	echo "<b>$ta</b><br>$_ver&nbsp;<b>$ver</b>&nbsp;/&nbsp;<b>";

	switch( $st ){
	case $_ini:
		echo "<font color='$_fg3'>$st</font></b><br>$dt";
		if( $istxt ){
			echo "<center><form action='/modify.phtml?xid=$xid' ";
			echo "method='post'>\n";
			$mode = "activate";
			echo "<input type=submit value='$t[16]'>";
			echo hidden_vars( $mode );
			echo "</form></center>";
		}
	break;
	case $_act:
		echo "$st</b><br>$dt";
		if( !$new_exists ){
			echo "<center><form action='/modify.phtml?xid=$xid' ";
			echo "method='post'>\n";
			$mode = "finish";
			echo "<input type=submit value='$t[17]'>";
			echo hidden_vars( $mode );
			echo "</form></center>";
		}
	break;
	case $_fin:
		echo "$st</b><br>$dt";
		$is_finished = TRUE;
	break;
	default:
		echo "$st</b><br>$dt";
	}
	echo "</font></td>\n";

	####################################################
	# 2nd col: title / description / subproject
	####################################################

	echo "<td valign='top'>\n<font $_fontbulk>\n";
	echo "<b>$ti</b><br>$ds";
	if(( $st == $_ini ) or ( $st == $_act )){
		echo "<br><font color='$_fg2'>";
		if( $rb ){
			echo "$t[10] $rbs";
		}
		else {
			echo "($t[20])";
		}
		echo "</font><br><font color='$_fg4'>";
		echo "$t[11] $ml</font>";
	}
	echo "</font></td>\n";

	####################################################
	# 3rd col: project data (metatext)
	####################################################

	echo "<td align=center>\n<font $_fontbulk>\n";

	# fill cols for new projects
	if( $st == $_ini ) {

		$ti = urlencode( $ti );
		$ds = urlencode( $ds );
		echo "<form action='/modify.phtml?xid=$xid' method='post'>\n";
		$mode = "data_change";
		echo "<input type=submit value='$t[18]'>";
		echo hidden_vars( $mode );
		echo "</form></font></td>\n";

		############################################
		# 4th col: edit base text
		############################################

		echo "<td align=center>\n<font $_fontbulk>\n";
		echo "<form action='/modify.phtml?xid=$xid' method='post'>\n";
		echo "<input type=submit value=";
			# is there an already submitted base text?
		if( $istxt ){
			echo "'$t[18]'>";
			$mode = "text_change";
		}
		else {
			echo "'  neu  '>";
			$mode = "text_new";
		}
		echo hidden_vars( $mode );
		echo "</form></font></td>\n";

		############################################
		# 5th col: view base text
		############################################

		echo "<td align=center>\n<font $_fontbulk>\n";

		# is there an already submitted base text?
		if( $istxt ){
			echo "<a href='".lnkxid( $lk, $xid )."'>HTML</a>";
			echo "<br><a href='";
			echo lnkxid( "$lk?action=ascii", $xid )."'>ASCII</a>";
		}
		else {
			echo $t[19];
		}
	}

	# fill cols for active, archives or finished projects
	else {
		if( $st == $_act ){
			echo "<form action='/modify.phtml?xid=$xid' ";
			echo "method='post'>\n";
			$mode = "data_change";
			echo "<input type=submit value='$t[18]'>";
			echo hidden_vars( $mode );
			echo "</form></font></td>\n";
		}
		else {
			echo "$t[21]</font></td>\n";
		}

		############################################
		# 4th col: edit base text
		############################################

		echo "<td align=center>\n<font $_fontbulk>\n";

		# active projects can have new versions
		if( !$new_exists && ( $st == $_act )){
			echo "<form action='modify.phtml?xid=$xid' ";
			echo "method='post'>\n";
			$mode = "text_version";
			echo hidden_vars( $mode );
			echo "\n<input type=submit value=";
			$ver++;
			echo "'$t[22] $ver'></form>";
		}
		elseif( $new_exists && ( $st == $_act )){
			echo "$t[23]<br>'$_ini'";
		}
		elseif( $is_finished ){
			echo $t[21];
		}
		else{
			echo "$t[23]<br>'$_act'";
		}
		echo "</font></td>\n";

		############################################
		# 5th col: view base text
		############################################

		echo "<td align=center>\n<font $_fontbulk>\n";
		echo "<a href='".lnkxid( $lk, $xid )."'>HTML</a>";
		echo "<br><a href='";
		echo lnkxid( "$lk?action=ascii", $xid )."'>ASCII</a>";
	}
	echo "</font></td></tr>\n";
}
echo "</table></center>\n<font $_fontbulk>\n";
echo "<h4>$t[30]</h4>\n";
echo "$t[31]\n";
eval( "\$x = \"$t[32]\";" );
echo "<ul>\n$x\n";
eval( "\$x = \"$t[33]\";" );
echo "<br>\n$x\n";
eval( "\$x = \"$t[34]\";" );
echo "<br>\n$x\n";
eval( "\$x = \"$t[35]\";" );
echo "<br>\n$x\n</ul></font>\n";
foot()
?>
