<?php
if( empty( $all ) || $all == '0' ){$all = 0;} else {$all = 1;}

#### has_sub_projects checks for sub projects ################################
function has_sub_projects( $pid ){
	if( !$sid = mysql_query( "select proj_id from metatext
		where ref_by = $pid and status = 'act'" )) {
		echo "Error No. 4: ".mysql_errno().": ".mysql_error();
		exit;
	}
	return( mysql_num_rows( $sid ) > 0 );
}

#### write_table outputs the html table #####################################
function write_table( $ta, $lk, $v, $dt, $ti, $ml, $nn, $pid, $l ){
	global $all, $_fontbulk, $xid, $t;
	echo "<tr>\n<td width=200>\n";
	echo "<font $_fontbulk>\n";
	for( $i = 0; $i < $l; $i++ ) echo "&nbsp;&nbsp; ";
	if( $l > 0 ){
		echo "<img src='/images/sub.gif' border=0 height=12 width=12>\n";
	}
	elseif( has_sub_projects( $pid )) {
		if( !$all ){
			$x = $t[16];
			echo "<a href='".lnkxid( '/?all=1', $xid );
			echo "'><img src='/images/blueplus.gif' border=0 ";
			echo "height=12 width=12 alt='[$x]'></a>\n";
		}else{
			$x = $t[17];
			echo "<a href='".lnkxid( '/', $xid );
			echo "'><img src='/images/blueminus.gif' border=0 ";
			echo "height=12 width=12 alt='[$x]'></a>\n";
		}
	}
	else {
		echo "<img src='/images/blueball.gif' border=0 height=12 width=12>\n";
	}
	echo "<b><a href='".lnkxid( $lk.'/', $xid )."'>$ta</a></b></font></td>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "<b>$v</b></font></td>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$dt</font></td>\n<td width=340>\n";
	echo "<font $_fontbulk>\n";
	echo "$ti</font></td>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$ml</font></td>\n<td align='center'>\n";
	echo "<font $_fontbulk>\n";
	echo "$nn</font></td></tr>\n";
}

#### create_qrystr creates query string with given sort order ################
function create_qrystr( $rb ){
	global $project, $submit, $title, $mlist, $mnum
	if( empty( $project ) && empty( $submit ) && empty( $title )
		&& empty( $mlist ) && empty( $mnum )) $submit = 'desc';
	$qry = "select * from metatext where ref_by = $rb and status = 'act' order by ";
	if( !empty( $project )){
		$qry .= "titlabb ";
		if( $project == 'desc' ) $qry .= "desc";
	}
	if( !empty( $submit )){
		$qry .= "submit ";
		if( $submit == 'desc' ) $qry .= "desc";
	}
	if( !empty( $title )){
		$qry .= "title ";
		if( $title == 'desc' ) $qry .= "desc";
	}
	if( !empty( $mlist )){
		$qry .= "maillst ";
		if( $mlist == 'desc' ) $qry .= "desc";
	}
	if( !empty( $mnum )){
		$qry .= "membnum ";
		if( $mnum == 'desc' ) $qry .= "desc";
	}
	return( $qry );
}

#### write_sub_projects reads recursively all sub projects ###################
function write_sub_projects( $pid, $lev ){
	if( !$sid = mysql_query( create_qrystr( $pid ))) {
		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" ];
		$ta = $row[ "titlabb" ];
		$ver = $row[ "vers_id" ];
		$dt = date_de( $row[ "submit" ]);
		$ti = $row[ "title" ];
		$lk = strtolower( $ta );
		$ml = substr( $row[ "maillst" ], 9 );
		$ml = substr( $ml, 0, strpos( $ml, '"' ));
		$nn = $row[ "membnum" ];
		write_table( $ta, $lk, $ver, $dt, $ti, $ml, $nn, $pid, $lev );
		write_sub_projects( $pid, $lev );
	}
	return 1;
}
#### M A I N #################################################################

$countfile = "counter.txt";
if( !file_exists( "../$countfile" )){
	$fp = fopen( $countfile, "w", 1 );
	fputs( $fp, "0" );
	fclose( $fp );
}
$fp = @fopen( $countfile, "r+", 1 );
$count = (int)fgets( $fp, 100 );
if(( !$all ) && empty( $project ) && empty( $submit ) && empty( $title )
	&& empty( $mlist ) && empty( $mnum )){
	$count++;
	rewind( $fp );
	fputs( $fp, $count );
}
fclose( $fp );

include( "menu.inc" );
include( "func.inc" );

$t = getmsg( 11, $lang );
if( !head( $t[0], $lang.$t[1], $xid, $xpar )) exit;

echo "<table width=100% cellpadding=6>\n<tr>\n<td width=45%>\n";
echo "<font $_fontbulk>\n";
echo add_xid( $t[2], $xid );
echo "</font></td>\n<td width=45%>\n";
echo "<font $_fontbulk>\n";
echo add_xid( $t[3], $xid );
echo "</font></td>\n<td width=10% rowspan=2 align=right valign=top bgcolor='$_bg4'>\n";
echo "<font $_fontbulk color='$_fg3'>\n";
echo "$t[4]: "; include($countfile); echo "<br>\n";
for( $i = 1; $i <= $t[20]; $i++ ){
	echo add_xid( $t[ 20+$i ], $xid )."<br>\n";
}
$maxdt = '';

# select 8 last submitted projects
if( !$id = mysql_query( "select titlabb, submit from metatext
	where status = 'act' order by submit desc limit 8" )) {
	echo "Error No. 2: ".mysql_errno().": ".mysql_error();
	exit;
}
echo "<br>$t[5]<br>\n";
while( $row = mysql_fetch_array( $id )){
	if( empty( $maxdt )) $maxdt = dt_de( $row[ 'submit' ]);
	$ta = $row[ 'titlabb' ];
	echo "<a href='".lnkxid( strtolower( $ta.'/' ), $xid );
	echo "'>$ta</a><br>\n";
}

# select 8 projects with most members
if( !$id = mysql_query( "select titlabb, membnum from metatext
	where status = 'act' order by membnum desc limit 8" )) {
	echo "Error No. 3: ".mysql_errno().": ".mysql_error();
	exit;
}
echo "<br>$t[6]<br>\n";
while( $row = mysql_fetch_array( $id )){
	$ta = $row[ 'titlabb' ];
	$nn = $row[ 'membnum' ];
	echo "<a href='".lnkxid( strtolower( $ta.'/' ), $xid );
	echo "'>$ta/$nn</a><br>\n";
}

echo "</font></td></tr>\n";
echo "<tr><td colspan=2>\n";

$url = '?all=';
echo "<table width=100%>\n<tr>\n<td width=33%>\n";
echo "<font $_fontbulk><b>";
echo "$t[7] $maxdt</b></font></td>\n<td width=33% align=center>\n";
echo "<font $_fontbulk>";
echo "<input type='hidden' name='all' value='$all'>\n";
echo "<b><a href=\"$url";
if( $all ){
	echo '0';
	if( !empty( $xid )) echo "&xid=$xid";
	echo "\">$t[8]";
	$url .= '1';
} else {
	echo '1';
	if( !empty( $xid )) echo "&xid=$xid";
	echo "\" alt='text'>$t[9]";
	$url .= '0';
}
$url = lnkxid( $url, $xid );
echo "</a></b></font></td>\n<td width=33% align=right>";
echo "<font $_fontbulk>\n<a href='";
echo lnkxid( '/finished.phtml', $xid );
echo "'>$t[10]</a>";
echo "</font></td></tr></table>\n<center>\n";

echo "<table bgcolor='$_bg3' cellpadding=4>\n";
echo "<tr bgcolor='$_bg2'>\n";
echo "<td colspan=2>\n";
echo "<font $_fontbulk><b>\n";
echo "<a href=\"$url"."&amp;project=";
if( $project == 'asc' ) {echo 'desc';} else {echo 'asc';};
echo "\">$t[11]</a></b></font></td>\n<td>\n";
echo "<font $_fontbulk><b>\n";
echo "<a href=\"$url"."&amp;submit=";
if( $submit == 'asc' ) {echo 'desc';} else {echo 'asc';};
echo "\">$t[12]</b></font></td>\n<td>\n";
echo "<font $_fontbulk><b>\n";
echo "<a href=\"$url"."&amp;title=";
if( $title == 'asc' ) {echo 'desc';} else {echo 'asc';};
echo "\">$t[13]</b></font></td>\n<td align='center'>\n";
echo "<font $_fontbulk><b>\n";
echo "<a href=\"$url"."&amp;mlist=";
if( $mlist == 'asc' ) {echo 'desc';} else {echo 'asc';};
echo "\">$t[14]</b></font></td>\n<td>\n";
echo "<font $_fontbulk><b>\n";
echo "<a href=\"$url"."&amp;mnum=";
if( $mnum == 'asc' ) {echo 'desc';} else {echo 'asc';};
echo "\">$t[15]</b></font></td></tr>\n";

# select project data
if( !$id = mysql_query( create_qrystr( 0 ))) {
	echo "Error No. 6: ".mysql_errno().": ".mysql_error();
	exit;
}
while( $row = mysql_fetch_array( $id )){
	$pid = $row[ "proj_id" ];
	$ta = $row[ "titlabb" ];
	$ver = $row[ "vers_id" ];
	$dt = date_de( $row[ "submit" ]);
	$ti = $row[ "title" ];
	$lk = strtolower( $ta );
	$ml = substr( $row[ "maillst" ], 9 );
	$ml = substr( $ml, 0, strpos( $ml, '"' ));
	$nn = $row[ "membnum" ];
	write_table( $ta, $lk, $ver, $dt, $ti, $ml, $nn, $pid, 0 );
	# select sub projects
	if( $all ) write_sub_projects( $pid, 0 );
}
echo "</table>\n</td></tr></table>\n";
foot()
?>
