<?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 = mysql_fetch_array(mysql_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'])) {
            mysql_query("DELETE FROM `public_coint` WHERE `id_public` = '" . intval($_GET['post']) . "'");
            mysql_query("DELETE FROM `public` WHERE `id` = '" . intval($_GET['post']) . "'");
            mysql_query("OPTIMIZE TABLE `public_coint`");
            mysql_query("OPTIMIZE TABLE `public`");
            msg('Сообщество удалено');
            header("Location: user.php?id=" . $ank['id'] . "&ank=ank");
        }

        aut();
        err();


        #Навигация
        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 = mysql_fetch_array(mysql_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 {
            $doc->Link('list-group-item-null list-group-item-info', "/public.php?add=add", 'plus', 'Создать сообщество');
            $doc->Link('list-group-item-null list-group-item-info', "/modules/public/user.php?id=" . $user['id'] . "&amp;ank=my", 'user', 'Мои подписки');

            $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `public` WHERE `id_user` = '" . $ank['id'] . "'"), 0);
            $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 = mysql_query("SELECT * FROM `public` WHERE `id_user` = '" . $ank['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str'] . "");
            while ($post = mysql_fetch_array($q)) {
                echo "<table class='list-group-item-null'><tr><td class='icon14'>";
                $foto = mysql_fetch_array(mysql_query("SELECT id,ras FROM `public_foto` WHERE `id_public` = '" . $post['id'] . "' AND `foto` = '1' LIMIT 1"));
                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='/modules/public/images/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>";
                }
                echo "<a href='/modules/public/index.php?id=" . $post['id'] . "'> " . $post['name'] . "</a><br />";
                $coint = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_coint` WHERE `id_public` = '" . $post['id'] . "'"), 0);
                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>";
                echo "<div class='hr'></div>";
            }

            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 = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '" . intval($_GET['id']) . ")'"));
    $set['title'] = "Подписки " . $ank['nick'];
    require_once H . 'sys/inc/thead.php';

    if (isset($_GET['dispodpis']) && $_GET['dispodpis'] == 'dispodpis' && intval($_GET['post'])) {
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "' AND `id_public` = '" . intval($_GET['post']) . "'"), 0) == 1) {
            $pubic = mysql_fetch_array(mysql_query("SELECT * FROM `public` WHERE `id` = '" . intval($_GET['post']) . "' LIMIT 1"));
            mysql_query("DELETE FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "' AND `id_public` = '" . $pubic['id'] . "'");
            mysql_query("UPDATE `public` SET `podpis` = '" . ($pubic['podpis'] - 1) . "' WHERE `id` = '" . $pubic['id'] . "' LIMIT 1");
            mysql_query("OPTIMIZE TABLE `public_coint`");
        }
        msg('Подписка деактивирована');
        header("Location: user.php?id=" . $ank['id'] . "&ank=my");
    }

    aut();
    err();

    #Навигация
    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 {
        $doc->Link('list-group-item-null list-group-item-info', "/public.php?add=add", 'plus', 'Создать сообщество');
        $doc->Link('list-group-item-null list-group-item-info', "/modules/public/user.php?id=" . $user['id'] . "&amp;ank=ank", 'user', 'Мои сообщества');

        $public = mysql_fetch_array(mysql_query("SELECT * FROM `public` WHERE `id_user` = '" . $ank['id'] . "'"));
        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "'"), 0);
        $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 = mysql_query("SELECT * FROM `public_coint` WHERE `id_user` = '" . $ank['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str'] . "");
        while ($post = mysql_fetch_array($q)) {
            $pub = mysql_fetch_array(mysql_query("SELECT * FROM `public` WHERE `id` = '" . $post['id_public'] . "' LIMIT 1"));

            echo "<table class='list-group-item-null'><tr><td class='icon14'>";
            $foto = mysql_fetch_array(mysql_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='/modules/public/images/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>";

            echo "<a href='/user/public/index.php?id=" . $pub['id'] . "'> " . $pub['name'] . "</a><br />";
            $coint = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_coint` WHERE `id_public` = '" . $pub['id'] . "'"), 0);
            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>";
            echo "<div class='hr'></div>";
        }

        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 = mysql_fetch_array(mysql_query("SELECT * FROM `public` WHERE `id` = '" . intval($_GET['id']) . ")'"));
    $set['title'] = "Топ подписчиков " . $public['name'];
    require_once H . 'sys/inc/thead.php';

    aut();
    err();
    $pp = mysql_query("SELECT * FROM `public_coint` WHERE `id_public` = '" . $public['id'] . "'  ORDER BY aktiv DESC LIMIT 60");
    while ($pro = mysql_fetch_array($pp)) {
        $uss = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id`='" . $pro['id_user'] . "' LIMIT 1"));
        if ($public['id_user'] != $uss['id']) {
            echo "<table class='list-group-item-null'><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>";
            echo "<div class='hr'></div>";
        }
    }
}

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