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


// ====================================================================================
session_start();
include("../conf/preferences.php");
include("../conf/langs/".$langs[$_SESSION['userlang']][0]."/MAIN.php");
include("../conf/langs/".$langs[$_SESSION['userlang']][0]."/orphans.php");
include("../conf/connections.php");
include("../phplib/auth.php");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");
function in_array_multi($needle, $haystack){
	$found = false;
	foreach($haystack as $value) if((is_array($value) && in_array_multi($needle, $value)) || $value == $needle) $found = true;
	return $found;
}
$xml=simplexml_load_file("../conf/files.xml");
$archivos=array();
$carpetas=array();
$faltan=array();
$sobran=array();
$upbase="../../" . SYS_BASE_UPLOAD;
unset($_SESSION['sobran']);
$time_start = microtime(true);
foreach($xml->resource as $tabla){
	$nombretabla=$tabla['name'];
	$campoextra=(string)$tabla['extra'];
	$consulta=(string)$tabla['sql'];
	$q=mysql_query($consulta) or die(mysql_error());
	
	while($f=mysql_fetch_array($q)){
		foreach($tabla->field as $campo){
			$archivo=$f[(string)$campo['name']];
			$prefijo=($archivo!="") ? $campo['prefix'] : "";
			$vinculo=$tabla->link;
			$vinculo=str_replace("id=xxx","id=" . $f[(string)$tabla['key']],$vinculo);
			$tmp=array('table'=>$nombretabla,'extra'=>$f[$campoextra],'file'=>$prefijo . $archivo,'path'=>$upbase . $campo['path'] . $prefijo . $archivo,'version'=>$campo['version'], 'link'=>$vinculo, 'field'=>$campo['name'], 'id'=>$f[(string)$tabla['key']], 'resource'=>$tabla['table'], 'key'=>$tabla['key']);
			$archivos[]=$tmp;
		}
	}
}
$carpetas_array=$xml->xpath('//@path');
foreach($carpetas_array as $carp){
	$carpetas[]=(string)$carp['path'];
}
$carpetas=array_unique($carpetas);
foreach($archivos as $item){
	if(!file_exists($item['path']) && $item['version']!="true") $faltan[]=$item;
}
foreach($carpetas as $carp){
	$handle=opendir($upbase . $carp);
	while (false !== ($file = readdir($handle))){
		if($file=="." || $file==".." || is_dir("../../" . SYS_BASE_UPLOAD . $carp . $file)) continue;
		if(!in_array_multi($upbase . $carp . $file, $archivos)){
			$tmp=array('name'=>$carp . $file,'path'=>SYS_HTTP_PATH . SYS_BASE_PATH . SYS_BASE_UPLOAD . $carp . $file,'size'=>filesize($upbase . $carp . $file));
			$sobran[]=$tmp;
		}
	}
}
$_SESSION['sobran']=$sobran;



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><? echo $_SESSION['application']['title'] ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="../resources/themes/default/main.css" rel="stylesheet" type="text/css" />
	<link href="../resources/themes/default/form_screen.css" rel="stylesheet" type="text/css" />
	<style type="text/css">
	</style>
	<script type="text/javascript">
	function deleteRemoteFile(path, id){
		var result=confirm("<? echo ADDON_JS_CONFIRM_DELFILE ?>");
		if(result==false) return;
		var nodo=document.getElementById("file_" + id);
		new Ajax.Request('orphans_op.php',{
			method:'get',
			parameters: {file: path, action: 'deleteFile'},
			onSuccess: function(transport){
				var response = transport.responseText || "<? echo ADDON_JS_NO_SERVER_RESPONSE ?>";
				if(response=="OK"){
					nodo.parentNode.removeChild(nodo);
				}else{
					alert("<? echo ADDON_JS_FILE_DEL_ERROR ?>");
				}
			},
			onFailure: function(){
				alert('<? echo ADDON_JS_SERVER_ERROR ?>')
			}
		});
  	}
	
	function deleteAllRemoteFiles(){
		var result=confirm("<? echo ADDON_JS_CONFIRM_DELFILES ?>");
		if(result==false) return;
		new Ajax.Request('orphans_op.php',{
			method:'get',
			parameters: {action: 'deleteAllFiles'},
			onSuccess: function(transport){
				var response = transport.responseText || "<? echo ADDON_JS_NO_SERVER_RESPONSE ?>";
				if(response=="OK"){
					location.reload();
				}else{
					alert(responseText);
				}
			},
			onFailure: function(){
				alert('<? echo ADDON_JS_SERVER_ERROR ?>')
			}
		});
	}
	
	function emptyField(table, field, id, key, numnodo){
		var result=confirm("<? echo ADDON_JS_CONFIRM_DELRECORD ?>");
		if(result==false) return;
		var nodo=document.getElementById("field_" + numnodo);
		new Ajax.Request('orphans_op.php',{
			method:'get',
			parameters: {action: 'emptyField', table:table, field:field, id:id, key:key},
			onSuccess: function(transport){
				var response = transport.responseText || "<? echo ADDON_JS_NO_SERVER_RESPONSE ?>";
				if(response=="OK"){
					nodo.parentNode.removeChild(nodo);
				}else{
					alert("<? echo ADDON_JS_FILE_EMPTY_ERROR ?>");
				}
			},
			onFailure: function(){
				alert('<? echo ADDON_JS_SERVER_ERROR ?>')
			}
		});
	}
	</script>
