<?php
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
//sigurnost:
if (!defined('SIGURICA')) die('Nedozvoljen pristup!');
if ($prava_pristup_admin!=1) die('Nedozvoljen pristup!');

// list expected fields
$expected = array('q_ime','q_ime_dropdown','q_opis','q_selectable','q_stil','sheet_id');
// set required fields
$required = array('q_ime','q_ime_dropdown','sheet_id');
// create empty array for any missing fields
$missing = array();

// process the $_POST variables
foreach ($_POST as $key => $value) {
	// assign to temporary variable and strip whitespace if not an array
	$temp = is_array($value) ? $value : trim($value);
	// if empty and required, add to $missing array
	if (empty($temp) && in_array($key, $required)) {
		array_push($missing, $key);
	}
	// otherwise, assign to a variable of the same name as $key
	elseif (in_array($key, $expected)) {
		${$key} = $temp;
		//${$key} = mysql_real_escape_string($temp);
	}
}
$error='';
$uspjeh='';
if (empty($missing))  {
	//uredi varijable
	$q_opis=uredi_var($q_opis);
	$q_ime=uredi_fajl($q_ime);
	
	//pripremi za unos u bazu
	$q_ime_mysql=mysql_real_escape_string($q_ime);
	$q_ime_dropdown_mysql=mysql_real_escape_string($q_ime_dropdown);
	$q_opis_mysql=mysql_real_escape_string($q_opis);
	$q_stil_mysql=mysql_real_escape_string($q_stil);
	
	
	$result = mysql_query("update b7_stylesheet set 
			ime='$q_ime_mysql',
			ime_dropdown='$q_ime_dropdown_mysql',
			opis='$q_opis_mysql',
			selectable='$q_selectable',
			stil='$q_stil_mysql'
			where id='$sheet_id'
			");
	if (!$result) {
		$error.='Dogodila se greška pri zapisu u bazu: '.mysql_error();
	} else {
		//kreiraj stylesheet
		$putanja_file = '../'; //putanja za spremanje fajlova
		$new_fajl = $q_ime;
		$file2open = $putanja_file . $new_fajl;
		$tekst_novi=$q_stil;
		if (!$listfile = fopen($file2open, 'w')) {
			$error.='Dogodila se greška pri kreiranju fajla';
		} else {
			fwrite($listfile, $tekst_novi);
			if (!fclose($listfile))	{
				echo 'dogodila se greška prilikom zapisivanja teksta u fajl.';
			}
		}
	}
	
	
} else {
	$error.='U formi nisu ispunjena sva potrebna polja za unos podataka u bazu!';
}
?>
<div id="main_content">
<h1>Spremanje podataka u bazu</h1>
<?php
if ($error) echo '<div class="greska">'.$error.'</div>'."\n";
if (empty($error)) {
	$uspjeh.='Uspješno spremljen stylesheet:<br />';
	$uspjeh.='File name: '.$q_ime.'<br />';
	$uspjeh.='Ime: '.$q_ime_dropdown.'<br />';
	$uspjeh.='Dostupnost: '.$q_selectable.'<br />';
}
if ($uspjeh) echo '<div class="uspjeh">'.$uspjeh.'</div>'."\n";
?>
<br />
<br />
<a class="gray_normal toggleopacity" href="glavna.php?modul=site_admin&amp;akcija=list_style_sheets"><img src="slike/action_back.gif" width="16" height="16" alt="" border="0" /> <b>Povratak</b></a>
<br />
<br />
</div>