<?php

/* DCMS Special
 * Дата последнего редактирования 01.10.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'])) {
    $id = intval($_GET['id']);
} elseif (isset($user)) {
    $id = $user['id'];
}
$ank = get_user($id);
if (!$ank || $ank['id'] == 0) {
    header("Location:/index.php?");
    exit;
}

$set['title'] = lang('Подарки') . ' ' . $ank['nick'];
require_once H . 'sys/inc/thead.php';

aut();

define('DIR_GIFT', '/modules/gifts/');

if (isset($_GET['do'])) {
    only_reg();
    if (isset($_GET['cat']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `gift_cat` WHERE `id` = '" . intval($_GET['cat']) . "'"), 0) != 0) {
        $cat = mysql_fetch_array(mysql_query("SELECT * FROM `gift_cat` WHERE `id` = '" . intval($_GET['cat']) . "'"));
        if ($cat['show'] == 1 || $cat['show'] == 0 && $user['level'] >= 3) {
            $cat = $cat;
        } else {
            $cat = array();
            $cat['id'] = 1;
            $cat['show'] = 1;
        }
    } else {
        $cat = array();
        $cat['id'] = 1;
        $cat['show'] = 1;
    }
    if (isset($_GET['gift']) && intval($_GET['gift']) != NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `gift` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_cat` = '$cat[id]'"), 0) != 0 && ($cat['show'] == 1 || $cat['show'] == 0 && $user['level'] >= 3)) {
        $gift = mysql_fetch_array(mysql_query("SELECT * FROM `gift` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_cat` = '$cat[id]'"));

        #Навигация
        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$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='" . DIR_GIFT . "'>" . lang('Подарки') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Новый подарок');
        echo "</small></div><br />";

        if (isset($_POST['submited'])) {
            $msg = $_POST['msg'];
            if (utf8_strlen($msg) > 512) {
                $_SESSION['err'] = lang('Слишком длинное сообщение');
            }
            if ($user['money'] < $gift['cena']) {
                $_SESSION['err'] = lang('Вам не хватает рублей');
            }
            if ($user['id'] == $ank['id']) {
                $_SESSION['err'] = lang('Нельзя дарить себе');
            }
            if (!isset($_SESSION['err'])) {
                if ($_POST['mode'] == 2 || $_POST['mode'] == 3) {
                    $type = intval($_POST['mode']);
                } else {
                    $type = 1;
                }
                mysql_query("INSERT INTO `gifts` SET `id_user` = '$ank[id]', `id_kont` = '$user[id]', `msg` = '" . mysql_real_escape_string($msg) . "', `time` = '$time', `id_gift` = '$gift[id]', `type` = '$type'");
                $id = mysql_insert_id();
                mysql_query("UPDATE `user` SET `money` = '" . ($user['money'] - $gift['cena']) . "' WHERE `id` = '$user[id]'");

                if ($user['sex'] == 0) {
                    $sex = lang('прислала');
                } elseif ($user['sex'] == 1) {
                    $sex = lang('прислал');
                }

                $msg = "$sex " . lang('Вам подарок') . ". [url=/modules/gifts/?id=$ank[id]&amp;gift=$id]" . lang('смотреть') . "[/url]";

                mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ank[id]', '6', '$msg', '$time')");

                $_SESSION['message'] = lang('Подарок успешно отправлен');
                header("Location: ?id=$ank[id]");
                exit;
            }
        }

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo lang('Подарок для') . " <a href='/info.php?id=$ank[id]'><b>$ank[nick]</b></a>";
        echo "</div><div class='list-group-item-null'>";
        echo "<center><img src='images/$gift[image]' /></center><br />";
        echo lang('Стоимость') . ": <font color='green'><b><i class='fa fa-money fa-fw'></i> $gift[cena]</b></font><br />";
        echo lang('У Вас на счету') . ": <font color='green'><b><i class='fa fa-money fa-fw'></i> $user[money]</b></font><br />";
        echo "</div>";

        echo "<form method='post' class='list-group-item-null list-group-item-grey' action=''>";
        echo lang('Тип подарка') . ":<br />";

        echo "<label class='custom-control custom-radio'><input value='1' name='mode' type='radio' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span><span class='custom-control-description'><i class='fa fa-globe fa-fw'></i> " . lang('Публичный') . "</span></label><br />";
        echo "<small style='color: grey;'>" . lang('Все будут видеть Ваш подарок, сообщение и логин') . ".</small><br />";
        echo "<label class='custom-control custom-radio'><input value='2' name='mode' type='radio' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span><span class='custom-control-description'><i class='fa fa-lock fa-fw'></i> " . lang('Личный') . "</span></label><br />";
        echo "<small style='color: grey;'>" . lang('Все будут видеть Ваш подарок, но только получатель сможет видеть Ваш логин и сообщение') . ".</small><br />";
        echo "<label class='custom-control custom-radio'><input value='3' name='mode' type='radio' checked='checked' class='custom-control-input'><span class='custom-control-indicator'></span><span class='custom-control-description'><i class='fa fa-eye-slash fa-fw'></i> " . lang('Анонимный') . "</span></label><br />";
        echo "<small style='color: grey;'>" . lang('Все будут видеть Ваш подарок. Только получатель увидит ваше сообщение. Никто не увидит Ваш логин') . ".</small><br /><br />";

        $doc->Textarea('msg', 'Комментарий к подарку', 512, "Лови подарок");
        echo "<br />";
        $doc->Button('btn btn-success btn-sm', 'submited', 'gift', 'Отправить');

        echo "<span style='color:red;'><i class='fa fa-money fa-fw'></i> -$gift[cena] " . lang('руб') . ".</span>";
        echo "</form>";

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

    $q = mysql_query("SELECT * FROM `gift_cat`");

    #Навигация
    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$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='" . DIR_GIFT . "'>" . lang('Подарки') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Новый подарок');
    echo "</small></div><br />";

    echo "<div class='list-group-item-null'>";
    echo "<span style='float: right;color: green;'><i class='fa fa-money fa-fw'></i> $user[money]</span>";
    echo lang('Выберите подарок для') . " ";
    echo "<a href = '/id$ank[id]'>";
    echo gradient("$ank[nick]", "$ank[ncolor]", "$ank[ncolor2]");
    echo "</a> ";
    echo "</div>";
    $k_p = mysql_result(mysql_query("SELECT COUNT(*) FROM `gift_cat`"), 0);
    if ($k_p == 0) {
        echo "<div class='list-group-item-null'><small style='color: grey;'>" . lang('Категории не созданы') . "</small></div>";
    } else {
        $i = 0;

        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<ul class='nav nav-pills'>";

        while ($post = mysql_fetch_array($q)) {
            if ($post['show'] == 1 || $post['show'] == 0 && $user['level'] >= 3) {
                echo ($i != 0 && $i < mysql_num_rows($q) ? "" : NULL) . ($post['id'] != $cat['id'] ? "<li class='nav-item'><a class='nav-link' href='?id=$ank[id]&amp;do&amp;cat=$post[id]'>" : "<li class='nav-item'><a class='nav-link active'>") . "$post[name]" . ($post['id'] != $cat['id'] ? "</a></li>" : "</a></li>") . "";
            }
            $i++;
        }
        echo "</ul>";
        echo "</div>";
    }


    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gift` WHERE `id_cat` = '$cat[id]'"), 0);
    if ($k_post == 0) {
        $doc->NoResult();
    }

    $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 `gift` WHERE `id_cat` = '$cat[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
    while ($post = mysql_fetch_array($q)) {
        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        echo "<img src='images/$post[image]' height='40' width='40' /> ";
        echo "</td><td class='null'>";
        echo "<span class='count'><i class='fa fa-money fa-fw'></i> $post[cena] " . lang('руб') . "</span>";

        echo "<a href='?id=$ank[id]&amp;do&amp;cat=$cat[id]&amp;gift=$post[id]'>";
        echo toOutput($post['name']);
        echo "</a>";
        echo "</td></tr></table>";
        echo "<div class='hr'></div>";
    }

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

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

if (isset($_GET['gift']) && intval($_GET['gift']) != NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `gifts` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_user` = '$ank[id]'"), 0) != 0) {
    $gift = mysql_fetch_array(mysql_query("SELECT * FROM `gifts` WHERE `id` = '" . intval($_GET['gift']) . "' AND `id_user` = '$ank[id]'"));
    $giftt = mysql_fetch_array(mysql_query("SELECT * FROM `gift` WHERE `id` = '$gift[id_gift]'"));
    $ank2 = get_user($gift['id_kont']);
    if ($gift['type'] == 2) {
        $type = "<i class='fa fa-lock fa-fw'></i> " . lang('Личный');
    } elseif ($gift['type'] == 3) {
        $type = "<i class='fa fa-eye-slash fa-fw'></i> " . lang('Анонимный');
    } else {
        $type = "<i class='fa fa-globe fa-fw'></i> " . lang('Публичный');
    }

    #Навигация
    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$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Подарки');
    echo "</small></div><br />";

    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    echo "<img src='images/$giftt[image]' /><br />";
    echo "</td><td class='null'>";
    if ($gift['type'] == 1 || $gift['type'] == 2 && ($ank['id'] == $user['id'] || $user['level'] >= 3) || $gift['type'] == 3 && $user['level'] >= 3) {
        echo user($ank2['id']);
        echo "<br />";
    } else {
        echo "<i class='fa fa-user-secret fa-fw'></i> Аноним<br />";
    }

    if ($gift['type'] == 1 || $gift['type'] == 2 && ($ank['id'] == $user['id'] || $user['level'] >= 3) || $gift['type'] == 3 && ($ank['id'] == $user['id'] || $user['level'] >= 3)) {
        if ($gift['msg'] != NULL) {
            echo "<div class='user__status user__status-message'>";
            echo toOutput($gift['msg']);
            echo "</div>";
        }
    }
    echo "<span style='color: grey;' title='" . date::time($gift['time']) . "'>$type " . date::times($gift['time']) . "</span>";

    if ($ank['id'] == $user['id'] || $user['level'] >= 3) {
        echo "<br />";
        if ($ank['id'] == $user['id'] && ($gift['type'] == 1 || $gift['type'] == 2 && ($ank['id'] == $user['id'] || $user['level'] >= 3) || $gift['type'] == 3 && $user['level'] >= 3)) {
            $doc->Link('btn btn-success btn-sm', "?id=$ank2[id]&amp;do", 'gift', 'Отправить подарок');
        }
        $doc->Link('btn btn-success btn-sm', "?id=$ank[id]&amp;del=$gift[id]", 'trash-o', 'Удалить');
    }

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

    require_once H . 'sys/inc/tfoot.php';
    exit();
}
if (isset($_GET['del']) && intval($_GET['del']) != NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `gifts` WHERE `id` = '" . intval($_GET['del']) . "'"), 0) != 0) {
    $gift = mysql_fetch_array(mysql_query("SELECT * FROM `gifts` WHERE `id` = '" . intval($_GET['del']) . "'"));
    if ($gift['id_user'] == $user['id'] || $user['level'] >= 3) {
        mysql_query("DELETE FROM `gifts` WHERE `id` = '" . intval($_GET['del']) . "'");
        header("Location:?id=$ank[id]");
        exit;
    }
}

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `gifts` WHERE `id_user` = '$user[id]' AND `read` = '0'"), 0) != 0 && $ank['id'] == $user['id']) {
    mysql_query("UPDATE `gifts` SET `read` = '1' WHERE `id_user` = '$user[id]' AND `read` = '0'");
    header("Location:?id=$user[id]");
}

$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gifts` WHERE `id_user` = '$ank[id]'"), 0);

#Навигация
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$ank[id]'>$ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Подарки');
echo "</small></div><br />";

$doc->Link('list-group-item-null list-group-item-grey', "?id=$ank[id]&amp;do", 'gift', 'Отправить подарок');

if ($k_post == 0) {
    $doc->NoResult();
}

$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 `gifts` WHERE `id_user` = '$ank[id]' ORDER BY `time` DESC LIMIT $start, $set[p_str]");
while ($post = mysql_fetch_array($q)) {
    $ank2 = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$post[id_kont]'"));
    $gift = mysql_fetch_array(mysql_query("SELECT * FROM `gift` WHERE `id` = '$post[id_gift]'"));

    echo "<table class='list-group-item-null'><tr><td class='icon14'>";
    echo "<img src='images/$gift[image]' height='30' width='30'/>";
    echo "</td><td class='null'>";
    echo "<div style='float:right;' id='hides'>";
    echo "<span title='" . date::time($post['time']) . "'>" . date::timek($post['time']) . "</span>";
    echo "</div>";

    echo "<a href='?id=$ank[id]&amp;gift=$post[id]'>" . gradient("$ank2[nick]", "$ank2[ncolor]", "$ank2[ncolor2]") . "</a>";
    echo "</td></tr></table>";
    echo "<div class='hr'></div>";
}

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

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

