<?php

/* DCMS Special
 * Дата последнего редактирования 12.10.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

only_reg();

$ank['id'] = $user['id'];

$set['title'] = lang('Заявки в друзья');

require_once H . 'sys/inc/thead.php';

aut();

#Навигация
echo "<div class='list-group-item-null list-group-item-grey'><small>";
echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='/id$user[id]'>$user[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Друзья');
echo "</small></div><br />";

if (isset($_GET['ok'])) {
    $ok = intval($_GET['ok']);
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$ok' LIMIT 1");

    if ($k['count'] == 0) {
        header("Location: index.php?");
        exit;
    }

    $db->query("INSERT INTO `friends` (`user`, `friends`, `time`, `i`) values('$ank[id]', '$ok', '$time', '1')");
    $db->query("INSERT INTO `friends` (`user`, `friends`, `time`, `i`) values('$ok', '$ank[id]', '$time', '1')");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$ok' AND `to` = '$user[id]' LIMIT 1");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$ok' LIMIT 1");
    $db->query("OPTIMIZE TABLE `friends`");
    $db->query("OPTIMIZE TABLE `friends_new`");

    $msg = "" . ($user['sex'] ? '' . lang('подтвердил') . '' : '' . lang('подтвердила') . '') . ", " . lang('что Вы друзья');
    $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ok', '8', '" . $db->safesql($msg) . "', '$time')");
    $_SESSION['message'] = lang('Пользователь добавлен в список Ваших друзей');
    header("Location: new.php?r=outgoing");
    exit();
}

#Подтверждение с страницы пользователя
if (isset($_GET['oks'])) {
    $oks = intval($_GET['oks']);
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$oks' LIMIT 1");

    if ($k['count'] == 0) {
        header("Location: index.php?");
        exit;
    }

    $db->query("INSERT INTO `friends` (`user`, `friends`, `time`, `i`) values('$ank[id]', '$oks', '$time', '1')");
    $db->query("INSERT INTO `friends` (`user`, `friends`, `time`, `i`) values('$oks', '$ank[id]', '$time', '1')");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$oks' AND `to` = '$user[id]' LIMIT 1");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$oks' LIMIT 1");
    $db->query("OPTIMIZE TABLE `friends`");
    $db->query("OPTIMIZE TABLE `friends_new`");

    $msg = "" . ($user['sex'] ? '' . lang('подтвердил') . '' : '' . lang('подтвердила') . '') . ", " . lang('что Вы друзья');
    $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$oks', '8', '" . $db->safesql($msg) . "', '$time')");
    $_SESSION['message'] = lang('Пользователь добавлен в список Ваших друзей');
    header("Location: /id$oks");
    exit();
}

if (isset($_GET['no'])) {
    $no = intval($_GET['no']);
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$no' LIMIT 1");

    if ($k['count'] == 0) {
        header("Location: index.php?");
        exit;
    }

    $db->query("DELETE FROM `friends` WHERE `user` = '$user[id]' AND `friends` = '$no' LIMIT 1");
    $db->query("DELETE FROM `friends` WHERE `user` = '$no' AND `friends` = '$user[id]' LIMIT 1");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");
    $db->query("OPTIMIZE TABLE `friends`");
    $db->query("OPTIMIZE TABLE `friends_new`");

    $msg = "" . ($user['sex'] ? '' . lang('отклонил') . '' : '' . lang('отклонила') . '') . " " . lang('Ваше предложение дружбы');
    $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$no', '8', '" . $db->safesql($msg) . "', '$time')");

    $_SESSION['message'] = lang('Предложение отклонено');

    header("Location: new.php?r=outgoing");
    exit();
}

#Отмена с страницы пользователя
if (isset($_GET['not'])) {
    $not = intval($_GET['not']);
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$not' LIMIT 1");

    if ($k['count'] == 0) {
        header("Location: index.php?");
        exit;
    }

    $db->query("DELETE FROM `friends` WHERE `user` = '$user[id]' AND `friends` = '$not' LIMIT 1");
    $db->query("DELETE FROM `friends` WHERE `user` = '$not' AND `friends` = '$user[id]' LIMIT 1");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$not' AND `to` = '$user[id]' LIMIT 1");
    $db->query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$not' LIMIT 1");
    $db->query("OPTIMIZE TABLE `friends`");
    $db->query("OPTIMIZE TABLE `friends_new`");

    $_SESSION['message'] = lang('Предложение отменено');

    header("Location: /id$not");
    exit();
}

if (isset($_GET['delet'])) {
    $no = intval($_GET['delet']);
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$no' LIMIT 1");

    if ($k['count'] == 0) {
        header("Location: index.php?");
        exit;
    }

    $fr = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$no') OR (`user` = '$no' AND `friends` = '$user[id]') LIMIT 1");

    if ($fr['count'] > 0) {
        $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$no', '8', '" . ($user['sex'] ? '' . lang('удалил') . '' : '' . lang('удалила') . '') . " " . lang('Вас из списка друзей') . ".', '$time')");
        $db->query("DELETE FROM `friends` WHERE `user` = '$user[id]' AND `friends` = '$no' LIMIT 1");
        $db->query("DELETE FROM `friends` WHERE `user` = '$no' AND `friends` = '$user[id]' LIMIT 1");
        $db->query("DELETE FROM `friends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");
        $db->query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");

        $db->query("OPTIMIZE TABLE `friends`");
        $db->query("OPTIMIZE TABLE `friends_new`");

        $_SESSION['message'] = lang('Пользователь успешно удален');

        header("Location: /id$no");
    }
    exit;
}
if (isset($_GET['del'])) {
    $no = intval($_GET['del']);
    $k = $db->super_query("SELECT COUNT(*) as count FROM `user` WHERE `id` = '$no' LIMIT 1");

    if ($k['count'] == 0) {
        header("Location: index.php?");
        exit;
    }

    $fr = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$no') OR (`user` = '$no' AND `friends` = '$user[id]') LIMIT 1");

    if ($fr['count'] > 0) {
        $db->query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$no', '8', '" . ($user['sex'] ? '' . lang('удалил') . '' : '' . lang('удалила') . '') . " " . lang('Вас из списка своих друзей') . ".', '$time')");
        $db->query("DELETE FROM `friends` WHERE `user` = '$user[id]' AND `friends` = '$no' LIMIT 1");
        $db->query("DELETE FROM `friends` WHERE `user` = '$no' AND `friends` = '$user[id]' LIMIT 1");
        $db->query("DELETE FROM `friends_new` WHERE `user` = '$no' AND `to` = '$user[id]' LIMIT 1");
        $db->query("DELETE FROM `friends_new` WHERE `user` = '$user[id]' AND `to` = '$no' LIMIT 1");

        $db->query("OPTIMIZE TABLE `friends`");
        $db->query("OPTIMIZE TABLE `friends_new`");

        $_SESSION['message'] = lang('Пользователь успешно удален');

        header("location:  " . htmlspecialchars($_SERVER['HTTP_REFERER']) . "");
    }
    exit;
}

$k = $db->super_query("SELECT COUNT(*) as count FROM `friends` INNER JOIN `user` ON `friends`.`friends`=`user`.`id` WHERE `friends`.`user` = '$ank[id]' AND `friends`.`i` = '1' AND `user`.`date_last`>'" . (time() - 300) . "'");
$k_online = $k['count'];
$k = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE `user` = '$ank[id]' AND `i` = '1'");
$k_post = $k['count'];

$k_new = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `to` = '$ank[id]'");
$k_new = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `to` = '$ank[id]'");
$k_new = (($k_new['count'] == 0) ? null : "<b>$k_new[count]</b>");

echo "<div class='list-group-item-null list-group-item-grey'>";
echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";
echo "<li class='nav-item'><a class='nav-link' href='index.php'>" . lang('Друзья') . "</a></li>";
echo "<li class='nav-item'><a class='nav-link active'>" . lang('Заявки') . " $k_new</a></li>";
echo "<li class='nav-item'><a class='nav-link' href='settings.php'>" . lang('Настройки') . "</a></li>";
echo "</ul>";
echo "</div>";

$m = date('m', $time);
if (substr($m, 0, 1) == 0) {
    $m = str_replace('0', '', $m);
}
$d = date('d', $time);

$k_f = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `to` = '$user[id]' LIMIT 1");
$k_f = $k_f['count'];

$r = (isset($_GET['r'])) ? htmlspecialchars($_GET['r']) : null;
switch ($r) {
    default:
        $k_t = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `user` = '$user[id]' LIMIT 1");
        $k_t = (($k_t['count'] == 0) ? null : "$k_t[count]");

        $k_new = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `to` = '$ank[id]'");
        $k_new = (($k_new['count'] == 0) ? null : "$k_new[count]");

        echo "<div class='list-group-item-null'>";
        echo "<ul class='nav nav-pills'>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Входящие') . " $k_new</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='new.php?r=outgoing'>" . lang('Исходящие') . " $k_t</a></li>";
        echo "</ul>";
        echo "</div>";

        if ($k_f == 0) {
            $doc->NoResult();
        }

        $q = $db->query("SELECT * FROM `friends_new` WHERE `to` = '$user[id]' ORDER BY time DESC");
        while ($f = $db->get_array($q)) {
            $ank = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '" . $f['user'] . "' LIMIT 1"));

            echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
            avatar($ank['id'], '48', 'border-radius: 2px;');
            echo "</td><td class='null'>";
            echo "<small class='right grey'>" . date::timek($f['time']) . "</small>";

            echo user($ank['id']);
            echo "<br />";
            echo "<a href='new.php?ok=$ank[id]'><i class='fa fa-check fa-fw'></i> " . lang('Подтвердить') . "</a> &#183; ";
            echo "<a href='new.php?no=$ank[id]'>" . lang('Отклонить') . "</a>";
            echo "</td></tr></table>";
        }

        break;

    case 'outgoing':
        $k_t = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `user` = '$user[id]' LIMIT 1");
        $k_t = (($k_t['count'] == 0) ? null : "$k_t[count]");

        $k_new = $db->super_query("SELECT COUNT(id) as count FROM `friends_new` WHERE `to` = '$user[id]'");
        $k_new = (($k_new['count'] == 0) ? null : "$k_new[count]");

        echo "<div class='list-group-item-null'>";
        echo "<ul class='nav nav-pills'>";
        echo "<li class='nav-item'><a class='nav-link' href='new.php?'>" . lang('Входящие') . " $k_new</a></li>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Исходящие') . " $k_t</a></li>";
        echo "</ul>";
        echo "</div>";

        if ($k_t == 0) {
            $doc->NoResult();
        }
        $q = $db->query("SELECT * FROM `friends_new` WHERE `user` = '$user[id]' ORDER BY time DESC");
        while ($f = $db->get_array($q)) {
            $ank = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '" . $f['to'] . "' LIMIT 1"));

            echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class = 'icon14'>";
            avatar($ank['id'], '48', 'border-radius: 2px;');
            echo "</td><td class='null'>";
            echo "<small class='right grey'>" . date::timek($f['time']) . "</small>";

            user($ank['id']);
            echo "<br /><span class='grey'>";
            echo lang('Заявка отправлена') . " &#183; ";
            echo "<a href='new.php?no=$ank[id]'>" . lang('Отменить') . "</a>";
            echo "</span></td></tr></table>";
        }

        break;
}

require_once H . 'sys/inc/tfoot.php';
