<?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=magazin&file=index&do=showgall&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=magazin&file=index&do=showgall&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 $imagepath, $admin, $user, $admin_file;
	if ($row[visible]==1 && !is_user($user)) $vis = '<font class="tiny">(*)</font>';
	if (is_admin($admin)) {
		switch ($row[visible]) {
			case 0:
			$vis .= '&nbsp;<a class="title" href="'.$admin_file.'.php?op=magazin&do=editcategory&type=status&visible=1&category='.$row[gallid].'" title="'._GALVISIBLEADMIN.'"><img src="'.$imagepath.'/red_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEADMIN.'"></a>&nbsp;';
			break;
			case 1:
			$vis .= '&nbsp;<a class="title" href="'.$admin_file.'.php?op=magazin&do=editcategory&type=status&visible=2&category='.$row[gallid].'" title="'._GALVISIBLEMEMBER.'"><img src="'.$imagepath.'/yellow_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEMEMBER.'"></a>&nbsp;';
			break;
			default:
			$vis .= '&nbsp;<a class="title" href="'.$admin_file.'.php?op=magazin&do=editcategory&type=status&visible=0&category='.$row[gallid].'" title="'._GALVISIBLEPUBLIC.'"><img src="'.$imagepath.'/green_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEPUBLIC.'"></a>&nbsp;';
			break;
		}
	}

	if ($row[visible]==1 && !is_user($user) && !is_admin($admin)) {
		$out = '<font class="title">'.underscoreTospace($row[gallname]).'</font> <font class="content">('.$row[total].') '.$vis.'</font>';
	} else {
		$out = '<a class="title" href="index.php?name=magazin&file=index&do=showgall&gid='.$row[gallid].'" title="'.$row[gallname].'">'.underscoreTospace($row[gallname]).'</a> <font class="content">('.$row[total].') '.$vis.'</font>';
	}
	if ($row[new_day] == 0) {
		$out .= ' <img src="'.$imagepath.'/newred.gif" border="0" alt="'._GALNEWMEDIA1.'">';
	} elseif($row[new_day] < 3) {
		$out .= ' <img src="'.$imagepath.'/newgreen.gif" border="0" width="34" height="14" alt="'.sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
	} elseif($row[new_day] < 7) {
		$out .= ' <img src="'.$imagepath.'/newblue.gif" border="0" width="34" height="14" alt="'.sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
	}
	return $out;
}

function navigationSubCat($row) {
	global $admin, $prefix, $db;
	if (is_admin($admin)) {
		$result = $db->sql_query("SELECT gallid, gallname FROM $prefix"._magazin_categories." WHERE parent=$row[gallid] AND visible>=0 ORDER by gallname");
	} else {
		$result = $db->sql_query("SELECT gallid, gallname FROM $prefix"._magazin_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=magazin&file=index&do=showgall&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"._magazin_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"._magazin_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"._magazin_categories." WHERE parent=-1 AND visible>0 ORDER BY gallname");
        $res = $db->sql_fetchrow($db->sql_query("SELECT count(*) as total FROM $prefix"._magazin_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();
	include("footer.php");
}
?>