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

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

	/* funkcija, premakni v osnutke */
	function premakni_v_osnutek($id)
	{
		global $db;
		global $mod_settings;

		//preverimo če sploh so osnutki
		if($mod_settings['osnutek'])
		{
			/* uredimo vrstni red če obstaja */
			if($mod_settings['vrstni_red'])
			{
				vrstni_red($id, "odstrani");
			}

			/* premaknemo item z določenem idjem v osnutke */
			$result = $db->query("UPDATE ".$mod_settings['tabela']." SET osnutek = 1 WHERE id = '$id'");

			/* logiramo in izpišemo error če je prišlo do napake */
			if(!$result)
			{
				echo "error";
				$log->log("", $logmode, "logonly", "Napaka pri premikanju v osnutek, id: ".$clean['id'].", tabela: ".$mod_settings['tabela'], 2);
			}
		}
	}

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

		$a = 0;
		while($r = $result->fetch())
		{
			/* preverimo če so v uporabi kategorije */
			if($mod_settings['kategorija'])
			{
				if($r['kat_parent'] == 0)
				{
					$prikaz_kategorija = $r['kategorija'];
					$puscica = false;
				}
				else
				{
					global $db;
					$kresult = $db->query("SELECT ime FROM ".$mod_settings['kat_tabela']." WHERE id = '$r[kat_parent]'");
					$kr = $kresult->fetch();
					$prikaz_kategorija = $kr['ime']." > ".$r['kategorija'];
					$puscica = true;
				}

				if($prikaz_kategorija != $trenutna_kategorija)
				{
					$tpl->newBlock("kategorija");
					$tpl->assign("kategorija", $prikaz_kategorija);

					$trenutna_kategorija = $prikaz_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");
			}

			if($mod_settings['custom_listing'])
			{
				include($ajax."mod/".$_SESSION['modul']."/".$_SESSION['act']."/custom_listing.php");
			}
			else
			{
				/* prvo polje */
				//odstranimo jezikovne posebnosti
				$contentField[0] = clearLang($contentField[0]);
				if(!$r[$contentField[0]])
				{
					$tpl->assign("first_field", $lang['view_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", $ajax."templates/".$settings['template']."/moduli/view.tpl");
		$tpl->prepare();

		//Pripravi naslov
		if($_SESSION['modul'] == "nastavitve")
		{
			$modul_naslov = $lang['nast_naslov'];
			$action_naslov = ucfirst($_SESSION['act']);
		}
		else
		{
			$result = $db->query("SELECT modul,id FROM core_modules WHERE modul_link='" . $clean['page'] . "'");
			$r = $result->fetch();
			$modul_naslov = $r['modul'];

			$result = $db->query("SELECT text FROM core_modules_menu WHERE modul_id='" . $r['id'] . "' AND link='" . $clean['act'] . "'");
			$r = $result->fetch();
			$action_naslov = $r['text'];
		}
		$tpl->assign("modul",$modul_naslov);
		$tpl->assign("modul_action",$action_naslov);

		//aktiviramo razvrščanje
		if($mod_settings['razvrscanje'])
		{
			$tpl->newBlock("razvrscanje");
			//aktiviramo razvrsti kot na spletni strani (če je vrstni red)
			if($mod_settings['vrstni_red'])
			{
				$tpl->newBlock("razvrsti_vrstni_red");
			}
		}
		//aktiviramo nove vnose
		if(!$mod_settings['disable_new'])
		{
			$tpl->newBlock("enabled_new");
		}
		//aktiviramo gumb premakni v osnutke
		if($mod_settings['osnutek'])
		{
			//osnutki
			$osnutki = steviloOsnutkov($mod_settings['tabela']);
			$tpl->assignGlobal("stevilo_osnutkov", $osnutki);

			$tpl->newBlock("osnutek_premakni");
			$osn_vsi_vnosi = " WHERE osnutek = 0";
		}

		//aktiviramo iskalnik
		if($mod_settings['iskalnik'])
		{
			$tpl->newBlock("iskalnik");
		}

		/* 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'].$osn_vsi_vnosi);
		$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;

		if($mod_settings['osnutek'])
		{
			$sql_osnutek = " AND osnutek = 0";
		}

		$tpl = new TemplatePower($ajax."templates/".$settings['template']."/moduli/view_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 (".$searchSql.") ".$sql_osnutek;

		$tpl->newBlock("kategorija");
		$tpl->assign("kategorija", $lang['view_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['view_iskanje_nizadetkov']);
		}
		$tpl->printToScreen();
	}


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

		if($mod_settings['osnutek'])
		{
			$sql_osnutek = " WHERE osnutek = 0";
			$osnutek = steviloOsnutkov($mod_settings['tabela']);
		}

		if(!$ajax)
		{
			$tpl->assignInclude("view_vsebina", $ajax."templates/".$settings['template']."/moduli/view_vsebina.tpl");
			$tpl->prepare();
			$offset = $clean['stran'] * $user_settings['num_results'];
		}
		else
		{
			$tpl = new TemplatePower($ajax."templates/".$settings['template']."/moduli/view_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]." ASC";
				break;
			case "last":
				$order = "p.zadnja_sprememba DESC";
				break;
			case "lnew":
				$order = "p.zadnji_vnos DESC";
				break;
			case "site":
				if($mod_settings['vrstni_red'])
				{
					$order = "p.vrstni_red ASC";
				}
				else
				{
					$order = $vsebina_polja[0];
				}
				break;
			default:
				if($mod_settings['vrstni_red'])
				{
					$order = "p.vrstni_red ASC";
				}
				elseif($mod_settings['uredi_po'])
				{
					$order = $mod_settings['uredi_po'];
				}
				else
				{
					$order = $vsebina_polja[0];
				}
				break;
		}

		/* generiramo del sqlja */
		$vsebinaSql = "";
		foreach($vsebina_polja AS $field)
		{
			if(!strpos($field, "."))
			{
				$append = "p.";
			}
			$vsebinaSql .= $append.$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";
			}
		}
		/* preverimo če so kategorije */
		if($mod_settings['kategorija'])
		{
			//preverimo če gre ali ne gre za multilevelske kategorije
			if($mod_settings['no_multi_kategorija'])
			{
				$Sparent = "";
				$Oparent = "";
			}
			else
			{
				$Sparent =", t.parent AS kat_parent";
				$Oparent = " t.parent,";
			}
			$sql = "SELECT p.id, ".$slika.$vsebinaSql." t.ime AS kategorija, t.id AS kat_id ".$Sparent." FROM ".$mod_settings['tabela']." AS p LEFT JOIN ".$mod_settings['kat_tabela']." AS t ON p.kategorija = t.id".$join_jezik.$sql_osnutek." ORDER BY ".$Oparent." t.ime ASC, ".$order." LIMIT ".$offset.",".$user_settings['num_results'];
		}

		/* ni kategorij */
		else
		{
			$sql = "SELECT ".$slika.$vsebinaSql." p.id FROM ".$mod_settings['tabela']." AS p ".$join_jezik.$sql_osnutek." ORDER BY ".$order." LIMIT ".$offset.",".$user_settings['num_results'];

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

		if(!$ajax)
		{
			$tpl->assignInclude("view_pages", $ajax."templates/".$settings['template']."/moduli/view_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'].$sql_osnutek);
			$stevilo_vseh_vnosov = $result->size();

			$ptpl = new TemplatePower($ajax."templates/".$settings['template']."/moduli/view_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_once('../lib_language.php');

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



?>