<?php

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

$forum = mysql_fetch_object(mysql_query('SELECT * FROM `forum` WHERE `id` = ' . intval($_GET['forum'])));

if (!$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_razdel'])) ? toOutput($forum->name, 1, 1, 0, 0, 0) . ' - Создание раздела' : 'Подфорум - ' . toOutput($forum->name, 1, 1, 0, 0, 0);

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

    aut();

    if (isset($_GET['create_razdel'])) {
        require_once 'action/create_razdel.php'; #Создание раздела.
    } elseif (isset($_GET['edit_razdel'])) {
        require_once 'action/edit_razdel.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 (access('forum_for_create')) {
                echo "<a data-toggle='modal' data-target='#add_razdel'><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 (access('forum_for_create')) {
                echo "<a href='" . DIR_FORUM . "$forum->id/create_razdel.html'><span title='" . lang('Создать раздел') . "'><i class='fa fa-plus fa-fw'></i></span></a>";
            }
            echo "<a data-toggle='modal' data-target='#menu_forum' href='/?users_set'><span style='padding-left: 10px;' title='" . lang('Выберите действие') . "'><i class='fa fa-ellipsis-v fa-fw'></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 toOutput(utf8_substr($forum->name, 20));
    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='add_razdel' 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 (access('forum_for_create')) {
        $next_number = mysql_result(mysql_query('SELECT MAX(`number`) FROM `forum_razdels` WHERE `id_forum` = ' . $forum->id), 0) + 1;

        echo "<form action='" . DIR_FORUM . "$forum->id/create_razdel.html' style='margin-top: -1px;' class='list-group-item' method='post'>";
        $doc->Input('name', 'Название раздела');
        $doc->Textarea('description', 'Описание раздела');
        $doc->Input('icon', 'Иконка', null, "folder");
        $doc->Input('number', 'Позиция', null, "$next_number");

        echo lang('Могут создавать темы только') . ":<br />";
        $doc->Checkbox('type', 'Администраторы + модераторы', '1');

        echo "<hr>";

        echo lang('Вывод раздела') . ":<br />";
        $doc->Checkbox('output', 'Отображать посл. 3 темы', '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_razdels` WHERE `id_forum` = ' . $forum->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 {

        $razdels = mysql_query('SELECT * FROM `forum_razdels` WHERE `id_forum` = ' . $forum->id . ' ORDER BY `number` ASC LIMIT ' . $start . ', ' . $set['p_str']);

        while ($razdel = mysql_fetch_object($razdels)) {
            $count_themes = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_themes` WHERE `id_razdel` = ' . $razdel->id), 0);

            echo "<div class='list-group-item-null'>\n";
            echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/'><i class='fa fa-$razdel->icon fa-fw'></i> " . toOutput($razdel->name, 1, 1, 0, 0, 0) . "</a> " . (access('forum_for_edit') ? "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/edit_razdel.html' class='hint--right' data-hint='" . lang('Редактировать') . "'><i class='fa fa-pencil-square-o'></i></a>" : null) . " <span class='badge'>$count_themes</span>";

            if ($razdel->description != NULL && $razdel->output == 0) {
                echo "<br /><small style='color: #9197a3;'>" . toOutput($razdel->description, 1, 1, 0, 1, 1) . "</small>";
            } elseif ($razdel->output == 1 && $count_themes > 0) {

                $themes = mysql_query('SELECT `id`, `name`, `reason_close`, `type` FROM `forum_themes` WHERE `id_razdel` = ' . $razdel->id . ' ORDER BY `time` DESC LIMIT 3');
                while ($theme = mysql_fetch_object($themes)) {

                    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 "<br /> &nbsp; <a style='color:grey;' href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id.html'> $type</a>";
                }
            }
            echo "</div>";
            echo "<div class='hr'></div>";
        }

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