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

if (!isset($user) && !isset($_GET['id_user'])) {
    header("Location: /foto/?" . SID);
    exit;
}

if (isset($user)) {
    $ank['id'] = $user['id'];
}

if (isset($_GET['id_user'])) {
    $ank['id'] = intval($_GET['id_user']);
}

$ank = get_user($ank['id']);

if (!$ank) {
    header("Location: /foto/?" . SID);
    exit;
}

$gallery['id'] = intval($_GET['id_gallery']);

$q = $db->super_query("SELECT COUNT(*) as count FROM `gallery` WHERE `id_user` = '$ank[id]' AND `name` = 'Личные фото'");
if ($q['count'] == 0) {
    $db->query("INSERT INTO `gallery` (`id_user`, `name`, `my`) values('$ank[id]', 'Личные фото', '1')");
}

$q = $db->super_query("SELECT COUNT(*) as count FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1");

if ($q['count'] == 0) {
    header("Location: " . DIR_FOTO . "$ank[id]/?" . SID);
    exit;
}

$gallery = $db->get_row($db->query("SELECT * FROM `gallery` WHERE `id` = '$gallery[id]' AND `id_user` = '$ank[id]' LIMIT 1"));

$set['title'] = $ank['nick'] . ' - ' . $gallery['name'] . ' - ' . lang('Фотоальбом'); // заголовок страницы

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

aut();
err();

include 'inc/gallery_show.php';

#Навигация
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='" . DIR_FOTO . "'>" . lang('Фотоальбомы') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FOTO . "$ank[id]/'>" . lang('Альбомы') . " $ank[nick]</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo toOutput($gallery['name']);
echo "</small></div><br />";

if ($ank['level'] < $user['level'] || isset($user) && $ank['id'] == $user['id']) {
    $umodd = 1;
}

if (isset($_POST['password'])) {
    setcookie("passfoto[$gallery[id]]", $_POST['password']);
    if (isset($_POST['password']) && $_POST['password'] == $gallery['password']) {
        header("Location:?enter=ok");
    }
}

if ($gallery['access'] == 'only_me') {
    if ($ank['id'] == $user['id'] && isset($user) || isset($umodd)) {
        $lock = "key";
    } else {
        echo "<div class='bg-danger'><i class='fa fa-lock fa-fw'></i> " . lang('Доступ к альбому ограничен') . "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($gallery['access'] == 'friends') {
    $access_friends = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank[id]') OR (`user` = '$ank[id]' AND `friends` = '$user[id]')");
    if ($ank['id'] == $user['id'] && isset($user) || isset($umodd) || $access_friends['count'] != 0) {
        $lock = "key";
    } else {
        echo "<div class='bg-danger'>";
        echo "<i class='fa fa-users fa-fw'></i> " . lang('Альбом доступен только друзьям автора');
        echo "</div>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($gallery['access'] == 'pass') {
    if (isset($_COOKIE['passfoto'][$gallery['id']]) && $_COOKIE['passfoto'][$gallery['id']] == $gallery['password'] || $ank['id'] == $user['id'] && isset($user) || isset($umodd)) {
        $lock = "key";
    } else {
        if (isset($_POST['password']) && $_POST['password'] != $gallery['password']) {
            $_SESSION['err'] = lang('Неверный пароль');
            header("Location: ?");
        }
        err();

        echo "<form class='list-group-item-null' method='post'>";
        echo lang('Чтобы просмотреть фотоальбом, введите пароль') . ":<br />\n";
        echo "<input name='password' type='text' required value='' class='form-control' style='width: 50%'><br />";

        $doc->Button('btn btn-primary btn-sm', null, null, 'Далее');

        echo "</form>";

        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
} elseif ($gallery['access'] == 'auth') {
    if (isset($user)) {
        $lock = "key";
    } else {
        echo "<div class='bg-danger'><i class='fa fa-user fa-fw'></i> " . lang('Фотоальбом доступен только авторизованным пользователям') . "!</div>";
        require_once H . 'sys/inc/tfoot.php';
        exit;
    }
}

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

echo "<div class='list-group-item-null list-group-item-info' style='color: grey;'><span style='float: right;'>\n";
#Кнопка меню действий
if (isset($user) && $user['id'] == $ank['id'] || access('foto_alb_del')) {

    if (isset($user) && $user['id'] == $ank['id']) {
        echo "<a href='?act=upload' class='hint--left' data-hint='" . lang('Новое фото') . "'><i class='fa fa-camera fa-fw'></i></a> ";
    }

    if (isset($user) && $user['id'] == $ank['id']) {
        echo "<a href='?act=edit' class='hint--left' data-hint='" . lang('Редактировать альбом') . "'><i class='fa fa-edit fa-fw'></i></a> ";
        echo "<a href='?act=delete' class='hint--left' data-hint='" . lang('Удалить альбом') . "'><i class='fa fa-trash fa-fw'></i></a> ";
    } elseif (access('foto_alb_del')) {
        echo "<a href='?act=edit' class='hint--left' data-hint='" . lang('Редактировать альбом') . "'><i class='fa fa-edit fa-fw'></i></a> ";
        echo "<a href='?act=delete' class='hint--left' data-hint='" . lang('Удалить альбом') . "'><i class='fa fa-trash fa-fw'></i></a> ";
    }
}
echo "</span>\n";
echo "<i class='fa fa-camera fa-fw'></i> $k_post</div>\n";

if ($k_post == 0) {
    $doc->NoResult();
}
?>
<link rel="stylesheet" href="/style/css/owl.carousel.css">
<link rel="stylesheet" href="/style/css/owl.theme.default.min.css">
<script src="/style/js/jquery.js"></script>
<script src="/style/js/owl.carousel.js"></script>

<script>
    $('.owl-carousel').owlCarousel({
        loop: true,
        margin: 10,
        nav: true,
        responsive: {
            0: {
                items: 1
            },
            600: {
                items: 3
            },
            1000: {
                items: 5
            }
        }
    })

    $(document).ready(function () {
        $(".owl-carousel").owlCarousel();
    });
</script>

<?php
$q = $db->query("SELECT * FROM `gallery_foto` WHERE `id_gallery` = '$gallery[id]' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

if ($k_post > 0) {
    echo "<div class='owl-carousel owl-theme'>";
    while ($post = $db->get_row($q)) {
        echo "<div class='item'><a href='" . DIR_FOTO . "$ank[id]/$gallery[id]/$post[id]/'><img src='" . DIR_FOTO . "foto640/$post[id].$post[ras]' alt='" . lang('Фото') . "_$post[id]' /></a></div>";
    }
    echo "</div>";
}

if ($k_page > 1) {
    str('?', $k_page, $page);
}

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