<?php

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

$q = mysql_query("SELECT * FROM `news` WHERE `main_time` > '" . TIME . "' ORDER BY `id` DESC LIMIT 1");
if (mysql_num_rows($q) == 1) {
    $news = mysql_fetch_assoc($q);
    if (isset($user)) {
        $hide = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_hide` WHERE `nid` = '$news[id]' AND uid = '$user[id]'"), 0);
    }
    $like = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '$news[id]'"), 0);
    $kum = @mysql_result(mysql_query("SELECT id_user FROM `news_like` WHERE `id_news` = '$news[id]'"), 0);
    $kom = @mysql_result(mysql_query("SELECT nick FROM `user` WHERE `id` = '$kum' LIMIT 1"), 0);
    $kem = $like - 1;
    $komm = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_komm` WHERE `id_news` = '$news[id]'"), 0);

    #Лайк
    if (isset($_GET['like']) && isset($user)) {
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '" . intval($_GET['id']) . "' AND `id_user` = '$user[id]' LIMIT 1"), 0) != 0) {
            
        } else {
            mysql_query("INSERT INTO `news_like` (`id_user`, `id_news`) values('$user[id]', '" . intval($_GET['id']) . "')");
        }
        header("Location: /" . SID);
    }

    if (isset($_GET['unlike']) && isset($user)) {
        mysql_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;
        }

        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news` WHERE `id` = '" . intval($_GET['id']) . "' LIMIT 1", $db), 0) == 0) {
            header("Location: /index.php?" . SID);
            exit;
        }

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

        mysql_query("INSERT INTO `news_hide` (`nid`,`uid`) VALUES ('" . intval($_GET['id']) . "','$user[id]')");
        header("Location: /index.php?" . SID);
        exit;
    }
    if (isset($user) && $hide == 0) {
        echo "<div class='list-group-item-null list-group-item-info'>";
        echo "<span style='float: right;'><i class='fa fa-feed fa-2x'></i></span>";
        echo "<h5>";

        echo "<a href='/modules/news/?news=$news[id]'>" . toOutput($news['title']) . "</a>";

        echo "</h5><small style='color: #9197a3;'><i class='fa fa-user fa-fw'></i> $news[avtor] &#183; ";
        echo "<span class='hint--right' data-hint='" . date::time($news['time']) . "'>" . date::times($news['time']) . "</span></small>";


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

        echo "<hr>";
        echo toOutput(cutStr($news['msg'], 300)) . "<br />";
        #Выводим изображение новости
        if ($user['set_news'] == 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') . "' title='Перейти, чтобы узнать подробности'><small style='color: #9197a3;'>" . htmlentities($news['link'], ENT_QUOTES, 'UTF-8') . "</small></a>  &nbsp;  ";
        }
        if (isset($user)) {
            echo "<hr>";
            if (isset($user)) {
                echo "<span style='float: right;'><a href='?id=$news[id]&amp;hide' class='btn hint--left' data-hint='" . lang('Закрыть новость') . "'><font color='grey'><i class='fa fa-close fa-fw'></i></font></a></span>";
            }
            if ($like == 0) {
                $l = null;
            } else {
                $l = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '$news[id]'"), 0);
            }

            if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news_like` WHERE `id_news` = '$news[id]' AND `id_user` = '$user[id]' LIMIT 1"), 0) == 0) {
                echo "<a href='?id=$news[id]&amp;like' class='hint--right btn' data-hint='" . lang('Мне нравится') . "'><font color='grey'><i class='fa fa-heart fa-fw'></i> $l</font></a> ";
            } else {
                echo "<a href='?id=$news[id]&amp;unlike' class='hint--bottom-right btn' data-hint='" . lang('Вам понравилась новость') . "'><font color='red'><i class='fa fa-heart fa-fw'></i> $l</font></a> ";
            }

            $kom = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_komm` WHERE `id_news` = '$news[id]' LIMIT 1"), 0);
            $komms = mysql_result(mysql_query("SELECT COUNT(*) FROM `news_komm` WHERE `id_news` = '$news[id]' LIMIT 1"), 0);

            if ($kom == 0) {
                echo "<a href='/modules/news/?news=$news[id]' class='hint--right btn' data-hint='" . lang('Прокомментировали') . " " . sklon_text($komms, array(lang('раз'), lang('раза'), lang('раз'))) . "'><font color='grey'><i class='fa fa-comments fa-fw'></i> $komms </font>";
                if ($news['close'] == 1) {
                    echo "<i class='fa fa-lock fa-fw'></i>";
                }
                echo "</a> ";
            } else {
                echo "<a href='/modules/news/?news=$news[id]' class='hint--top btn' data-hint='" . lang('Прокомментировали') . " " . sklon_text($komms, array(lang('раз'), lang('раза'), lang('раз'))) . "'><font color='grey'><i class='fa fa-comments fa-fw'></i> $komms </font>";
                if ($news['close'] == 1) {
                    echo "<i class='fa fa-lock fa-fw'></i>";
                }
                echo "</a> ";
            }
            #Просмотры
            echo "<span class='hint--top btn' data-hint='" . lang('Просмотрели') . " " . sklon_text($news['prosm'], array(lang('раз'), lang('раза'), lang('раз'))) . "'><font color='grey'><i class='fa fa-eye fa-fw'></i> $news[prosm]</font></span>";

            if ($like > 0) {
                echo "<br /><font color='grey' size='1'>" . lang('Понравилось') . " " . sklon_text($like, array(lang('пользователю'), lang('пользователям'), lang('пользователям'))) . "</font><br />";

                if (IS_WEB) {
                    $q_like = mysql_query("SELECT * FROM `news_like` WHERE `id_news` = '$news[id]' ORDER BY id DESC LIMIT 15");
                } else {
                    $q_like = mysql_query("SELECT * FROM `news_like` WHERE `id_news` = '$news[id]' ORDER BY id DESC LIMIT 8");
                }
                while ($post_like = mysql_fetch_array($q_like)) {
                    $ank = get_user($post_like['id_user']);
                    echo "<a href='/info.php?id=$ank[id]'>";
                    echo avatar($ank['id'], '30', 'border-radius: 50%;');
                    echo "</a> ";
                }
            }
        }
        echo "</div>";
    }
}