<?php

require_once ("include/bittorrent.php");
dbconn ( false );
validcan ( false, 'can_admin' );
///////////////////// I N C A T \\\\\\\\\\\\\\\\\\\\\\\\\\\\
function bark($msg) {
	genbark ( $msg, $tracker_lang ['error'] );
}
if ($_GET ['incat'] == "addincat") {
	addincat ();
	exit ();
} elseif ($_GET ['incat'] == "editcat") {
	editcat ();
	exit ();
} elseif ($_GET ['incat'] == "delcat") {
	delcat ();
	exit ();
} elseif ($_GET ['incat'] == "showincat") {
	incat ();
	exit ();
} else {
	category ();
	exit ();
}
function category() {
	global $pic_base_url;
	stdhead ( "Категории" );
	begin_frame ( "Категории", "100", true );
	///////////////////// D E L E T E C A T E G O R Y \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	

	$sure = $_GET ['sure'];
	if ($sure == "yes") {
		$delid = ( int ) $_GET ['delid'];
		$query = "DELETE FROM categories WHERE id=" . sqlesc ( $delid ) . " LIMIT 1";
		$sql = sql_query ( $query );
		echo ("Категория успешно удалена! [ <a href='category.php'>Назад</a> ]");
		end_frame ();
		stdfoot ();
		die ();
	}
	$delid = ( int ) $_GET ['delid'];
	$name = htmlspecialchars_uni ( $_GET ['cat'] );
	if ($delid > 0) {
		echo ("Вы действителньо хотите удалить эту категорию? ($name) ( <strong><a href=\"" . $_SERVER ['PHP_SELF'] . "?delid=$delid&cat=$name&sure=yes\">Да</a></strong> / <strong><a href=\"" . $_SERVER ['PHP_SELF'] . "\">Нет</a></strong> )");
		end_frame ();
		stdfoot ();
		die ();
	
	}
	
	///////////////////// E D I T A C A T E G O R Y \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	$edited = $_GET ['edited'];
	if ($edited == 1) {
		$id = ( int ) $_GET ['id'];
		$cat_name = htmlspecialchars_uni ( $_GET ['cat_name'] );
		$cat_img = htmlspecialchars_uni ( $_GET ['cat_img'] );
		$cat_sort = ( int ) $_GET ['cat_sort'];
		$query = "UPDATE categories SET
name = " . sqlesc ( $cat_name ) . ",
image = " . sqlesc ( $cat_img ) . ",
sort = " . sqlesc ( $cat_sort ) . " WHERE id=" . sqlesc ( $id );
		$sql = sql_query ( $query );
		if ($sql) {
			echo ("<table class=main cellspacing=0 cellpadding=5 width=50%>");
			echo ("<tr><td><div align='center'>Ваша категория отредактирована <strong>успешно!</strong> [ <a href='category.php'>Назад</a> ]</div></tr>");
			echo ("</table>");
			end_frame ();
			stdfoot ();
			die ();
		}
	}
	
	$editid = ( int ) $_GET ['editid'];
	$name = htmlspecialchars_uni ( $_GET ['name'] );
	$img = htmlspecialchars_uni ( $_GET ['img'] );
	$sort = ( int ) $_GET ['sort'];
	if ($editid > 0) {
		echo ("<form name='form1' method='get' action='" . $_SERVER ['PHP_SELF'] . "'>");
		echo ("<table class=main cellspacing=0 cellpadding=5 width=50%>");
		echo ("<div align='center'><input type='hidden' name='edited' value='1'>Редактирование категории <strong>&quot;$name&quot;</strong></div>");
		echo ("<br />");
		echo ("<input type='hidden' name='id' value='$editid'><table class=main cellspacing=0 cellpadding=5 width=50%>");
		echo ("<tr><td>Название: </td><td align='right'><input type='text' size=50 name='cat_name' value='$name'></td></tr>");
		echo ("<tr><td>Картинка: </td><td align='right'><input type='text' size=50 name='cat_img' value='$img'></td></tr>");
		echo ("<tr><td>Сортировка: </td><td align='right'><input type='text' size=50 name='cat_sort' value='$sort'></td></tr>");
		echo ("<tr><td></td><td><div align='right'><input type='Submit' value='Редактировать'></div></td></tr>");
		echo ("</table></form>");
		end_frame ();
		stdfoot ();
		die ();
	}
	
	///////////////////// A D D A N E W C A T E G O R Y \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	global $pic_base_url;
	$add = $_GET ['add'];
	if ($add == 'true') {
		$cat_name = htmlspecialchars_uni ( $_GET ['cat_name'] );
		$cat_img = htmlspecialchars_uni ( $_GET ['cat_img'] );
		$cat_sort = ( int ) $_GET ['cat_sort'];
		$query = "INSERT INTO categories (name, image, sort) VALUE(
" . sqlesc ( $cat_name ) . ",
" . sqlesc ( $cat_img ) . ",
" . sqlesc ( $cat_sort ) . ")";
		$sql = sql_query ( $query );
		if ($sql) {
			$success = TRUE;
		} else {
			$success = FALSE;
		}
	}
	print ( "<strong>Добавить новую категорию</strong>" );
	print ( "<br />" );
	print ( "<br />" );
	echo ("<form name='form1' method='get' action='" . $_SERVER ['PHP_SELF'] . "'>");
	echo ("<table class=main cellspacing=0 cellpadding=5 width=50%>");
	echo ("<tr><td>Название: </td><td align='right'><input type='text' size=50 name='cat_name'></td></tr>");
	echo ("<tr><td>Картинка: </td><td align='right'><input type='text' size=50 name='cat_img'><input type='hidden' name='add' value='true'></td></tr>");
	echo ("<tr><td>Сортировка: </td><td align='right'><input type='text' size=50 name='cat_sort'></td></tr>");
	echo ("<tr><td></td><td><div align='right'><input type='Submit' value='Создать категорию'></div></td></tr>");
	echo ("</table>");
	if ($success == TRUE) {
		print ( "<strong>Удачно!</strong>" );
	}
	echo ("<br />");
	echo ("</form>");
	///////////////////// E X I S T I N G C A T E G O R I E S \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	

	print ( "<strong>Существующие категории</strong>" );
	print ( "<br />" );
	print ( "<br />" );
	echo ("<table class=main cellspacing=0 cellpadding=5>");
	echo ("<td>ID</td><td>Сортировка</td><td>Название</td><td>Картинка</td><td>Просмотр категории</td><td>Редактировать</td><td>Подкатегория</td><td>Удалить</td>");
	$query = "SELECT * FROM categories ORDER BY sort";
	$sql = sql_query ( $query ) or sqlerr ( __FILE__, __LINE__ );
	while ( $row = mysql_fetch_array ( $sql ) ) {
		$id = ( int ) $row ['id'];
		$sort = $row ['sort'];
		$name = $row ['name'];
		$img = $row ['image'];
		echo ("<tr><td><strong>$id</strong> </td> <td><strong>$sort</strong></td> <td><strong>$name</strong></td> <td><img src=" . $pic_base_url . "cats/" . $img . " border='0' /></td><td><div align='center'><a href='browse.php?cat=$id'><img src=" . $pic_base_url . "viewnfo.gif border='0' class=special /></a></div></td> <td><a href='category.php?editid=$id&name=$name&img=$img&sort=$sort'><div align='center'><img src=" . $pic_base_url . "pen.gif border='0' class=special /></a></div></td> <td><form method='post' action='category.php?incat=addincat'><div align='center'><input type='submit' value='Создать'><input type='hidden' value='$id' name='incat_id'></div></form></td> <td><div align='center'><a href='category.php?delid=$id&cat=$name'><img src=" . $pic_base_url . "warned2.gif border='0' class=special align='center' /></a></div></td></tr>");
	}
	echo ("</table>");
	echo ("<table class=main cellspacing=0 cellpadding=5><tr><form method=post action=category.php?incat=showincat><input type=\"submit\" value=\"Подкатегории\"></form></tr></table>");
	end_frame ();
	stdfoot ();
}
///////////////////// E X I S T I N G I N C A T \\\\\\\\\\\\\\\\\\\\\\\\\\\\
function incat() {
	global $pic_base_url;
	stdhead ( "Подкатегории" );
	begin_frame ( "Подкатегории", "100", true );
	$sql = sql_query ( "SELECT * FROM incat ORDER BY sort" ) or sqlerr ( __FILE__, __LINE__ );
	print ( "<strong>Существующие подкатегории</strong>" );
	print ( "<br />" );
	print ( "<br />" );
	echo ("<table class=main cellspacing=0 cellpadding=5>");
	echo ("<td>ID</td><td>Сортировка</td><td>Название</td><td>Название категории</td><td>Картинка</td><td>Редактировать</td><td>Удалить</td>");
	while ( $row = mysql_fetch_array ( $sql ) ) {
		$id = $row ['id'];
		$sort = $row ['sort'];
		$name = $row ['name'];
		$img = $row ['image'];
		$incat_ca = $row ['incat_cat'];
		$res = sql_query ( "SELECT name FROM categories WHERE id = $incat_ca LIMIT 1" ) or sqlerr ( __FILE__, __LINE__ );
		$rown = mysql_fetch_array ( $res );
		$incat_cat = $rown ['name'];
		echo ("<tr><td><strong>$id</strong> </td> <td><strong>$sort</strong></td> <td><strong>$name</strong></td> <td><strong>$incat_cat</strong></td> <td><img src=" . $pic_base_url . "cats/$img border='0' /></td> <td><form method=\"post\" action=\"category.php?incat=editcat\"><input type=\"hidden\" value=\"$id\" name=\"id\"><div align='center'><input type=\"submit\" value=\"Редактировать\"/></div></form></td> <td><form method=\"post\" action=\"category.php?incat=delcat\"><input type=\"hidden\" value=\"$id\" name=\"id\"><div align='center'><input type=\"submit\" value=\"Удалить\"/></div></form></td></tr>");
	}
	echo ("</table>");
	echo ("<table class=main cellspacing=0 cellpadding=5><tr><form method=post action=category.php><input type=\"submit\" value=\"Категории\"></form></tr></table>");
	end_frame ();
	stdfoot ();
}
function addincat() {
	global $pic_base_url;
	stdhead ( "Создать подкатегорию" );
	begin_frame ( "Создать подкатегорию", "100", true );
	///////////////////// A D D A N E W I N C A T \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	if ($_POST ["ok"]) {
		$incat_name = $_POST ['incat_name'];
		$incat_img = $_POST ['incat_img'];
		$incat_sort = intval ( $_POST ['incat_sort'] );
		$incat_cat = intval ( $_POST ['incat_cat'] );
		$query = "INSERT INTO incat (name, image, incat_cat, sort) VALUE(
" . sqlesc ( $incat_name ) . ",
" . sqlesc ( $incat_img ) . ",
" . sqlesc ( $incat_cat ) . ",
" . sqlesc ( $incat_sort ) . ")";
		$sql = sql_query ( $query );
		if ($sql) {
			$success = TRUE;
		} else {
			$success = FALSE;
		}
	}
	print ( "<strong>Добавить новую подкатегорию</strong>" );
	print ( "<br />" );
	print ( "<br />" );
	echo ("<form method='post' action=$sql>");
	echo ("<input type='hidden' name='incat_cat' value='$_POST[incat_id]'><input type='hidden' name='ok' value='1'>");
	echo ("<table class=main cellspacing=0 cellpadding=5 width=50%>");
	echo ("<tr><td>Название: </td><td align='right'><input type='text' size=50 name='incat_name'></td></tr>");
	echo ("<tr><td>Картинка: </td><td align='right'><input type='text' size=50 name='incat_img'><input type='hidden' name='add' value='true'></td></tr>");
	echo ("<tr><td>Сортировка: </td><td align='right'><input type='text' size=50 name='incat_sort'></td></tr>");
	echo ("<tr><td></td><td><div align='right'><input type='Submit' value='Создать подкатегорию' name='ok'></div></td></tr>");
	echo ("</table>");
	if ($success == TRUE) {
		print ( "<strong>Удачно! <a href=\"category.php?incat=showincat\">Назад!</a></strong>" );
	}
	echo ("<br />");
	echo ("</form>");
	end_frame ();
	stdfoot ();
}
function editcat() {
	global $pic_base_url;
	stdhead ( "Редактировать подкатегорию" );
	begin_frame ( "Редактировать подкатегорию", "100", true );
	///////////////////// E D I T I N C A T \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	$id = $_POST ["id"];
	if ($_POST ["ok"]) {
		$id = ( int ) $_POST ['id'];
		$incat_name = $_POST ['incat_name'];
		$incat_img = $_POST ['incat_img'];
		$incat_sort = intval ( $_POST ['incat_sort'] );
		$incat_cat = intval ( $_POST ['incat_cat'] );
		$query = "UPDATE incat SET
name = " . sqlesc ( $incat_name ) . ",
image = " . sqlesc ( $incat_img ) . ",
sort = " . sqlesc ( $incat_sort ) . ",
incat_cat = " . sqlesc ( $incat_cat ) . " WHERE id = $id";
		$sql = sql_query ( $query );
		if ($sql) {
			$success = TRUE;
			$a = "";
		} else {
			$success = FALSE;
		}
	}
	print ( "<strong>Редактировать подкатегорию</strong>" );
	print ( "<br />" );
	print ( "<br />" );
	$sqla = sql_query ( "SELECT * FROM incat WHERE id = " . sqlesc ( $_POST [id] ) ) or sqlerr ( __FILE__, __LINE__ );
	$row = mysql_fetch_array ( $sqla );
	$id = $row ['id'];
	$sort = $row ['sort'];
	$name = $row ['name'];
	$img = $row ['image'];
	$incat_cat = $row ['incat_cat'];
	$a = "<form method='post' action='$sql'>
<input type='hidden' name='id' value='$_POST[id]'>
<table class=main cellspacing=0 cellpadding=5 width=50%>
<tr><td>Название: </td><td align='right'><input type='text' size=50 name='incat_name' value='$name'></td></tr>
<tr><td>Картинка: </td><td align='right'><input type='text' size=50 name='incat_img' value='$img'></td></tr>
<tr><td>Сортировка: </td><td align='right'><input type='text' size=50 name='incat_sort' value='$sort'></td></tr>
<tr><td>ID категории: </td><td align='right'><input type='text' size=50 name='incat_cat' value='$incat_cat'></td></tr>
<tr><td></td><td><div align='right'><input type='submit' value='Редактировать' name='ok'></div></td></tr>
</table></form>";
	print ( $a );
	if ($success == TRUE) {
		print ( "<strong>Удачно! <a href=\"category.php?incat=showincat\">Назад!</a></strong>" );
	}
	echo ("<br />");
	end_frame ();
	stdfoot ();
}
function delcat() {
	global $pic_base_url;
	stdhead ( "Удалить подкатегорию" );
	begin_frame ( "Удалить подкатегорию", "100", true );
	///////////////////// D E L I N C A T \\\\\\\\\\\\\\\\\\\\\\\\\\\\
	$id = ( int ) $_POST ['id'];
	$query = "DELETE FROM incat WHERE id = $id LIMIT 1";
	$sql = sql_query ( $query );
	if ($sql) {
		$success = TRUE;
	} else {
		$success = FALSE;
	}
	if ($success == TRUE) {
		print ( "<strong>Удачно! <a href=\"category.php?incat=showincat\">Назад!</a></strong>" );
	}
	echo ("<br />");
	echo ("</form>");
	end_frame ();
	stdfoot ();
}
?>