<?php
include( "menu.inc" );
include( "func.inc" );
$t = getmsg( 21, $lang );

#### write_projects reads recursively all sub projects #####################
function write_projects( $proj_id, $pid, $lev, $xid ){
	global $t;
	if( !$sid = mysql_query( "select * from metatext
		where proj_id = $pid 
		and ( status = 'act' or status = 'fin' )
		order by submit desc" )) {
		echo "Error No. 4: ".mysql_errno().": ".mysql_error();
		exit;
	}
	while( $row = mysql_fetch_array( $sid )){
		$ta = $row[ "titlabb" ];
		$ver = $row[ "vers_id" ];
		$dt = date_de( $row[ "submit" ]);
		$ti = $row[ "title" ];
		$lk = strtolower( $ta );
		$link = $row[ "link" ];
		$li = '/'.strtolower( $ta ).'/';
		$st = $row[ "status" ];
		for( $i = 0; $i < $lev; $i++ ) echo "&nbsp; &nbsp; ";
		if( $proj_id != $pid ) {
			if( $lev > 0 ){
				echo "<img src='/images/sub.gif' border=0 height=12 width=12>\n";
			} else { 
				echo "<img src='/images/blueball.gif' border=0 height=12 width=12>\n";
			}
			$li = lnkxid( $li, $xid );
			echo "<a href='$li'>Projekt '$ta', $ti</a>";
		} else {
			echo "<img src='/images/more.gif' border=0 height=12 width=12>\n";
			$link = lnkxid( $link, $xid );
			echo "<b><a href='$link'>Projekt '$ta', $ti (Version ";
			echo "$ver, $dt)</a></b>";
		}
		if( $st == 'fin' ) echo "<font color='#FF0000'> - abgeschlossen!</font>";
		echo "<br>\n";
	}
	if( !$sid = mysql_query( "select proj_id from metatext
		where ref_by = $pid and ( status = 'act' or status = 'fin')" )) {
		echo "Error No. 5: ".mysql_errno().": ".mysql_error();
		exit;
	}
	if( mysql_num_rows( $sid ) == 0 ) return 0;
	$lev++;
	while( $row = mysql_fetch_array( $sid )){
		$pid = $row[ "proj_id" ];
		write_projects( $proj_id, $pid, $lev, $xid );
	}
}

function indexfile( $proj_id ){
	global $maildom, $_fontbulk, $_fontfoot, $xid, $t, $_x;
	# select project data
	if( !$id = mysql_query( "select m.last_n, m.first_n, m.email, t.ref_by,
		t.vers_id, t.title, t.titlabb, t.status, t.maillst, t.submit,
		t.link, t.descr, t.keywds from member m, metatext t
		where m.memb_id = t.memb_id and t.proj_id = $proj_id
		and ( t.status = 'act' or t.status = 'fin' )" )){
		echo "Error No. 1: ".mysql_errno().": ".mysql_error();
		exit;
	}
	if( !$row = mysql_fetch_array( $id )){
		head( $t[1], '', $xid, $xpar );
		echo "<font $_fontbulk>\n";
		echo "<p>$t[2]</p>\n<br><br>";
		foot();
		exit;
	}
	$fn = $row[ "first_n" ];
	$ln = $row[ "last_n" ];
	$maint = $fn." ".$ln;
	$email = $row[ "email" ];
	$vers_id = $row[ "vers_id" ];
	$title = $row[ "title" ];
	$ta = $row[ "titlabb" ];
	$st = $row[ "status" ];
	$ml = htmlentities( $row[ "maillst" ]);
	$rb = $row[ "ref_by" ];
	$submit = dt_de( $row[ "submit" ]);
	$year = substr( $submit, 6, 4 );
	$link = $row[ "link" ];
	$descr = add_xid( $row[ 'descr' ], $xid );
	$keyw = $row[ "keywds" ];

	$ref_by = $rb;
	while( $rb > 0 ){
		$ref_by = $rb;
		if( !$id = mysql_query( "select ref_by from metatext 
			where proj_id = $rb and ( status = 'act' or status = 'fin' )" )){
			echo "Error No. 3: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( !$row = mysql_fetch_array( $id )){
			echo "Error No. 4: no rows selected";
			exit;
		}
		$rb = $row[ 'ref_by' ];
	}
	if( $ref_by == 0 ) $ref_by = $proj_id;

	head( "$t[0] '$ta'", $keyw, $xid, $xpar );
	$mid = $xpar[ 'mid' ];
	echo "<font $_fontbulk>\n";
	echo "<h3><b>$t[3]</b></h3>\n<ul>\n";
	echo "<b><a href='mailto:$email'>$maint</a>:<br><br>";
	echo "<img src='/images/more.gif' border=0>\n";
	$link = lnkxid( $link, $xid );
	echo "<a href='$link'>$title ($t[4] $vers_id, $submit)</a>";
	if( $st == 'fin' ) echo "<font color='#FF0000'> - $t[5]!</font>";
	echo "</b></ul>\n";
	echo "<h3><b>$t[6]:</b></h3>\n<ul>";
	if( !$id = mysql_query( "select distinct substring( send_by, 20 ) as m from paragraph
		where proj_id = $proj_id and send_by is not null order by m" )) {
		echo "Error No. 5: ".mysql_errno().": ".mysql_error();
		exit;
	}
	while( $row = mysql_fetch_array( $id )){
		if( !empty( $row[ "m" ] )) echo " - ".$row[ "m" ];
	}
	echo " -</ul>\n<h3><b>$t[7]</b></h3>\n<ul>$descr</ul>\n";
	echo "<h3>$t[8]</h3>\n<ul>\n";
	write_projects( $proj_id, $ref_by, 0, $xid );
	echo "</ul>\n";
	echo "<h3><b>$t[9]...</b></h3>\n<ul>\n";
	if( $st == 'fin' ){
		echo "... $t[10]</ul>\n";
		
	}
	else{
		eval( "\$x = \"$t[11]\";" );
		echo "... ".add_xid( $x, $xid );
		if( !empty( $xid )){
			if( !$id = mysql_query( "select proj_id
				from projmemb
				where memb_id = $mid
				and proj_id = $proj_id" )) {
				echo "Error No. 6: ".mysql_errno().": ".mysql_error();
				exit;
			}
			if( !mysql_fetch_row( $id )){
				echo "<form action='/member.phtml?";
				echo "xid=$xid' method='post'>";
				echo "<font $_fontfoot>";
				echo "<input type='hidden' name='pid' value=$proj_id>";
				echo "<input type='hidden' name='res' value=1>";
				echo "<input type='hidden' name='action' value='pj_ins'>";
				echo "<input type='submit' value='$t[17]'>";
				echo "</font></form>\n";
			} else {
				eval( "\$x = \"$t[18]\";" );
				echo "<br><br><b>$x</b>";
			}
		}
		echo "</ul>\n";
	}

	echo "<h3><b>$t[12]</b></h3>\n<ul>";

	# select archives versions  
	if( !$id = mysql_query( "select vers_id, submit, link
		from metatext, member
		where member.memb_id = metatext.memb_id
		and proj_id = $proj_id
		and status = 'arc'
		order by vers_id desc" )) {
		echo "Error No. 10: ".mysql_errno().": ".mysql_error();
		exit;
	}
	$noarc = 1;
	while( $row = mysql_fetch_array( $id )){
		$noarc = 0;
		$vers_id = $row[ "vers_id" ];
		$submit = dt_de( $row[ "submit" ]);
		$lk = lnkxid( $row[ 'link' ], $xid );
		echo "\n<a href='$lk'>$t[4] $vers_id, $submit</a><br>";
	}
	if( $noarc ) {
		echo "\n($t[13])";
	}

	echo "</ul>\n<h3><b>$t[14]</b></h3>\n<ul>";
	eval( "\$x = \"$t[15]\";" );
	echo "<p>$x</p>";
	echo "<p>$t[16] (c) $year $maint</p>\n";
	echo add_xid( $_x[18], $xid );
	echo "</ul></font>\n";
	foot();
}
?>
