<?
////////////////////////////////////////////////////
// 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");

$arr=array();
$pagesize=25;

$sdata=simplexml_load_file('../conf/search.xml') or die("El archivo de configuración de búsquedas no existe o no tiene un formato correcto.");
foreach($sdata->section as $item){
	if($_REQUEST['area']!=0 && $item['id']!=$_REQUEST['area']) continue;
	$cadena=(string)$item;
	if(isset($item['authorId']) && $_SESSION[session_id()]['level']>2){
		$cadena=str_replace('{authorId}','AND ' . (string)$item['authorId'] . "=" . $_SESSION[session_id()]['id'],$cadena);
	}else{
		$cadena=str_replace('{authorId}','',$cadena);
	}
	$cadena=str_replace('%param%','%'.$_REQUEST['termino'].'%',$cadena);
	$q=mysql_query($cadena) or die(mysql_error() . "<br />" . $cadena);
	while($f=mysql_fetch_array($q)){
		$temp=array('id'=>$f[0],'title'=>$f[1],'resume'=>$f[2],'resId'=>$item['resId'],'category'=>$item['title'],'dest'=>$item['dest']);
		array_push($arr,$temp);
	}
}
$numrows=count($arr);
$pages=ceil($numrows/$pagesize);
$page=isset($_REQUEST['page']) ? $_REQUEST['page'] : 0;
?>
<!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" />
	<script type="text/javascript">
	function putPaginator(page){
		try{
			var pagesize=<? echo $pagesize ?>;
			var pages=<? echo $pages ?>;
			var numrows=<? echo $numrows ?>;
		}catch(err){
			return;
		}
		$('reg_desde').innerHTML=numrows>0 ? (page*pagesize)+1 : 0;
		if(page==pages-1){
			$('reg_hasta').innerHTML=numrows;
		}else{
			$('reg_hasta').innerHTML=numrows>0 ? (page*pagesize)+pagesize : 0;
		}
		if(pages<2) return;
		var items=pagesize;
		var cadena='';
		
		if(page>0){
			cadena+='<a href="javascript:showPage(0)" style="font-style:normal"><? echo PAGINATOR_FIRST ?></a>';
			cadena+='<a href="javascript:showPage('+(page-1)+')" style="font-style:normal"><? echo PAGINATOR_PREV ?></a>';
		}else{
			cadena+='<span class="inactive"><? echo PAGINATOR_FIRST ?></span>';
			cadena+='<span class="inactive"><? echo PAGINATOR_PREV ?></span>';
		}
		
		var l1=page>items ? page-items : 0;
		var l2=pages-page>items ? page+items : pages;
		for(i=l1;i<l2;i++){
			if(page==i){
				cadena+='<span class="current">' + (i+1) + '</span>';
			}else{
				cadena+='<a href="javascript:showPage('+(i)+')" style="font-style:normal">' + (i+1) + '</a>';
			}
		}		
		
		if(page<pages-1){
			cadena+='<a href="javascript:showPage('+(page+1)+')" style="font-style:normal"><? echo PAGINATOR_NEXT ?></a>';
			cadena+='<a href="javascript:showPage('+(pages-1)+')" style="font-style:normal"><? echo PAGINATOR_LAST ?></a>';
		}else{
			cadena+='<span class="inactive"><? echo PAGINATOR_NEXT ?></span>';
			cadena+='<span class="inactive"><? echo PAGINATOR_LAST ?></span>';
		}
		
		$('paginator').innerHTML=cadena;
	}
	function showPage(param){
		var numrows=<? echo $numrows ?>;
		var pagesize=<? echo $pagesize ?>;
		for(i=0;i<numrows;i++){
			$('row_'+i).style.display="none";
		}
		var g=((param*pagesize)+pagesize)>numrows ? numrows : (param*pagesize)+pagesize;
		for(n=(param*pagesize);n<g;n++){
			$('row_'+n).style.display="";
		}
		putPaginator(param);
	}
	</script>
</head>

<body bgcolor="#FFFFFF" style="background-image:none;">
<div id="pageBar">
	<h1><? echo SEARCH_TITLE ?></h1>
</div>
<table width="100%" cellpadding="0" cellspacing="0" style="">
<tr>
<td id="panel_central" valign="top">
<form action="config.php" method="post" name="dataform" id="dataform" enctype="multipart/form-data">
<br />
<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>Mostrando: <span id="reg_desde" style="font-weight:bold">xxx</span> a <span id="reg_hasta" style="font-weight:bold">yyy</span> de <strong><? echo $numrows ?></strong> resultados</strong></td>
	</tr>
	</thead>
	
	<tbody id="table_body">
	<? if($numrows==0){ ?>
	<tr class="<? echo $clase ?>" style="border-bottom:1px solid #999999;">
		<td class="validacion" colspan="2"><? echo SEARCH_NORESULTS ?></td>
	</tr>
	<?
	}else{
		for($i=0;$i<$numrows;$i++){
			$clase=($i/2==floor($i/2)) ? "row2" : "row1";
			// construimos la url de destino...
			$url=$arr[$i]['dest']."?_action=edit&resId=".$arr[$i]['resId']."&id=".$arr[$i]['id']."&mymasterpage=search.php?termino=".$_REQUEST['termino']."&area=".$_REQUEST['area']."&page=".(floor($i/$pagesize));
	?>
	<tr class="<? echo $clase ?>" style="border-bottom:1px solid #999999;" id="row_<? echo $i ?>">
		<td width="464" class="<? echo $clase ?>"><a href="<? echo $url ?>"><strong><? echo $arr[$i]['title'] ?></strong></a></td>
		<td width="126" align="right" class="<? echo $clase ?>"><? echo $arr[$i]['category'] ?></td>
	</tr>
	<?
		}
	}
	?>
	</tbody>
	
	<tfoot id="table_foot">
	<tr>
		<td colspan="2" class="barra_paginator" id="paginator"></td>
		</tr>
	</tfoot>
</table>
</form>
</td>
</tr>
</table>
<script type="text/javascript">
showPage(<? echo $page ?>);
</script>
</body>
</html>
