<?php

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

if (isset($_GET['category_id'])) {
    $category_id = (int) $_GET['category_id'];
    $set['p_str'] = 24;
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id_user` = '$user[id]' AND `id_gallery` = '$category_id'"), 0);
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];

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

    $q = mysql_query("SELECT * FROM `gallery_foto` WHERE `id_user` = '$user[id]' AND `id_gallery` = '$category_id' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

    echo "<div class='list-group-item-null'>";

    while ($post = mysql_fetch_assoc($q)) {
        echo "<a href='?type=$type&amp;attachments_id=$post[id]'><img src='/modules/foto/foto48/$post[id].$post[ras]' /></a>";
    }

    echo "</div>";

    if ($k_page > 1) {
        str('?type=' . $type . '&amp;category_id=' . $category_id . '&amp;', $k_page, $page);
    }

    $urlBack = '?type=' . $type . '&amp;category';
} elseif (isset($_GET['category'])) {
    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery` WHERE `id_user` = '$user[id]'"), 0);
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];

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

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

    while ($post = mysql_fetch_assoc($q)) {
        $count = mysql_result(mysql_query("SELECT COUNT(*) FROM `gallery_foto` WHERE `id_gallery` = '$post[id]'"), 0);

        echo "<div class='list-group-item-null'>";
        echo "<a href='?type=$type&amp;category_id=$post[id]'><i class='fa fa-folder fa-fw'></i> $post[name] ($count " . lang('фото') . ")</a>";
        echo "</div>";
    }

    if ($k_page > 1) {
        str('?type=' . $type . '&amp;category&amp;', $k_page, $page);
    }

    $urlBack = '?type=' . $type;
} else {
    $q = mysql_query("SELECT * FROM `gallery_foto` WHERE `id_user` = '$user[id]' ORDER BY `id` DESC LIMIT 5");

    if (mysql_num_rows($q) > 0) {
        echo "<div class='list-group-item-null'>";

        while ($post = mysql_fetch_assoc($q)) {
            echo "<a href='?type=$type&amp;attachments_id=$post[id]'><img src='/modules/foto/foto48/$post[id].jpg' /></a> ";
        }
        echo "</div>";
        echo "<div class='list-group-item-null list-group-item-grey'>";
        echo "<a href='?type=$type&amp;category'>" . lang('Показать все фото') . "</a>";
        echo "</div>";
    } else {
        echo "<div class='alert alert-info'>" . lang('У вас еще нет на сайте загруженных фотографий, но вы можете воспользоваться формой ниже') . ".</div>";
    }

    echo "<form class='list-group-item-null' action='?type=$type' method='POST' enctype='multipart/form-data'>";
    $doc->File('file', 'Файл', 'Выберите изображение');
    $doc->Textarea('msg', 'Описание', 256);
    echo "<br />";
    $doc->Button('btn btn-success btn-sm', 'submit', 'upload', 'Загрузить');

    echo "</form>";

    $urlBack = '/user/mail/messageList.php?contact=' . urlencode($_SESSION['mail']['to']);
}
$doc->Link('list-group-item-null list-group-item-grey', "$urlBack", 'arrow-left', 'Назад');
