<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
session_cache_limiter("none");
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/pf_sistemalog_variables.php");



if (@$_REQUEST["a"]=="") 
{
	$strSQL=$_SESSION[$strTableName."_sql"];
	$options = "1";
}
else
{
	$options = "";
	
$sWhere = "1=0";	
foreach(@$_POST["mdelete"] as $ind)
{
	$sWhere = $sWhere . " or ";
		$sWhere = $sWhere . GetFullFieldName("id","") . "=" . make_db_value("id", $_POST["mdelete1"][$ind-1],"","");
        
}
$strSQL = AddWhere($gstrSQL,$sWhere);
$_SESSION[$strTableName."_SelectedSQL"] = $strSQL;
}

if ($_SESSION[$strTableName."_SelectedSQL"]!="" && @$_REQUEST["records"]=="") $strSQL = $_SESSION[$strTableName."_SelectedSQL"] ;
 

$mypage=1;
if(@$_REQUEST["type"]) 
{
	$conn=db_connect();
	LogInfo($strSQL);
//	pagination
	$nPageSize=0;
	$numrows=GetRowCount($strSQL);
	if(@$_REQUEST["records"]=="page" && $numrows)
	{
		$mypage=(integer)@$_SESSION[$strTableName."_pagenumber"];
		$nPageSize=(integer)@$_SESSION[$strTableName."_pagesize"];
		if($numrows<=($mypage-1)*$nPageSize)
			$mypage=ceil($numrows/$nPageSize);
		if(!$nPageSize)
			$nPageSize=$gPageSize;
		if(!$mypage)
			$mypage=1;

		$strSQL = AddTop($strSQL, $mypage*$nPageSize);
	}
	$rs=db_query($strSQL,$conn);
	db_pageseek($rs,$nPageSize,$mypage);

	if(!ini_get("safe_mode"))
		set_time_limit(300);
	
	if(@$_REQUEST["type"]=="excel")
		ExportToExcel();
	else if(@$_REQUEST["type"]=="word")
		ExportToWord();
	else if(@$_REQUEST["type"]=="xml")
		ExportToXML();
	else if(@$_REQUEST["type"]=="csv")
		ExportToCSV();
	else if(@$_REQUEST["type"]=="pdf")
		ExportToPDF();

	db_close($conn);
	return;
}

header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 

include('libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->assign("options",$options);
$smarty->display("pf_sistemalog_export.htm");


function ExportToExcel()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment;Filename=pf_sistemalog.xls");

	echo "<html>";
	echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
	
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToWord()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-word");
	header("Content-Disposition: attachment;Filename=pf_sistemalog.doc");

	echo "<html>";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToXML()
{
	global $nPageSize,$rs,$strTableName,$conn;
	header("Content-type: text/xml");
	header("Content-Disposition: attachment;Filename=pf_sistemalog.xml");
	if(!db_numrows($rs))
		return;
	echo "<?xml version=\"1.0\" standalone=\"yes\"?>\r\n";
	echo "<table>\r\n";
	$i=0;
	while((!$nPageSize || $i<$nPageSize) && $row=db_fetch_array($rs))
	{
		echo "<row>\r\n";
		$field=htmlspecialchars(XMLNameEncode("id"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"id",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("stringerror"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"stringerror",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("fecha"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"fecha",""));
		echo "</".$field.">\r\n";
		$field=htmlspecialchars(XMLNameEncode("procedimiento"));
		echo "<".$field.">";
		echo htmlspecialchars(GetData($row,"procedimiento",""));
		echo "</".$field.">\r\n";
		echo "</row>\r\n";
		$i++;
	}
	echo "</table>\r\n";
}

function ExportToCSV()
{
	global $rs,$nPageSize,$strTableName,$conn;
	header("Content-type: application/csv");
	header("Content-Disposition: attachment;Filename=pf_sistemalog.csv");

	if(!db_numrows($rs))
		return;

	$totals=array();

	
// write header
	$outstr="";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"id\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"stringerror\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"fecha\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"procedimiento\"";
	echo $outstr;
	echo "\r\n";

// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row=db_fetch_array($rs))
	{
		$outstr="";
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"id",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"stringerror",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="Short Date";
		$outstr.='"'.htmlspecialchars(GetData($row,"fecha",$format)).'"';
		if($outstr!="")
			$outstr.=",";
			$format="";
		$outstr.='"'.htmlspecialchars(GetData($row,"procedimiento",$format)).'"';
		echo $outstr;
		echo "\r\n";
		$iNumberOfRows++;
	}

//	display totals
	$first=true;

}


function WriteTableData()
{
	global $rs,$nPageSize,$strTableName,$conn;
	if(!db_numrows($rs))
		return;
// write header
	echo "<tr>";
	if($_REQUEST["type"]=="excel")
	{
		echo '<td style="width: 100" x:str>'.PrepareForExcel("id").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("stringerror").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("fecha").'</td>';
		echo '<td style="width: 100" x:str>'.PrepareForExcel("procedimiento").'</td>';
	}
	else
	{
		echo "<td>id</td>";
		echo "<td>stringerror</td>";
		echo "<td>fecha</td>";
		echo "<td>procedimiento</td>";
	}
	echo "</tr>";

	$totals=array();
// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row=db_fetch_array($rs))
	{
		echo "<tr>";
	echo '<td>';

		$format="";
			echo htmlspecialchars(GetData($row,"id",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"stringerror",$format));
		else
			echo htmlspecialchars(GetData($row,"stringerror",$format));
	echo '</td>';
	echo '<td>';

		$format="Short Date";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"fecha",$format));
		else
			echo htmlspecialchars(GetData($row,"fecha",$format));
	echo '</td>';
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';

		$format="";
			if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"procedimiento",$format));
		else
			echo htmlspecialchars(GetData($row,"procedimiento",$format));
	echo '</td>';
		echo "</tr>";
		$iNumberOfRows++;
	}

}

