<?

require "include/bittorrent.php";

dbconn ();

validcan ( false, 'can_admin' );

$search = trim ( $_GET ['search'] );
$group = $_GET ['group'];
global $ALLGROUPS, $CURGROUP, $tracker_lang;
if ($group == '-' || ! is_valid_user_group ( $group ))
	$group = '';

if ($search != '' || $group) {
	$query = "username LIKE '%" . sqlwildcardesc ( "$search" ) . "%' AND status='confirmed'";
	if ($search)
		$q = "search=" . htmlspecialchars_uni ( $search );
} else {
	$letter = trim ( $_GET ["letter"] );
	if (strlen ( $letter ) > 1)
		die ();
	
	if ($letter != "" && strpos ( "abcdefghijklmnopqrstuvwxyz", $letter ) === false)
		$letter = "a";
	$query = ($letter != "" ? "username LIKE '$letter%' AND " : "") . "status='confirmed'";
	$q = "letter=$letter";
}

if (is_valid_user_group ( $group )) {
	$query .= " AND `group` = $group";
	$q .= ($q ? "&amp;" : "") . "`group`=$group";
}
stdhead ( "Пользователи с пожертвованиями" );

begin_frame ( "Пользователи с пожертвованиями" );

print ( "<center><form method=\"get\" action=\"donatedusers.php\">\n" );
print ( "Поиск: <input type=\"text\" size=\"30\" name=\"search\" value=\"" . htmlspecialchars_uni ( $search ) . "\">\n" );
print ( "<select name=\"group\">\n" );
print ( "<option value=\"-\">(Все уровни)</option>\n" );
foreach ( $ALLGROUPS as $id => $group )
	print ( "<option value=\"$id\">" . $tracker_lang [$group ['name']] . "</option>\n" );
print ( "</select>\n" );
print ( "<input type=\"submit\" value=\"Вперед\">\n" );
print ( "</form>\n" );

print ( "<p>\n" );

for($i = 97; $i < 123; ++ $i) {
	$l = chr ( $i );
	$L = chr ( $i - 32 );
	if ($l == $letter)
		print ( "<b>$L</b>\n" );
	else
		print ( "<a href=\"doantedusers.php?letter=$l\"><b>$L</b></a>\n" );
}

print ( "</p>\n" );

$page = $_GET ['page'];
$perpage = 100;

$res = sql_query ( "SELECT COUNT(*) FROM users WHERE donated > 0 AND " . ($query ? $query : "1=1") ) or sqlerr ( __FILE__, __LINE__ );
$arr = mysql_fetch_row ( $res );
$pages = floor ( $arr [0] / $perpage );
if ($pages * $perpage < $arr [0])
	++ $pages;

if ($page < 1)
	$page = 1;
else if ($page > $pages)
	$page = $pages;

for($i = 1; $i <= $pages; ++ $i)
	if ($i == $page)
		$pagemenu .= "<b>$i</b>\n";
	else
		$pagemenu .= "<a href=\"donatedusers.php?$q&page=$i\"><b>$i</b></a>\n";

if ($page == 1)
	$browsemenu .= "<b>&lt;&lt; Пред</b>";
else
	$browsemenu .= "<a href=\"donatedusers.php?$q&page=" . ($page - 1) . "\"><b>&lt;&lt; Пред</b></a>";

$browsemenu .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

if ($page == $pages)
	$browsemenu .= "<b>След &gt;&gt;</b>";
else
	$browsemenu .= "<a href=\"donatedusers.php?$q&page=" . ($page + 1) . "\"><b>След &gt;&gt;</b></a>";

print ( "<p>$browsemenu<br />$pagemenu</p>" );

$offset = ($page * $perpage) - $perpage;

$res = sql_query ( "SELECT u.*, c.name, c.flagpic FROM users AS u LEFT JOIN countries AS c ON c.id = u.country WHERE donated > 0 AND " . ($query ? $query : "1=1") . " ORDER BY username LIMIT $offset,$perpage" ) or sqlerr ( __FILE__, __LINE__ );
$num = mysql_num_rows ( $res );

print ( "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\" width=\"100%\">\n" );
print ( "<tr><td class=\"colhead\" align=\"left\" width=\"33%\">Имя</td><td class=\"colhead\" width=\"5%\">Рейтинг</td><td class=\"colhead\" width=\"5%\">Пожертвований</td><td class=\"colhead\" width=\"5%\">Пол</td><td class=\"colhead\" align=\"left\" width=\"10%\">Уровень</td><td class=\"colhead\" width=\"7%\">Страна</td><td class=\"colhead\">Удалить пожертвование</td><td class=\"colhead\">Повысить/Понизить</td></tr>\n" );
for($i = 0; $i < $num; ++ $i) {
	$arr = mysql_fetch_assoc ( $res );
	if ($arr ['country'] > 0) {
		$country = "<td style=\"padding: 0px\" align=\"center\"><img src=\"pic/flag/$arr[flagpic]\" alt=\"$arr[name]\" title=\"$arr[name]\"></td>";
	} else
		$country = "<td align=\"center\">---</td>";
	if ($arr ["downloaded"] > 0) {
		$ratio = number_format ( $arr ["uploaded"] / $arr ["downloaded"], 2 );
		if (($arr ["uploaded"] / $arr ["downloaded"]) > 100)
			$ratio = "100+";
		$ratio = "<font color=\"" . get_ratio_color ( $ratio ) . "\">$ratio</font>";
	} else if ($arr ["uploaded"] > 0)
		$ratio = "Inf.";
	else
		$ratio = "------";
	
	if ($arr ["gender"] == "1")
		$gender = "<img src=\"" . $pic_base_url . "male.gif\" alt=\"Парень\" style=\"margin-left: 4pt\">";
	elseif ($arr ["gender"] == "2")
		$gender = "<img src=\"" . $pic_base_url . "female.gif\" alt=\"Девушка\" style=\"margin-left: 4pt\">";
	print ( "<tr><td align=\"left\">" . ($CURGROUP ['can_viewprof'] ? "<a href=\"userdetails.php?id=$arr[id]\"><b>" . get_user_group_color ( $arr ["group"], $arr ["username"] ) . "</b></a>" : "<b>" . get_user_group_color ( $arr ["group"], $arr ["username"] ) . "</b>") . ($arr ["donated"] > 0 ? "<img src=\"pic/star.gif\" border=\"0\" alt=\"Донор\">" : "") . "</td>" . "<td>$ratio</td><td><center>$arr[donated]</center></td><td>$gender</td>" . "<td align=\"left\">" . get_user_group_name ( $arr ["group"] ) . "</td>$country<form method=\"post\" action=\"donusdel.php\"><input type=\"hidden\" value=\"$arr[username]\" name=\"dud\">\n<input type=\"hidden\" value=\"1\" name=\"raz\">\n<td><input type=\"submit\" value=\"Удалить пожертвование\" name=\"submit\"></td></form><form method=\"post\" action=\"userdetails.php?id=$arr[id]\"><td><input type=\"hidden\" value=\"$arr[id]\" name=\"id\">\n<input type=\"submit\" value=\"Повысить/Понизить\" name=\"submit\"></td></form></tr>\n" );
}
print ( "</table>\n" );

print ( "<p>$pagemenu<br />$browsemenu</p></center>" );
end_frame ();
stdfoot ();
die ();

?>