<?php
define('NTOP', 1);
$title = 'Админка - Управление пользователями';
require_once('../system/connect.php');
require_once('../system/core.php');
require_once('../system/function.php');
require_once('../m/head.php');
reg();
level(2);
switch($act)
	{
		default:
		echo '<div class="top"><div class="title2"><font color="#fff">Управление пользователями</font></div><div class="clear"></div></div>';
		
if (isset($_POST['sort1']))
$_SESSION['sort1'] = $_POST['sort1'] ? 1 : 0;
if (isset($_POST['sort2']))
$_SESSION['sort2'] = $_POST['sort2'] ? 1 : 0;
$sort = '`id` DESC';
if (isset($_SESSION['sort1'])) $sort = $_SESSION['sort1'] ? '`login`' : '`id`';
if (isset($_SESSION['sort2'])) $sort .= $_SESSION['sort2'] ? ' ASC' : ' DESC';

$count = mysql_num_rows(mysql_query("SELECT * FROM `".$prefix."users`"));
						if($count > 0)
							{
								$total=intval(($count-1)/$page_users)+1; 
								$page=abs(intval($_GET['page'])); 
								if(empty($page) OR $page < 0)
									{
										$page = 1; 
									}
								if($page > $total)
									{
										$page = $total; 
									}
								$past=intval($count/$page_users);  
								$start=$page*$page_users-$page_users; 
								$users = mysql_query("SELECT * FROM `".$prefix."users` ORDER BY ".$sort." LIMIT ".$start.",".$page_users."");
								while($row = mysql_fetch_array($users)) 
									{
										$start++;
	    $platforms_c = mysql_num_rows(mysql_query("SELECT * FROM `".$prefix."sait` WHERE `id_user` = '".$row['id']."'"));
				echo '<div class="navi3">'.$start.'. Логин: <strong>'.$row['login'].'</strong> (Площадок: <strong>'.$platforms_c.'</strong>)</div>';
										echo '<div class="main">';
										echo '<a href="http://'.$set['home'].'/adm_panel/users.php?act=del_user&id='.$row['id'].'"><strong>Удалить</strong></a><br/>';
										echo '</div>';
									}
								navigation($count,$page_users,$page,'http://'.$set['home'].'/adm_panel/users.php/',$total);
							}
else
							{
				echo '<div class="error">Пока нет ни одного пользователя</div>';
							}


// Вывод меню сортировки
echo '<div class="main"><form action="users.php" method="post"><b>Сортировка: </b><select name="sort1"><option value="0"' . (!$_SESSION['sort1'] ? ' selected="selected"' : '') . '>по ID</option><option value="1"' . ($_SESSION['sort1'] ?
            ' selected="selected"' : '') . '>по логину</option></select> &amp; <select name="sort2"><option value="0"' . (!$_SESSION['sort2'] ? ' selected="selected"' : '') . '>по убыванию</option><option value="1"' . ($_SESSION['sort2'] ?
            ' selected="selected"' : '') . '>по возрастанию</option></select><input type="submit" value="&gt;&gt;"/></form></div>';
          break;

		case 'del_user':
		$user_isset = mysql_query("SELECT * FROM `".$prefix."users` WHERE `id` = '".$id."' LIMIT 1");
		$user_isset_c = mysql_num_rows($user_isset);
		if($user_isset_c > 0)
			{
				$user = mysql_fetch_array($user_isset );
				echo '<div class="top"><div class="title2"><font color="#fff">Удаление пользователя <strong>'.$user['login'].'</strong></font></div><div class="clear"></div></div>';
				if(!isset($_GET['ok']))
					{
						echo '<div class="main">';
						echo 'Вы действительно хотите удалить пользователя <strong>'.$user['login'].'</strong> из Топ-Рейтинга ?<br/>';
						echo '<a href="http://'.$set['home'].'/adm_panel/users.php?act=del_user&id='.$id.'&ok">Да</a> | <a href="http://'.$set['home'].'/adm_panel/index.php">Нет</a><br/>';
						echo '</div>';
					}
				else
				{
					$platforms = mysql_query("SELECT * FROM `".$prefix."sait` WHERE `id_user` = '".$id."' LIMIT 1");
					while($sait = mysql_fetch_array($platforms))
						{
							$del_hosts = mysql_query("DELETE FROM `".$prefix."hosts` WHERE `id_sait` = '".$platforms['id']."'");
							$del_hits = mysql_query("DELETE FROM `".$prefix."hits` WHERE `id_sait` = '".$platforms['id']."'");
							$del_online = mysql_query("DELETE FROM `".$prefix."sait_online` WHERE `id_sait` = '".$platforms['id']."'");
							$del_reputation = mysql_query("DELETE FROM `".$prefix."reputation` WHERE `id_sait` = '".$platforms['id']."'");
							$del_sait_comments = mysql_query("DELETE FROM `".$prefix."sait_comments` WHERE `id_sait` = '".$platforms['id']."'");
							$del_platform = mysql_query("DELETE FROM `".$prefix."sait` WHERE `id` = '".$platforms['id']."'");
						}
					$del_user = mysql_query("DELETE FROM `".$prefix."users` WHERE `id` = '".$id."'");
					if($del_user)
						{
							echo '<div class="main">';
							echo 'Пользователь успешно удален!<br/>';
							echo '</div>';
						}
					else
						{
							echo '<div class="error">';
							echo 'В процессе удаления произошла ошибка!<br/>';
							echo '</div>';
						}
				}
			}
		else
			{
				echo '<div class="error">';
				echo 'Нет такого пользователя!<br/>';
				echo '</div>';
			}
		break;
	}

echo '<div class="main">';
echo '<a href="http://'.$set['home'].'/adm_panel/users.php">К управлению пользователями</a><br/>';
echo '<a href="http://'.$set['home'].'/adm_panel/index.php">В Админку</a><br/>';
echo '</div>';
require_once('../m/foot.php');
?>