<?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['id'])) {
    $them['id'] = intval($_GET['id']);
}
$them = $db->get_array($db->query("SELECT * FROM `public_forum` WHERE `id` = '" . $them['id'] . ")'"));
$public = $db->get_array($db->query("SELECT * FROM `public` WHERE `id` = '" . $them['id_public'] . ")'"));


if ($them['id'] == 0) {
    $set['title'] = lang('Нет такой темы'); // заголовок страницы
    require_once H . 'sys/inc/thead.php';

    aut();
    err();

    echo "<span class='list-group-item-null list-group-item-danger'>" . lang('Нет такой темы') . "</span><br />";

    $msg = "[url=/info.php?id=" . $user['id'] . "] " . $user['nick'] . "[/url] предупрежден за некоректые запросы в сообществах";
    $db->query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '1', '" . $msg . "', '$time')");
    if (!isset($_GET['ajax'])) {
        $_SESSION['message'] = lang('Нет такой темы, пока предупреждение');
        header('Location: /user/public/index.php?id=' . $them['id_public']);
    }
    exit;

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


$set['title'] = $them['name'];
require_once H . 'sys/inc/thead.php';

if (isset($_POST['msg']) && isset($user)) {
    $komm = $_POST['msg'];
    if (utf8_strlen($komm) > 1024) {
        $err[] = 'Сообщение превышает 1024 символа';
    }
    if (utf8_strlen($komm) < 2) {
        $err[] = 'Слишком короткое сообщение';
    }
    if (!isset($err)) {
// отправка сообщения
        $db->query("INSERT INTO `public_forum` (`id_user`, `time`, `komm`, `id_them`) values('" . $user['id'] . "', '$time', '" . $komm . "', '" . $them['id'] . "')");
        $o['id'] = $db->insert_id();
        if (isset($user) && $user['id'] != $them['id_user']) {
            $db->query("INSERT INTO `notification` (`id_user`, `avtor`, `type`, `time`, `id_object`) values('" . $them['id_user'] . "', '" . $user['id'] . "', 'o_komm', '$time', '" . $o['id'] . "')");
        }

        if (!isset($_GET['ajax'])) {
            $_SESSION['message'] = 'Сообщение успешно отправлено';
            header('Location: ?id=' . $them['id']);
        }
        exit;
    }
}
if (isset($user) && $user['id'] == $public['id_user']) {
    if (isset($_GET['komm']) && $_GET['komm'] == 'del' && intval($_GET['post'])) {
        $p = $db->get_array($db->query("SELECT * FROM `public_forum` WHERE `id` = '" . intval($_GET['post']) . "' LIMIT 1"));
        $db->query("DELETE FROM `public_forum` WHERE `id` = '" . $p['id'] . "'");
        $_SESSION['message'] = lang('Сообщение успешно удалено');
        header("Location: ?id=" . $them['id']);
    }

    if (isset($_GET['them']) && $_GET['them'] == 'close') {
        $db->query("UPDATE `public_forum` SET `close` = '1' WHERE `id` = '" . $them['id'] . "' LIMIT 1");

        $_SESSION['message'] = lang('Тема успешно закрыта');
        header("Location: ?id=" . $them['id']);
    }

    if (isset($_GET['them']) && $_GET['them'] == 'open') {
        $db->query("UPDATE `public_forum` SET `close` = '0' WHERE `id` = '" . $them['id'] . "' LIMIT 1");

        $_SESSION['message'] = lang('Тема успешно открыта');
        header("Location: ?id=" . $them['id']);
    }

    if (isset($_GET['del']) && $_GET['del'] == 'del') {
        $res = $db->query("SELECT * FROM `public_forum` WHERE `id_them` = '" . $them['id'] . "'");
        while ($row = $db->get_array($res)) {
            $db->query("DELETE FROM `public_forum` WHERE `id` = '" . $row['id'] . "'");
        }
        $db->query("DELETE FROM `public_forum` WHERE `id` = '" . $them['id'] . "'");
        $_SESSION['message'] = lang('Тема успешно удалена');
        header("Location: forum.php?id=" . $them['id_public']);
    }
}


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 "<a href='index.php?id=$public[id]'>" . toOutput($public['name']) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='forum.php?id=$public[id]'>" . lang('Обсуждения') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo toOutput(cutStr($them['name'], 30));
echo "</small></div><br />";

