<?php
/********************************************************************
** Knjižnica, kjer so zbrane funkcije za vodenje zadnjih sprememb  **
** Author: Dave @ zabec.net	 												 **
** Date: 2007 						 												 **
********************************************************************/

/* ????
 * klicano iz: @izbrisi() - lib_mod_general.php
 *					@izbrisi_slika() - lib_mod_general.php
 *					@izbrisi_slika_galerija() - lib_mod_general.php
 *					@saveEdit() - lib_editor_save.php
 *					@addDatoteka() - lib_editor_upload.php
 *					@addSlika() - lib_editor_upload.php
 * ????
 */
function novaSprememba($item_id, $sprememba)
{
	global $db;
	/* spremenljivka $modul_selected je definirana v ajax.php (ter left_menu.php) */
	global $mod_settings;

	/* generiramo joine za jezik */
	if($mod_settings['active_lang'])
	{
		foreach($mod_settings['active_lang'] AS $jezik)
		{
			$join_jezik .= " LEFT JOIN ".$mod_settings['tabela']."_".$jezik." AS ".$jezik." ON p.id = ".$jezik.".item_id";
		}
	}
	$result = $db->query("SELECT ".$mod_settings['listing_db_polja'][0]." FROM ".$mod_settings['tabela']." AS p".$join_jezik." WHERE p.id = '$item_id'");
	$r = $result->fetch();

	if($_SESSION['modul'] == "nastavitve")
	{
		$modul = $_SESSION['act'];
	}
	else
	{
		$modul = $_SESSION['modul'];
	}
	$db->query("INSERT INTO core_spremembe (modul, item_id, first_field, change_time, change_user, sprememba) VALUES ('$modul', '$item_id', '$r[0]', NOW(), '$_SESSION[id]', '$sprememba')");
}


/* ????
 * klicano iz: @generateViewListing() - lib_listing.php
 * ????
 */
function getLastSprememba($item_id)
{
	global $db;
	global $ajax;
	if(!$ajax)
	{
		global $clean;
		$modul = $clean['page'];
	}
	else
	{
		global $modul_selected;
		$modul = $modul_selected;
		if($modul == "nastavitve")
		{
			$modul = $_SESSION['act'];
		}
	}
	$result = $db->query("SELECT change_time, username FROM core_spremembe AS p LEFT JOIN core_uporabniki AS d ON p.change_user = d.id WHERE item_id = '$item_id' AND modul = '$modul' ORDER BY change_time DESC LIMIT 0,1");
	$r = $result->fetch();
	/* ni vnosa s tem idjem, dodamo trenutnega uporabnika in čas */
	if(!$r['username'])
	{
		$sprememba = array("uporabnik" => $_SESSION['uporabnik'], "cas" => date("d.m.Y H:i:s"));
	}
	else
	{
		$sprememba = array("uporabnik" => $r['username'], "cas" => formatDateFromDB($r['change_time']));
	}
	return $sprememba;
}

function getFirstSprememba($item_id)
{
	global $db;
	global $ajax;
	if(!$ajax)
	{
		global $clean;
		$modul = $clean['page'];
	}
	else
	{
		global $modul_selected;
		$modul = $modul_selected;
		if($modul == "nastavitve")
		{
			$modul = $_SESSION['act'];
		}
	}
	$result = $db->query("SELECT change_time, username FROM core_spremembe AS p LEFT JOIN core_uporabniki AS d ON p.change_user = d.id WHERE item_id = '$item_id' AND modul = '$modul' ORDER BY change_time ASC LIMIT 0,1");
	$r = $result->fetch();
	/* ni vnosa s tem idjem, dodamo trenutnega uporabnika in čas */
	if(!$r['username'])
	{
		$sprememba = array("uporabnik" => $_SESSION['uporabnik'], "cas" => date("d.m.Y H:i:s"));
	}
	else
	{
		$sprememba = array("uporabnik" => $r['username'], "cas" => formatDateFromDB($r['change_time']));
	}
	return $sprememba;
}

/* Funkcija dobi čas in uporabnika zadnjega vnosa v posameznemu modulu
 * klicano iz: @listing - lib_listing.php
 */
function getGlobalSprememba() {
	global $db;
	global $ajax;
	//preverimo če funkcija ni klicana preko ajaxa
	if(!$ajax) {
		global $clean;
		$modul = $clean['page'];
	}
	//funkcija je klicana preko ajaxa
	else {
		$modul = $_SESSION['modul'];
		//posebnost modula nastavitve
		if($modul == "nastavitve") {
			$modul = $_SESSION['act'];
		}
	}
	//zadnji vnos globalno v modulu
	$result = $db->query("SELECT change_time, username FROM core_spremembe AS p LEFT JOIN core_uporabniki AS d ON p.change_user = d.id WHERE modul = '$modul' AND sprememba='Nova objava' ORDER BY change_time DESC LIMIT 0,1");
	$r = $result->fetch();

	$sprememba = array("uporabnik" => $r['username'], "cas" => formatDateFromDB($r['change_time']));
	return $sprememba;
}

/* Funkcija generira celovit prikaz sprememb za določen id
 * klicano iz: @detail - lib_detail.php
 *					@editor:editor() - lib_editor_generate.php
 * $id je id itema za katerega delamo prikaz sprememb
 */
function spremembeBox($id) {
		global $tpl;

		/* prva sprememba */
		$fsprememba = getFirstSprememba($id);

		$tpl->newBlock("prvi_vnos");
		$tpl->assign("uporabnik",$fsprememba['uporabnik']);
		$tpl->assign("cas",$fsprememba['cas']);

		/* zadnja sprememba */
		$zsprememba = getLastSprememba($id);

		$tpl->newBlock("zadnja_sprememba");
		$tpl->assign("uporabnik",$zsprememba['uporabnik']);
		$tpl->assign("cas",$zsprememba['cas']);
}
?>