<?php

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


$forum = mysql_fetch_object(mysql_query('SELECT * FROM `forum` WHERE `id` = ' . intval($_GET['forum'])));
$razdel = mysql_fetch_object(mysql_query('SELECT * FROM `forum_razdels` WHERE `id_forum` = ' . $forum->id . ' AND `id` = ' . intval($_GET['razdel'])));

if (!$razdel || !$forum || ($forum->access == 1 && $user['group_access'] < 8) || ($forum->access == 2 && $user['group_access'] < 3)) {
    header('Location: ' . DIR_FORUM);
    exit;
} else {
    $set['title'] = (isset($_GET['create_theme'])) ? toOutput($razdel->name) . ' - ' . lang('Создание темы') : lang('Раздел') . ' - ' . toOutput($razdel->name, 1, 1, 0, 0, 0);

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

    aut();

    if (isset($user) && isset($_GET['create_theme'])) {
        require_once 'action/create_theme.php'; #Создание темы.
    }

    if (isset($_SESSION['msg'])) {
        echo $_SESSION['msg'];
        unset($_SESSION['msg']);
    }

    if (isset($user)) {
        $new_journal = '+' . mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0) . "";
        $f_journal = mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `id_user` = ' . $user['id']), 0);
        $my_themes = (isset($user)) ? mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_themes` WHERE `id_user` = ' . $user['id']), 0) : NULL;
    }

    $new_themes = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_themes` WHERE `time` > ' . (time() - 60 * 60 * 24)), 0);
    $reports = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_reports`'), 0);

    #Навигация
    echo "<div class='list-group-item-null list-group-item-grey'><small>";
    if (isset($user)) {
        echo "<span style='float: right'>";
        #Кнопка меню действий
        if (IS_WEB) {
            if (isset($user) && $razdel->type == 0 || ($razdel->type == 1) && $user['group_access'] > 2) {
                echo "<a data-toggle='modal' data-target='#them_add'><span data-toggle='tooltip' data-placement='left' title='" . lang('Создать тему') . "'><i class='fa fa-plus fa-fw fa-lg'></i></span></a>";
            }
            echo "<a data-toggle='modal' data-target='#menu_forum'><span style='padding-left: 10px;' data-toggle='tooltip' data-placement='left' title='" . lang('Выберите действие') . "'><i class='fa fa-ellipsis-v fa-fw fa-lg'></i></span></a>";
        } else {
            if (isset($user) && $razdel->type == 0 || ($razdel->type == 1) && $user['group_access'] > 2) {
                echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/create_theme.html'><span title='" . lang('Создать тему') . "'><i class='fa fa-plus fa-fw fa-lg'></i></span></a>";
            }
            echo "<a data-toggle='modal' data-target='#menu_forum'><span style='padding-left: 10px;' title='" . lang('Выберите действие') . "'><i class='fa fa-ellipsis-v fa-fw fa-lg'></i></span></a>";
        }
        echo "</span>";
    }
    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='" . DIR_FORUM . "'>" . lang('Форум') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_FORUM . "$forum->id/'>" . toOutput(utf8_substr($forum->name, 10)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo toOutput(utf8_substr($razdel->name, 10));
    echo "</small></div><br />";

    #Всплывающее меню
    echo "<div class='modal fade' id='menu_forum' tabindex='-1' role='dialog' aria-hidden='true'>\n";
    echo "<div class='modal-dialog'>\n";
    echo "<div class='modal-content'>\n";
    echo "<div class='modal-header'>\n";
    echo "<button type='button' class='close' data-dismiss='modal' aria-label='Close'>";
    echo "<span aria-hidden='true'>&times;</span>";
    echo "<span class='sr-only'>Close</span>";
    echo "</button>";
    echo "<h5 class='modal-title'>" . lang('Меню форума') . "</h5>";
    echo "</div>";

    echo "<div style='margin-top: -1px;margin-bottom: -1px;'>";
    echo "<a href='" . DIR_FORUM . "search.html' class='list-group-item'><i class='fa fa-search fa-fw'></i> " . lang('Поиск') . "</a>\n";
    echo "<a href='" . DIR_FORUM . "new_themes.html' class='list-group-item'><i class='fa fa-file-text-o fa-fw'></i> " . lang('Новые темы') . " <span class='badge'>$new_themes</span></a>\n";

    if (isset($user)) {
        echo "<a href='" . DIR_FORUM . "my_themes.html' class='list-group-item'><i class='fa fa-file-text-o fa-fw'></i> " . lang('Мои темы') . " <span class='badge'>$my_themes</span></a>\n";
        echo "<a href='" . DIR_FORUM . "journal.php' class='list-group-item'><i class='fa fa-bell fa-fw'></i> " . lang('Уведомления') . " <span class='badge'>$f_journal $new_journal</span></a>\n";
    }

    if (access('forum_them_edit')) {
        echo "<a href='" . DIR_FORUM . "reports.html' class='list-group-item'><i class='fa fa-flag fa-fw'></i> " . lang('Жалобы на темы') . " <span class='badge'>$reports</span></a>\n";
    }
    echo "</div>\n";
    echo "</div>\n";
    echo "</div>\n";
    echo "</div>\n";

    echo "<div class='modal fade' id='them_add' tabindex='-1' role='dialog' aria-hidden='true'>\n";
    echo "<div class='modal-dialog'>\n";
    echo "<div class='modal-content'>\n";
    echo "<div class='modal-header'>\n";
    echo "<button type='button' class='close' data-dismiss='modal' aria-label='Close'>";
    echo "<span aria-hidden='true'>&times;</span>";
    echo "<span class='sr-only'>Close</span>";
    echo "</button>";
    echo "<h5 class='modal-title'>" . lang('Новая тема') . "</h5>";
    echo "</div>";

    if (isset($user) && $razdel->type == 0 || ($razdel->type == 1) && $user['group_access'] > 2) {
        echo "<form class='list-group-item' name='form' style='margin-top: -1px;' action='" . DIR_FORUM . "$forum->id/$razdel->id/create_theme.html' method='post'>";

        $doc->Input('name', 'Название темы');
        echo auto_bb("form", "description");
        $doc->Textarea('description', 'Содержание темы');

        if ($user['group_access'] > 7) {
            echo lang('Вывод темы в разделе') . ":<br />";
            $doc->Checkbox('type', 'Тема всегда вверху', 1);
        }

        echo "<hr>";

        $doc->Button('btn btn-primary btn-sm', 'create', null, 'Создать');
        $doc->Button('btn btn-secondary btn-sm', 'cancel', null, 'Отменить');

        echo "</form>";
    }

    echo "</div>\n";
    echo "</div>\n";
    echo "</div>\n";

    $k_post = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_themes` WHERE `id_razdel` = ' . $razdel->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();
    } else {

        $themes = mysql_query('SELECT * FROM `forum_themes` WHERE `id_razdel` = ' . $razdel->id . ' ORDER BY `type` DESC, `time_post` DESC LIMIT ' . $start . ', ' . $set['p_str']);

        while ($theme = mysql_fetch_object($themes)) {
            $creater = mysql_fetch_object(mysql_query('SELECT `id`, `nick` FROM `user` WHERE `id` = ' . $theme->id_user));
            $count_posts = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_posts` WHERE `id_theme` = ' . $theme->id), 0);
            $hide = (access('forum_post_ed')) ? NULL : '`hide` = "0" AND';
            $last_post = mysql_fetch_object(mysql_query('SELECT `id`, `hide`, `id_user`, `time` FROM `forum_posts` WHERE ' . $hide . ' `id_theme` = ' . $theme->id . ' ORDER BY `id` DESC'));

            if ($last_post && $last_post->id_user != 0) {
                $who = mysql_fetch_object(mysql_query('SELECT `id`, `nick` FROM `user` WHERE `id` = ' . $last_post->id_user));
                $who_id = $who->id;
                $who_nick = $who->nick;
            } elseif ($last_post && $last_post->id_user == 0) {
                $who_id = 0;
                $who_nick = lang('Система');
            }

            if ($theme->reason_close != NULL) {
                $type = "<i class='fa fa-file-text fa-fw'></i> " . toOutput($theme->name, 1, 1, 0, 0, 0) . " <i class='fa fa-lock fa-fw'></i>";
            } elseif ($theme->type == 1) {
                $type = "<i class='fa fa-file-text fa-fw'></i> " . toOutput($theme->name, 1, 1, 0, 0, 0) . " <i class='fa fa-thumb-tack fa-fw'></i>";
            } else {
                $type = "<i class='fa fa-file-text fa-fw'></i> " . toOutput($theme->name, 1, 1, 0, 0, 0) . "";
            }

            echo "<div class='list-group-item-null'>\n";
            echo "<span style='float:right;'><i class='fa fa-comments fa-fw'></i> <b>$count_posts</b></span>";

            echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id.html'>$type</a><br />";

            if ($last_post) {
                echo user_one($theme->id_user);
                echo "  &nbsp;<small style='color: #9197a3;' class='hint--right' data-hint='" . date::time($theme->time) . "'>" . date::timek($theme->time) . "</small>";
            } else {
                $continue = (mb_strlen($theme->description) > 150) ? '...' : NULL;
                echo "<span style='color: #9197a3;'>";
                echo toOutput(mb_substr($theme->description, 0, 150), 1, 1, 0, 1, 1) . $continue;
                echo "</span>";
            }
            echo "</div>";
            echo "<div class='hr'></div>";
        }

        if ($k_page > 1) {
            str(DIR_FORUM . '' . $forum->id . '/' . $razdel->id . '/', $k_page, $page);
        }
    }
}
