<?php

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

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

only_reg();

$set['title'] = lang('Уведомления');

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

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='/id$user[id]'>$user[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Уведомления');
echo "</small></div><br />";

$razdel = (isset($_GET['act'])) ? htmlspecialchars($_GET['act']) : null;
switch ($razdel) {
    case 'events':
        if (isset($_GET['delete']) && $_GET['delete'] == 'all') {
            if (isset($user)) {
                $db->query("DELETE FROM `events` WHERE `id_kont` = '$user[id]'");
                header("Location: index.php?act=events");
                exit;
            }
        }


        if (isset($_GET['del'])) {
            $del = $db->super_query("SELECT COUNT(*) as count FROM `events` WHERE `id` = '" . intval($_GET['del']) . "'");

            if (isset($_GET['del']) && $del['count'] == 1) {
                $post = $db->get_row($db->query("SELECT * FROM `events` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
                $db->query("DELETE FROM `events` WHERE `id` = '$post[id]'");
                header("Location: /user/notification/?act=events");
                exit;
            }
        }

        $db->query("UPDATE `events` SET `read` = '1' WHERE `id_kont` = '$user[id]' AND `read` = '0'");

        $k_post = $db->super_query("SELECT COUNT(*) as count FROM `events`  WHERE `id_kont` = '$user[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'];

        $q = $db->query("SELECT * FROM `events` WHERE `id_kont` = '$user[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

        $k = $db->super_query("SELECT COUNT(*) as count FROM `f_journal` WHERE `read` = '0' AND `id_user` = '$user[id]'");
        $new_j = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_n = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_ns = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_ev = (($k['count'] == 0) ? null : "+ $k[count]");

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-tabs' style='margin-bottom: -12px;'>";
        echo "<li class='nav-item'><a class='nav-link' href='?'>" . lang('Ответы') . " $new_n</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='?act=feed'>" . lang('Новости') . " $new_ns</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='/modules/forum/journal.php'>" . lang('Форум') . " $new_j</a></li>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('События') . " $new_ev</a></li>";
        echo "</ul>";
        echo '</div>';

        if ($k_post == 0) {
            echo "<br />";
            $doc->NoResult();
        }

        while ($post = $db->get_row($q)) {

            $ank = get_user($post['id_user']);

            if ($post['read'] == 0) {
                $db->query("UPDATE `notification_set` SET `read` = '1' WHERE `id` = '$post[id]'");
            }

            echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td>";
            echo "<div class='row_photo'>";
            if ($post['id_img'] == 1) {#лайки
                echo "<div class='row_photo_other'><i class='fa fa-heart' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 2) {#фото
                echo "<div class='row_photo_other'><i class='fa fa-camera' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 3) {#коммы
                echo "<div class='row_photo_other'><i class='fa fa-comment' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 4) {#ответы
                echo "<div class='row_photo_other'><i class='fa fa-pencil' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 5) {#ляпы
                echo "<div class='row_photo_other'><i class='fa fa-paw' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 6) {#Подарки
                echo "<div class='row_photo_other'><i class='fa fa-gift' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 7) {#Приглашения или деления
                echo "<div class='row_photo_other'><i class='fa fa-share' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 8) {#соощества и тд
                echo "<div class='row_photo_other'><i class='fa fa-users' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 9) {#валюта и деньги
                echo "<div class='row_photo_other'><i class='fa fa-money' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 10) {#поддержка
                echo "<div class='row_photo_other'><i class='fa fa-support' style='color: #4C6B8F;'></i></div>";
            } else {
                
            }
            avatar($ank['id'], '50', 'border-radius: 2px;');
            echo "</div>";
            echo "</td><td class='null'>";
            echo "<div style='float:right;'><font color='grey'><small title='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</small></font></div>";

            if ($ank['id'] == 0) {
                echo "<b><font color='#4C6B8F'>" . lang('Система') . "</font></b><br />";
            } else {
                echo user($ank['id']);
                echo "<br />";
            }

            echo "<span style='float:right;'>";
            //echo "<a href='set_user.php?id=$ank[id]' title='" . lang('Настроить') . "'><i class='fa fa-cog fa-fw'></i></a>";
            echo "<a href='index.php?act=events&amp;del=$post[id]' title='" . lang('Удалить') . "'><i class='fa fa-trash-o'></i></a>";
            echo "</span>";

            if ($post['msg'] == null) {
                echo "<font color='grey'>" . lang('Сообщение удалено') . "...</font>";
            } else {
                echo "<font color='grey'>" . toOutput($post['msg']) . "</font>";
            }

            echo "</td></tr></table>";
        }

        if ($k_page > 1) {
            str("?id=$user[id]&amp;", $k_page, $page);
        }
        if ($k_post == 0) {
            
        } else {
            $doc->Link('list-group-item-null', "index.php?act=events&amp;delete=all", 'trash-o', 'Очистить события');
        }
        break;
    case 'feed':
        if (isset($_GET['delete']) && $_GET['delete'] == 'all') {
            if (isset($user)) {
                $db->query("DELETE FROM `notifications` WHERE `id_kont` = '$user[id]'");
                header("Location: /user/notification/?act=feed");
                exit;
            }
        }


        if (isset($_GET['del'])) {
            $del = $db->super_query("SELECT COUNT(*) as count FROM `notifications` WHERE `id` = '" . intval($_GET['del']) . "'");

            if (isset($_GET['del']) && $del['count'] == 1) {
                $post = $db->get_row($db->query("SELECT * FROM `notifications` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
                $db->query("DELETE FROM `notifications` WHERE `id` = '$post[id]'");

                header("Location: /user/notification/?act=feed");
                exit;
            }
        }

        $db->query("UPDATE `notifications` SET `read` = '1' WHERE `id_kont` = '$user[id]' AND `read` = '0'");

        $k_post = $db->super_query("SELECT COUNT(*) as count FROM `notifications`  WHERE `id_kont` = '$user[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'];

        $q = $db->query("SELECT * FROM `notifications` WHERE `id_kont` = '$user[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

        $k = $db->super_query("SELECT COUNT(*) as count FROM `f_journal` WHERE `read` = '0' AND `id_user` = '$user[id]'");
        $new_j = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_n = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_ns = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_ev = (($k['count'] == 0) ? null : "+ $k[count]");

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-tabs' style='margin-bottom: -12px;'>";

        echo "<li class='nav-item'><a class='nav-link' href='?'>" . lang('Ответы') . " $new_n</a></li>";
        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Новости') . " $new_ns</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='/modules/forum/journal.php'>" . lang('Форум') . " $new_j</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='?act=events'>" . lang('События') . " $new_ev</a></li>";
        echo "</ul>";
        echo '</div>';

        if ($k_post == 0) {
            echo "<br />";
            $doc->NoResult();
        }

        while ($post = $db->get_row($q)) {

            $ank = get_user($post['id_user']);

            if ($post['read'] == 0) {
                $db->query("UPDATE `notification_set` SET `read` = '1' WHERE `id` = '$post[id]'");
            }

            echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td>";
            echo "<div class='row_photo'>";
            if ($post['id_img'] == 1) {#лайки
                echo "<div class='row_photo_other'><i class='fa fa-heart' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 2) {#фото
                echo "<div class='row_photo_other'><i class='fa fa-camera' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 3) {#коммы
                echo "<div class='row_photo_other'><i class='fa fa-comment' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 4) {#ответы
                echo "<div class='row_photo_other'><i class='fa fa-pencil' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 5) {#ляпы
                echo "<div class='row_photo_other'><i class='fa fa-paw' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 6) {#Подарки
                echo "<div class='row_photo_other'><i class='fa fa-gift' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 7) {#Приглашения или деления
                echo "<div class='row_photo_other'><i class='fa fa-share' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 8) {#соощества и тд
                echo "<div class='row_photo_other'><i class='fa fa-users' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 9) {#валюта и деньги
                echo "<div class='row_photo_other'><i class='fa fa-money' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 10) {#поддержка
                echo "<div class='row_photo_other'><i class='fa fa-support' style='color: #4C6B8F;'></i></div>";
            } else {
                
            }
            avatar($ank['id'], '50', 'border-radius: 2px;');
            echo "</div>";
            echo "</td><td class='null'>";
            echo "<div style='float:right;'><font color='grey'><small title='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</small></font></div>";

            if ($ank['id'] == 0) {
                echo "<b><font color='#4C6B8F'>" . lang('Система') . "</font></b><br />";
            } else {
                echo user($ank['id']);
                echo "<br />";
            }

            echo "<span style='float:right;'>";
            echo "<a href='set_user.php?id=$ank[id]' title='" . lang('Настроить') . "'><i class='fa fa-cog fa-fw'></i></a>";
            echo "<a href='index.php?act=feed&amp;del=$post[id]' title='" . lang('Удалить') . "'><i class='fa fa-trash-o'></i></a>";
            echo "</span>";

            if ($post['msg'] == null) {
                echo "<font color='grey'>" . lang('Сообщение удалено') . "...</font>";
            } else {
                echo "<font color='grey'>" . toOutput($post['msg']) . "</font>";
            }

            echo "</td></tr></table>";
        }

        if ($k_page > 1) {
            str("?id=$user[id]&amp;", $k_page, $page);
        }
        if ($k_post == 0) {
            
        } else {
            $doc->Link('list-group-item-null', "?act=feed&amp;delete=all", 'trash-o', 'Очистить журнал');
        }
        break;

    default:
        if (isset($_GET['delete']) && $_GET['delete'] == 'all') {
            if (isset($user)) {
                $db->query("DELETE FROM `notification` WHERE `id_kont` = '$user[id]'");
                header("Location: ?");
                exit;
            }
        }


        if (isset($_GET['del'])) {
            $del = $db->super_query("SELECT COUNT(*) as count FROM `notifications` WHERE `id` = '" . intval($_GET['del']) . "'");

            if (isset($_GET['del']) && $del['count'] == 1) {
                $post = $db->get_row($db->query("SELECT * FROM `notification` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
                $db->query("DELETE FROM `notification` WHERE `id` = '$post[id]'");

                header("Location: ?");
                exit;
            }
        }

        #Помечаем, что прочитали
        $db->query("UPDATE `notification` SET `read` = '1' WHERE `id_kont` = '$user[id]' AND `read` = '0'");

        $k_post = $db->super_query("SELECT COUNT(*) as count FROM `notification`  WHERE `id_kont` = '$user[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'];

        $q = $db->query("SELECT * FROM `notification` WHERE `id_kont` = '$user[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

        $k = $db->super_query("SELECT COUNT(*) as count FROM `f_journal` WHERE `read` = '0' AND `id_user` = '$user[id]'");
        $new_j = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_n = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_ns = (($k['count'] == 0) ? null : "+ $k[count]");
        $k = $db->super_query("SELECT COUNT(*) as count FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'");
        $new_ev = (($k['count'] == 0) ? null : "+ $k[count]");

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-tabs' style='margin-bottom: -12px;'>";

        echo "<li class='nav-item'><a class='nav-link active'>" . lang('Ответы') . " $new_n</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='?act=feed'>" . lang('Новости') . " $new_ns</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='/modules/forum/journal.php'>" . lang('Форум') . " $new_j</a></li>";
        echo "<li class='nav-item'><a class='nav-link' href='?act=events'>" . lang('События') . " $new_ev</a></li>";
        echo "</ul>";
        echo '</div>';

        if ($k_post == 0) {
            echo "<br />";
            $doc->NoResult();
        }

        while ($post = $db->get_row($q)) {

            $ank = get_user($post['id_user']);

            echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td>";
            echo "<div class='row_photo'>";
            if ($post['id_img'] == 1) {#лайки
                echo "<div class='row_photo_other'><i class='fa fa-heart' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 2) {#фото
                echo "<div class='row_photo_other'><i class='fa fa-camera' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 3) {#коммы
                echo "<div class='row_photo_other'><i class='fa fa-comment' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 4) {#ответы
                echo "<div class='row_photo_other'><i class='fa fa-pencil' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 5) {#ляпы
                echo "<div class='row_photo_other'><i class='fa fa-paw' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 6) {#Подарки
                echo "<div class='row_photo_other'><i class='fa fa-gift' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 7) {#Приглашения или деления
                echo "<div class='row_photo_other'><i class='fa fa-share' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 8) {#соощества и тд
                echo "<div class='row_photo_other'><i class='fa fa-users' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 9) {#валюта и деньги
                echo "<div class='row_photo_other'><i class='fa fa-money' style='color: #4C6B8F;'></i></div>";
            } elseif ($post['id_img'] == 10) {#поддержка
                echo "<div class='row_photo_other'><i class='fa fa-support' style='color: #4C6B8F;'></i></div>";
            } else {
                
            }
            avatar($ank['id'], '50', 'border-radius: 2px;');
            echo "</div>";
            echo "</td><td class='null'>";

            if ($ank['id'] == 0) {
                echo "<b><font color='#4C6B8F'>" . lang('Система') . "</font></b> ";
            } else {
                echo "<b><a href='/id$ank[id]'><font color='#4C6B8F'>$ank[nick]</font></a></b> ";
            }
            echo "<font color='grey'><small title='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</small></font><br />";

            #Кнопка удаления уведомления
            echo "<span style='float:right;'><a href='?del=$post[id]' title='" . lang('Удалить') . "'><i class='fa fa-trash-o'></i></a></span>";

            if ($post['msg'] == null) {
                echo "<font color='grey'>" . toOutput($post['msg']) . "</font>";
            } else {
                echo "<font color='grey'>" . toOutput($post['msg']) . "</font>";
            }

            echo "</td></tr></table>";
        }

        if ($k_page > 1) {
            str("?id=$user[id]&amp;", $k_page, $page);
        }

        if ($k_post == 0) {
            
        } else {
            $doc->Link('list-group-item-null list-group-item-grey', "?page=$page&amp;delete=all", 'trash-o', 'Очистить журнал');
        }

        break;
}

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