<?php

require_once '../../incfiles/core.php';
require_once '../../incfiles/func.php';
require_once '../../incfiles/auth.php';
require_once '../../incfiles/user.php';

if (empty($user_id)) go(URL);

if (!empty($_GET['id']) && is_numeric($_GET['id']))
{
	$id = abs(intval($_GET['id']));
	$result = mysql_query("SELECT * FROM `groups` WHERE `id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true) $arr_group = mysql_fetch_array($result);
	else go(URL);
	if (!in_array($user_id, explode("||", substr($arr_group['admins'], 1, -1)))) go(URL);
}
else go(URL);

$title = lang('Вложения','Вкладення');
require_once '../../incfiles/head.php';

echo '<div class="title">
	<a href="'.URL.'/groups/?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Сообщество','Спільнота').'</a>
	<a href="'.URL.'/groups/adm/?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Управление сообществом','Керування спільнотою').'</a>
	</div>';

$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `groups_wall_photos` WHERE `wall_id` = '".$id."'"),0);

if (!empty($_GET['del']))
{
	$del = abs(intval($_GET['del']));	
	$result = mysql_query("SELECT * FROM `groups_wall_photos` WHERE `id` = '".$del."' AND `wall_id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == false) go(URL.'/groups/adm/wall_photos.php?id='.$id);
	else $arr_photo = mysql_fetch_array($result);
	
	if (mysql_query("DELETE FROM `groups_wall_photos` WHERE `id` = '".$del."' LIMIT 1") == true)
	{
		mysql_query("UPDATE `groups` SET `count_wall_photos` = '".($arr_group['count_wall_photos']-1)."' WHERE `id` = '".$id."' LIMIT 1");
		if (file_exists(HOME .'/files/groups/wall/'.$id.'/photos/'.$arr_photo['name'])) {@unlink(HOME .'/files/groups/wall/'.$id.'/photos/'.$arr_photo['name']);}
		if (file_exists(HOME .'/files/groups/wall/'.$id.'/photos/icons/'.$arr_photo['name'])) {@unlink(HOME .'/files/groups/wall/'.$id.'/photos/icons/'.$arr_photo['name']);}
		if (file_exists(HOME .'/files/groups/wall/'.$id.'/photos/mini/'.$arr_photo['name'])) {@unlink(HOME .'/files/groups/wall/'.$id.'/photos/mini/'.$arr_photo['name']);}
		if (file_exists(HOME .'/files/groups/wall/'.$id.'/photos/preview/'.$arr_photo['name'])) {@unlink(HOME .'/files/groups/wall/'.$id.'/photos/preview/'.$arr_photo['name']);}
	}
	go(URL.'/groups/adm/wall_photos.php?id='.$id);



}
else if (!empty($_GET['n']) && is_numeric($_GET['n']))
{
	echo '<div class="title">'.lang('Просмотр фотографии','Перегляд фотографії').'</div><div class="block">';

	$n = ($_GET['n']>1 and $_GET['n']<=$all) ? $_GET['n'] : 1;
	$start = $n*1-1;
	$result = mysql_query("SELECT * FROM `groups_wall_photos` WHERE `wall_id` = '".$id."' ORDER by `time` DESC LIMIT $start,1");
	if (mysql_num_rows($result) == false) go(URL.'/groups/adm/wall_photos.php?id='.$id);
	while ($arr_photo = mysql_fetch_array($result))
	{
		echo '<center><img class="preview" src="'.URL.'/files/groups/wall/'.$id.'/photos/preview/'.$arr_photo['name'].'"/></center>';

		echo '<p><center>';
		p($n,$all,URL.'/groups/adm/wall_photos.php?id='.$id.'&');
		echo ' | <b>'.$n.' '.lang('из','з').' '.$all.'</b> |';
		n($n,$all,URL.'/groups/adm/wall_photos.php?id='.$id.'&');
		echo'</center></p>';
	
		echo '</div><div class="block">
		Фотоальбом: <a href="'.URL.'/groups/adm/wall_photos.php?id='.$id.'" onclick="Page.Go(this.href); return false">'.$title.'</a><br/>
		'.lang('Добавлено','Додана').': '.vtime($arr_photo['time']).'</div>';

		echo '<div class="title">
		<a href="'.URL.'/groups/adm/wall_photos.php?id='.$id.'&del='.$arr_photo['id'].'" onclick="Page.Go(this.href); return false">'.lang('Удалить фото','Видалити фото').'</a>
		</div>';
	}
	
	
	
}
else
{



echo '<div class="title">'.$title.'</div>
	<div class="block"><center>';

	if ($all > 0)
	{
		$cp = ceil($all/12);
		$p = (isset($_GET['p']) and is_numeric($_GET['p']) and $_GET['p']>1 and $_GET['p']<=$cp) ? $_GET['p'] : 1;
		$start = $p*12-12;
		$n = $start;
		$res = mysql_query("SELECT * FROM `groups_wall_photos` WHERE `wall_id` = '".$id."' ORDER by `time` DESC LIMIT $start,12");
		while ($arr = mysql_fetch_array($res))
		{
			$n++;
			echo '<a href="'.URL.'/groups/adm/wall_photos.php?id='.$id.'&n='.$n.'" onclick="Page.Go(this.href); return false;"><img class="preview_mini" src="'.URL.'/files/groups/wall/'.$id.'/photos/mini/'.$arr['name'].'"></a>';
		}
	}
	echo'</center></div>';

	navi($p,$cp,'/groups/adm/wall_photos.php?id='.$id.'&');
}

require_once '../../incfiles/foot.php';

?>