<?php
/******************************************************************************/
// Knjižnica, kjer so zbrane funkcije za listing
// Author: Dave @ zabec.net
// Date: 2008
/******************************************************************************/

// FUNKCIJE MODULA listing
/******************************************************************************/

	/* funkcija ki generira vsebino */
	function generateViewListing($result, $contentField)
	{
		global $tpl;
		global $slika;
		global $mod_settings;

		$a = 0;
		while($r = $result->fetch())
		{
			/* preverimo če so v uporabi kategorije */
			if($mod_settings['kategorija'])
			{
				if($r['kategorija'] != $trenutna_kategorija)
				{
					$tpl->newBlock("kategorija");
					$tpl->assign("kategorija", $r['kategorija']);
				}
				$trenutna_kategorija = $r['kategorija'];
			}

			$tpl->newBlock("zapis");
			$tpl->assign("id", $r['id']);

			/* zadnje spremembe */
			$sprememba = getLastSprememba($r['id']);
			$tpl->assign("last_change_user", $sprememba['uporabnik']);
			$tpl->assign("last_change", $sprememba['cas']);

			/* preverimo če je v uporabi vrstni red */
			if($mod_settings['vrstni_red'])
			{
				$tpl->newBlock("vrstni_red");
				$tpl->assign("id", $r['id']);
				$tpl->gotoBlock("zapis");
			}


			/* slika */
			if($r['slika'] != "")
			{
				$tpl->assign("slika", $mod_settings['slika_path'].$r['slika']);
			}
			else
			{
				$tpl->assign("slika_display", "none");
			}

			/* prvo polje */
			//odstranimo jezikovne posebnosti
			$contentField[0] = clearLang($contentField[0]);

			if(!$r[$contentField[0]])
			{
				$tpl->assign("first_field", $lang['osnutki_nipodatka']);
			}
			else
			{
				$tpl->assign("first_field", $r[$contentField[0]]);
			}

			/* velikost arraya $contentField */
			$numContent = count($contentField);

			/* generiramo vsebino za template (polja brez prvega) */
			for($i = 1; $i<$numContent; $i++)
			{
				//odstranimo jezikovne posebnosti
				$contentField[$i] = clearLang($contentField[$i]);

				$tpl->newBlock("content_field");
				$tpl->assign("field", $r[$contentField[$i]]);
				/* če je zadnje polje */
				if($i == $numContent)
				{
					$tpl->assign("listing_zadnji", " class='listing_zadnji'");
				}
			}
			$a++;
		}
		return $a;
	}

/******************************************************************************/

// GENERIRANJE LISTINGA
/******************************************************************************/

/* view modul */
/**************/
	/* preverimo če je slika */
	$slika = "";
	if($mod_settings['slika'])
	{
		$slika = "slika, ";
	}

/** Osnovni listing */
/********************/
	/* Ni klicano preko ajaxa */
	if(!$ajax)
	{
		$tpl->assignInclude("main_content", "templates/".$settings['template']."/moduli/osnutek.tpl");

		//osnutki
		$osnutki = steviloOsnutkov($mod_settings['tabela']);
		$tpl->assignGlobal("stevilo_osnutkov", $osnutki);

		$tpl->assignInclude("main_content", $ajax."templates/".$settings['template']."/moduli/osnutek.tpl");
		$tpl->prepare();

		//Pripravi naslov
		$result = $db->query("SELECT modul,id FROM core_modules WHERE modul_link='" . $clean['page'] . "'");
		$r = $result->fetch();
		$tpl->assign("modul",$r['modul']);
		//Pripravi akcijo
		$result = $db->query("SELECT text FROM core_modules_menu WHERE modul_id='" . $r['id'] . "' AND link='" . $clean['act'] . "'");
		$r = $result->fetch();
		$tpl->assign("modul_action",$r['text']);

		/* Generiramo naslovna polja */
		foreach($naslov_polja AS $field)
		{
			$tpl->newBlock("naslovno_polje");
			$tpl->assign("polje", $field);
		}

		/* števiko vseh vnosov */
		$result = $db->query("SELECT id FROM ".$mod_settings['tabela']." WHERE osnutek = 1");
		$stevilo_vseh_vnosov = $result->size();
		$tpl->gotoBlock("_ROOT");
		$tpl->assign("stevilo_vseh_vnosov", $stevilo_vseh_vnosov);

		/* zadnji vnos globalno */
		$sprememba = getGlobalSprememba();

		$tpl->assign("global_last_change", $sprememba['cas']);
		$tpl->assign("global_last_change_user", $sprememba['uporabnik']);

		$tpl->gotoBlock("_ROOT");
		$tpl->assign("modpath", "sys/modul_lib");
	}