</head>

<body bgcolor="#FFFFFF" style="background-image:none;">
<div id="pageBar">
	<h1><? echo ADDON_HTML_TITLE ?></h1>
</div>
<table width="100%" cellpadding="0" cellspacing="0" style="">
<tr>
<td id="panel_central" valign="top">
<? echo ADDON_HTML_INTRO ?><br />
<br />
<table width="600" border="0" cellspacing="0" cellpadding="2" style="border:1px solid #333333" align="center">
	<thead>
	<tr>
		<td colspan="4" class="barra_recuento"><strong><? echo ADDON_HTML_FILES_NOT_PRESENT ?> (<? echo count($faltan) ?> <? echo ADDON_HTML_FILES ?>)</strong></td>
	</tr>
	<tr>
		<td class="barra_nombres_columnas style1"><? echo ADDON_HTML_RESOURCE ?></td>
		<td width="106" class="barra_nombres_columnas style1"><? echo ADDON_HTML_EXTRA ?></td>
		<td width="176" class="barra_nombres_columnas style1"><? echo ADDON_HTML_FILE ?></td>
		<td width="121" class="barra_nombres_columnas style1" align="right"><? echo ADDON_HTML_ACTION ?></td>
	</tr>
	</thead>
	
	<tbody id="table_body">
	<?
	for($i=0;$i<count($faltan);$i++){
		$clase=$i/2==floor($i/2) ? "row2" : "row1";
	?>
	<tr class="<? echo $clase ?>" style="border-bottom:1px solid #999999;" id="field_<? echo $i ?>">
		<td width="179"><? echo $faltan[$i]['table'] ?></td>
		<td align="left"><a href="<? echo $faltan[$i]['link'] ?>"><? echo $faltan[$i]['extra'] ?></a></td>
		<td align="left"><? echo $faltan[$i]['file'] ?></td>
		<td align="right">
			<input type="button" name="button" id="button" value="<? echo ADDON_HTML_EMPTY ?>" class="but_default" onclick="emptyField('<? echo $faltan[$i]['resource'] ?>', '<? echo $faltan[$i]['field'] ?>',<? echo $faltan[$i]['id'] ?>,'<? echo $faltan[$i]['key'] ?>',<? echo $i ?>)" />
		</td>
	</tr>
	<? } ?>
	</tbody>
	
	<tfoot id="table_foot">
	</tfoot>
</table>
  
<br />
&nbsp;
<table width="600" border="0" cellspacing="0" cellpadding="2" style="border:1px solid #333333" align="center">
	<thead>
	<tr>
		<td colspan="2" class="barra_recuento"><strong><? echo ADDON_HTML_UNNECESSARY ?> (<? echo count($sobran) ?> <? echo ADDON_HTML_FILES ?>)</strong></td>
	</tr>
	<tr>
		<td class="barra_nombres_columnas style1"><? echo ADDON_HTML_FILE ?></td>
		<td width="117" class="barra_nombres_columnas style1" align="right"><? echo ADDON_HTML_ACTION ?></td>
	</tr>
	</thead>
	
	<tbody id="table_body">
	<?
	for($i=0;$i<count($sobran);$i++){
		$clase=$i/2==floor($i/2) ? "row2" : "row1";
	?>
	<tr class="<? echo $clase ?>" style="border-bottom:1px solid #999999;" id="file_<? echo $i ?>">
		<td width="473"><a href="<? echo $sobran[$i]['path'] ?>" target="_blank"><? echo $sobran[$i]['name'] ?></a> (<? echo number_format($sobran[$i]['size']/1024,2) ?> kb)</td>
		<td align="right">
			<input type="button" name="delfile<? echo $i ?>" id="delfile<? echo $i ?>" value="<? echo ADDON_HTML_DELETE ?>" class="but_default" onclick="deleteRemoteFile('<? echo $sobran[$i]['name'] ?>',<? echo $i ?>)" />
		</td>
		</tr>
	<? } ?>
	</tbody>
	<? if(count($sobran)>0){
		$tamanototal=0;
		for($i=0;$i<count($sobran);$i++){
			$tamanototal+=$sobran[$i]['size'];
		}
	?>
	<tfoot id="table_foot">
		<tr>
			<td>&nbsp;</td>
			<td align="right"><input type="button" name="delallfiles" id="delallfiles" value="<? echo ADDON_HTML_DELETE_ALL ?>" class="but_default" onclick="deleteAllRemoteFiles()" /></td>
		</tr>
	</tfoot>
	<? } ?>
</table>
</td>
</tr>
</table>

<?
$time_end = microtime(true);
$time = $time_end - $time_start;
?>
</body>
</html>
