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


	if(!isset($_REQUEST['resId'])) header("Location:error.php");
	$resId=$_REQUEST['resId'];
	$_SESSION['currentRes']=$resId;
	$detail=simplexml_load_file("../conf/forms/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->detailview);
	$master=simplexml_load_file("../conf/resources/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->masterview);
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['postback'])){
	
	$key=(string)$master->data->field_key;
	$table=$master->data->table;
	$valores=$_POST['checkRow'];
	
	
	$recordset=array();
	$qevent=mysql_query("SELECT * FROM ".$master->data->table." WHERE ".$master->data->field_key." IN (".implode(",",$valores).")");
	while($fevent=mysql_fetch_assoc($qevent)){
		$recordset[]=$fevent;
	}
	
	if(isset($master->data->events)){
		foreach($master->data->events->event as $evento){
			if((string)$evento['type']!='onBeforeDelete') 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;
			}
			$objeto=new $nombreclase();
			$objeto->resId=$resId;
			$objeto->recordset=$recordset;
			$objeto->mail=$mail;
			$objeto->$metodo($params);
		}
	}
	
	
	
	$sql="SELECT * FROM " . SYS_MEDIA_TABLE . " WHERE idres=$resId AND idrec IN(" . implode(",",$valores) . ")";
	$q=mysql_query($sql);
	while($item=mysql_fetch_assoc($q)){
		if($item['readonly']!=1){
			@unlink($item['filename']);
			mysql_query("DELETE FROM " . SYS_MEDIA_TABLE . " WHERE id={$item['id']}");
		}
	}
	
	foreach($valores as &$valor){
		if(!is_numeric($valor)) $valor="'" . $valor . "'";
	}
	$sql="DELETE FROM $table WHERE $key IN (" . implode(",",$valores) . ")";
	$resultadosql=mysql_query($sql);
	if(!resultadosql){
	}else{
	}
	if(SYS_LOG_OPS==true){
		registerLog($resId,3);
	}

	
	if(isset($master->data->events)){
		foreach($master->data->events->event as $evento){
			if((string)$evento['type']!='onAfterDelete') 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=$recordset;
			$objeto2->mail=$mail;
			$objeto2->$metodo($params);
		}
	}

	header("Location: " . $_REQUEST['mymasterpage']);
}
?>
<!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/common.css" rel="stylesheet" type="text/css" media="screen" />
	<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" />
	<link type="text/css" href="../jslib/jq/ui/css/<? echo UI_THEME ?>/jquery-ui.custom.css" rel="stylesheet" />
	<link type="text/css" href="../jslib/jq/ui/css/<? echo UI_THEME ?>/jquery-ui.ekain.css" rel="stylesheet" />
	<script type="text/javascript" src = "../jslib/jq/jquery-1.7.2.min.js" ></script>
	<script type="text/javascript" src = "../jslib/jq/jquery.blockUI.js" ></script>
	<script type="text/javascript" src = "../jslib/jq/ui/js/jquery-ui-1.8.21.custom.min.js" ></script>
	<script type="text/javascript" src = "../jslib/jq/ui/jquery.ui.selectmenu.js" ></script>
	<link type="text/css" href="../jslib/jq/ui/css/<? echo UI_THEME ?>/jquery.ui.selectmenu.css" rel="stylesheet" />
	<script type="text/javascript" src = "../jslib/jq/fancybox/jquery.fancybox-1.3.4.pack.js" ></script>
	<link rel="stylesheet" href="../jslib/jq/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />

	<script type="text/javascript" src="../jslib/utils.js"></script>
	<script src="../jslib/swfobject2.js" type="text/javascript"></script>
	<script type="text/javascript">
	$(document).ready(function(){
		$('#tabla').tabs();
		$('#tipo').focus();
		$('select').selectmenu();
		$('button, input:submit, input:button, input:reset, .button, .link_button').button();
	});
	</script>
</head>

<body class="bkg-detail-1">
<div id="pageBar">
	<h1>Bienvenido</h1>
</div>
<table width="100%" cellpadding="0" cellspacing="0" style="">
<tr>
<td id="panel_central" valign="top">
	<form name="dataform" id="dataform" method="post" action="#">
<? 
if(count($_POST['checkRow'])>0){
	
	
	$precheck=true;
	$ev=$master->xpath("//event[@type='onDeletionValidate']");
	if(count($ev)>0){ 
		$evento=$ev[0];		
		$valores=$_POST['checkRow']; $recordset=array();
		foreach($valores as $id) $recordset[]=array((string)$master->data->field_key=>$id);
		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;
		}
		$objeto=new $nombreclase();
		$objeto->resId=$resId;
		$objeto->recordset=$recordset;
		$objeto->mail=$mail;
		$resultado=$objeto->$metodo($params);
		$precheck=$resultado->result;
	}
	if($precheck==false){
?>
		<div class='introbox sombra roundcorners bkg_red bkg_icon_error'><? echo $resultado->msg ?><br /><br />
		<input type="button" value="<? echo BUTTON_BACK ?>" class="but_default wpx75 grad_red bold" onclick="location.href='<? echo $_REQUEST['mymasterpage'] ?>'" /></div>
<?
	}else{
?>
		<div class='introbox sombra roundcorners bkg_orange bkg_icon_warning'><? echo isset($master->del_warning) ? $master->del_warning : CONFIRM_DELETION; ?><br /><br />
		<input type="button" value="<? echo BUTTON_CONTINUE ?>" class="but_default wpx75 grad_red bold" onclick="document.forms[0].submit()" />
		<input type="button" value="<? echo BUTTON_NO ?>" class="but_default wpx75 bold" onclick="location.href='<? echo $_REQUEST['mymasterpage'] ?>'" /></div>
		<input type="hidden" name="resId" value="<? echo $_REQUEST['resId'] ?>" />
		<input type="hidden" name="mymasterpage" value="<? echo $_REQUEST['mymasterpage'] ?>" />
		<input type="hidden" name="postback" value="true" />
		<? foreach($_POST['checkRow'] as $clave=>$valor){ ?><input type="hidden" name="checkRow[]" value="<? echo $valor ?>" /><? } ?>
<?			
	}
}else{
?>
		<div class='introbox sombra roundcorners bkg_red bkg_icon_error'><? echo PANEL_VAL_NOSELECT ?><br /><br />
		<input type="button" value="<? echo BUTTON_BACK ?>" class="but_default wpx75 grad_red bold" onclick="location.href='<? echo $_REQUEST['mymasterpage'] ?>'" /></div>
<?
}
?>
	</form>
</td>
</tr>
</table>
<pre>
</body>
</html>