/* Listing - iskanje */
/*********************/
	elseif($_POST['action'] == "isci")
	{
		$mod_settings['kategorija'] = false;

		$tpl = new TemplatePower($ajax."templates/".$settings['template']."/moduli/osnutek_vsebina.tpl");
		$tpl->prepare();
		$tpl->assignGlobal("template_dir", $ajax."/templates/".$settings['template']);

		/* Language */
		require('../lib_language.php');

		/* generiramo del sqlja */
		$vsebinaSql = "";
		$i = 0;
		foreach($vsebina_polja AS $field)
		{
			$vsebinaSql .= $field.", ";
			if($i == 0)
			{
				$searchSql = $field." LIKE '%".$clean['iskano_geslo']."%'";
			}
			else
			{
				$searchSql .= " OR ".$field." LIKE '%".$clean['iskano_geslo']."%'";
			}
			$i++;
		}
		/* 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";
			}
		}
		$sql = "SELECT ".$slika.$vsebinaSql." p.id FROM ".$mod_settings['tabela']." AS p".$join_jezik." WHERE osnutek = 1 AND (".$searchSql.")";

		$tpl->newBlock("kategorija");
		$tpl->assign("kategorija", $lang['osnutki_iskaliste'] .": ".$clean['iskano_geslo']);

		$tpl->newBlock("iskalnik_nazaj");

		$num = generateViewListing($db->query($sql), $vsebina_polja);
		if($num == 0)
		{
			$tpl->newBlock("ni_vnosov");
			$tpl->assign("colspan", 5+count($naslov_polja));
			$tpl->assign("napis", $lang['osnutki_iskanje_nizadetkov']);
		}
		$tpl->printToScreen();
	}


/* Listing */
/***********/
	if(!$ajax OR $_POST['action'] == "listing")
	{
		/* več strani (dobimo spremenljivko) */
		$clean['stran'] = intval($_POST['stran']);

		if(!$ajax)
		{
			$tpl->assignInclude("view_vsebina", $ajax."templates/".$settings['template']."/moduli/osnutek_vsebina.tpl");
			$tpl->prepare();
			$offset = $clean['stran'] * $user_settings['num_results'];
		}
		else
		{
			$tpl = new TemplatePower($ajax."templates/".$settings['template']."/moduli/osnutek_vsebina.tpl");
			$tpl->prepare();
			$tpl->assignGlobal("template_dir", $ajax."/templates/".$settings['template']);

			/* Language */
			require('../lib_language.php');
		}
		/* generiramo offset */
		if($clean['stran'] > 0)
		{
			$offset = ($clean['stran']-1) * $user_settings['num_results'];
		}
		else
		{
			$offset = 0;
		}

		/* order */
		switch($_POST['order'])
		{
			case "abc":
				$order = $vsebina_polja[0];
				break;
			case "site":
				if($mod_settings['vrstni_red'])
				{
					$order = "vrstni_red";
				}
				else
				{
					$order = $vsebina_polja[0];
				}
				break;
			default:
				$order = $vsebina_polja[0];
				break;
		}

		/* generiramo del sqlja */
		$vsebinaSql = "";
		foreach($vsebina_polja AS $field)
		{
			$vsebinaSql .= $field.", ";
		}
		/* 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";
			}
		}
		$sql = "SELECT ".$slika.$vsebinaSql." id FROM ".$mod_settings['tabela']." AS p".$join_jezik." WHERE osnutek = 1 ORDER BY ".$order." LIMIT ".$offset.",".$user_settings['num_results'];
		$tpl->newBlock("kategorija");
		$num = generateViewListing($db->query($sql), $vsebina_polja);
		if($num == 0)
		{
			$tpl->newBlock("ni_vnosov");
			$tpl->assign("colspan", 5+count($naslov_polja));
			$tpl->assign("napis", $lang['osnutki_niosnutkov']);
		}
		if(!$ajax)
		{
			$tpl->assignInclude("view_pages", $ajax."templates/".$settings['template']."/moduli/osnutek_pages.tpl");
			$tpl->prepare();
			strani($stevilo_vseh_vnosov, $user_settings['num_results'], $tpl);
		}
		else
		{
			/* število vnosov */
			$result = $db->query("SELECT id FROM ".$mod_settings['tabela']." WHERE osnutek = 1");
			$stevilo_vseh_vnosov = $result->size();

			$ptpl = new TemplatePower($ajax."templates/".$settings['template']."/moduli/osnutek_pages.tpl");
			$ptpl->prepare();
			$ptpl->assignGlobal("template_dir", $ajax."/templates/".$settings['template']);
			strani($stevilo_vseh_vnosov, $user_settings['num_results'], $ptpl, $clean['stran']);

			/* Language */
			require('../lib_language.php');

			/* za output */
			$listing = $tpl->getOutputContent();
			$pages = $ptpl->getOutputContent();
			$sprememba = getGlobalSprememba();
			$osnutek = steviloOsnutkov($mod_settings['tabela']);
			$zadnja_sprememba = $sprememba['cas']." ".$sprememba['uporabnik'];
		}
	}



?>