<?php

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

$q = $db->query("SELECT * FROM `news` WHERE `main_time` > '" . TIME . "' ORDER BY `id` DESC LIMIT 1");
if ($db->num_rows($q) == 1) {
    $news = $db->get_row($q);
    if (isset($user)) {
        $query = $db->super_query("SELECT COUNT(*) as count FROM `news_hide` WHERE `nid` = '$news[id]' AND uid = '$user[id]'");
        $hide = $query['count'];
    }
    $query = $db->super_query("SELECT COUNT(*) as count FROM `news_like` WHERE `id_news` = '$news[id]'");
    $like = $query['count'];
    $query = $db->super_query("SELECT COUNT(*) as count FROM `news_komm` WHERE `id_news` = '$news[id]'");
    $komm = $query['count'];

    #Лайк
    if (isset($_GET['like']) && isset($user)) {
        $query = $db->super_query("SELECT COUNT(*) as count FROM `news_like` WHERE `id_news` = '" . intval($_GET['id']) . "' AND `id_user` = '$user[id]' LIMIT 1");

        if ($query['count'] != 0) {
            
        } else {
            $db->query("INSERT INTO `news_like` (`id_user`, `id_news`) values('$user[id]', '" . intval($_GET['id']) . "')");
        }
        header("Location: /" . SID);
    }

    if (isset($_GET['unlike']) && isset($user)) {
        $db->query("DELETE FROM `news_like` WHERE `id_user` = '$user[id]' AND `id_news` = '" . intval($_GET['id']) . "' LIMIT 1");

        header("Location: /" . SID);
    }

    #Сворачиваем новость
    if (isset($_GET['hide']) && isset($user)) {
        #Только для зарегистрированных
        only_reg();

        if (!isset($_GET['id']) && !is_numeric($_GET['id'])) {
            header("Location: /index.php?" . SID);
            exit;
        }
        $query = $db->super_query("SELECT COUNT(*) as count FROM `news` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1");

        if ($query['count'] == 0) {
            header("Location: /index.php?" . SID);
            exit;
        }
        $query = $db->super_query("SELECT COUNT(*) as count FROM `news_hide` WHERE `nid` = '" . intval($_GET['id']) . "' AND `uid` = '$user[id]' LIMIT 1");

        if ($query['count']) {
            header("Location: /index.php?" . SID);
            exit;
        }

        $db->query("INSERT INTO `news_hide` (`nid`,`uid`) VALUES ('" . intval($_GET['id']) . "','$user[id]')");
        unset($query);
        header("Location: /index.php?" . SID);
        exit;
    }
    if (isset($user) && $hide == 0) {
        echo "<div class='list-group-item-null list-group-item-grey' style='margin-top: 5px;margin-bottom: 5px;'>";
        echo "<a href='?id=$news[id]&amp;hide' style='float: right;' class='btn btn-sm hint--left' data-hint='" . lang('Скрыть новость') . "'><i class='fa fa-eye-slash fa-fw'></i></a>";

        echo "<h5><a href='/modules/news/?news=$news[id]'>" . toOutput($news['title']) . "</a></h5>";
        #Выводим автора и время создания
        echo "<small style='color: #9197a3;'><i class='fa fa-user fa-fw'></i> $news[avtor] &#183; ";
        echo "<span class='hint--top' data-hint='" . date::time($news['time']) . "'>" . date::times($news['time']) . "</span></small>";

        if ($news['close'] == 1) {
            echo " &#183; <i class='fa fa-lock' style='color: grey;'></i>";
        }

        $q = $db->query("SELECT id FROM user WHERE date_last > '" . (TIME - 600) . "' ORDER BY `date_last` DESC LIMIT 7");

        echo "<div class='hr'></div>";
        echo toOutput(cutStr($news['msg'], 300)) . "<br />";
        #Выводим изображение новости
        if ($user['set_news_images'] == 1) {
            if (is_file(H . 'modules/news/images/' . $news['id'] . '.png')) {
                echo "<img src='/modules/news/images/$news[id].png' style='border: 1px solid #CCDDED; padding: 2px; border-radius: 5px; max-width: 200px;' /><br />";
            }
        } elseif (!isset($user)) {
            if (is_file(H . 'modules/news/images/' . $news['id'] . '.png')) {
                echo "<img src='/modules/news/images/$news[id].png' style='border: 1px solid #CCDDED; padding: 2px; border-radius: 5px; max-width: 200px;' /><br />";
            }
        }
        if ($news['link'] != NULL) {
            echo "<a href='" . htmlentities($news['link'], ENT_QUOTES, 'UTF-8') . "'><small style='color: #9197a3;'>" . htmlentities($news['link'], ENT_QUOTES, 'UTF-8') . "</small></a>  &nbsp;  ";
        }
        if (isset($user)) {
            echo "<div class='hr'></div>";

            if ($like == 0) {
                $l = null;
            } else {
                $query = $db->super_query("SELECT COUNT(*) as count FROM `news_like` WHERE `id_news` = '$news[id]'");

                $l = $query['count'];
            }

            $query = $db->super_query("SELECT COUNT(*) as count FROM `news_like` WHERE `id_news` = '$news[id]' AND `id_user` = '$user[id]' LIMIT 1");

            if ($query['count'] == 0) {
                echo "<a href='?id=$news[id]&amp;like' class='hint--right btn btn-secondary btn-sm' data-hint='" . lang('Мне нравится') . "'><i class='fa fa-heart-o fa-fw' style='color: grey;'></i> $l</a> ";
            } else {
                echo "<a href='?id=$news[id]&amp;unlike' class='hint--top-right btn btn-secondary btn-sm' data-hint='" . lang('Вам понравилась новость') . "'><i class='fa fa-heart fa-fw' style='color: grey;'></i> $l</a> ";
            }

            $query = $db->super_query("SELECT COUNT(*) as count FROM `news_komm` WHERE `id_news` = '$news[id]' LIMIT 1");
            $komms = $query['count'];

            echo "<span style='float: right;'>";
            echo " <a href='/modules/news/?news=$news[id]' class='hint--left btn btn-secondary btn-sm' data-hint='" . lang('Прокомментировали') . " " . sklon_text($komms, array(lang('раз'), lang('раза'), lang('раз'))) . "'><i class='fa fa-comments fa-fw' style='color: grey;'></i> $komms </a> ";

            #Просмотры
            echo " <span class='hint--top-left btn btn-secondary btn-sm' data-hint='" . lang('Просмотрели') . " " . sklon_text($news['prosm'], array(lang('раз'), lang('раза'), lang('раз'))) . "'><i class='fa fa-eye fa-fw' style='color: grey;'></i> $news[prosm]</span> ";
            echo "</span>";

            if ($like > 0) {

                if (IS_WEB) {
                    $q_like = $db->query("SELECT * FROM `news_like` WHERE `id_news` = '$news[id]' ORDER BY id DESC LIMIT 10");
                } else {
                    $q_like = $db->query("SELECT * FROM `news_like` WHERE `id_news` = '$news[id]' ORDER BY id DESC LIMIT 5");
                }
                while ($post_like = $db->get_row($q_like)) {
                    $ank = get_user($post_like['id_user']);
                    echo "<a href='/id$ank[id]' class='hint--top' data-hint='$ank[nick]'>";
                    echo avatar($ank['id'], '25', 'border-radius: 2px;');
                    echo "</a> ";
                }
            }
        }
        echo "</div>";
    }
}