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

function force_download ($data, $name, $mimetype='', $filesize=false) {
	if ($filesize == false OR !is_numeric($filesize)) {
		$filesize = strlen($data);
	}
	
	if (empty($mimetype)) {
		$mimetype = 'application/octet-stream';
	}
	
	header("Pragma: public"); 
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false);
	header("Content-Transfer-Encoding: binary");
	header("Content-Type: " . $mimetype);
	header("Content-Length: " . $filesize);
	header("Content-Disposition: attachment; filename=\"" . $name . "\";" );
	
	echo $data;
	die();
}

function ob_clean_all () {
	$ob_active = ob_get_length () !== false;
	while($ob_active) {
		ob_end_clean();
		$ob_active = ob_get_length () !== false;
	}
	return true;
} 


if(!isset($_GET['resId'])) header("Location:error.php");
$resId=$_GET['resId'];
if(isset($_GET['parentId'])) $parentId=$_GET['parentId'];
$conf=simplexml_load_file("../conf/resources/".$langs[$_SESSION['userlang']][0]."/".$_SESSION['conf'][$resId]->masterview);
$titulogeneral=$conf->title!=NULL ? $conf->title : $_SESSION['conf'][$resId]->title;

$table=array();										
$errores=array();									
$cols=0;											
$page=isset($_GET['page']) ? $_GET['page'] : 0;										
$pagesize=(isset($_GET['pagesize']) && $_GET['pagesize']!="0") ? $_GET['pagesize'] : $conf->data->pagesize;		
if($pagesize==0) $pagesize=1000000;	
$orderby=isset($_GET['orderby']) ? $_GET['orderby'] : $conf->data->orderby;			
$sortby=isset($_GET['sortby']) ? $_GET['sortby'] : $conf->data->sortby;				

$consulta=(string)$conf->data->sql;
$filtros=isset($conf->filters) ? FilterProcess::getQuery($consulta,$conf->filters) : '';	
$consulta=str_replace("{@filters}",$filtros,$consulta);
$consulta=Content::parse($consulta);														
$consulta=exclContent($consulta);															
$q=mysql_query($consulta . " ORDER BY " . $orderby) or die(mysql_error()."<br>" . $consulta);
$totalrows=mysql_num_rows($q);
$pages=ceil($totalrows/$pagesize);
if((int)$page>=$pages-1 || (string)$page=='last') $page=$pages-1;
if((int)$page<0 || (string)$page=='first') $page=0;
$qlimit=((int)$pagesize>0) ? " LIMIT " . ($pagesize*$page) . "," . $pagesize : "";
$q=mysql_query($consulta . " ORDER BY " . $orderby . " " . $sortby . " " . $qlimit) or die(mysql_error());
$currows=mysql_num_rows($q);		


while($f=mysql_fetch_assoc($q)){ $tabla[]=$f; }						




$cadena="";
foreach($conf->column_model->column as $columna){
	$cadena.=$columna['label'] . chr(9);
}
$cadena.="\r\n";

for($i=0;$i<count($tabla);$i++){
	foreach($conf->column_model->column as $columna){
		$cadena.=$tabla[$i][(string)$columna['boundTo']] . chr(9);
	}
	if($i<count($tabla)-1) $cadena.="\r\n";
}

$cadena=iconv("utf-8",SYS_APP_ENCODING,$cadena);
force_download($cadena,(string)$conf->title.".csv");
?>