if (isset($_GET['v']) && $_GET['v'] == 's') {
    echo "<div class='list-group-item-null list-group-item-info'>";
    echo "Подтвердите удаление<br />";
    echo "<a href='?id=" . $them['id'] . "&amp;del=del' class='btn btn-success btn-sm'>Да, подтверждаю</a> ";
    echo "<a href='?id=" . $them['id'] . "' class='btn btn-secondary btn-sm'>Отмена</a>";
    echo "</div>";
}


echo "<div class='list-group-item-null'>";
echo "<b>" . toOutput($them['name']) . "</b><br />";
echo toOutput($them['msg']) . "<br />";
echo "</div>";

if (isset($user) && $user['id'] == $public['id_user']) {
    echo "<div class='list-group-item-null list-group-item-info'>";
    if ($them['close'] == '0') {
        echo "<a href='?id=" . $them['id'] . "&amp;them=close'><i class='fa fa-lock fa-fw'></i> " . lang('Закрыть тему') . "</a>";
    } else {
        echo "<a href='?id=" . $them['id'] . "&amp;them=open'><i class='fa fa-unlock fa-fw'></i> " . lang('Открыть тему') . "</a>";
    }

    echo " &#183; <a href='?id=" . $them['id'] . "&amp;v=s'><i class='fa fa-trash-o fa-fw'></i> " . lang('Удалить') . "</a>";

    echo "</div>";
}
$pro_ons = $db->get_array($db->query("SELECT * FROM `public_coint` WHERE `id_public` = '" . $public['id'] . "' AND `id_user` = '" . $user['id'] . "'"), 0);

if (isset($user) && $user['id'] == $pro_ons['id_user']) {
    if ($them['close'] == '0') {
        echo "<form method='post' name='message' class='list-group-item-null list-group-item-grey' action='?id=" . $them['id'] . "&amp;$passgen'>";
        echo "<textarea name='msg' required class='form-control'></textarea><br />";
        $doc->Button('btn btn-primary btn-sm', null, 'pencil', 'Опубликовать');
        echo "</form>";
    } else {
        echo "<div class='list-group-item-null list-group-item-warning'><i class='fa fa-lock fa-fw'></i> " . lang('Тема закрыта') . "</div>";
    }
} else {
    echo "<div class='list-group-item-null list-group-item-warning'> " . lang('Доступно только чтение') . "</div>";
}

$k_post = $db->super_query("SELECT COUNT(*) as count FROM `public_forum` WHERE `id_them` = '$them[id]'");
$k_post = $k_post['count'];
$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 = $db->query("SELECT * FROM `public_forum` WHERE `id_them` = '" . $them['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str'] . "");
while ($post = $db->get_array($q)) {
    $ank = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '" . $post['id_user'] . ")'"));

    echo "<table class='list-group-item-null list-group-item-grey dop'><tr><td class='icon14'>";
    avatar($ank['id'], '48', 'border-radius: 2px;');
    echo "</td><td class='null'>";

    if ($user['id'] == $pro_ons['id_user']) {
        echo "<span style='float:right;'>";
        if ($them['close'] == '0') {
            if (isset($user) && $user['id'] != $ank['id']) {
                echo "<a href='forum_otvet.php?id=" . $post['id'] . "' class='hint--left' data-hint='" . lang('Ответить') . "'><i class='fa fa-pencil fa-fw'></i></a> ";
            }
        }
        if (isset($user) && $user['id'] == $them['id_user']) {
            echo "<a href='?id=" . $them['id'] . "&amp;komm=del&amp;post=" . $post['id'] . "' class='hint--left' data-hint='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
        }
        echo "</span>";
    }


    echo user($ank['id']);
    echo " <small style='color: grey;'>" . date::timek($post['time']) . "</small>";

    echo "<br />";
    $otv = $db->get_array($db->query("SELECT * FROM `public_forum` WHERE `id` = '" . $post['otv'] . ")'"));
    $ank2 = $db->get_array($db->query("SELECT * FROM `user` WHERE `id` = '" . $otv['id_user'] . ")'"));
    if ($post['otv'] > 0) {
        echo "<div class='cit'> " . $ank2['nick'] . ", ";
        $otv = $db->get_array($db->query("SELECT * FROM `public_forum` WHERE `id` = '" . $post['otv'] . ")'"));
        echo toOutput($otv['komm']) . "<br />";
        echo "</div>";
    }

    echo toOutput($post['komm']) . "<br />";
    echo "</td></tr></table>";
}

if ($k_page > 1) {
    str("?id=" . $them['id'] . "&amp;", $k_page, $page);
} // Вывод страниц

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