<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.0
*/
session_start;

require "../conexion/conectar.php";

$conexion = new CONDB();
$conexion->conectar();

$campos = explode(";;",$_POST['campos']);
$where="";
$set="";
for($i = 0; $i < count($campos); $i++){
	$camposDiv = explode("|",$campos[$i]);
	$camposNameKey[$i]["name"] = trim($camposDiv[0]);
	
	if(isset($_FILES[$camposNameKey[$i]["name"]]) && trim($_FILES[$camposNameKey[$i]["name"]]["name"]) != ""){
		$_POST[$camposNameKey[$i]["name"]]=$_FILES[$camposNameKey[$i]["name"]]["name"];
		move_uploaded_file($_FILES[$camposNameKey[$i]["name"]]["tmp_name"],"upload/ArchivosConfig/".$_POST[$camposNameKey[$i]["name"]]);
	}else if(isset($_POST["upload_".$camposNameKey[$i]["name"]])){
		$_POST[$camposNameKey[$i]["name"]]=$_POST["upload_".$camposNameKey[$i]["name"]];
	}
	
	if(isset($_POST["password_".$camposNameKey[$i]["name"]]) && trim($_POST[$camposNameKey[$i]["name"]]) != ""){
		$_POST[$camposNameKey[$i]["name"]]=md5($_POST[$camposNameKey[$i]["name"]]);
	}else if(isset($_POST["password_".$camposNameKey[$i]["name"]])){
		$_POST[$camposNameKey[$i]["name"]]=$_POST["password_".$camposNameKey[$i]["name"]];
	}
	
		if($set == "")
			$set = $camposNameKey[$i]["name"]."='".$_POST[$camposNameKey[$i]["name"]]."'";
		else
			$set.= ",".$camposNameKey[$i]["name"]."='".$_POST[$camposNameKey[$i]["name"]]."'";
}

$keys = explode("|",$_POST['keys']);
$valkeys = explode("|",$_POST['valkeys']);

for($i = 0; $i < count($keys); $i++){
	if($where == "")
		$where=" where ".$keys[$i]."='".$valkeys[$i]."'";
	else
		$where.=" and ".$keys[$i]."='".$valkeys[$i]."'";
}

if($_POST['oper'] == "add"){
	$tipoQ = "insert into ".$_POST['tabla']." set ";
	$sql=$tipoQ.$set;
}else if($_POST['oper'] == "edit"){
	$tipoQ = "update ".$_POST['tabla']." set ";
	$sql=$tipoQ.$set.$where;
}

mysqli_query($conexion->CONEXION,$sql);

if(mysqli_errno($conexion->CONEXION) > 0){
	echo "Ocurrio un error al guardar los datos";
}else{
	//echo "Datos guardados correctamente";
	if($_POST['reloadGridAfterSave'] != ""){
		if($_POST['valLiga'] != ""){
			$valLiga = explode("|",$_POST['valLiga']);
			$ids = "";
			for($i = 0; $i < count($valLiga); $i++){
					$ids.=",'".$valLiga[$i]."'";
			}
		}
		?><script>tools('<?=$_POST['reloadGridAfterSave']?>'<?=$ids?>); $j_6('#closeModal').click();</script><?
	}
}