<?php
/* ************************************************************************** *
 *																										*
 * Core mod - lib_mod_general.php v3.1 by Slodave, 2007 - avgust 2008,			*
 * http://www.zabec.net																			*
 * -------- -------- -------- -------- -------- -------- -------- --------		*
 * 																									*
 * Datoteka vsebuje osnovne funkcionalnosti CMSja klicane preko ajaxa			*
 * ************************************************************************** */

// Vključimo potrebne datoteke in nastavitve
/******************************************************************************/
$ajax = "../../";

//posebnost swfuploada (prenos sessiona) - session start je klican v config.php
if ((isset($_POST["PHPSESSID"]) AND $_POST['action'] == "upload")OR($_GET['ovca'])){
	session_id($_POST["PHPSESSID"]);
	session_name('ZabecCMS');
}

require($ajax.'sys/lib_language.php');
require($ajax."sys/config.php");

//vkluči inicializacijo
require($ajax."sys/init.php");

/* Osebne nastavitve																				*/
/* ************************************************************************** */
if($_POST['action'] == "osebne_nastavitve" OR $_POST['action'] == "osebne_save") {
	include("../../sys/ostalo_lib/osebne_nastavitve.php");
}

//preverimo če ima uporabnik dovoljenje za branje
if(checkView($_SESSION['modul_id'])) {

	//uredimo potrebne spremenljivke
	$mod = $_SESSION['modul'];
	if($_SESSION['act'] == "osnutek") {
		$_SESSION['act'] = "view";
	}
	//za sublevel se izognemo
	$path = $ajax."mod/".$_SESSION['modul']."/".$_SESSION['act'];
	$modul_selected = $_SESSION['modul'];

	/* Izbris itema																				*/
	/* *********************************************************************** */
	if($_POST['action'] == "izbrisi" AND $_POST['id'])	{
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			//kličemo funkcijo izbrisi() iz lib_mod_general.php
			izbrisi($_POST['id']);
		}
		else {
			noWritePermission();
		}
	}

	/* Izbris subitema																				*/
	/* *********************************************************************** */
	if($_POST['action'] == "izbrisi_sub" AND $_POST['id'] AND $_POST['tabelca']){
		//if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			//kličemo funkcijo izbrisi() iz lib_mod_general.php
			izbrisi_sub($_POST['id'],$_POST['tabelca']);
		/*}
		else {
			noWritePermission();
		}*/
	}

	/* Vrstni red																					*/
	/* *********************************************************************** */
	if($_POST['action'] == "vrstni_red" AND $_POST['premakni']  AND $_POST['id']) {
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			vrstni_red($_POST['id'], $_POST['premakni']);
		}
		else {
			noWritePermission();
		}
	}

	/* Premikanje itema v osnutek																*/
	/* *********************************************************************** */
	if($_POST['action'] == "osnutek" AND $_POST['id'])	{
		if(checkWrite($_SESSION['modul_id'])) {
		$clean['act'] = "view";
		include($path."/settings.php");
		//klicemo funkcijo premakni_v_osnutek() iz lib_listing.php
		premakni_v_osnutek($_POST['id']);
		}
		else {
			noWritePermission();
		}
	}

	/* Standardni listing																		*/
	/* *********************************************************************** */
	if($_POST['action'] == "listing" AND $_POST['stran'] AND $_POST['order']){
		$clean['act'] = "view";
		include($path."/settings.php");
	}

	/* Iskanje																						*/
	/* *********************************************************************** */
		if($_POST['action'] == "isci" AND $_POST['iskano_geslo']) {
			$clean['act'] = "view";
			include($path."/settings.php");
		}

	/* Standardni detailni pregled															*/
	/* *********************************************************************** */
	if($_POST['action'] == "detail")	{
		include($path."/settings.php");
		require($ajax."sys/modul_lib/lib_detail.php");
	}

	/* Kopiranje obstoječega vnosa v novega												*/
	/* *********************************************************************** */
		if($_POST['action'] == "copynew") {
			$clean['id'] = intval($_POST['id']);
			include($path."/settings.php");
			include("lib_copynew.php");
			$copynew = new copyNew($clean['id']);
			echo $copynew->output();
		}

	/* Zgodovina sprememb na itemu															*/
	/* *********************************************************************** */
	if($_POST['action'] == "zgodovina")	{
		include($path."/settings.php");
		include("lib_zgodovina.php");
	}

	/* Generiranje editorja																		*/
	/* *********************************************************************** */
	if($_POST['action'] == "editor")	{
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			//cel proces se izvede v lib_editor_generate.php
			include("lib_editor_generate.php");
		}
		else {
			noWritePermission();
		}
	}
	
	/* Generiranje novega editorja za sublevel																	*/
	/* *********************************************************************** */
	if($_POST['action'] == "editor_novi")	{
		$path = $ajax."mod/".$_POST['modul']."/".$_POST['page'];
		$modul_selected = $_POST['modul'];
		$_SESSION['sublevel_active'] = '1';
		$_SESSION['modul'] = $_POST['modul'];
		$_SESSION['act'] = $_POST['page'];
		//if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			//cel proces se izvede v lib_editor_generate.php
			include("lib_editor_generate.php");
		/*}
		else {
			noWritePermission();
		}*/
	}
	
	/* Odstranjevanje sessionov, ki so bili v uporabi med editiranjem za sublevel				*/
	/* *********************************************************************** */
	if($_POST['action'] == "odstrani_editor_podatke_sublevel")	{
		//if(checkWrite($_SESSION['modul_id'])) {
			$clean['id'] = intval($_POST['id']);
			include($path."/settings.php");
			//funkcija iz lib_mod_general.php
			editor_remove_session_required($mod_settings['editor_stopnja'], $clean['id']);
			$_SESSION['sublevel_active'] = '0';
			$_SESSION['modul'] = $_SESSION['back_modul'];
			$_SESSION['act'] = $_SESSION['back_act'];
			$_SESSION['modul_id'] = $_SESSION['back_modul_id'];
		/*}
		else {
			noWritePermission();
		}*/
	} 
	/* Odstranjevanje sessionov, ki so bili v uporabi med editiranjem				*/
	/* *********************************************************************** */
	if($_POST['action'] == "odstrani_editor_podatke")	{
		if(checkWrite($_SESSION['modul_id'])) {
			$clean['id'] = intval($_POST['id']);
			include($path."/settings.php");
			//funkcija iz lib_mod_general.php
			editor_remove_session_required($mod_settings['editor_stopnja'], $clean['id']);
		}
		else {
			noWritePermission();
		}
	}

	/* Shranjevanje editorja																	*/
	/* *********************************************************************** */
	if($_POST['action'] == "editor_save") {
		if(checkWrite($_SESSION['modul_id'])) {
			//preverimo spremenljivke
			$clean['id'] = intval($_POST['id']);
			$clean['objava'] = intval($_POST['objava']);
			include($path."/settings.php");
			include("lib_editor_save.php");
			if(in_array($_POST['lang'],$mod_settings['active_lang'])) {
				$clean['lang'] = $_POST['lang']."_";
			}
			$editor_stage = "response";
			include($path."/custom_editor.php");
			//če so dodatna polja pri galeriji
			if(count($galerija_field) > 0) {
				//kličemo funkcijo, ki shrani ta dodatna polja (iz lib_editor_save.php)
				saveGalerijaField($clean['id'], $galerija_field);
			}
			//če so dodana polja pri datoteki
			if(count($datoteka_field) > 0) {
				//kličemo funkcijo, ki shrani ta dodatna polja (iz lib_editor_save.php)
				saveDatotekaField($clean['id'], $datoteka_field);
			}
			//kličemo funkcijo, ki shrani vsebino v standardne tabele (iz lib_editor_save.php)
			$id = saveEdit($sqlclean, $clean, $mod_settings, $lang);
			checkRequired($_POST['required'], $id);

			echo $id;
		}
		else {
			noWritePermission();
		}
	}

	/* Upload datotek in slik																	*/
	/* *********************************************************************** */
	if($_POST['action'] == "upload") {
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			include("lib_editor_upload.php");
			$clean['id'] = intval($_POST['id']);
			if(in_array($_POST['lang'],$mod_settings['active_lang'])) {
				$clean['trenutni_jezik'] = $_POST['lang'];
			}
			else {
				$clean['trenutni_jezik'] = $mod_settings['active_lang'][0];
			}

			switch($_POST['tip']) {
				case "datoteke":
					//poskrbimo za prenos datotek (funkcija iz lib_editor_upload.php)
					addDatoteka($_FILES["Filedata"], $clean, $lang);
				break;
				default:
					//poskrbimo za prenos slik (funkcija iz lib_editor_upload.php)
					addSlika($_FILES["Filedata"], $clean['id'], $_POST['tip'], $lang);
				break;
			}
		}
		else {
			noWritePermission();
		}
		
	}

	/* Odstranjevanje posamezne slike v editorju											*/
	/* *********************************************************************** */
	if($_POST['action'] == "remove_slika")	{
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			$clean['id'] = intval($_POST['id']);
			//nastavimo standardno obliko slike
			$custom = "slika";
			//če ne gre za standardno sliko, preverimo če je $_POST['custom'] veljavna
			if(isset($_SESSION[$clean['id']."_"."customslike"])) {
				if(in_array($_POST['custom'], $_SESSION[$clean['id']."_"."customslike"])) {
					$custom = $_POST['custom'];
				}
			}
			//odstranimo sliko (funkcija iz mod_lib_general.php)
			izbrisi_slika($clean['id'], false, true, $custom);
			}
		else {
			noWritePermission();
		}
	}

	/* Odstranjevanje posamezne slike iz galerije v editorju							*/
	/* *********************************************************************** */
	if($_POST['action'] == "remove_slika_galerija") {
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			$clean['id'] = intval($_POST['id']);
			$clean['id_slika'] = intval($_POST['id_slika']);
			//odstranimo sliko iz galerije (funkcija iz lib_mod_general.php)
			izbrisi_slika_galerija($clean['id'], $clean['id_slika']);
		}
		else {
			noWritePermission();
		}
	}

	/* Odstranjevanje posameznega videa iz galerije v editorju						*/
	/* *********************************************************************** */
	if($_POST['action'] == "remove_video_galerija") {
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");

			$clean['id'] = intval($_POST['id']);
			$clean['id_video'] = intval($_POST['id_video']);
			//odstranimo video iz galerije (funkcija iz lib_mod_general.php)
			izbrisi_video_galerija($clean['id'], $clean['id_video']);
		}
		else {
			noWritePermission();
		}
	}

	/* Vrstni red galerije v editorju														*/
	/* *********************************************************************** */
	if($_POST['action'] == "vrstni_red_galerija" AND $_POST['premakni']  AND $_POST['id']) {
		if(checkWrite($_SESSION['modul_id'])) {
			$clean['id'] = intval($_POST['id']);
			include($path."/settings.php");
			//popravimo vrstni red (funkcija iz lib_mod_general.php)
			vrstni_red($clean['id'], $_POST['premakni'], $vrsta = "galerija");
		}
		else {
			noWritePermission();
		}
	}

	/* Odstranjevanje posamezne datoteke v editorju										*/
	/* *********************************************************************** */
	if($_POST['action'] == "remove_datoteka")	{
		if(checkWrite($_SESSION['modul_id'])) {
			include($path."/settings.php");
			$clean['id'] = intval($_POST['id']);
			$clean['id_datoteka'] = intval($_POST['id_datoteka']);
			//izbrisemo datoteko (funkcija iz lib_mod_general.php)
			izbrisi_datoteka($clean['id'], $clean['id_datoteka']);
		}
		else {
			noWritePermission();
		}
	}

	/* File browser, zaenkrat se uporablja za dodajanje videov v galerijo		*/
	/* *********************************************************************** */
	if($_POST['action'] == "file_browser" OR $_POST['action'] == "file_browser_save") {
		$clean['id'] = intval($_POST['id']);
		require($path."/settings.php");
		include("../../sys/modul_lib/lib_filebrowser.php");
	}
	
	if($_POST['action'] == "dodaj_element_seznam"){
		$clean['id'] = intval($_POST['id']);
		$clean['parent_id'] = intval($_POST['parent_id']);
		$clean['tabela'] = $_POST['tabela'];
		require($path."/settings.php");
		
		if($clean['tabela']){
			$tabela=$clean['tabela'];
		}else{
			$tabela='seznam_tabela';	
		}
		
		dodaj_element_seznam($clean['id'],$clean['parent_id'],$tabela);
	}
	
	if($_POST['action'] == "odstrani_element_seznam"){
		$clean['id'] = intval($_POST['id']);
		$clean['tabela'] = $_POST['tabela'];
		require($path."/settings.php");
		
		if($clean['tabela']){
			$tabela=$clean['tabela'];
		}else{
			$tabela='seznam_tabela';	
		}
		
		odstrani_element_seznam($clean['id'],$tabela);
	}
	
	/* Insert dummy zaradi slike															*/
	/* *********************************************************************** */
	if($_POST['action'] == "insert_dummy") {
			include($path."/settings.php");

			$result = $db->query("INSERT INTO ".$mod_settings['tabela']." (osnutek) VALUES (1)");
			$usedid = $result->insertID();
			//nastavimo session, ki definira da gre za nov vnos
			$_SESSION[$usedid.'_newitem'] = true;

			echo $usedid;
	}
	
}

?>