<?

$title = "Фотографии";
$l = "Фотографии";
require (CORE."head.php");
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("".$_SERVER['DOCUMENT_ROOT']."/template/groups_files/".intval($_GET['dell_file']).".jpg"))
    {
        unlink("".$_SERVER['DOCUMENT_ROOT']."/template/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']."&edit_komm=".$post['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_post` = '".$post['id']."' AND `id_group` = '".$group['id']."'") >= 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`, `id_post`, `time`) VALUES (?, ?, ?, ?);", array($user['id'], $group['id'], $post['id'], time()));
            $last_id = DB :: $pdo -> lastInsertId();
            copy("".$_SERVER['DOCUMENT_ROOT']."/template/photos/".$r['hash']."", "".$_SERVER['DOCUMENT_ROOT']."/template/groups_files/".$last_id.".jpg");
            chmod("".$_SERVER['DOCUMENT_ROOT']."/template/groups_files/".$last_id.".jpg", 0777);
            header("Location: /groups/?group=".$group['id']."&edit_komm=".$post['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="sim_groups112">
            <?
            $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 class="sim_groups113" href="/groups/?group=<?echo $group['id'];?>&edit_komm=<?echo $post['id'];?>&komm_files&album=<?echo intval($_GET['album']);?>&photo=<?echo $r['id'];?>">
                    <img class="sim_groups114" src="<?echo "/template/photos/".$r['hash']."";?>" alt="*">
                </a>
            <?
            }
            ?>
            </div>
            <?
        if ($total > 10)
        {
            echo $pagination -> printPages("/groups/?group=".$group['id']."&edit_komm=".$post['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="sim_groups105">
        <?
        $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 class="sim_groups106" href="/groups/?group=<?echo $group['id'];?>&edit_komm=<?echo $post['id'];?>&komm_files&album=<?echo $r['id'];?>">
                <div class="sim_groups107">
                    <div class="sim_groups108">
                        <img class="sim_groups109" src="<?echo group_photo($r['id']);?>" alt="*">
                    </div>
                    <div class="sim_groups110">
                        <?echo stripcslashes(htmlspecialchars($r['name']));?>
                    </div>
                    <div class="sim_groups111">
                        <?echo $photos;?>
                    </div>
                </div>
            </a>
        <?
        }
        ?>
        </div>
        <?
        if ($total > 10)
        {
            echo $pagination -> printPages("/groups/?group=".$group['id']."&edit_komm=".$r['id']."&komm_files&"); 
        }
    }
}
echo system :: touch("/groups/?group=".$group['id']."&edit_komm=".$post['id']."", system :: icon("arr-l")."Назад");
require (CORE."foot.php");
?>