<?php

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

if (!access('forum_them_edit') && $user['id'] != $theme->id_user) {
    header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '.html');
    exit;
} else {
    if (isset($_POST['save'])) {
        $name = mysql_real_escape_string(trim($_POST['name']));
        $description = mysql_real_escape_string(trim($_POST['description']));
        $type = ($user['group_access'] > 7 && isset($_POST['type'])) ? 1 : 0;
        $razdel_id = intval($_POST['razdel']);
        $forum_id = mysql_fetch_object(mysql_query('SELECT `id_forum` FROM `forum_razdels` WHERE `id` = ' . $razdel_id))->id_forum;

        if (mb_strlen($name) < 3 || mb_strlen($name) > 100) {
            echo "<div class='alert alert-danger'>" . lang('В поле Название темы можно использовать от 3 до 100 символов') . ".</div>";
        } elseif (mb_strlen($description) < 3) {
            echo "<div class='alert alert-danger'>" . lang('Слишком короткое содержание темы') . ".</div>";
        } else {
            $_SESSION['message'] = lang('Изменения сохранены');
            mysql_query('UPDATE `forum_themes` SET `id_forum` = ' . $forum_id . ', `id_razdel` = ' . $razdel_id . ', `id_admin` = ' . $user['id'] . ', `name` = "' . $name . '", `description` = "' . $description . '", `type` = ' . $type . ', `time_edit` = "' . $time . '" WHERE `id` = ' . $theme->id);
            header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/' . $theme->id . '.html');
            exit;
        }
    } elseif (isset($_POST['delete']) && (access('forum_them_del'))) {
        admin_log(lang('Форум'), lang('Темы'), lang('Тема') . ' "' . $theme->name . '" ' . lang('была удалена из раздела') . ' "' . $razdel->name . '".');

        $files = mysql_query('SELECT `name` FROM `forum_post_files` WHERE `id_theme` = ' . $theme->id);

        while ($file = mysql_fetch_object($files)) {
            unlink(DIR_FORUM_FILES . $file->name);
        }

        $votes = mysql_query('SELECT `id` FROM `forum_votes` WHERE `id_theme` = ' . $theme->id);

        while ($vote = mysql_query($votes)) {
            mysql_query('DELETE FROM `forum_vote_voices` WHERE `id_vote` = ' . $vote->id);
        }
        mysql_query('DELETE FROM `forum_votes` WHERE `id_theme` = ' . $theme->id);
        mysql_query('DELETE FROM `forum_post_rating` WHERE `id_theme` = ' . $theme->id);
        mysql_query('DELETE FROM `forum_posts` WHERE `id_theme` = ' . $theme->id);
        mysql_query('DELETE FROM `forum_post_files` WHERE `id_theme` = ' . $theme->id);
        mysql_query('DELETE FROM `forum_votes_var` WHERE `id_theme` = ' . $theme->id);
        mysql_query('DELETE FROM `forum_themes` WHERE `id` = ' . $theme->id);
        $_SESSION['message'] = lang('Тема успешно удалена');
        header('Location: ' . DIR_FORUM . $forum->id . '/' . $razdel->id . '/');
        exit;
    }

    #Навигация
    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='" . 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 "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/'>" . toOutput(utf8_substr($razdel->name, 10)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id.html'>" . toOutput(utf8_substr($theme->name, 10)) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Редактирование');
    echo "</small></div><br />";

    echo "<form action='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id/edit_theme.html' class='list-group-item-null' method='post'>";
    echo lang('Название темы') . "<br />";
    echo "<input name='name' type='text' value='$theme->name' class='form-control'>";
    echo lang('Содержание темы') . "<br />";
    echo "<textarea name='description' rows='4' class='form-control'>$theme->description</textarea>";
    echo lang('Категория') . ":<br />";
    echo "<select class='form-control' name='razdel'>";

    $forums = mysql_query('SELECT `id`, `name`, `access` FROM `forum`');
    while ($forum_s = mysql_fetch_object($forums)) {
        if ($forum_s->access == 0 || ($forum_s->access == 1 && $user['group_access'] > 7) || ($forum_s->access == 2 && $user['group_access'] > 2)) {
            echo "<option disabled='disabled'><b>$forum_s->name</b></option>";

            $razdels = mysql_query('SELECT `id`, `name` FROM `forum_razdels` WHERE `id_forum` = ' . $forum_s->id);

            while ($cat = mysql_fetch_object($razdels)) {
                echo "<option value='$cat->id' " . (($cat->id == $theme->id_razdel) ? 'selected = "selected"' : NULL) . ">>> $cat->name</option>";
            }
        } else {
            continue;
        }
    }
    echo "</select><br />";

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

    echo "<hr>";

    $doc->Button('btn btn-success btn-sm', 'save', 'save', 'Сохранить');

    if (access('forum_for_delete')) {
        $doc->Button('btn btn-danger btn-sm', 'delete', 'trash-o', 'Удалить тему');
    }

    echo "</form>";

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