<?
$title = "Фотографии";
$l = "Фотографии";

require (CORE."head.php");
echo"<div class='app-block'>";
require('inc.php');
if (isset($_GET['dell_file']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_files` WHERE `id` = '".intval($_GET['dell_file'])."' AND `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' AND `time` = '0'") > 0)
{
    if (is_file("".FILES_PATH."/groups/files/".intval($_GET['dell_file']).".jpg"))
    {
        unlink("".FILES_PATH."/groups/files/".intval($_GET['dell_file']).".jpg");
    }
    DB :: $pdo -> query("DELETE FROM `groups_files` WHERE `id` = '".intval($_GET['dell_file'])."' AND `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' AND `time` = '0' LIMIT 1");
    header("Location: /groups/?group=".$group['id']."");
    exit;
}
if (isset($_GET['album']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `photos_album` WHERE `id` = '".intval($_GET['album'])."'") > 0)
{
    if (isset($_GET['photo']) && DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `photos` WHERE `id` = '".intval($_GET['photo'])."' AND  `id_user` = '".$user['id']."' AND `id_album` = '".intval($_GET['album'])."'") > 0)
    {
        if (DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `groups_files` WHERE `id_user` = '".$user['id']."' AND `id_group` = '".$group['id']."' AND `time` = '0'") >= 4)
        {
        ?>
            <div class="list _gray _font-s _center">Вы прикрепили максимальное количество фотографий!</div>
        <?
        } else {
            $r = DB :: $pdo -> queryFetch("SELECT * FROM `photos` WHERE `id` = ? LIMIT 1;", array(intval($_GET['photo'])));
            DB :: $pdo -> query("INSERT INTO `groups_files` (`id_user`, `id_group`) VALUES (?, ?);", array($user['id'], $group['id']));
            $last_id = DB :: $pdo -> lastInsertId();
            copy("".FILES_SERVER.system :: photosGetView($r['hash'])."", "".FILES_PATH."/groups/files/".$last_id.".jpg");
            chmod("".FILES_PATH."/groups/files/".$last_id.".jpg", 0777);
            header("Location: /groups/?group=".$group['id']."");
            exit;
        }
    } else {
        $total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `photos` WHERE `id_user` = '".$user['id']."' AND `id_album` = '".intval($_GET['album'])."'");
        if ($total == 0)
        {
        ?>
            <div class="list _gray _font-s _center">Фотографии не найдены!</div>
        <?
        } else {
            if ($total > 10)
            {
                $p = (($_GET['p']) ? intval($_GET['p']) : 1);
                $pagination = new pagination(10, $total, $p);
                $total_lin = $pagination -> limit;
            } else {
                $total_lin = '';
            }
            ?>
           <div class="_photos-main list">
            <?
            $query = DB :: $pdo -> query("SELECT * FROM `photos` WHERE `id_user` = '".$user['id']."' AND `id_album` = '".intval($_GET['album'])."' ORDER BY `id` DESC ".$total_lin."");
            while($r = $query -> fetch())
            {
            ?>
            
             <a href="/groups/?group=<?echo $group['id'];?>&komm_files&album=<?echo intval($_GET['album']);?>&photo=<?echo $r['id'];?>" class="_photos-item">
				<div class="_photos-item-padd">
				<div class="_photos-cont">
					<div class="_photos-ph">
						<div class="_photos-ph-t"><img src="<?=FILES_SERVER.system :: photosGetView($r['hash'],3)?>" class="_photos-prew">
						</div>
					</div>
				</div>
				</div>
			</a>
               
            <?
            }
            ?>
            </div>
            <?
        if ($total > 10)
        {
            echo $pagination -> printPages("/groups/?group=".$group['id']."&komm_files&album=".intval($_GET['album']).""); 
        }
        }
    }
    echo system :: touch("/groups/?group=".$group['id']."&komm_files", system :: icon("arr-l")."К альбомам");
} else {
    $total = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `photos_album` WHERE `id_user` = '".$user['id']."'");
    if ($total == 0)
    {
    ?>
        <div class="list _gray _font-s _center">Альбомы не найдены!</div>
    <?
    } else {
        if ($total > 10)
        {
            $p = (($_GET['p']) ? intval($_GET['p']) : 1);
            $pagination = new pagination(10, $total, $p);
            $total_lin = $pagination -> limit;
        } else {
            $total_lin = '';
        }
        ?>
       <div class="_photos-main list">
        <?
        $query = DB :: $pdo -> query("SELECT * FROM `photos_album` WHERE `id_user` = '".$user['id']."' ORDER BY `id` DESC ".$total_lin."");
        while($r = $query -> fetch())
        {
        $photos = DB :: $pdo -> querySingle("SELECT COUNT(*) FROM `photos` WHERE `id_user` = '".$user['id']."' AND `id_album` = '".$r['id']."'");
        ?>
         <a href="/groups/?group=<?echo $group['id'];?>&komm_files&album=<?echo $r['id'];?>" class="_photos-item">
				<div class="_photos-item-padd">
				<div class="_photos-cont">
					<div class="_photos-ph">
						<div class="_photos-ph-t"><img src="<?=FILES_SERVER.system :: photosGetCover($r['id']); ?>" class="_photos-prew">
						</div>
						<div>
							<div class="_font-s _photos-title"> <?echo stripcslashes(htmlspecialchars($r['name']));?></div>
							<div class="_font-m"><?echo $photos;?> фотографий</div>
						</div>
					</div>
				</div>
				</div>
			</a>
        
        

        <?
        }
        ?>
        </div>
        <?
        if ($total > 10)
        {
            echo $pagination -> printPages("/groups/?group=".$group['id']."&komm_files&"); 
        }
    }
}
echo system :: touch("/groups/?group=".$group['id']."", system :: icon("arr-l")."Назад");
require (CORE."foot.php");
?>