<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


include("../conf/preferences.php");
session_name(SYS_APP_SESSNAME);
session_start();
include("../conf/langs/".$langs[$_SESSION['userlang']][0]."/MAIN.php");
include("../conf/connections.php");
include("../phplib/auth.php");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");

$resId=$_REQUEST['resId'];
$conf=simplexml_load_file("../conf/resources/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->masterview);
$detail=simplexml_load_file("../conf/forms/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->detailview);
$accion=$_REQUEST['action'];

$obj=NULL;		
$result="";		

function getVarTable($conf){
	$arr=array();
	$arr['tabla']=(string)$conf->data->table;
	$arr['id']=(string)$conf->data->field_key;
	$arr['nombre']=(string)$conf->data->field_name;
	$arr['descripcion']=(string)$conf->data->field_desc;
	$arr['val']=(string)$conf->data->field_value;
	$arr['tipodatos']=(string)$conf->data->field_datatype;
	$arr['control']=(string)$conf->data->field_control;
	$arr['multilang']=(string)$conf->data->field_multilang;
	$arr['lockdel']=(string)$conf->data->field_lock_del;
	$arr['lockedit']=(string)$conf->data->field_lock_edit;
	$arr['tab']=(string)$conf->data->field_tab;
	$arr['grupo']=(string)$conf->data->field_group;
	$arr['estado']=(string)$conf->data->field_status;
	return $arr;
}
extract(getVarTable($conf));

switch($accion){
	case "delete":
		$sql="DELETE FROM $tabla WHERE $id='{$_REQUEST['id']}'";
		$q=mysql_query($sql);
		$result="OK";
		break;
	case "save":
		$errores='';
		if($_REQUEST['multilang']=='true'){
			$temp='';
			foreach($_REQUEST as $key=>$value){
				if(stripos($key,"value_")!==false){
					$key=substr($key,-2);
					$temp.="<!-- lang:$key -->".anti_sql_inject($value)."<!-- /lang:$key -->";
				}
			}
			$valor=$temp;
		}else{
			$valor=anti_sql_inject($_REQUEST['value']);
		}
		$validation=new ValidationServices();
		$sql="SELECT * FROM $tabla WHERE $id='{$_REQUEST['id']}'";
		$q=mysql_query($sql) or die($sql);
		if(mysql_num_rows($q)>0){
			$f=mysql_fetch_assoc($q);
			$xml=simplexml_load_string($f['control']);
			if(isset($xml->validation)){
				$metodo=(string)$xml->validation['type'];
				$x=$validation->$metodo($valor,(string)$xml->validation['params']);
				$result= !$x ? (string)$xml->validation['errortext'] : "OK";
			}else{
				$result="OK";
			}
		}
		
		if($result=="OK"){
			$sql="UPDATE $tabla SET $val='$valor' WHERE $id='{$_REQUEST['id']}'";
			$q=mysql_query($sql) or die($sql);
			if($f['eventos']!=''){
				$xml=simplexml_load_string($f['eventos']);
				foreach($xml->event as $evento){
					if((string)$evento['type']!='onAfterUpdate') continue;
					if(!estaArchivoIncluido((string)$evento['src'])) include_once("../".$evento['src']);
					$nombreclase=(string)$evento['class'];
					$metodo=(string)$evento['method'];
					if(isset($evento['params'])){
						$params=explode("@@@",(string)$evento['params']);
						for($i=0;$i<count($params);$i++) $params[$i]=Content::parse($params[$i]);
					}else{
						$params=NULL;
					}
					$objeto2=new $nombreclase();
					$objeto2->resId=$resId;
					$objeto2->recordset=$f;
					$objeto2->mail=$mail;
					$objeto2->$metodo($params);
				}
			}
		}
		break;
}
echo $result;
?>