<?php

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

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

only_reg();

$doc->Title('Уведомления');

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)) {
                mysql_query("DELETE FROM `events` WHERE `id_kont` = '$user[id]'");
                header("Location: index.php?act=events");
                exit;
            }
        }


        if (isset($_GET['del'])) {
            if (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id` = '" . intval($_GET['del']) . "'"), 0) == 1) {
                $post = mysql_fetch_assoc(mysql_query("SELECT * FROM `events` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
                mysql_query("DELETE FROM `events` WHERE `id` = '$post[id]'");
            }
        }

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

        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `events`  WHERE `id_kont` = '$user[id]' "), 0);
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

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

        $new_j = mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0) . "";
        $new_n = mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        $new_ns = mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        $new_ev = mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);

        if ($new_j > 0) {
            $new_j = '+' . mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0);
        } else {
            $new_j = null;
        }
        if ($new_n > 0) {
            $new_n = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_n = null;
        }
        if ($new_ns > 0) {
            $new_ns = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_ns = null;
        }
        if ($new_ev > 0) {
            $new_ev = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_ev = null;
        }

        echo "<div class='list-group-item-null list-group-item-grey'>";
        if ($k_post == 0) {
            echo "<ul class='nav nav-tabs' style='margin-bottom: -12px;'>";
        } else {
            echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";
        }
        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 = mysql_fetch_assoc($q)) {

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

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

            echo "<table class='list-group-item-null'><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>";
            } 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;' id='hides'>";
            //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>";
            echo "<div class='hr'></div>";
        }

        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)) {
                mysql_query("DELETE FROM `notifications` WHERE `id_kont` = '$user[id]'");
                header("Location: /user/notification/?act=feed");
                exit;
            }
        }


        if (isset($_GET['del'])) {
            if (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id` = '" . intval($_GET['del']) . "'"), 0) == 1) {
                $post = mysql_fetch_assoc(mysql_query("SELECT * FROM `notifications` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
                mysql_query("DELETE FROM `notifications` WHERE `id` = '$post[id]'");
            }
        }

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

        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications`  WHERE `id_kont` = '$user[id]' "), 0);
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

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

        $new_j = mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0) . "";
        $new_n = mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        $new_ns = mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        $new_ev = mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);

        if ($new_j > 0) {
            $new_j = '+' . mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0);
        } else {
            $new_j = null;
        }
        if ($new_n > 0) {
            $new_n = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_n = null;
        }
        if ($new_ns > 0) {
            $new_ns = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_ns = null;
        }
        if ($new_ev > 0) {
            $new_ev = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_ev = null;
        }

        echo "<div class='list-group-item-null list-group-item-grey'>";
        if ($k_post == 0) {
            echo "<ul class='nav nav-tabs' style='margin-bottom: -12px;'>";
        } else {
            echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";
        }
        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 = mysql_fetch_assoc($q)) {

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

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

            echo "<table class='list-group-item-null'><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>";
            } 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>";
            echo "<div class='hr'></div>";
        }

        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)) {
                mysql_query("DELETE FROM `notification` WHERE `id_kont` = '$user[id]'");
                header("Location: ?");
                exit;
            }
        }


        if (isset($_GET['del'])) {
            if (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id` = '" . intval($_GET['del']) . "'"), 0) == 1) {
                $post = mysql_fetch_assoc(mysql_query("SELECT * FROM `notification` WHERE `id` = '" . intval($_GET['del']) . "' LIMIT 1"));
                mysql_query("DELETE FROM `notification` WHERE `id` = '$post[id]'");
            }
        }

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

        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `notification`  WHERE `id_kont` = '$user[id]' "), 0);
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

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

        $new_j = mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0) . "";
        $new_n = mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        $new_ns = mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        $new_ev = mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);

        if ($new_j > 0) {
            $new_j = '+' . mysql_result(mysql_query('SELECT COUNT(*) FROM `f_journal` WHERE `read` = "0" AND `id_user` = ' . $user['id']), 0);
        } else {
            $new_j = null;
        }
        if ($new_n > 0) {
            $new_n = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `notification` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_n = null;
        }
        if ($new_ns > 0) {
            $new_ns = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `notifications` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_ns = null;
        }
        if ($new_ev > 0) {
            $new_ev = '+' . mysql_result(mysql_query("SELECT COUNT(*) FROM `events` WHERE `id_kont` = '$user[id]' AND `read` = '0'"), 0);
        } else {
            $new_ev = null;
        }

        echo "<div class='list-group-item-null list-group-item-grey'>";
        if ($k_post == 0) {
            echo "<ul class='nav nav-tabs' style='margin-bottom: -12px;'>";
        } else {
            echo "<ul class='nav nav-tabs' style='margin-bottom: -11px;'>";
        }
        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 = mysql_fetch_assoc($q)) {

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

            echo "<table class='list-group-item-null'><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>";
            } 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;' id='hides'><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>";
            echo "<div class='hr'></div>";
        }

        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';
