<?php
/******************************************************************************/
// Posebna stran modula, kjer se nastavlja editor (request in response)
// Date: 2008
/******************************************************************************/

/* NE SPREMINJAJ **************************************************************/
if($editor_stage == "generate")
{
/******************************************************************************/
	$editor->required(array("anketa", "enabled"));

	switch($clean['stopnja'])
	{
		case "1":
			$_SESSION['editor_stopnja'] = 1;
			$editor->generate_stopnja("Vprašanje");
			$editor->generate_field("anketa", "Vprašanje ankete", "text");
			$status = array("1" => "Aktivna", "0" => "Neaktivna");
			$editor->generate_field("enabled", "Status ankete", "izberi", "", $status);

		break;
		case "2":
			$_SESSION['editor_stopnja'] = 2;
			$editor->generate_stopnja("Odgovori", false);
			$editor->generate_razdelek("Odgovori", "anketa");
			$result1 = $db->query("SELECT * FROM modul_anketa_odgovori WHERE anketa_id = '$clean[id]'");
			$a = 0;
			while($r1 = $result1->fetch())
			{
				$a++;
				$editor->generate_field("odgovor_".$r1['id'], "Odgovor ".$a, "text", "", $r1['odgovor']);
			}
			$editor->generate_addField("anketa");
			$tpl->assign("num_old_fields",$a);
			break;

		default:
			$editor->generate_zakljucek();
		break;
	}

/* NE SPREMINJAJ **************************************************************/
}
elseif($editor_stage == "response")
{
/******************************************************************************/

	switch($_SESSION['editor_stopnja'])
	{
		case 1:
			$sqlclean['anketa'] = inputXSScheck($_POST['anketa']);
			$sqlclean['enabled'] = intval($_POST['enabled']);

		break;
		case 2:
			$sqlclean = array();
			//vnos novih odgovorov
			$clean['num_fields'] = intval($_POST['num_fields']);
			for($i=1; $i<=$clean['num_fields']; $i++)
			{
				$clean['n_odgovor'] = inputXSScheck($_POST['n_odgovor_'.$i]);

				//če polje ni prazno vnesemo v bazo
				if($clean['n_odgovor'])
				{
					$db->query("INSERT INTO modul_anketa_odgovori (anketa_id, odgovor) VALUES ('$clean[id]', '$clean[n_odgovor]')");
				}
			}

			//update starih odgovorov če so spremenjeni
			$result1 = $db->query("SELECT * FROM modul_anketa_odgovori WHERE anketa_id = '$clean[id]'");
			while($r1 = $result1->fetch())
			{
				$clean['odgovor'] = inputXSScheck($_POST['odgovor_'.$r1['id']]);

				//odgovor je spremenjen in ni prazen
				if($r1['odgovor'] != $clean['odgovor'] AND $clean['odgovor'])
				{

					$db->query("UPDATE modul_anketa_odgovori SET odgovor = '$clean[odgovor]' WHERE id = '$r1[id]' AND anketa_id = '$clean[id]'");
				}
			}
		break;
		default:
			$sqlclean = array();
		break;
	}

/******************************************************************************/
}
/******************************************************************************/
?>