<?php

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

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

if (isset($_GET['ank']) && $_GET['ank'] == 'ank' && intval($_GET['id'])) {
    $ank = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '" . intval($_GET['id']) . ")'"));
    if ($user['id'] == intval($_GET['id'])) {
        $set['title'] = "Сообщества " . $ank['nick'];
        require_once H . 'sys/inc/thead.php';

        if (isset($_GET['del']) && $_GET['del'] == 'del' && intval($_GET['post'])) {
            $db->query("DELETE FROM `public_coint` WHERE `id_public` = '" . intval($_GET['post']) . "'");
            $db->query("DELETE FROM `public` WHERE `id` = '" . intval($_GET['post']) . "'");
            $db->query("OPTIMIZE TABLE `public_coint`");
            $db->query("OPTIMIZE TABLE `public`");
            msg('Сообщество удалено');
            header("Location: user.php?id=" . $ank['id'] . "&ank=ank");
        }

        aut();
        err();

        #Путь папки
        define('DIR_PUBLIC', "/modules/public/");

        #Навигация
        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='/public.php'>" . lang('Сообщества') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Сообщества') . " " . $ank['nick'];
        echo "</small></div><br />";

        if (isset($_GET['del']) && $_GET['del'] == 'delete' && intval($_GET['post'])) {
            $publ = $db->get_array($db->query("SELECT * FROM `public` WHERE `id` = '" . intval($_GET['post']) . "' LIMIT 1"));
            echo "<div class='list-group-item-null list-group-item-warning'>Подтвердите удаление сообщества " . $publ['name'] . "<br /><br />";
            echo "<a href='user.php?id=" . $user['id'] . "&amp;ank=ank&amp;del=del&amp;post=" . $publ['id'] . "' class='btn btn-success btn-sm'>Удалить</a> ";
            echo " <a href='user.php?id=" . $user['id'] . "&amp;ank=ank' class='btn btn-secondary btn-sm'>Отмена</a>";
            echo "</div>";
        }

        if (!isset($user)) {
            echo '<div class="list-group-item-null list-group-item-warning">' . lang('Авторизуйтесь пожалуйста') . '</div>';
        } else {
            echo "<div class='list-group-item-null'>";
            $doc->Link('btn btn-secondary btn-sm', "/public.php?add=add", 'plus', 'Создать сообщество');
            $doc->Link('btn btn-secondary btn-sm', "" . DIR_PUBLIC . "user.php?id=$user[id]&amp;ank=my", 'user', 'Мои подписки');
            echo "</div>";

            $k_post = $db->super_query("SELECT COUNT(*) as count FROM `public` WHERE `id_user` = '$ank[id]'");
            $k_post = $k_post['count'];
            $k_page = k_page($k_post, $set['p_str']);
            $page = page($k_page);
            $start = $set['p_str'] * $page - $set['p_str'];

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

            $q = $db->query("SELECT * FROM `public` WHERE `id_user` = '" . $ank['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str'] . "");
            while ($post = $db->get_array($q)) {
                $foto = $db->get_array($db->query("SELECT id,ras FROM `public_foto` WHERE `id_public` = '" . $post['id'] . "' AND `foto` = '1' LIMIT 1"));

                echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
                if (is_file(H . "sys/public/ava/" . $post['id'] . "." . $foto['ras'] . "")) {
                    echo "<img src='/sys/public/ava/" . $post['id'] . "." . $foto['ras'] . "' width='50'>";
                } else {
                    echo "<img src='/sys/public/ava/no_foto.jpg' width='50' />";
                }
                echo "</td><td class='null'>";
                if ($user['level'] > 2) {
                    echo "<span style='float: right;'> <a href='user.php?id=" . $user['id'] . "&amp;ank=ank&amp;del=delete&amp;post=" . $post['id'] . "' class='hint--left' data-hint='" . lang('Удалить сообщество') . "'><i class='fa fa-trash-o fa-fw'></i></a></span>";
                }
                $doc->Link2(null, "" . DIR_PUBLIC . "index.php?id=$post[id]", null, "$post[name]");
                echo "<br />";
                $coint = $db->super_query("SELECT COUNT(*) as count FROM `public_coint` WHERE `id_public` = '$post[id]'");
                $coint = $coint['count'];
                echo "<small style='color: grey;'><i class='fa fa-users fa-fw'></i> " . lang('Подписано') . ": " . sklon_text($coint, array('человек', 'человека', 'человек')) . "</small>";

                if ($post['privat'] == 1) {
                    echo "<span style='float: right;color: #808080;'><i class='fa fa-lock fa-fw'></i></span>";
                }
                echo "</td></tr></table>";
            }

            if ($k_page > 1) {
                str("user.php?id=" . $ank['id'] . "&amp;ank=ank&amp;", $k_page, $page);
            }
        }
    }
}

