<?php
if (!defined('MODULE_FILE')) {
	Header("Location: ../../../index.php");
	exit;
}

if (isset($_GET['basepath']) || isset($_GET['adminpath'])) { 
	Header("Location: index.php"); 
	die(); 
}

function displayIconCat($row) {
	global $gallerypath, $admin, $user;
	
	$imgsize = @getimagesize("$gallerypath/$row[galloc]/$row[gallimg]");
	if ($row[visible]==1) {
		if (is_admin($admin) || is_user($user)) {
			return '<a href="index.php?name=Album&file=index&do=showgall&amp;gid='.$row[gallid].'" title="'.$row[gallname].'">'
			.'<img src="'.$gallerypath.'/'.$row[galloc].'/'.$row[gallimg].'" alt="'.$row[gallname].'" '.$imgsize[3].' border="0"></a>';
		} else {
			return '<img src="'.$gallerypath.'/'.$row[galloc].'/'.$row[gallimg].'" alt="'.$row[gallname].'" '.$imgsize[3].' border="0">';
		}
	} else {
		return '<a href="index.php?name=Album&file=index&do=showgall&amp;gid='.$row[gallid].'" title="'.$row[gallname].'">'
		.'<img src="'.$gallerypath.'/'.$row[galloc].'/'.$row[gallimg].'" alt="'.$row[gallname].'" '.$imgsize[3].' border="0"></a>';
	}
}

function displayDescriptionCat($row) {
	return ''.nl2br($row[description]).'';
}

function displayCatName($row) {
	global $prefix, $imagepath, $admin, $user, $admin_file, $db;

	if ($row[visible]==1 && !is_user($user)) $vis = '<img src="'.$imagepath.'/users.png" border="0" title="Только для пользователей">&nbsp;</td><td>';

	if (is_admin($admin)) {
		switch ($row[visible]) {
			case 0:
			$vis2 = '&nbsp;<a class="title" href="'.$admin_file.'.php?op=album&do=editcategory&amp;type=status&amp;visible=1&amp;category='.$row[gallid].'" title="'._GALVISIBLEADMIN.'"><img src="'.$imagepath.'/red_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEADMIN.'"></a>';
			break;
			case 1:
			$vis2 .= '&nbsp;<a class="title" href="'.$admin_file.'.php?op=album&do=editcategory&amp;type=status&amp;visible=2&amp;category='.$row[gallid].'" title="'._GALVISIBLEMEMBER.'"><img src="'.$imagepath.'/yellow_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEMEMBER.'"></a>';
			break;
			default:
			$vis2 .= '&nbsp;<a class="title" href="'.$admin_file.'.php?op=album&do=editcategory&amp;type=status&amp;visible=0&amp;category='.$row[gallid].'" title="'._GALVISIBLEPUBLIC.'"><img src="'.$imagepath.'/green_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEPUBLIC.'"></a>';
			break;
		}
	}

	list($count) = $db->sql_fetchrow($db->sql_query("SELECT count(*) FROM $prefix"._album_pictures." WHERE gid=$row[gallid]"));
	
	$out = '<table border="0" cellpadding="0" cellspacing="0"><tr><td>';
	if ($row[visible]==1 && !is_user($user) && !is_admin($admin)) {
		$out .= ''.$vis.'<b>'.underscoreTospace($row[gallname]).'&nbsp;('.$count.')</b></td><td>'.$vis2.'</td>';
	} else {
		$out .= ''.$vis.'<a class="title" href="index.php?name=Album&file=index&do=showgall&amp;gid='.$row[gallid].'" title="'.$row[gallname].'">'.underscoreTospace($row[gallname]).'&nbsp;('.$count.')</a></td><td>'.$vis2.'</td>';
	}

	if ($row[new_day] == 0) {
		$out .= '<td>&nbsp;<img src="'.$imagepath.'/newred.gif" border="0" alt="'._GALNEWMEDIA1.'"></td>';
	} elseif($row[new_day] < 3) {
		$out .= '<td>&nbsp;<img src="'.$imagepath.'/newgreen.gif" border="0" width="34" height="14" alt="'.sprintf(_GALNEWMEDIA2, $row[new_day]).'"></td>';
	} elseif($row[new_day] < 7) {
		$out .= '<td>&nbsp;<img src="'.$imagepath.'/newblue.gif" border="0" width="34" height="14" alt="'.sprintf(_GALNEWMEDIA2, $row[new_day]).'"></td>';
	}
	$out .= '</tr></table>';
	return $out;
}

function navigationSubCat($row) {
	global $bgcolor1, $bgcolor2, $admin, $prefix, $db;

	if (is_admin($admin)) {
		$result = $db->sql_query("SELECT gallid, gallname FROM $prefix"._album_categories." WHERE parent=$row[gallid] AND visible>=0 ORDER by gallname");
	} else {
		$result = $db->sql_query("SELECT gallid, gallname FROM $prefix"._album_categories." WHERE parent=$row[gallid] AND visible>0 ORDER by gallname");
	}

	if (!$db->sql_numrows($result)) {
		return;
	}
	$c = false;
	$out = '<table border="0" cellpadding="0" cellspacing="0"><tr><td>';
	while($row = $db->sql_fetchrow($result)) {
		if($c) {
			$out.= ', ';
		}
		$out .= '<a class="title" href="index.php?name=Album&file=index&do=showgall&amp;gid='.$row[gallid].'" title="'.$row[gallname].'">'.$row[gallname].'</a>';
		$c = true;
	}
	$out .= '</td></tr></table>';
	return $out;
}

