<?php

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

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

#Константа облегчает ситуацию с перемещением папки в другие разделы сайта, меньше надо будет путь менять
#Путь к папке форума
define('DIR_FORUM', '/modules/forum/');
define('DIR_FORUM_FILES', '/modules/forum/files/');

$menu = (isset($_GET['menu'])) ? htmlspecialchars($_GET['menu']) : null;

switch ($menu) {
    default:

        $set['title'] = (isset($_GET['create_forum'])) ? 'Форум - создание подфорума' : 'Форум';

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

        aut();

        if (isset($_GET['create_forum'])) {
            require_once 'action/create_forum.php'; #Создание подфорума.
        } elseif (isset($_GET['edit_forum'])) {
            require_once 'action/edit_forum.php'; #Редактирование подфорума.
        }

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

        $set['title'] = (isset($_GET['create_forum'])) ? '' . lang('Форум - Создание подфорума') . '' : '' . lang('Форум') . '';

        $q = $db->super_query("SELECT COUNT(*) as count FROM forum");
        $k_post = $q['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

        if (isset($user)) {
            $q = $db->super_query("SELECT COUNT(*) as count FROM `f_journal` WHERE `read` = '0' AND `id_user` = '$user[id]'");
            $new_journal = '+' . $q['count'] . "";
            $q = $db->super_query("SELECT COUNT(*) as count FROM `f_journal` WHERE `id_user` = '$user[id]'");
            $f_journal = $q['count'];
            $q = $db->super_query("SELECT COUNT(*) as count FROM `forum_themes` WHERE `id_user` = '$user[id]'");
            $my_themes = (isset($user)) ? $q['count'] : NULL;
            unset($q);
        }

        $q = $db->super_query("SELECT COUNT(*) as count FROM `forum_themes` WHERE `time` > '" . (time() - 60 * 60 * 24) . "'");
        $new_themes = $q['count'];
        $q = $db->super_query("SELECT COUNT(*) as count FROM forum_reports");
        $reports = $q['count'];

        #Навигация
        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='#forum_add' href='" . DIR_FORUM . "?create_forum'><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' href='/?users_set'><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 data-toggle='modal' data-target='#forum_add' href='" . DIR_FORUM . "?create_forum'><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>";
        echo "</small></div><br />";

        #Всплывающее меню
        echo "<div class='modal fade' id='forum_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 (access('forum_for_create')) {
            $q = $db->super_query("SELECT MAX(`number`) as number FROM forum");
            $next_number = $q['number'] + 1;

            echo "<form class='list-group-item' style='margin-top: -1px;' action='" . DIR_FORUM . "?create_forum' method='post'>";

            $doc->Input('name', 'Название подфорума');
            $doc->Textarea('description', 'Описание подфорума');
            $doc->Input('icon', 'Иконка', null, "folder");
            $doc->Input('number', 'Позиция', null, "$next_number");

            echo lang('Доступ') . ":<br />";
            echo "<label class='custom-control custom-radio'><input id='access0' name='access' type='radio' value='0' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span> <span class='custom-control-description'>" . lang('Все') . "</span></label><br />";
            echo "<label class='custom-control custom-radio'><input id='access1' name='access' type='radio' value='1' class='custom-control-input'><span class='custom-control-indicator'></span> <span class='custom-control-description'>" . lang('Только администраторы') . "</span></label><br />";
            echo "<label class='custom-control custom-radio'><input id='access2' name='access' type='radio' value='2' class='custom-control-input'><span class='custom-control-indicator'></span> <span class='custom-control-description'>" . lang('Администраторы + модераторы') . "</span></label>";

            echo "<hr>";

            $doc->Checkbox('output', 'Список разделов', 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";

        #Меню форума
        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='tag tag-info right'>$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='tag tag-info right'>$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='tag tag-info right'>$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='tag tag-info right'>$reports</span></a>\n";
        }
        echo "</div>\n";
        echo "</div>\n";
        echo "</div>\n";
        echo "</div>\n";
        #---------------------#

        if ($k_post == 0) {
            $doc->NoResult();
        } else {
            $forums = $db->query('SELECT * FROM `forum` ORDER BY `number` ASC LIMIT ' . $start . ', ' . $set['p_str']);

            while ($forum = mysqli_fetch_object($forums)) {
                if ($forum->access == 0 || ($forum->access == 1 && $user['group_access'] > 7) || ($forum->access == 2 && $user['group_access'] > 2)) {
                    $q = $db->super_query("SELECT COUNT(*) as count FROM `forum_razdels` WHERE `id_forum` = '$forum->id'");
                    $count_razdels = $q['count'];
                    $q = $db->super_query("SELECT COUNT(*) as count FROM `forum_themes` WHERE `id_forum` = '$forum->id'");
                    $count_themes = $q['count'];

                    echo "<div class='list-group-item-null list-group-item-grey dop'>\n";
                    echo "<a href='" . DIR_FORUM . "$forum->id/'><i class='fa fa-$forum->icon fa-fw'></i> " . toOutput($forum->name, 1, 1, 0, 0, 0) . "</a> &nbsp; " . (access('forum_for_edit') ? "<a href='" . DIR_FORUM . "?edit_forum=$forum->id' class='hint--right' data-hint='" . lang('Редактировать') . "'><i class='fa fa-edit'></i></a>" : null) . " <span class='tag tag-info right' style='float: right;'>$count_razdels / $count_themes</span><br />";

                    #Выводим описание подфорума и разделы (если включены)
                    if ($forum->description != NULL && $forum->output == 0) {
                        echo "<small style='color: #9197a3;'>" . toOutput($forum->description, 1, 1, 0, 1, 1) . "</small>\n";
                    } elseif ($forum->output == 1 && $count_razdels > 0) {
                        $razdels = $db->query('SELECT `id`, `name`, `icon` FROM `forum_razdels` WHERE `id_forum` = ' . $forum->id);

                        while ($razdel = mysqli_fetch_object($razdels)) {
                            echo " &nbsp; <a style='color:grey;' href='" . DIR_FORUM . "$forum->id/$razdel->id/'> <i class='fa fa-$razdel->icon fa-fw'></i> " . toOutput($razdel->name) . "</a><br />\n";
                        }
                    }

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

            if ($k_page > 1) {
                str('?', $k_page, $page);
            }
        }

        break;
    case 'forum':
        require_once 'view/forum.php'; #Подфорум.
        break;
    case 'razdel':
        require_once 'view/razdel.php'; #Раздел.
        break;
    case 'theme':
        require_once 'view/theme.php'; #Тема.
        break;
    case 'who':
        require_once 'view/who.php'; #Кто в теме.
        break;
    case 'files':
        require_once 'view/files_theme.php'; #Файлы темы.
        break;
    case 'add_file':
        require_once 'action/add_file_post.php'; #Добавление файла.
        break;
    case 'reports':
        require_once 'view/reports.php'; #Жалобы на темы.
        break;
    case 'new_themes':
        require_once 'view/new_themes.php'; #Новые темы.
        break;
    case 'my_themes':
        require_once 'view/my_themes.php'; #Новые темы.
        break;
    case 'search':
        require_once 'view/search.php'; #Поиск по форуму.
        break;
}

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