if (isset($_GET['ank']) && $_GET['ank'] == 'my' && intval($_GET['id'])) {
    $ank = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '" . intval($_GET['id']) . ")'"));
    $set['title'] = lang('Подписки') . " " . $ank['nick'];
    require_once H . 'sys/inc/thead.php';

    if (isset($_GET['dispodpis']) && $_GET['dispodpis'] == 'dispodpis' && intval($_GET['post'])) {
        $k = $db->super_query("SELECT COUNT(*) as count FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "' AND `id_public` = '" . intval($_GET['post']) . "'");
        if ($k['count'] == 1) {
            $pubic = $db->get_array($db->query("SELECT * FROM `public` WHERE `id` = '" . intval($_GET['post']) . "' LIMIT 1"));
            $db->query("DELETE FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "' AND `id_public` = '" . $pubic['id'] . "'");
            $db->query("UPDATE `public` SET `podpis` = '" . ($pubic['podpis'] - 1) . "' WHERE `id` = '" . $pubic['id'] . "' LIMIT 1");
            $db->query("OPTIMIZE TABLE `public_coint`");
        }
        $_SESSION['message'] = lang('Вы успешно отписались от сообщества');
        header("Location: user.php?id=" . $ank['id'] . "&ank=my");
        exit;
    }

    aut();
    err();

    #Путь папки
    define('DIR_PUBLIC', "/modules/public/");

    #Навигация
    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='/public.php'>" . lang('Сообщества') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Сообщества') . " " . $ank['nick'];
    echo "</small></div><br />";

    if (!isset($user)) {
        echo '<div class="list-group-item-null list-group-item-warning">' . lang('Авторизуйтесь пожалуйста') . '</div>';
    } else {
        echo "<div class='list-group-item-null'>";
        $doc->Link('btn btn-secondary btn-sm', "/public.php?add=add", 'plus', 'Создать сообщество');
        $doc->Link('btn btn-secondary btn-sm', "" . DIR_PUBLIC . "user.php?id=$user[id]&amp;ank=ank", 'user', 'Мои сообщества');
        echo "</div>";

        $public = $db->get_array($db->query("SELECT * FROM `public` WHERE `id_user` = '" . $ank['id'] . "'"));
        $k_post = $db->super_query("SELECT COUNT(*) as count FROM `public_coint` WHERE `id_user` = '$ank[id]'");
        $k_post = $k_post['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

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

        $q = $db->query("SELECT * FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str'] . "");
        while ($post = $db->get_array($q)) {
            $pub = $db->get_array($db->query("SELECT * FROM `public` WHERE `id` = '" . $post['id_public'] . "' LIMIT 1"));

            echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
            $foto = $db->get_array($db->query("SELECT id,ras FROM `public_foto` WHERE `id_public` = '" . $pub['id'] . "' AND `foto` = '1' LIMIT 1"));
            if (is_file(H . "sys/public/ava/" . $pub['id'] . "." . $foto['ras'] . "")) {
                echo "<img src='/sys/public/ava/" . $pub['id'] . "." . $foto['ras'] . "' width='50'/>";
            } else {
                echo "<img src='/sys/public/ava/no_foto.jpg' width='50'/>";
            }
            echo "</td><td class='null'>";

            echo "<span style='float: right;'>";
            if (isset($user) && $user['id'] != $pub['id_user']) {
                echo "<a href='user.php?id=" . $ank['id'] . "&amp;ank=my&amp;dispodpis=dispodpis&amp;post=" . $pub['id'] . "'><small style='color: grey;'>" . lang('Отписаться') . "</small></a>";
            } else {
                echo "<small style='color: grey;'>" . lang('Ваше сообщество') . "</small>";
            }
            echo "</span>";

            $doc->Link2(null, "" . DIR_PUBLIC . "index.php?id=$pub[id]", null, "$pub[name]");
            echo "<br />";
            $coint = $db->super_query("SELECT COUNT(*) as count FROM `public_coint` WHERE `id_public` = '$pub[id]'");
            $coint = $coint['count'];
            echo "<small style='color: grey;'>";
            echo "<i class='fa fa-users fa-fw'></i> " . sklon_text($coint, array('подписчик', 'подписчика', 'подписчиков'));
            echo "</small>";

            if ($pub['privat'] == 1) {
                echo "<span style='float: right;color: #808080;' class='hint--left' data-hint='" . lang('Закрытое сообщество') . "'><i class='fa fa-lock fa-fw'></i></span>";
            }
            echo "</td></tr></table>";
        }

        if ($k_page > 1) {
            str("user.php?id=" . $ank['id'] . "&amp;ank=my&amp;", $k_page, $page);
        }
    }
}

if (isset($_GET['public']) && $_GET['public'] == 'coint' && intval($_GET['id'])) {
    $public = $db->get_array($db->query("SELECT * FROM `public` WHERE `id` = '" . intval($_GET['id']) . ")'"));
    $set['title'] = lang('Топ подписчиков') . " " . $public['name'];
    require_once H . 'sys/inc/thead.php';

    aut();
    err();
    $pp = $db->query("SELECT * FROM `public_coint` WHERE `id_public` = '" . $public['id'] . "'  ORDER BY aktiv DESC LIMIT 60");
    while ($pro = $db->get_array($pp)) {
        $uss = $db->get_array($db->query("SELECT * FROM `user` WHERE `id`='" . $pro['id_user'] . "' LIMIT 1"));
        if ($public['id_user'] != $uss['id']) {
            echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
            echo avatar($uss['id'], '48', 'border-radius: 2px;');
            echo "</td><td class='null'>";
            echo user($uss['id']);
            echo "</td></tr></table>";
        }
    }
}

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