<?php if (!defined("SITE")) die(-1);?>
<?php
class jaRender{
public $reports = array();
public $oF = array();
public function __construct(){
}
public function __destruct(){
foreach ($this->oF as $file){
if (is_file($file)) :
@unlink($file);
endif;
}
}
public function addReport($reportTitle, $reportData, $reportFields, $reportShortTitle, $reportType, $js, $uid){
$this->reports[] = array('title'=>$reportTitle, 'data'=>$reportData, 'fields'=>$reportFields, 'shortTitle'=>$reportShortTitle, 'type'=>$reportType, 'js'=>$js, 'uid'=>$uid);
}
public function render($type='view'){
switch (strtolower($type)):
case 'view':
if ($this->numReports() > 1){
$reports =& $this->reports;
require TEMPLATES . DIRECTORY_SEPARATOR . 'multiple.php';
} else {
$report = $this->reports[0];
require TEMPLATES . DIRECTORY_SEPARATOR . 'single.php';
}
break;
case 'zip':
ob_start();
$content = array();
$this->oF = array();
$arg = array();
$title = array();
$zipFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('temp_zip', true) .'.zip';
foreach ($this->reports as $report):
ob_start();
require TEMPLATES . DIRECTORY_SEPARATOR . 'single.php';
$content = ob_get_contents();
ob_clean();
//write to a file
$input = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('temp_html', true) .'.html';
file_put_contents($input, $content);
//echo $input ."<br/>";
//convert to pdf
$title = preg_replace('/(\d{2}\/\d{2}\/\d{4})/e', "dTransform('\\1')", $report['title']);
$output = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $title .'.pdf';
$cmd = 'wkhtmltopdf --orientation Landscape --footer-center "Page [page] of [toPage]" ' . escapeshellarg($input) . ' '. escapeshellarg($output);
//echo $cmd . '<br/>';
exec($cmd);
//delete html file
unlink($input);
//add to zip
$option = file_exists($zipFile) ? '-g': '';
$cmd = "zip -D $option " . escapeshellarg($zipFile) ." " . escapeshellarg($output);
//echo $cmd . '<br/>';
exec($cmd);
//delete the pdf file
unlink($output);
endforeach;
if (is_file($zipFile)):
header ('Content-Type: application/zip');
header('Content-Length: ' . filesize($zipFile));
header('Content-Disposition: attachment; filename="RocketRoute Reports.zip"');
@readfile($zipFile);
endif;
unlink($zipFile);
exit;
break;
case 'pdf':
ob_start();
$content = array();
$this->oF = array();
$arg = array();
foreach ($this->reports as $report):
require TEMPLATES . DIRECTORY_SEPARATOR . 'single.php';
$content[] = ob_get_contents();
ob_clean();
endforeach;
ob_end_clean();
foreach ($content as $c):
$this->oF[] = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('temp_html', true) .'.html';
$arg[] = escapeshellarg(end($this->oF));
file_put_contents(end($this->oF), $c);
endforeach;
$output = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('temp_pdf', true) .'.pdf';
$cmd = ' --orientation Landscape --footer-center "Page [page] of [toPage]" ' . implode (' ', $arg) . ' '. escapeshellarg($output);
$this->oF[] = $output;
exec($cmd, $outputString, $return);
if (is_file($output)):
//echo 'ok';
header ('Content-Type: application/pdf');
header('Content-Length: ' . filesize($output));
header('Content-Disposition: attachment; filename="RocketRoute Reports.pdf"');
@readfile($output);
endif;
break;
case 'xls':
case 'excel':
ob_end_clean();
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer();
foreach($this->reports as $report):
// Creating a worksheet
$worksheet = $workbook->addWorksheet($report['shortTitle']);
if($worksheet instanceof PEAR_error):
die ($worksheet->getMessage());
endif;
$rowCount=1;
$worksheet->write(0,0,$report['title']);
foreach(array_keys($report['fields']) as $key=>$field):
$worksheet->write($rowCount, $key, $field );
endforeach;
foreach($report['data'] as $row):
$rowCount++;
foreach(array_values($row) as $key=>$value):
$worksheet->write($rowCount, $key, $value);
endforeach;
endforeach;
endforeach;
$workbook->send('RocketRouteReports.xls');
$workbook->close();
endswitch;
}
public function numReports(){
return count ($this->reports);
}
}
?>