<?php
if (! defined ( "ADMIN_FILE" ))
	die ( "Illegal File Access" );

function MTitles() {
	global $admin_file, $pic_base_url;
	begin_frame ( "Звания" );
	?><table width='100%' align='center'>
	<tr>
		<td>Название</td>
		<td>Нужно торрентов</td>
		<td>Нужно комментариев</td>
		<td>Должен загрузить(ГБ)</td>
		<td>Должен скачать(ГБ)</td>
		<td>Необходимо бонусов</td>
		<td>Пользователей с этим званием</td>
		<td>Редакт.</td>
		<td>Удал.</td>
	</tr><?
	$res = sql_query ( "SELECT mt.*, (SELECT COUNT(*) FROM users WHERE users.titleid=mt.id) AS ucount FROM mtitles AS mt ORDER BY mt.torrents, mt.comments, mt.uploaded, mt.downloaded, mt.bonus" ) or sqlerr ( __FILE__, __LINE__ );
	while ( $row = mysql_fetch_array ( $res ) ) {
		$ucount = $row ['ucount'];
		$title = htmlspecialchars_uni ( $row ['title'] );
		$torrents = $row ['torrents'];
		$comments = $row ['comments'];
		$uploaded = $row ['uploaded'];
		$downloaded = $row ['downloaded'];
		$bonus = $row ['bonus'];
		?><tr>
		<td><?=$title?></td>
		<td><?=$torrents?></td>
		<td><?=$comments?></td>
		<td><?=$uploaded?></td>
		<td><?=$downloaded?></td>
		<td><?=$bonus?></td>
		<td><?=$ucount?></td>
		<td><a href="<?=$admin_file?>.php?op=AddMTitle&id=<?=$row ['id']?>"><img
			src="<?=$pic_base_url?>pen.gif"></a></td>
		<td><a href="<?=$admin_file?>.php?op=DelMTitle&id=<?=$row ['id']?>"><img
			src="<?=$pic_base_url?>warned2.gif"></a></td>
	</tr><?
	}
	print ( "</table><table width='100%' align='center'><tr><td align='center'><a href=\"" . $admin_file . ".php?op=AddMTitle&id=" . $row ['id'] . "\">Добавить звание</a></td></tr></table>" );
}
function AddMTitle() {
	global $admin_file;
	if (! $_POST ['title'] || ! isset ( $_POST ['torrents'] ) || ! isset ( $_POST ['comments'] ) || ! isset ( $_POST ['uploaded'] ) || ! isset ( $_POST ['downloaded'] ) || ! isset ( $_POST ['bonus'] )) {
		$id = intval ( $_GET ['id'] );
		$row = @mysql_fetch_array ( @sql_query ( "SELECT * FROM mtitles WHERE id=" . $id ) );
		$title = htmlspecialchars_uni ( $row ['title'] );
		$torrents = $row ['torrents'];
		$comments = $row ['comments'];
		$uploaded = $row ['uploaded'];
		$downloaded = $row ['downloaded'];
		$bonus = $row ['bonus'];
		if (! $row)
			begin_frame ( "Добавление звания" );
		else
			begin_frame ( "Редактирование звания" );
		?><form action='<?=$admin_file?>.php?op=AddMTitle' method='post'>
	<table width='100%'>
		<tr>
			<td>Название</td>
			<td><input type='text' name='title' value='<?=$title?>'></td>
		</tr>
		<tr>
			<td>Нужно торрентов</td>
			<td><input type='text' name='torrents' value='<?=$torrents?>'></td>
		</tr>
		<tr>
			<td>Нужно комментариев</td>
			<td><input type='text' name='comments' value='<?=$comments?>'></td>
		</tr>
		<tr>
			<td>Должен загрузить(ГБ)</td>
			<td><input type='text' name='uploaded' value='<?=$uploaded?>'></td>
		</tr>
		<tr>
			<td>Должен скачать(ГБ)</td>
			<td><input type='text' name='downloaded' value='<?=$downloaded?>'></td>
		</tr>
		<tr>
			<td>Необходимо бонусов</td>
			<td><input type='text' name='bonus' value='<?=$bonus?>'></td>
		</tr>
		<tr>
			<td></td>
			<td><input type='submit' value='Далее'></td>
		</tr>
	</table>
	<input type='hidden' name='id' value='<?=$id?>'></form><?
	} else {
		$title = sqlesc ( htmlspecialchars_uni ( $_POST ['title'] ) );
		$torrents = ( int ) $_POST ['torrents'];
		$comments = ( int ) $_POST ['comments'];
		$uploaded = ( int ) $_POST ['uploaded'];
		$downloaded = ( int ) $_POST ['downloaded'];
		$bonus = ( int ) $_POST ['bonus'];
		$id = ( int ) $_POST ["id"];
		if ($id)
			sql_query ( "UPDATE mtitles SET title=$title, torrents=$torrents, comments=$comments, uploaded=$uploaded, downloaded=$downloaded, bonus=$bonus WHERE id=" . $id ) or sqlerr ( __FILE__, __LINE__ );
		else
			sql_query ( "INSERT INTO mtitles (title, torrents, comments, uploaded, downloaded, bonus) VALUE($title, $torrents, $comments, $uploaded, $downloaded, $bonus)" ) or sqlerr ( __FILE__, __LINE__ );
		location ( $admin_file . '.php?op=MTitles' );
	}
}
function DelMTitle() {
	global $admin_file;
	$id = ( int ) $_GET ['id'];
	begin_frame ( "Удаление звания", 100 );
	$sure = $_GET ['sure'];
	if (! $sure)
		print ( "Вы действительно хотите удалить это звание? <a href='" . $_SERVER ['PHP_SELF'] . "?id=" . $id . "&op=DelMTitle&sure=yes'>Да</a> | <a href='" . $_SERVER ['PHP_SELF'] . "?op=MTitles'>Нет</a>" );
	else {
		sql_query ( "DELETE FROM mtitles WHERE id=" . $id );
		location ( $admin_file . ".php?op=MTitles" );
	}
}
switch ($op) {
	case "AddMTitle" :
		AddMTitle ();
		break;
	case "DelMTitle" :
		DelMTitle ();
		break;
	case "MTitles" :
		MTitles ();
		break;
}
?>