<?php
if (!defined('MODULE_FILE')) {
	Header("Location: ../../index.php");
	exit;
}
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
require_once("config/config_page.php");
require_once("function/comments.php");
$pagetitle = "$defis "._CONTENT_NAME."";

function categories($id="") {
	global $prefix, $db, $module_name, $multilingual, $currentlang, $tablenumhome, $homesubcat, $catdescrhome, $shownumeric;
	static $massiv, $massiv2;
	$id = (intval($id)) ? $id : 0;
	if ($id) {
		$where = "where parentid = '$id'";
	} elseif ($id && $multilingual) {
		$where = "where parentid = '$id' AND (planguage='$currentlang' OR planguage='')";
	} elseif ($multilingual) {
		$where = "where planguage='$currentlang' OR planguage=''";
	} else {
		$where = "";
	}
	$tdwidth = intval(100/$tablenumhome);
	if (!is_array($massiv)) {
		$result = $db->sql_query("SELECT f.cid, f.title, f.description, f.parentid, count(c.pid) as kolvo FROM ".$prefix."_page_categories as f left join ".$prefix."_page as c on (f.cid = c.cid and c.active = 1) ".$where." group by f.cid order by 1");
		while (list($cid, $title, $description, $parentid, $kolvo) = $db->sql_fetchrow($result)) $massiv[] = array($cid, $title, $description, $parentid, $kolvo);
	}
	if (!is_array($massiv2)) {
		$result = $db->sql_query("SELECT f.cid, f.title, f.description, f.parentid, count(c.pid) as kolvo FROM ".$prefix."_page_categories as f left join ".$prefix."_page as c on (f.cid = c.cid and c.active = 1) group by f.cid order by 1");
		while (list($cid, $title, $description, $parentid, $kolvo) = $db->sql_fetchrow($result)) $massiv2[] = array($cid, $title, $description, $parentid, $kolvo);
	}
	if ($massiv) {
		foreach ($massiv as $key => $val) {
			if ($val[3] == $id) {
				

				$description = ($catdescrhome) ? "<tr><td colspan=\"2\"><i>".$val[2]."</i></td></tr>" : "";

				#$kolvo = $db->sql_numrows($db->sql_query("SELECT cid  FROM ".$prefix."_page WHERE active = 1 and cid = '$val[0]'"));
				$ccontent .= "<td valign=\"top\" width=\"".$tdwidth."%\"><table border=\"0\"><tr><td><img border=\"0\" src=\"images/blocks/Pages.gif\" title=\"$title\"></td><td width=\"100%\"><a href=\"index.php?name=$module_name&op=cat&id=$val[0]\" title=\"$val[1]\"><b>$val[1]</b> ($val[4])</a></td></tr>".$description."";
				if ($homesubcat == 1 && $massiv2) {
					foreach ($massiv2 as $key => $val2) {
						if ($val[0] == $val2[3]) {
							$ccontent .= "<tr><td colspan=\"2\"><img border=\"0\" src=\"images/navi.gif\" title=\"$val2[1]\"> <a href=\"index.php?name=$module_name&op=cat&id=$val2[0]\" title=\"$val2[1]\">$val2[1] ($val2[4])</a></td></tr>";
						}
					}
				}
				$ccontent .= "</table></td>";
				if ($cont == ($tablenumhome - 1)) {
					$ccontent .= "</tr><tr>";
					$cont = 0;
				} else {
					$cont++;
				}
			}
		}
	}
	if ($ccontent) {
		OpenTable();
		echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\"><tr>".$ccontent."</td></tr></table>";
		if ($shownumeric == 1) {
			list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(pid) FROM ".$prefix."_page WHERE active='1'"));
			list($cat_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(cid) FROM ".$prefix."_page_categories"));
			echo "<hr><center>"._ALLP.": <b>$pages_num</b> "._ALLP2." <b>$cat_num</b> "._ALLP3."</center>";
		}
		CloseTable();
	}
}

function main() {
	global $db, $prefix, $module_name, $multilingual, $currentlang, $tablenumhome, $newhome, $newhomenum, $datetime;
    head();
	title(""._PAGESNOTE."");
	categories();
	$newhomenum = intval($newhomenum);
	if ($newhome == 1) {
		$result = $db->sql_query("SELECT pid, title, hometext, date FROM ".$prefix."_page WHERE cid != '0' AND date <= now() AND active = '1' ORDER BY pid DESC LIMIT $newhomenum");
		if ($db->sql_numrows($result) > 0) {
			OpenTable();
			echo "<font class=\"option\">"._NEWPAGES."</font><hr>";
			while (list($p_pid, $p_title, $p_hometext, $date) = $db->sql_fetchrow($result)) {
				formatTimestamp($date);
				echo "<a href=\"index.php?name=$module_name&op=page&pid=$p_pid\" title=\"$p_title\"><b>$p_title</b></a><br />".bb_decode($p_hometext)."<br /><br />"._SENDDATE.": $datetime<br /><br />";
			}
			CloseTable();
		}
	}
	$result2 = $db->sql_query("SELECT pid, title, hometext, date FROM ".$prefix."_page WHERE cid = '0' AND date <= now() AND active = '1' ORDER BY pid DESC");
	if ($db->sql_numrows($result2) > 0) {
		OpenTable();
		echo "<font class=\"option\">"._PAGESHOMECATS."</font><hr>";
		while (list($p_pid, $p_title, $p_hometext, $date) = $db->sql_fetchrow($result2)) {
			formatTimestamp($date);
			echo "<a href=\"index.php?name=$module_name&op=page&pid=$p_pid\" title=\"$p_title\"><b>$p_title</b></a><br/>".bb_decode($p_hometext)."<br /><br />"._SENDDATE.": $datetime<br /><br />";
		}
		CloseTable();
	}
	get_page($module_name);
	foot();
}

function cat() {
    global $db, $prefix, $module_name, $numpage, $pagenum, $datetime, $pagetitle, $defis, $hometext;
	$cid = intval($_GET['id']);
	list($c_title, $c_description) = $db->sql_fetchrow($db->sql_query("SELECT title, description FROM ".$prefix."_page_categories WHERE cid='$cid'"));
	$pagetitle = "$defis "._CONTENT_NAME." $defis $c_title";
	$hometext = $c_description;
	head();
	title(""._PAGESNOTE."");
	OpenTable();
	echo "<center><font class=\"option\">$c_title</font><br>$c_description</center>";
	CloseTable();
	categories($cid);
    if ($pagenum == "") $pagenum = 1;
	$offset = ($pagenum - 1) * $numpage;
	$offset = intval($offset);
	$numpage = intval($numpage);

	$result = $db->sql_query("SELECT pid, title, hometext, date, counter FROM ".$prefix."_page WHERE cid ='$cid' AND date <= now() AND active = '1' ORDER BY pid DESC LIMIT $offset, $numpage");
    if ($db->sql_numrows($result) > 0) {
		OpenTable();
		$i = 0;
		while (list($p_pid, $p_title, $p_hometext, $date, $p_counter) = $db->sql_fetchrow($result)) {

			formatTimestamp($date);
			$hr = ($i) ? "<hr>" : "";
			echo "$hr<b><a href=\"index.php?name=$module_name&op=page&pid=$p_pid\" title=\"$p_title\">$p_title</a></b><br />".bb_decode($p_hometext)."<br /><br />"._SENDDATE.": $datetime<br />"._COUNTPAGE.": $p_counter "._COUNT."";
			$i++;
		}
		CloseTable();
    }
	list($numstories) = $db->sql_fetchrow($db->sql_query("SELECT Count(pid) FROM ".$prefix."_page WHERE cid ='$cid' AND date <= now() AND active = '1'"));
    $numpages = ceil($numstories / $numpage);
	num_page($module_name, $numstories, $numpages, $numpage, "op=cat&id=".$cid."&");
	get_page($module_name);
    foot();
}

function page($pid, $pagenum=0) {
    global $pagetitle, $hometext, $bodytext, $db, $prefix, $module_name, $admin, $pagecatlink, $pagecatnumlinc, $admin_file, $defis, $datetime;
    $pid = intval($pid);
	$result = $db->sql_query("SELECT cid, title, hometext, bodytext, date, counter  FROM ".$prefix."_page WHERE pid='$pid' AND date <= now() AND active = '1'");
	if ($db->sql_numrows($result) > 0) {
		if ($pagenum == 0) $db->sql_query("UPDATE ".$prefix."_page SET counter=counter+1 WHERE pid='$pid'");
		list($cid, $title, $hometext, $bodytext, $date, $counter) = $db->sql_fetchrow($result);
		$cid = intval($cid);
		$pagetitle = "$defis "._CONTENT_NAME." $defis $title";	
		formatTimestamp($date);
		$contentpages = explode("[pagebreak]", $bodytext);
		$pageno = count($contentpages);
		if ($pagenum=="" || $pagenum < 1) $pagenum = 1;
		if ($pagenum > $pageno) $pagenum = $pageno;
		$arrayelement = (int)$pagenum;
		$arrayelement --;
		list($c_title, $c_description) = $db->sql_fetchrow($db->sql_query("SELECT title, description FROM ".$prefix."_page_categories WHERE cid='$cid'"));
		$c_title = ($c_title) ? $c_title : ""._PAGESHOMECATS."";
		$pagetitle = "$defis "._CONTENT_NAME." $defis $c_title $defis $title";	
		head();
		title(""._PAGESNOTE."");
		OpenTable();
		echo "<center><font class=\"option\">$c_title</font><br />$c_description</center>";
		CloseTable();
		OpenTable();
		echo "<font class=\"option\">$title</font><br />".bb_decode($hometext)."<hr>".bb_decode($contentpages[$arrayelement]).""
		."<hr>"._SENDDATE.": $datetime<br />"._COUNTPAGE.": $counter "._COUNT."";
		if (is_admin($admin)) {
			echo "<hr><center>[ <a href=".$admin_file.".php?op=delete_pages&id=$pid>"._DELETE."</a> | <a href=".$admin_file.".php?op=add_page&id=$pid>"._EDIT."</a> | <a href=".$admin_file.".php?op=active_page&id=$pid&act=0>"._OFFPAGE."</a> ]</center>";
		}
		CloseTable();

ShowComments($pid);

		num_page($module_name, 1, $pageno, 1, "op=page&pid=".$pid."&");
		if ($pagecatlink == 1) {
			$pagecatnumlinc = intval($pagecatnumlinc);
			$result2 = $db->sql_query("SELECT pid, title FROM ".$prefix."_page WHERE cid='$cid' AND pid !='$pid' AND date <= now() AND active = '1' LIMIT $pagecatnumlinc");
			if ($db->sql_numrows($result2) > 0) {
				OpenTable();
				echo "<font class=\"option\">"._LINKPAGES."</font><hr>";
				while (list($l_pid, $l_title) = $db->sql_fetchrow($result2)) {
					echo "<table border=\"0\"><tr><td><a href=\"index.php?name=$module_name&op=page&pid=$l_pid\" title=\"$l_title\"><img border=\"0\" src=\"images/blocks/News.gif\" alt=\"$l_title\"></a></td><td><a href=\"index.php?name=$module_name&op=page&pid=$l_pid\" title=\"$l_title\"><b>$l_title</b></a></td></tr></table>";
				}
				CloseTable();
			}
		}
		get_page($module_name);
		foot();
	} else {
		Header("Location: index.php?name=".$module_name."");
	}
}

switch($op) {
	case "cat":
    cat();
    break;

    case "page":
    page($pid, $pagenum);
    break;

    default:
    main();
    break;
}
?>