function XMLNameEncode($strValue)
{	
	$search=array(" ","#","'","/","\\","(",")",",","[","]","+","\"","-","_","|","}","{","=");
	return str_replace($search,"",$strValue);
}

function PrepareForExcel($str)
{
	$ret = htmlspecialchars($str);
	if (substr($ret,0,1)== "=") 
		$ret = "&#61;".substr($ret,1);
	return $ret;

}




function ExportToPDF()
{
	global $nPageSize,$rs,$strTableName,$conn;
		global $colwidth,$leftmargin;
	if(!db_numrows($rs))
		return;


	include("libs/fpdf.php");

	class PDF extends FPDF
	{
	//Current column
		var $col=0;
	//Ordinate of column start
		var $y0;
		var $maxheight;

	function AcceptPageBreak()
	{
		global $colwidth,$leftmargin;
		if($this->y0+$this->rowheight>$this->PageBreakTrigger)
			return true;
		$x=$leftmargin;
		if($this->maxheight<$this->PageBreakTrigger-$this->y0)
			$this->maxheight=$this->PageBreakTrigger-$this->y0;
		$this->Rect($x,$this->y0,$colwidth["id"],$this->maxheight);
		$x+=$colwidth["id"];
		$this->Rect($x,$this->y0,$colwidth["stringerror"],$this->maxheight);
		$x+=$colwidth["stringerror"];
		$this->Rect($x,$this->y0,$colwidth["fecha"],$this->maxheight);
		$x+=$colwidth["fecha"];
		$this->Rect($x,$this->y0,$colwidth["procedimiento"],$this->maxheight);
		$x+=$colwidth["procedimiento"];
		$this->maxheight = $this->rowheight;
//	draw frame	
		return true;
	}

	function Header()
	{
		global $colwidth,$leftmargin;
	    //Page header
		$this->SetFillColor(192);
		$this->SetX($leftmargin);
		$this->Cell($colwidth["id"],$this->rowheight,"id",1,0,'C',1);
		$this->Cell($colwidth["stringerror"],$this->rowheight,"stringerror",1,0,'C',1);
		$this->Cell($colwidth["fecha"],$this->rowheight,"fecha",1,0,'C',1);
		$this->Cell($colwidth["procedimiento"],$this->rowheight,"procedimiento",1,0,'C',1);
		$this->Ln($this->rowheight);
		$this->y0=$this->GetY();
	}

	}

	$pdf=new PDF();

	$leftmargin=5;
	$pagewidth=200;
	$pageheight=290;
	$rowheight=5;


	$defwidth=$pagewidth/4;
	$colwidth=array();
    $colwidth["id"]=$defwidth;
    $colwidth["stringerror"]=$defwidth;
    $colwidth["fecha"]=$defwidth;
    $colwidth["procedimiento"]=$defwidth;
	
	$pdf->AddFont('CourierNewPSMT','','courcp1252.php');
	$pdf->rowheight=$rowheight;
	
	$pdf->SetFont('CourierNewPSMT','',8);
	$pdf->AddPage();
	

	$i=0;
	while((!$nPageSize || $i<$nPageSize) && $row=db_fetch_array($rs))
	{
		$pdf->maxheight=$rowheight;
		$x=$leftmargin;
		$pdf->SetY($pdf->y0);
		$pdf->SetX($x);
		$pdf->MultiCell($colwidth["id"],$rowheight,GetData($row,"id",""));
		$x+=$colwidth["id"];
		if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
			$pdf->maxheight=$pdf->GetY()-$pdf->y0;
		$pdf->SetY($pdf->y0);
		$pdf->SetX($x);
		$pdf->MultiCell($colwidth["stringerror"],$rowheight,GetData($row,"stringerror",""));
		$x+=$colwidth["stringerror"];
		if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
			$pdf->maxheight=$pdf->GetY()-$pdf->y0;
		$pdf->SetY($pdf->y0);
		$pdf->SetX($x);
		$pdf->MultiCell($colwidth["fecha"],$rowheight,GetData($row,"fecha","Short Date"));
		$x+=$colwidth["fecha"];
		if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
			$pdf->maxheight=$pdf->GetY()-$pdf->y0;
		$pdf->SetY($pdf->y0);
		$pdf->SetX($x);
		$pdf->MultiCell($colwidth["procedimiento"],$rowheight,GetData($row,"procedimiento",""));
		$x+=$colwidth["procedimiento"];
		if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
			$pdf->maxheight=$pdf->GetY()-$pdf->y0;
//	draw fames
		$x=$leftmargin;
		$pdf->Rect($x,$pdf->y0,$colwidth["id"],$pdf->maxheight);
		$x+=$colwidth["id"];
		$pdf->Rect($x,$pdf->y0,$colwidth["stringerror"],$pdf->maxheight);
		$x+=$colwidth["stringerror"];
		$pdf->Rect($x,$pdf->y0,$colwidth["fecha"],$pdf->maxheight);
		$x+=$colwidth["fecha"];
		$pdf->Rect($x,$pdf->y0,$colwidth["procedimiento"],$pdf->maxheight);
		$x+=$colwidth["procedimiento"];
		$pdf->y0+=$pdf->maxheight;
		$i++;
	}
	$pdf->Output();
}

?>