<?php

defined('_IN_JOHNCMS') or die('Error: restricted access');
require('../incfiles/head.php');

if ($rights == 9) {
if (!$al) {
    echo functions::display_error($lng['error_wrong_data']);
    require('../incfiles/end.php');
    exit;
}

$req = mysql_query("SELECT * FROM `album_cat` WHERE `id` = '$al'");
if (!mysql_num_rows($req)) {
    echo functions::display_error($lng['error_wrong_data']);
    require('../incfiles/end.php');
    exit;
}
$album = mysql_fetch_assoc($req);
$view = isset($_GET['view']);

/*
-----------------------------------------------------------------
Просмотр альбома и фотографий
-----------------------------------------------------------------
*/
if ($view) {
    $kmess = 1;
    $start = (mysql_result(mysql_query("SELECT COUNT(*) FROM `album_files` WHERE `album_id` = '$al' AND `id` > '$img'"), 0));
    // Обрабатываем ссылку для возврата
    if (empty($_SESSION['ref']))
        $_SESSION['ref'] = htmlspecialchars($_SERVER['HTTP_REFERER']);
} else {
    unset($_SESSION['ref']);
}

echo '<div class="phdr">'.($view ? '<a href="?act=moder&amp;al=' . $al . '"><b>' . functions::checkout($album['name']) . '</b></a>' : '<b>' . functions::checkout($album['name']) . '</b>').'</div>';
if (isset($_GET['ok'])) {
mysql_query("UPDATE `album_files` SET `moder` = '0' WHERE `id`");
echo '<div class="menu">Qabul qilindi</div>';
}
$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `album_files` WHERE `album_id` = '$al' AND `moder` = '1'"), 0);
if ($total) {
    $req = mysql_query("SELECT * FROM `album_files` WHERE `album_id` = '$al' AND `moder` = '1' ORDER BY `id` DESC LIMIT $start, $kmess");
    while (($res = mysql_fetch_assoc($req)) !== FALSE) {
        if ($view) {
            /*
            -----------------------------------------------------------------
            Предпросмотр отдельного изображения
            -----------------------------------------------------------------
            */
            echo '<div class="menu"><img src="../files/album/' . $res['tmb_name'] . '" />';
            // Счетчик просмотров
            if (!mysql_result(mysql_query("SELECT COUNT(*) FROM `album_views` WHERE `file_id` = '" . $res['id'] . "'"), 0)) {
                mysql_query("INSERT INTO `album_views` SET `file_id` = '" . $res['id'] . "', `time` = '" . time() . "'");
                $views = mysql_result(mysql_query("SELECT COUNT(*) FROM `album_views` WHERE `file_id` = '" . $res['id'] . "'"), 0);
                mysql_query("UPDATE `album_files` SET `views` = '$views' WHERE `id` = '" . $res['id'] . "'");
            }
        echo '<div class="sub">';
            echo functions::display_menu(array(
                '<a href="?act=moder&amp;al=' . $al . '&amp;img=' . $res['id'] . '&amp;ok">Aktivlash</a>',
                '<a href="?act=image_edit&amp;img=' . $res['id'] . '">' . $lng['edit'] . '</a>',
                '<a href="?act=image_move&amp;img=' . $res['id'] . '">' . $lng['move'] . '</a>',
                '<a href="?act=image_delete&amp;img=' . $res['id'] . '">' . $lng['delete'] . '</a>'
            ));
        echo  '<div class="gray">' . $lng['count_views'] . ': ' . $res['views'] . ', ' . $lng['count_downloads'] . ': ' . $res['downloads'] . '</div>' .
            '<div class="gray">' . $lng['date'] . ': ' . functions::display_date($res['time']) . '</div>' .
            '<a href="?act=image_download&amp;img=' . $res['id'] . '">' . $lng['download'] . '</a>' .
            '</div></div>';
        } else {
            /*
            -----------------------------------------------------------------
            Предпросмотр изображения в списке
            -----------------------------------------------------------------
            */
            echo '<div class="menu"><img src="../files/album/' . $res['tmb_name'] . '" style="width: 105px; height: 105px; float: left;" alt="#"/>';
        echo '<div class="gray" style="min-height: 110px; margin-left: 110px;">
		    ' . $lng['count_views'] . ': <b>' . $res['views'] . '</b><br/>
		    ' . $lng['count_downloads'] . ': <b>' . $res['downloads'] . '</b><br/>
            <b>' . functions::display_date($res['time']) . '</b><br/><br/>
			<a href="?act=moder&amp;al=' . $al . '&amp;img=' . $res['id'] . '&amp;view"><input type="submit" value="Просмотр"/></a>
			</div><div style="clear: both;"></div></div>';
        }
    }
} else {
    echo '<div class="menu"><p>' . $lng['list_empty'] . '</p></div>';
}
if (!$view) {
echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
if ($total > $kmess) {
    echo '<div class="topmenu">' . functions::display_pagination('?act=moder&amp;al=' . $al . '&amp;' . ($view ? 'view&amp;' : ''), $start, $total, $kmess) . '</div>';
}
}
}
