<?php
/********************************************
** Knjižnica, ki skrbi za logiranje napak	 **
** Author: Dave @ zabec.net	 				 **
** Date: 2007 						 				 **
********************************************/

/* class errorHandling */
/***********************/
class errorHandling
{

	var $db;
	var $debug;
	var $sporocilo;
	var $nacin;
	var $debug_data;
	var $vrsta;

	//constructor
	function errorHandling($debug, $nacin, $path_template = "", $mod_path = "", $db = "")
	{
		$this->db = $db;
		$this->debug = $debug;
		$this->mod_path = $mod_path;
		$this->path_template = $path_template;
		$this->nacin = $nacin;
	}

	//logiranje, zahteva sporočilo, debug data in tip napake
	function log($sporocilo, $output, $debug_data = "", $nacin = "")
	{
		//prenesemo spremenljivke
		if(!$nacin)
		{
			$this->nacin = $this->nacin;
		}
		else
		{
			$this->nacin = $nacin;
		}
		$this->output = $output;
		$this->sporocilo = $sporocilo;

		//preverimo če je debug mode
		if ($this->debug)
		{
			$this->debug_data = $debug_data;
		}
		//ni debug mode
		else
		{
			$this->debug_data = "";
		}

		//preverimo tip loga (databaza ali samo prikaz)
		switch($this->nacin)
		{
			case "db":
				$this->log_db();
			break;
			case "none":
				$this->checkOutput();
			break;
		}
	}

	//logiramo v mysql (tabela core_log)
	function log_db()
	{
		//preverimo če je aktivna povezava do baze
		if ($this->db != "")
		{
			$napaka = $this->sporocilo." ".$this->debug_data;
			//zapišemo log v databazo
			$this->db->query("INSERT INTO core_log (datum, sporocilo, ip, uporabnik) VALUES (NOW(), '$napaka', '$_SERVER[REMOTE_ADDR]', '$_SESSION[id]')");
		}
		//povezava ni aktivna, dodamo v error
		else
		{
			$this->debug_data .= "<br />Log v databazo ni mogoč, ker povezava ni na voljo!";
		}

		//prikažemo obvestilo v brskalnik
		$this->checkOutput();
	}

	//preveromo kakšen je output
	function checkOutput()
	{
		global $ajax;

		/* če je ajax aplikacija */
		if($ajax)
		{
			/* izpišemo specifičen header, ki ga potem preberemo v javascriptu in prikažemo popup */
			header("HTTP/1.0 800 Napaka");
		}
		switch($this->output)
		{
			case "fatal":
				$this->displayFatal();
			break;
			case "warning":
				$this->displayWarning();
			break;
			case "echo":
				$this->displayEcho();
			break;
			case "logonly":
			break;
		}
	}

	//error, ki se izpiše preko Templatov - fatal
	function displayFatal()
	{
		$tpl = new TemplatePower($this->mod_path."templates/".$this->path_template."/error.tpl");
		$tpl->assignInclude("header", $this->mod_path."templates/".$this->path_template."/header.tpl");
		$tpl->prepare();
		$tpl->assign("sporocilo", $this->sporocilo);
		$tpl->assign("debug_data", $this->debug_data);

		/* Language */
		require('lib_language.php');
		
		$tpl->printToScreen();
		exit;
	}

	function displayEcho()
	{
		echo $this->sporocilo."<br>".$this->debug_data;
		exit;
	}

	//error, ki se izpiše preko Templatov - fatal
	function displayWarning()
	{
		global $tpl;
		//izpišemo napako na pripravljeno mesto
		$tpl->assign("error", $this->sporocilo);
	}
}
?>