<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/pf_configuracion_variables.php");



$filename="";	
$message="";
$error_happened=false;
$readevalues=false;

//connect database
$conn = db_connect();


$keys=array();
$keys["id_configuracion"]=postvalue("editid1");

//	prepare data for saving
if(@$_POST["a"]=="edited")
{
	$strWhere=KeyWhere($keys);
	$strSQL = "update ".AddTableWrappers($strOriginalTableName)." set ";
	$evalues=array();
	$efilename_values=array();
	$files_delete=array();
	$files_move=array();
//	processing id_configuracion - start
	$value = postvalue("value_id_configuracion");
	$type=postvalue("type_id_configuracion");
	if (in_assoc_array("type_id_configuracion",$_POST) || in_assoc_array("value_id_configuracion",$_POST) || in_assoc_array("value_id_configuracion",$_FILES))	
	{
		$value=prepare_for_db("id_configuracion",$value,$type);
	}
	else
		$value=false;
	if(!($value===false))
	{	
		$evalues["id_configuracion"]=$value;
	}

//	update key value
	$keys["id_configuracion"]=$value;

//	processibng id_configuracion - end

//	processing valor - start
	$value = postvalue("value_valor");
	$type=postvalue("type_valor");
	if (in_assoc_array("type_valor",$_POST) || in_assoc_array("value_valor",$_POST) || in_assoc_array("value_valor",$_FILES))	
	{
		$value=prepare_for_db("valor",$value,$type);
	}
	else
		$value=false;
	if(!($value===false))
	{	
		$evalues["valor"]=$value;
	}


//	processibng valor - end


	foreach($efilename_values as $ekey=>$value)
		$evalues[$ekey]=$value;
//	do event
	$retval=true;
	if(function_exists("BeforeEdit"))
		$retval=BeforeEdit($evalues,$strWhere);
	if($retval)
	{		
//	construct SQL string
		foreach($evalues as $ekey=>$value)
		{
			$strSQL.=AddFieldWrappers($ekey)."=".add_db_quotes($ekey,$value).", ";
		}
		if(substr($strSQL,-2)==", ")
			$strSQL=substr($strSQL,0,strlen($strSQL)-2);
		$strSQL.=" where ".$strWhere;
		set_error_handler("edit_error_handler");
		db_exec($strSQL,$conn);
		set_error_handler("error_handler");
		if(!$error_happened)
		{
//	delete & move files
			foreach ($files_delete as $file)
			{
				if(file_exists($file))
					@unlink($file);
			}
			foreach ($files_move as $file)
			{
				move_uploaded_file($file[0],$file[1]);
				if(strtoupper(substr(PHP_OS,0,3))!="WIN")
					chmod($file[1],0777);
			}
			$message="<div class=message><<< "."Registro actualizado"." >>></div>";
//	after edit event
			if(function_exists("AfterEdit"))
				AfterEdit();
		}
	}
	else
		$readevalues=true;
}

//	get current values and show edit controls

$strSQL = $gstrSQL;

$strWhere=KeyWhere($keys);
$strSQL = AddWhere($strSQL,$strWhere);

LogInfo($strSQL);
$rs=db_query($strSQL,$conn);
$data=db_fetch_array($rs);

if($readevalues)
{
	$data["id_configuracion"]=$evalues["id_configuracion"];
	$data["valor"]=$evalues["valor"];
}

include('libs/Smarty.class.php');
$smarty = new Smarty();

//	include files

$includes="";

//	validation stuff
$bodyonload="";
$onsubmit="";

if($bodyonload)
{
	$onsubmit="return validate();";
	$bodyonload="onload=\"".$bodyonload."\"";
}

if ($useAJAX) {
$includes.="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\" src=\"include/ajaxsuggest.js\"></script>\r\n";
}
$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\">\r\n".
"var locale_dateformat = ".$locale_info["LOCALE_IDATE"].";\r\n".
"var locale_datedelimiter = \"".$locale_info["LOCALE_SDATE"]."\";\r\n".
"var bLoading=false;\r\n".
"var TEXT_PLEASE_SELECT='".addslashes("Por favor seleccione")."';\r\n";
if ($useAJAX) {
$includes.="var AUTOCOMPLETE_TABLE='pf_configuracion_autocomplete.php';\r\n";
$includes.="var SUGGEST_TABLE='pf_configuracion_searchsuggest.php';\r\n";
$includes.="var SUGGEST_LOOKUP_TABLE='pf_configuracion_lookupsuggest.php';\r\n";
}
$includes.="</script>\r\n";
if ($useAJAX) {
$includes.="<div id=\"search_suggest\"></div>\r\n";
}





$smarty->assign("includes",$includes);
$smarty->assign("bodyonload",$bodyonload);
if(strlen($onsubmit))
	$onsubmit="onSubmit=\"".$onsubmit."\"";
$smarty->assign("onsubmit",$onsubmit);

$smarty->assign("key1",htmlspecialchars($keys["id_configuracion"]));
	$smarty->assign("show_key1", htmlspecialchars(GetData($data,"id_configuracion", "")));

$smarty->assign("message",$message);


$readonlyfields=array();

$smarty->assign("value_id_configuracion",@$data["id_configuracion"]);
$smarty->assign("value_valor",@$data["valor"]);


$linkdata="";

if ($useAJAX) {
	$linkdata .= "<script type=\"text/javascript\">\r\n";
	$linkdata .= "$(document).ready(function(){ \r\n";
	$linkdata .= "
function loadSelectContent(txt, selectControl, selectValue) 
{
	$('#'+selectControl).get(0).options[0]=new Option(TEXT_PLEASE_SELECT,'');
	var str = txt.split('\\n');
	var index = 0;
	for(i=0,j=0; i < str.length - 1; i=i+2, j++) {
		$('#'+selectControl).get(0).options[j+1]=new Option(unescape(str[i+1]),unescape(str[i]));
		if ( unescape(str[i]) == selectValue ) {index = j+1;}
	}
	$('#'+selectControl).get(0).selectedIndex = index;
}"."\r\n";
	$linkdata .= "});\r\n";
	$linkdata .= "</script>\r\n";
} else {
}

$smarty->assign("linkdata",$linkdata);

$where=$strWhere;

$templatefile = "pf_configuracion_edit.htm";
if(function_exists("BeforeShowEdit"))
	BeforeShowEdit($smarty,$templatefile);

$smarty->display($templatefile);

function edit_error_handler($errno, $errstr, $errfile, $errline)
{
	global $readevalues, $message, $error_happened;
	$message="<div class=message><<< "."El registro no ha sido editado"." >>><br><br>".$errstr."</div>";
	$readevalues=true;
	$error_happened=true;
}

?>