<?php
header("Content-type:text/html; charset=utf-8");
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo '<title>JAMOB</title>';
echo '<link rel="stylesheet" href="http://'.$_SERVER['HTTP_HOST'].'http://jamob.ru/style/vesna.css" type="text/css" />';
require_once 'etc/config.inc.php';
require_once 'etc/Mybase.class.php';
$mysqli = new Mybase(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
require_once 'etc/Paging.class.php';
$paging = new Paging($mysqli);
$paging->set_link_padding(5);
$paging->set_page_size(20);
$mysqli->query('SET NAMES `utf8`');

if(empty($_GET['mod'])){
	$query_categories = $mysqli->query('SELECT * FROM `icq_status_category` ORDER BY `id`');
	echo '<div class="head"><b>Смешные ICQ статусы:</b></div><div class="katalog">
		<b><div class="link1"><a href="./new/">Новые</a></b><br/ >';
	while($category = $query_categories->fetch_array()){
		echo '<img src="../style/img/dot.gif" alt="" /> <a href="./category'.$category['id'].'/">'.$category['name'].' ('.$category['number'].')</a><br/>';
	}
	echo '</div></div><div class="link1"><b><a href="./add/">Добавить статус</a></b><br/ ><a href="/">Главная</a> / Статусы</div>';
} elseif(isset($_GET['mod']) && $_GET['mod'] == 'category' && isset($_GET['category'])){
	if(isset($_GET['type']) && ($_GET['type'] == 'minus' || $_GET['type'] == 'plus') && empty($_SESSION['vote'][$_GET['id']])){
		$id = intval($_GET['id']);
		if($_GET['type'] == 'minus'){
			$mysqli->query('UPDATE `icq_status` SET `rating` = `rating` - 1 WHERE `id` = ' . $id . ' LIMIT 1');
		} else {
			$mysqli->query('UPDATE `icq_status` SET `rating` = `rating` + 1 WHERE `id` = ' . $id . ' LIMIT 1');
		}
		$_SESSION['vote'][$id] = true;
	}
	$sort = empty($_SESSION['sort']) ? 'id' : $_SESSION['sort'];
	if($_GET['category'] == 'new'){
		$query_statuses = $paging->get_page('SELECT * FROM `icq_status` ORDER BY `'.$sort.'` DESC');
		$category['name'] = 'Новые';
	} else {
		$query_statuses = $paging->get_page('SELECT * FROM `icq_status` WHERE `category_id` = '.$_GET['category'].' ORDER BY `'.$sort.'` DESC');
		$category['name'] = $mysqli->result('SELECT `name` FROM `icq_status_category` WHERE `id` = ' . $_GET['category'] . ' LIMIT 1');
	}
	echo '<div class="head"><b>'.$category['name'].':</b></div>
			<div class="katalog">';
	while($status = $query_statuses->fetch_array()){
		echo nl2br($status['status']) . '<br/>';
		if(isset($_SESSION['vote'][$status['id']])){
			echo '<b>Рейтинг:</b> ' . $status['rating'];
		} else {
			echo '<b>Голосовать:</b> <a href="./plus'.$status['id'].'">+</a>/<a href="./minus'.$status['id'].'">-</a>';
		}
		echo '<br/ ><br/ >';
	}
	//echo '</div><div class="info">';
	echo $paging->get_page_links() . '</div>
		<div class="anketa"><a href="/"> Главная</a> / <a href="../index.php"> Статусы</a> / '.$category['name'].'</div>';
} elseif(isset($_GET['mod']) && $_GET['mod'] == 'add' && empty($_POST['status'])){
	echo '<div class="head"><b>Добавить статус:</b></div>
			<div class="katalog">
			<form action="." method="post">
			<b>Категория:</b><br/><select name="category">';
	$query_categories = $mysqli->query('SELECT * FROM `icq_status_category` ORDER BY `id`');
	while($category = $query_categories->fetch_array()){
		echo '<option value="'.$category['id'].'">'.$category['name'].'</option>';
	}
	echo '</select><br/>
		<b>Статус:</b><br/><textarea name="status"></textarea><br/>
		<input type="submit" value="Добавить"><br/>
		<small>* статусы проходят проверку</small>
		</div><div class="anketa"><a href="/"> Главная</a> / <a href="../index.php"> Статусы</a> / Добавить статус</div>';
} elseif(isset($_GET['mod']) && $_GET['mod'] == 'add' && isset($_POST['status'])){
	echo '<div class="bar"><b>Добавить статус:</b></div><div class="ads">';
	$category['id'] = intval($_POST['category']);
	$query_category = $mysqli->query('SELECT `id` FROM `icq_status_category` WHERE `id` = ' . $category['id'] . ' LIMIT 1')->num_rows;
	$status = $mysqli->real_escape_string(htmlspecialchars($_POST['status']));
	if($query_category && $status && $mysqli->query('INSERT INTO `icq_status_moder`(`category_id`,`status`) VALUES('.$category['id'].', "'.$status.'")')){
		echo 'Ваш статус успешно добавлен.<br/>Он появится в списке после проверки администратором.';
	} else {
		echo 'Ошибка при добалении статуса.<br/>Попробуйте позже.';
	}
	echo '</div><div class="anketa"><img src="http://mobsa.ru/img/ico.png" alt=""><a href="/">Главная</a> / <a href="../index.php"> Статусы</a> / Добавить статус</div>';

}

?>