<?php defined( '_BIGNIGHT_EXEC' ) or die( 'Restricted access' );?>
<?php
	global $user;
	require_once("include/admin_profile.php");
?>
	<form name="search" method="get" action="index.php">
		<table style="padding:5px;width:400px;">
			<tr><td>Username:</td><td><input type="text" size="15" name="search"><td><td><input type="submit" name="search-submit" value="Search" value="<?php if(!empty($_GET["search"])) echo $_GET["search"]; ?>"></td></tr>
		</table>
	</form>
<?php

	$DB = DB::Open();
	$limit = get_page_limit();
	if(!empty($_GET["search"])) $search = " WHERE u.username LIKE '" . str_replace("'","''",$_GET["search"]) . "' OR u.name LIKE '" . str_replace("'","''",$_GET["search"]) . "'";
	$sql = "SELECT SQL_CALC_FOUND_ROWS u.*, r.rank, COUNT(DISTINCT e.id) AS cnt_events, COUNT(DISTINCT s.subscriber) AS cnt_subs
			FROM users u 
				LEFT JOIN 
					events e ON e.username=u.username 
				LEFT JOIN 
					subscriptions s ON u.username=s.host 
				LEFT JOIN
					ranks r ON u.permissions=r.id
				{$search} 
			GROUP BY u.username ORDER BY u.username ASC {$limit};";
	$result = $DB->qry($sql);
	$total = $DB->qry("SELECT FOUND_ROWS();",3);
	if ($DB->numrows()===0)
		echo "No users found.";
	else
		display_users($result,$total);


?>