<?php

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

if (!isset($user) && !isset($_GET['id_user'])) {
    header("Location: " . DIR_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: " . DIR_FOTO . "?" . SID);
    exit;
}

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

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

$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')");
}

err();
aut();

include 'inc/gallery.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 lang('Альбомы') . " $ank[nick]";
echo "</small></div>";

if (isset($user) && ($user['id'] == $ank['id'])) {
    echo "<div class='list-group-item-null'>";
    $doc->Link('btn btn-secondary btn-sm', "" . DIR_FOTO . "$user[id]/?act=create", 'plus', 'Создать альбом');
    echo "</div>\n";
}

$q = $db->super_query("SELECT COUNT(*) as count FROM `gallery` WHERE `id_user` = '$ank[id]' AND `my` = '0'");
$k_post = $q['count'];
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];

$where = NULL;
$q = $db->query("SELECT * FROM `gallery`");
while ($post = $db->get_row($q)) {
    $ank2 = get_user($post['id_user']);
    $friends = $db->super_query("SELECT COUNT(*) as count FROM `friends` WHERE (`user` = '$user[id]' AND `friends` = '$ank2[id]') OR (`user` = '$ank2[id]' AND `friends` = '$user[id]')");
    if ($post['access'] == 'all' || $post['access'] == 'friends' && ($ank2['level'] < $user['level'] || isset($user) && $ank2['id'] == $user['id']) || $post['access'] == 'pass' || $post['access'] == 'friends' && ($friends['count'] != 0 || $ank2['level'] < $user['level'] || isset($user) && $ank2['id'] == $user['id']) || $post['access'] == 'auth' && isset($user)) {
        $where = $where . " " . ($where == NULL ? "AND (" : "OR ") . "`id` = '$post[id]'";
    }
    unset($friends);
}
if ($where != NULL) {
    $where = $where . ")";
}

$z = $db->query("SELECT * FROM `gallery` WHERE `id_user` = '$ank[id]' AND `my` = '1' ORDER BY `time` DESC LIMIT $start, $set[p_str]");

while ($posts = $db->get_row($z)) {
    $fotos = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id_gallery` = '$posts[id]' ORDER BY RAND()"));

    $q = $db->super_query("SELECT COUNT(*) as count FROM `gallery_foto` WHERE `id_gallery` = '$posts[id]'");

    echo "<table class='list-group-item-null-mini list-group-item-info dop'><tr><td class='icon14'>";
    if ($fotos == null) {
        echo "<div class='photos'>";
        echo "<div class='photos_blok'><i class='fa fa-camera fa-fw'></i> <b>" . $q['count'] . "</b></div>";
        echo "<a href='" . DIR_FOTO . "$ank[id]/$posts[id]/'><img style='width:100px;height:100px;' src='" . DIR_FOTO . "foto128/0.png' alt='" . lang('Нет фото') . "' />";
        echo "</a></div>";
    } else {
        echo "<div class='photos'>";
        echo "<div class='photos_blok'><i class='fa fa-camera fa-fw'></i> <b>" . $q['count'] . "</b></div>";
        echo "<a href='" . DIR_FOTO . "$ank[id]/$posts[id]/'>";
        echo "<img style='width:100px;height:100px;' src='" . DIR_FOTO . "foto150/$fotos[id].$fotos[ras]' title='" . $q['count'] . " " . lang('фото') . "' alt='" . lang('Фото') . "_$fotos[id]' />";
        echo "</a></div>";
    }
    echo "</td><td class='null'>\n";
    echo "<a href='" . DIR_FOTO . "$ank[id]/$posts[id]/'><i class='fa fa-photo fa-fw'></i> " . toOutput($posts['name']) . "</a><br />";
    echo "</td></tr></table>\n";
}

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

$q = $db->query("SELECT * FROM `gallery` WHERE `id_user` = '$ank[id]' AND `my` = '0' ORDER BY `time` DESC LIMIT $start, $set[p_str]");

while ($post = $db->get_row($q)) {
    $foto = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id_gallery` = '$post[id]' ORDER BY RAND()"));
    $count = $db->super_query("SELECT COUNT(*) as count FROM `gallery_foto` WHERE `id_gallery` = '$post[id]'");

    echo "<table class='list-group-item-null-mini list-group-item-grey dop'><tr><td class='icon14'>";
    if ($foto == null) {
        echo "<div class='photos'>";
        echo "<div class='photos_blok'><i class='fa fa-camera fa-fw'></i> <b>" . $count['count'] . "</b></div>";
        echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/'><img style='width:100px;height:100px;' src='" . DIR_FOTO . "foto128/0.png' alt='" . lang('Нет фото') . "' />";
        echo "</a></div>";
    } else {
        echo "<div class='photos'>";
        echo "<div class='photos_blok'><i class='fa fa-camera fa-fw'></i> <b>" . $count['count'] . "</b></div>";
        echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/'>";
        echo "<img style='width:100px;height:100px;' src='" . DIR_FOTO . "foto150/$foto[id].$foto[ras]' title='" . $count['count'] . " " . lang('фото') . "' alt='" . lang('Фото') . "_$foto[id]' />";
        echo "</a></div>";
    }
    echo "</td><td class='null'>\n";
    echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/'><i class='fa fa-photo fa-fw'></i> " . toOutput($post['name']) . "</a><br />";

    if ($post['opis'] == null) {
        echo "";
    } else {
        if ($post['opis'] == null) {
            echo lang('Без описания') . "<br />";
        } else {
            echo toOutput($post['opis']) . "<br />\n";
        }
    }

    echo "<small style='color:grey;'>";
    if ($post['access'] == 'all') {
        echo "<i class='fa fa-globe fa-fw'></i> " . lang('Доступен всем');
    } elseif ($post['access'] == 'only_me') {
        echo "<i class='fa fa-lock fa-fw'></i> " . lang('Доступен только автору');
    } elseif ($post['access'] == 'friends') {
        echo "<i class='fa fa-users fa-fw'></i> " . lang('Доступен только друзьям автора');
    } elseif ($post['access'] == 'pass') {
        echo "<i class='fa fa-key fa-fw'></i> " . lang('Доступен только по паролю');
    } elseif ($post['access'] == 'auth') {
        echo "<i class='fa fa-user fa-fw'></i> " . lang('Доступен только авторизованным');
    }

    echo "<br />";
    echo user($ank['id']);
    echo " <span title='" . date::time($post['time_create']) . "'>" . date::timek($post['time_create']) . "</span><br />";
    if (isset($user) && (access('foto_alb_del') || $user['id'] == $ank['id'])) {
        if (IS_WEB) {
            echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/?act=edit'><i class='fa fa-edit fa-fw'></i> " . lang('Редактировать') . "</a> &#183; ";
            echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/?act=delete'><i class='fa fa-trash-o fa-fw'></i> " . lang('Удалить') . "</a>";
        } else {
            echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/?act=edit'><i class='fa fa-edit fa-fw'></i> " . lang('Ред') . "</a> &#183; ";
            echo "<a href='" . DIR_FOTO . "$ank[id]/$post[id]/?act=delete'><i class='fa fa-trash-o fa-fw'></i> " . lang('Удалить') . "</a>";
        }
    }
    echo "</small>";
    echo "</td></tr></table>\n";
}

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

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