<?php

if (! defined ( 'BLOCK_FILE' )) {
	if (function_exists ( "location" ))
		location ( "../index.php" );
	else
		die ();
	exit ();
}
global $CURGROUP;
$content = ReadCache ( 'lottery', true );
if (! $content) {
	$res = mysql_query ( "SELECT * FROM lottery_config" ) or sqlerr ( __FILE__, __LINE__ );
	$content .= "<table width=80% border=1 cellspacing=0 cellpadding=10 align=\"center\">";
	while ( $arr = mysql_fetch_assoc ( $res ) )
		$arr_config [$arr ['name']] = $arr ['value'];
	
	$who_won = explode ( "|", $arr_config ['lottery_winners'] );
	$who_won = array_unique ( $who_won );
	$lottery_winners = '';
	for($x = 0; $x < count ( $who_won ); $x ++) {
		$username = '';
		$res2 = mysql_query ( "SELECT id, username FROM users" ) or sqlerr ( __FILE__, __LINE__ );
		while ( $arr2 = mysql_fetch_assoc ( $res2 ) ) {
			if ($arr2 ['id'] == $who_won [$x]) {
				$username = WriteCurgroup ( '<a href="userdetails.php?id=' . $arr2 ['id'] . '">', '$CURGROUP["can_viewprof"]' ) . $arr2 ['username'] . WriteCurgroup ( '</a>', '$CURGROUP["can_viewprof"]' );
				$lottery_winners .= (! $lottery_winners) ? $username : ', ' . $username;
				break;
			}
		}
	}
	
	if (count ( $who_won ) > 1)
		$winners = 'Победителей';
	else
		$winners = 'Победитель';
	
	if (count ( $who_won ) > 1)
		$each = ' (Каждый)';
	else
		$each = '';
	
	$content .= "<tr><td class=\"a\" width=\"50%\">&nbsp;&nbsp;&nbsp;&nbsp;<b>Последняя лотерея</b></td><td class=\"a\" align=\"left\"><b>" . count ( $who_won ) . " победителя</b></td></tr>";
	$content .= "<tr><td class=\"b\">&nbsp;&nbsp;&nbsp;&nbsp;<b>Последние победители</b></td><td class=\"b\" align=\"left\"><b>" . $lottery_winners . "</b></td></tr>";
	$content .= "<tr><td class=\"a\">&nbsp;&nbsp;&nbsp;&nbsp;<b>Каждый победитель получил</b></td><td class=\"a\" align=\"left\"><b>" . mksize ( $arr_config ['lottery_winners_amount'] ) . "</b></td></tr>";
	$content .= "<tr><td class=\"b\">&nbsp;&nbsp;&nbsp;&nbsp;<b>Дата последнего розыгрыша</b></td><td class=\"b\" align=\"left\"><b>" . $arr_config ['lottery_winners_time'] . "</b></td></tr>";
	$content .= WriteCurgroup ( "<tr><td colspan=2 style='border: none;'><center><font size=\"2\" color=\"red\"><b>Хотите принять участие в розыгрыше? Посетите страничку <a href=\"tickets.php\">лотереи</a></b></font></center></td>", '$CURGROUP["can_playgames"] && $CURUSER' );
	$content .= "</tr></table>";
	$content = WriteCache ( $content );
}
?>