<?
////////////////////////////////////////////////////
// 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");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");

$resId=$_REQUEST['resId'];
$action=isset($_REQUEST['action']) ? $_REQUEST['action'] : 'update';
$master=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);

$validation=new Validation($detail);
$process=new UserProcessing();

function fileExists($image,$dir) {
   $i=1; $probeer=$image;
	while(file_exists("../../".$dir.$probeer)) {		
       $punt=strrpos($image,".");
       if(substr($image,($punt-3),1)!==("[") && substr($image,($punt-1),1)!==("]")) {
           $probeer=substr($image,0,$punt)."[".$i."]".
           substr($image,($punt),strlen($image)-$punt);
       } else {
           $probeer=substr($image,0,($punt-3))."[".$i."]".
           substr($image,($punt),strlen($image)-$punt);
       }
       $i++;
   }
   return $probeer;
}

$nombre='';								
$nombrefinal='';						
$errores='';							

$control=isset($_GET['control'])? $_GET['control'] : "imagen";

$tempnodo=$detail->xpath("//control[@name='".$control."']");
$nodo=$tempnodo[0];

$destinofinal=SYS_BASE_PATH . SYS_BASE_UPLOAD . $nodo->uploaddir . "/";
	
if((string)$nodo->autorename=="true"){
	$tmp=fileExists(basename($_FILES['Filedata']['name']),$destinofinal);
}else{
	$tmp=basename($_FILES['Filedata']['name']);
}
$tmp=mb_convert_case($tmp, MB_CASE_LOWER, "UTF-8");
$tmp=str_replace(" ","_",$tmp);

if($_FILES['Filedata']['size']>(float)$_GET['maxSize']*1024){
	$errores.="- " . FILECTRL_TOOBIG . "<br />";
}else{

}


if($errores==''){
	$nombre=$tmp;
	$nombrefinal=session_id()."_".$nombre;
	
	$_SESSION['uploads'][$resId][$control]=$destinofinal.$nombre;
	
	$uploaddir = "../../" . SYS_TEMP_UPLOADS;
	$uploadfile = $uploaddir . utf8_decode($nombrefinal);
	
	if(!move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadfile)){
		$errores.="- " . FILECTRL_NO_TMPDIR . "<br />";
	}else{
		chmod($uploadfile,0777);
	}
}



$form_caption=$action=='insert' ? $detail->formcaption_insert : $detail->formcaption_update;
$page_caption=$detail->pagetitle;
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?  if($errores!=''){ ?>
	<title><? echo $_SESSION['application']['title'] ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="../resources/themes/default/form_screen.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../jslib/utils.js"></script>
<?
}else{
?>
	<script type="text/javascript">
	parent.updateFileControl('<? echo $control ?>','<? echo $nombre ?>','11');
	parent.$.fancybox.close();
	</script>
<?
}
?>
</head>

<body>
<table width="100%" height="100" cellspacing="0" cellpadding="0" border="0" id="outer_table">
<thead>
<tr>
	<td class="pageBar" height="1" ><h1><? echo FILECTRL_POPUP_TITLE ?></h1></td>
</tr>
</thead>
<tbody>
<tr class="row1">
	<td valign="top" align="left" style="font-size:11px;">
	<?
	echo $errores;
	?>
	</td>
</tr>
</tbody>
<tfoot>
<tr>
	<td colspan="2">
		<input type="button" id="Back" name="Back" value="<? echo BUTTON_BACK ?>" class="but_medium" onClick="javascript:history.back();" />
		<input type="button" id="Cancel" name="Cancel" value="<? echo BUTTON_CANCEL ?>" class="but_medium" onClick="javascript:window.close();" />
	</td>
</tr>
</tfoot>
</table>
</body>
</html>