function viewcats() {
	global $module_name, $galleryvar, $adminpath, $admin, $user, $prefix, $db;
	include("modules/$module_name/admin/config.php");
	
	$sql = "SELECT templateCategory, templateCSS FROM $prefix"._album_template_types." WHERE id=".$galleryvar['maintemplate']."";
	$trow = $db->sql_fetchrow($db->sql_query($sql));

	if (is_admin($admin)) {
		$result = $db->sql_query("SELECT *, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._album_categories." WHERE parent=-1 AND visible>=0 ORDER BY gallname");
	} else {
		$result = $db->sql_query("SELECT *, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._album_categories." WHERE parent=-1 AND visible>0 ORDER BY gallname");
        $res = $db->sql_fetchrow($db->sql_query("SELECT count(*) as total FROM $prefix"._album_categories." WHERE parent=-1 AND visible=1"));
	}

	$nb = $db->sql_numrows($result);
	if($nb >= $galleryvar['numColMain']) {
		$pc = 100/$galleryvar['numColMain'];
	} else {
		if ($nb > 0) {
			$pc = 100/$nb;
			$galleryvar['numColMain'] = $nb;
		} else {
			$pc = 100;
			$galleryvar['numColMain'] = 1;
		}
	}
	OpenTable();
	print ''.navigationGall().'<br><table width="100%" border="0" cellspacing="0" cellpadding="2"><tr>';
	$c = 0;
	while($row = $db->sql_fetchrow($result)) {
		$template = $trow[templateCategory];
		$template = str_replace('<:IMAGE:>', displayIconCat($row), $template);
		$template = str_replace('<:GALLNAME:>', displayCatName($row), $template);
		$template = str_replace('<:DESCRIPTION:>', displayDescriptionCat($row), $template);
		$template = str_replace('<:SUBCATEGORIES:>', navigationSubCat($row), $template);
		print '<td align="center" valign="top" width="'.$pc.'%">'.$template.'</td>';
		$c++;
		if ($c == $galleryvar['numColMain'] & $nb != $galleryvar['numColMain']) {
			print '</tr><tr>';
			$c = 0;
		}
	}
	print '</tr><tr><td colspan="'.$galleryvar['numColMain'].'" align="center">';
	if ($res[total] > 0 && !is_user($user)) {
		print '<font class="tiny"><i>'._GALCATMEMBERS.'</i></font>';
	}
	print '</td></tr></table>';
	CloseTable();
	
	//m@rt coding go...
if ($galleryvar['allowlastp']) {
OpenTable();

$kolonok = $galleryvar['lastpkol'];
echo "<center><font class=\"title\">Последние изображения</font></center><br><br>"
."<table align=\"center\" border=\"0\"><tr><td align=\"center\" valign=\"top\">";

$row = mysql_query("SELECT p.pid, p.img, p.gid, p.date, p.submitter, p.counter, p.votes, p.rate, c.galloc, c.gallname FROM $prefix"._Album_pictures." AS p LEFT JOIN $prefix"._Album_categories." AS c on c.gallid=p.gid order by p.pid DESC limit ".$galleryvar['lastplimit']."");
while($pic = mysql_fetch_array($row)) {

$i++;$j++;

echo "<a href=\"index.php?name=Album&file=index&do=showgall&gid=$pic[gid]\">$pic[gallname]<br><a href=\"index.php?name=Album&file=index&do=showpic&pid=$pic[pid]&orderby=dateD\"><img src=\"modules/Album/album/$pic[galloc]/$pic[img]\" width=\"".$galleryvar['lastpsize']."\" hspace=\"4\" vspace=\"4\" border=\"0\"></a><br>$pic[date]<br>Добавил: <b>$pic[submitter]</b><br>Голосов: $pic[votes] / Рейтинг: $pic[rate]<br><a href=\"index.php?name=Album&file=index&do=showpic&pid=$pic[pid]&orderby=dateD\">Просмотров:</a> $pic[counter]<br><br>";

	if ($kolonok!=1) {
		if (($i == $kolonok)) {
			$i = 0; 
			echo "</td></tr>";
			if ($limit!=$j) {echo "<tr><td bgcolor=\"$bgcolor1\" align=\"center\" valign=\"top\">";}
		} else {
			echo "</td><td bgcolor=\"$bgcolor1\" align=\"center\" valign=\"top\">";
		}
}
}
if ($limit==$kolonok) {echo "</table>";} else {echo "</td></tr></table>";}
CloseTable();
}
//Finish m@rt coding
	
include("footer.php");
}
?>