<?php

$str = "";

if ($this->oUrl->page == $this->oUrl->pageDefault)
	$page = $this->getGeneralConfig("main_page");
else
	$page = $this->oUrl->page;
$module = $this->oMenu->getModule($page);
$typeOfMenu = $this->oMenu->getTypeOfMenu($page);
$nadpis = $this->oMenu->getNadpis($page);
$pageID = $this->oMenu->getID($page);

if ($typeOfMenu == "redir")
	$this->_home($module);
if ($typeOfMenu == "class" || $typeOfMenu == "core" || $typeOfMenu == "tmpl") {
	if ($typeOfMenu == "tmpl")
		$obj = new tmpl($this->oDb, $this->oUrl, $this->oSession, $this->action);
	else
		$obj = new $module($this->oDb, $this->oUrl, $this->oSession, $this->action, $this->oDbS);
	$printList = TRUE;
	$postID = array_key_exists("id",$_POST) ? $_POST['id'] : 0;
	switch ($obj->action) {
	case "New":
			$obj->rights = $obj->oSession->getRights($pageID, -1);
			$obj->checkRights($pageID, 0, "can_insert");
			$str .= $obj->printForm($this->action, 0);
			$printList = FALSE;
		break;
	case "Edit":
			$obj->rights = $obj->oSession->getRights($pageID, 0);
			if (!$obj->hasRights("can_write", $pageID, 0)) { 
				$obj->rights = $obj->oSession->getRights($pageID, $postID);
				$obj->checkRights($pageID, $postID, "can_write");
			}
			$obj->getDb($postID);
			$str .= $obj->printForm($this->action, $postID);
			$printList = FALSE;
		break;
	case "Insert":
			$obj->rights = $obj->oSession->getRights($pageID, -1);
			$obj->checkRights($pageID, 0, "can_insert");
			$obj->dbInsert();
		break;
	case "Save":
			$obj->rights = $obj->oSession->getRights($pageID, 0);
			if (!$obj->hasRights("can_write", $pageID, 0)) { 
				$obj->rights = $obj->oSession->getRights($pageID, $postID);
				$obj->checkRights($pageID, $postID, "can_write");
			}
			$obj->dbUpdate($postID);
		break;
	case "Delete":
			$obj->rights = $obj->oSession->getRights($pageID, 0);
			if (!$obj->hasRights("can_write", $pageID, 0)) { 
				$obj->rights = $obj->oSession->getRights($pageID, $postID);
				$obj->checkRights($pageID, $postID, "can_write");
			}
			$obj->dbDelete($postID);
		break;
	case "Back":
	default:
		break;
	}
	if ($printList) {
		$obj->rights = $obj->oSession->getRights($pageID, $obj->oUrl->rightsAction);
		$obj->checkRights($pageID, $obj->oUrl->rightsAction, "can_read");
		if ($obj->formError && $this->action != "Delete") {
			$obj->getDb($postID);
			if ($this->action == "Insert") $act = "New";
			if ($this->action == "Save") $act = "Edit";
			$obj->readFormInPP = TRUE;
			$str .= $obj->MSG;
			$str .= $obj->printForm($act, $postID);
		} else {
			if ($typeOfMenu == "tmpl") {
				$tRes = $this->oDb->obtain("SELECT id_tmpl FROM tmpl
					WHERE `module` = '".$module."'
					AND tmpl_type = 'page'
					ORDER BY id_tmpl DESC LIMIT 1");
				if (is_array($tRes) && array_key_exists("id_tmpl",$tRes))
					$obj->getDb($tRes['id_tmpl']);
				else
					$obj->getDb();
				$str .= $obj->MSG;
				$str .= $obj->printList();
			} elseif ($this->oUrl->action > 0) {
				$obj->getDb($this->oUrl->action);
				$str .= $obj->MSG;
				$str .= $obj->printDetail();
			} else {
				$obj->getDb();
				$str .= $obj->MSG;
				$str .= $obj->printList();
			}
		}
	}
	if (property_exists($obj, "nadpisOverride"))
		$nadpis = $obj->nadpisOverride;
} elseif ($typeOfMenu == "page") {
	$this->rights = $this->oSession->getRights($pageID, $this->oUrl->action);
	$this->checkRights($pageID, $this->oUrl->action, "can_read");
	$pluginPath = $this->path['plugins'] . $module . "/";
	$pluginURI = $this->path['plugins_url'] . $module . "/";
	$pluginFile = $pluginPath . "{$module}.tmpl.php";
	$corePluginFile = $this->path['core_tmpl'] . "_".$module.".tmpl.php";
	$postData = $_POST;
	if (file_exists($pluginFile)) {
		ob_start();
		include $pluginFile;
		$str .= ob_get_contents();
		ob_end_clean();
	} elseif (file_exists($corePluginFile))
		$str = include $corePluginFile;
	else
		showErrorTemplate("plugin",$pluginFile);
}

return $this->_HTML($nadpis, $str);

?>
