<?php
/*~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
!		!!! Разработка сайтов и отдельных модулей на PHP !!!
!	* - Автор скрипата   :  lugaro
!	* - E-mail           :  lugaro@bk.ru
!	* - Сайт             :  wapNEW.ru
!	* - ICQ              :  2000882
~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!*/
/*Просмотр списка фото*/
$tpl->assign_file('mod', 'tpl/inc/dir.tpl');
$id = (int)$_GET['id'];
$dir = $db->assoc($db->query("SELECT id AS dir_id, name AS dir_name, (SELECT id FROM foto_albom WHERE id_user = '$user_id' AND id_dir = foto_dir.id) AS id_albom FROM foto_dir WHERE id = '$id'"));
if (empty($dir['dir_name'])) header('Location: ?'); //Нет такой папки
$dir['linc'] = 'mod=dir&amp;id=' . $id . '&amp;page=' . (int)$_GET['page'];
$tpl->assign($dir);
if ($user_id) {
    if ($dir['id_albom']) $tpl->parse('index.dir.albom');
    else $tpl->parse('index.dir.new_albom');
}

// Удаление фото
if (isset($_GET['del_file']) && $moder) {
    $del_file = (int)$_GET['del_file'];
    $file = $db->assoc($db->query("SELECT * FROM `foto_file` WHERE `id` = '$del_file'"));
    $file = 'files/' . $file['id_dir'] . '/' . $file['id_albom'] . '/' . $file['id'] . '.' . $file['ext'];
    if (is_file($file)) unlink($file);
    $db->query("DELETE FROM `foto_file` WHERE `id` = '$del_file'");
    $com = new comments_class_by_lugaro('fotoalbom', $del_file);
	$com->del_group();
}

$all = $db->result($db->query("
		SELECT COUNT(*) FROM `foto_file` WHERE
		(
			(`close` = 0 OR `close` = 1 )
			OR
			(`id_user` = '$user_id' AND close = 2)
		)
		AND
		(`id_user` = '$user_id' OR `mod` = 0)
		 AND `id_dir` = $id
	"));

if ($all) {
    $n = new navigator($all, $konf_albom['navi'], '?mod=dir&amp;id=' . $id . '&amp;');
    $n->no_button = true;
    $tpl->assign('page', $n->page);
    // Сортировка
    $tpl->parse('index.dir.sort');
    if (isset($_GET['sort'])) $_SESSION['AlbomSort'] = $_GET['sort'];
    $sort = array('`time` DESC', '`name`', '`ocenka` DESC');
    $sort = empty($sort[(int)$_SESSION['AlbomSort']]) ? $sort[0] : $sort[(int)$_SESSION['AlbomSort']];
    // Настройки предпросмотра
    $tpl->parse('index.dir.prev');
    if (isset($_GET['prev'])) $_SESSION['AlbomPrev'] = $_GET['prev'];

    $sql = $db->query("
			SELECT *  FROM `foto_file` WHERE
			(
				(`close` = 0 OR `close` = 1 )
				OR
				(`id_user` = '$user_id' AND close = 2)
			)
			AND
			(`id_user` = '$user_id' OR `mod` = 0)
			AND `id_dir` = $id ORDER BY $sort LIMIT {$n->start()},{$konf_albom['navi']}
		");

    while (false != ($res = $db->assoc($sql))) {
        $res['color'] = $res['ocenka'] < 0 ? '#FF5353' : '#00FF80';
        $tpl->assign($res);
        if (($admin || $moder) && $vid_adm) $tpl->parse('index.dir.while.admin');
        if ($res['mod']) $tpl->parse('index.dir.while.foto_mod'); //На модерации
        $tpl->parse('index.dir.while');
    }
    $tpl->assign('navi', $n->navi());
} else {
    $tpl->parse('index.dir.no_file');
}

$tpl->parse('index.dir');

?>