<?php

include 'connect.php';
include 'config.php';
include 'head.php';
$link = connect_db();
list($user, $id, $ps) = check_login($link);

include 'head.php';
include 'navigator.php';

whorm(0, 'lenta');

	$do = isset($_GET['do']) ? $_GET['do'] : NULL;
	switch($do) {
	default:
	    // полная очистка
		if (isset($_GET['clean'])) {
		   mysql_query("DELETE FROM `lenta` WHERE `user` = '$user[id]'");
		   header('Location: lenta.php?' . $ref);
		}

	    // всего
		$sql_1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]'"), 0);
		$sql_2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]'"), 0);
		$sql_3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]'"), 0);

       // не прочитаные
		$new_1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'friends' AND `user` = '$user[id]'"), 0);
		$new_2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'groups' AND `user` = '$user[id]'"), 0);
		$new_3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'comments' AND `user` = '$user[id]'"), 0);

		$new_1 = ($new_1 != 0) ? '<font color="#FF0000">+' . $new_1 . '</font>' : '';
		$new_2 = ($new_2 != 0) ? '<font color="#FF0000">+' . $new_2 . '</font>' : '';
		$new_3 = ($new_3 != 0) ? '<font color="#FF0000">+' . $new_3 . '</font>' : '';

				   mysql_query("UPDATE `lenta` SET `read` = '0' WHERE `type` = 'friends' AND `user` = '$user[id]'");

		echo $div_title . us($user['id']) . ' / Лента
		     ' . $div_end . $div_left . $div_menu . '
			 <b>Друзья(' . $sql_1 . ') ' . $new_1 . '</b> |
			 <a href="lenta.php?do=group">Группы(' . $sql_2 . ') ' . $new_2 . '</a> |
			 <a href="lenta.php?do=comment">Комментарии(' . $sql_3 . ') ' . $new_3 . '</a>' . $div_end;

			if (isset($_GET['x'])) {
			   $x = my_int($_GET['x']);
			   $empty = mysql_query("SELECT `id` FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]' AND `id` = '$x' LIMIT 1");
			   if (mysql_num_rows($empty) != FALSE) {
			      mysql_query("DELETE FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]' AND `id` = '$x' LIMIT 1");
				  header('Location: lenta.php?' . $ref);
			   } else {
			      header('Location: lenta.php?' . $ref);
			   }
			}

			$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]'"), 0);
			$n = new navigator($count, $user['onp_comments'], '?');
				if ($count != FALSE) {
				   $look = mysql_query("SELECT * FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]' ORDER BY `id` DESC {$n->limit}");
				   $i = 0;
				   while($a = mysql_fetch_assoc($look)) {
					   $d = '<a href="lenta.php?x='.$a['id'].'"><img src="ico/delete.gif" alt=""/></a> ';
				       if ($i ++ % 2) echo $div_razdel . $d . $a['text'] . $div_end;
					   else echo $div_tworazdel . $d . $a['text'] . $div_end;
				   }
				   echo $n->navi();
				} else {
				  echo 'Записей не найдено!<br/>';
				}
			if ($count != FALSE) {
				echo $div_menu . '<a href="lenta.php?clean">Очистить ленту</a>' . $div_end;
			}
		echo $div_end;
	break;

	case group:
	    // всего
		$sql_1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]'"), 0);
		$sql_2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]'"), 0);
		$sql_3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]'"), 0);

       // не прочитаные
		$new_1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'friends' AND `user` = '$user[id]'"), 0);
		$new_2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'groups' AND `user` = '$user[id]'"), 0);
		$new_3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'comments' AND `user` = '$user[id]'"), 0);

		$new_1 = ($new_1 != 0) ? '<font color="#FF0000">+' . $new_1 . '</font>' : '';
		$new_2 = ($new_2 != 0) ? '<font color="#FF0000">+' . $new_2 . '</font>' : '';
		$new_3 = ($new_3 != 0) ? '<font color="#FF0000">+' . $new_3 . '</font>' : '';

				   mysql_query("UPDATE `lenta` SET `read` = '0' WHERE `type` = 'groups' AND `user` = '$user[id]'");

		echo $div_title . us($user['id']) . ' / Лента
		     ' . $div_end . $div_left . $div_menu . '
			 <a href="lenta.php?'.$ref.'">Друзья(' . $sql_1 . ') ' . $new_1 . '</a> |
			 <b>Группы(' . $sql_2 . ') ' . $new_2 . '</b> |
			 <a href="lenta.php?do=comment">Комментарии(' . $sql_3 . ') ' . $new_3 . '</a>' . $div_end;

			if (isset($_GET['x'])) {
			   $x = my_int($_GET['x']);
			   $empty = mysql_query("SELECT `id` FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]' AND `id` = '$x' LIMIT 1");
			   if (mysql_num_rows($empty) != FALSE) {
			      mysql_query("DELETE FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]' AND `id` = '$x' LIMIT 1");
				  header('Location: lenta.php?do=group');
			   } else {
			      header('Location: lenta.php?do=group');
			   }
			}

			$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]'"), 0);
			$n = new navigator($count, $user['onp_comments'], '?do=group&amp;');
				if ($count != FALSE) {
				   $look = mysql_query("SELECT * FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]' ORDER BY `id` DESC {$n->limit}");
				   $i = 0;
				   while($a = mysql_fetch_assoc($look)) {
					   $d = '<a href="lenta.php?do=group&amp;x='.$a['id'].'"><img src="ico/delete.gif" alt=""/></a> ';
				       if ($i ++ % 2) echo $div_razdel . $d . $a['text'] . '<br/>' . date('d-m-y H:i', $a['date']) . $div_end;
					   else echo $div_tworazdel . $d . $a['text'] . '<br/>' . date('d-m-y H:i', $a['date']) . $div_end;
				   }
				   echo $n->navi();
				} else {
				  echo 'Записей не найдено!<br/>';
				}
			if ($count != FALSE) {
				echo  $div_menu . '<a href="lenta.php?clean">Очистить ленту</a>' . $div_end;
			}
		echo $div_end;
	break;

	case comment:
	    // всего
		$sql_1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'friends' AND `user` = '$user[id]'"), 0);
		$sql_2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'groups' AND `user` = '$user[id]'"), 0);
		$sql_3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]'"), 0);

       // не прочитаные
		$new_1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'friends' AND `user` = '$user[id]'"), 0);
		$new_2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'groups' AND `user` = '$user[id]'"), 0);
		$new_3 = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `read` = '1' AND `type` = 'comments' AND `user` = '$user[id]'"), 0);

		$new_1 = ($new_1 != 0) ? '<font color="#FF0000">+' . $new_1 . '</font>' : '';
		$new_2 = ($new_2 != 0) ? '<font color="#FF0000">+' . $new_2 . '</font>' : '';
		$new_3 = ($new_3 != 0) ? '<font color="#FF0000">+' . $new_3 . '</font>' : '';

				   mysql_query("UPDATE `lenta` SET `read` = '0' WHERE `type` = 'comments' AND `user` = '$user[id]'");

		echo $div_title . us($user['id']) . ' / Лента
		     ' . $div_end . $div_left . $div_menu . '
			 <a href="lenta.php?'.$ref.'">Друзья(' . $sql_1 . ') ' . $new_1 . '</a> |
			 <a href="lenta.php?do=group">Группы(' . $sql_2 . ') ' . $new_2 . '</a> |
			 <b>Комментарии(' . $sql_3 . ') ' . $new_3 . '</b>' . $div_end;

			if (isset($_GET['x'])) {
			   $x = my_int($_GET['x']);
			   $empty = mysql_query("SELECT `id` FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]' AND `id` = '$x' LIMIT 1");
			   if (mysql_num_rows($empty) != FALSE) {
			      mysql_query("DELETE FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]' AND `id` = '$x' LIMIT 1");
				  header('Location: lenta.php?do=comment');
			   } else {
			      header('Location: lenta.php?do=comment');
			   }
			}

			$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]'"), 0);
			$n = new navigator($count, $user['onp_comments'], '?do=comment&amp;');
				if ($count != FALSE) {				
				   $look = mysql_query("SELECT * FROM `lenta` WHERE `type` = 'comments' AND `user` = '$user[id]' ORDER BY `id` DESC {$n->limit}");
				   $i = 0;
				   while($a = mysql_fetch_assoc($look)) {
					   $d = '<a href="lenta.php?do=comment&amp;x='.$a['id'].'"><img src="ico/delete.gif" alt=""/></a> ';
				       if ($i ++ % 2) echo $div_razdel . $d . $a['text'] . '<br/>' . date('d-m-y H:i', $a['date']) . $div_end;
					   else echo $div_tworazdel . $d . $a['text'] . '<br/>' . date('d-m-y H:i', $a['date']) . $div_end;
				   }
				   echo $n->navi();
				} else {
				  echo 'Записей не найдено!<br/>';
				}
			if ($count != FALSE) {
				echo  $div_menu . '<a href="lenta.php?clean">Очистить ленту</a>' . $div_end;
			}
		echo $div_end;
	break;

}

include 'foot.php';
?>