<?php
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');

// Table Print

$mFunction  		= $_GET['function'];
$mConnect   		= $_GET['connection'];
$mUser				= $_GET['user'];
$mPwd				= $_GET['pword'];
$mHost              = $_GET['host'];
$mDbName			= $_GET['dbase'];
$mTableName 		= $_GET['table'];
$mFilter			= $_GET['filter'];
$mReturnFields		= $_GET['returnfields'];

$mFilter = str_replace("|AND|","&",$mFilter);
$mFilter = str_replace("/dbase/",$mDbName,$mFilter);

include('../barcode/php-barcode.php');
require('../barcode/fpdf.php');
require(dirname(__FILE__) . '/../../vimsconfig/phpConfig.php');

class PDF extends FPDF
{
// Load data
	function LoadData($mConnection, $mTableName, $mFilter)
	{

// Read file lines
		$data = array();

		$mFilter = str_replace(";",",",$mFilter);
// If Input table doesn't start with SELECT assume it's just a table name
		if (strtolower(substr($mTableName,0,6)) != "select")
			$mTableName = "SELECT * FROM ".$mTableName;
// Check for Filter / selection
//	if (strtolower(substr($mFilter,0,5)) != "where")
//		$mFilter = "WHERE ".$mFilter;
		$mDataQuery=$mTableName." ".$mFilter;
		$s = strpos(strtolower($mDataQuery),"limit");
		if ($s == 0)
			$mDataQuery = $mDataQuery." LIMIT 1";

// mysql query construct
		$mData = mysqli_query($mConnection,$mDataQuery);
		$i = 0;

		while($mRow = mysqli_fetch_assoc($mData)) 
		{
			$data[] =($mRow);
			$i++;
		}
	
//    print_r($data.":".$mDataQuery.":".$i);
	
		return $data;

	}

// barcode table
	function BarcodeTable($header, $data, $prefix)
	{
// -------------------------------------------------- //
//                  PROPERTIES
// -------------------------------------------------- //  
		$fontSize = 8;
		$marge    = 5;   // between barcode and hri in pixel
		$x        = 70;  // barcode center
		$y        = 25;  // barcode center
		$height   = 10;   // barcode height in 1D ; module size in 2D
		$width    = 0.35;    // barcode width in 1D ; not use in 2D
		$angle    = 0;   // rotation in degrees  
		$code     = 'VANTEC'; // barcode text;)
		$type     = 'code39';
		$black    = '000000'; // color in hex
// -------------------------------------------------- //  
// Header
		foreach($header as $col)
			$this->Cell(30,7,$col,1);
		$this->Ln();
// Data
		foreach($data as $row)
		{
			foreach($row as $key=>$col)
			{
				$this->Cell(30,9,$col,1);
				$this->Ln();
				$barCol = $col;
				if ($barCol != "")
				{
					for($i=0;$i<count($prefix);$i++)
					{
						if (strtolower($key) == strtolower($prefix[$i]['fieldName']))
							$barCol = $prefix[$i]['prefix'].$col;
					}
				}
				if (($barCol != $col) || ($key == "hash_code"))
				{
					$barcode = Barcode::fpdf($this, $black, $x, $y, $angle, $type, $barCol, $width, $height);
					$this->TextWithRotation($x, $y+10, $col, $angle);
//					$x = $x+50;
				}
			}
			$y = $y+18;
		}
	}
// Simple table
	function BasicTable($header, $data)
	{
// Header
		foreach($header as $col)
			$this->Cell(30,7,$col,1);
		$this->Ln();
// Data
		foreach($data as $row)
		{
			foreach($row as $col)
				$this->Cell(30,6,$col,1);
			$this->Ln();
		}
	}

// Better table
	function ImprovedTable($header, $data)
	{
// Column widths
		$w = array(10, 20, 40, 45);
// Header
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],7,$header[$i],1,0,'C');
		$this->Ln();
// Data
		foreach($data as $row)
		{
			$i = 0;
			foreach($row as $col)
			{
				$this->Cell($w[0],6,$col,'LR');
//				$this->Cell($w[1],6,$row[1],'LR');
//				$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
//				$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
				$i++;
				if ($i > 3)
					$i = 0;
			}	
			$this->Ln();
		}
    // Closing line
		$this->Cell(array_sum($w),0,'','T');
	}

// Colored table
	function FancyTable($header, $data)
	{
// Colors, line width and bold font
		$this->SetFillColor(255,0,0);
		$this->SetTextColor(255);
		$this->SetDrawColor(128,0,0);
		$this->SetLineWidth(.3);
		$this->SetFont('','B');
// Header
		$w = array(40, 35, 40, 45);
		for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
		$this->Ln();
// Color and font restoration
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('');
// Data
		$fill = false;
		foreach($data as $row)
		{
			$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
			$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
			$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
			$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
			$this->Ln();
			$fill = !$fill;
		}
// Closing line
		$this->Cell(array_sum($w),0,'','T');
	}
// Rotate Text
	function TextWithRotation($x, $y, $txt, $txt_angle, $font_angle=0)
    {
        $font_angle+=90+$txt_angle;
        $txt_angle*=M_PI/180;
        $font_angle*=M_PI/180;
    
        $txt_dx=cos($txt_angle);
        $txt_dy=sin($txt_angle);
        $font_dx=cos($font_angle);
        $font_dy=sin($font_angle);
    
        $s=sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET',$txt_dx,$txt_dy,$font_dx,$font_dy,$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
        if ($this->ColorFlag)
            $s='q '.$this->TextColor.' '.$s.' Q';
        $this->_out($s);
    }
}
// Get Field Prefixs
function getPrefix($mConnection)
{
// Read Headers
	$prefix = array();
		
	$mDataQuery = "SELECT field_name AS fieldName, prefix_name AS prefix FROM default_setting";
// mysql query construct
	if(!mysqli_query($mConnection,$mDataQuery))
		exit("FAIL-".mysqli_error($mConnection));
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$prefix[] =($mRow);
		$i++;
	}
	return $prefix;
//    print_r($data.":".$mDataQuery.":".$i);
}

$pdf = new PDF();
// Column headings
$header = array("Cell1","Cell2","Cell3","Cell4");

// mysql connection

if (!$mConnection=mysqli_connect($mHost,$mUser,$mPwd,$mDbName))
	$mConnection = $connection;

// Field Prefixes
$prefix = getPrefix($mConnection);
	
$data = $pdf->LoadData($mConnection, $mTableName, $mFilter);
$pdf->SetFont('Arial','',14);

$pdf->AddPage();
$pdf->BarcodeTable($header,$data,$prefix);

$pdf->AddPage();
$pdf->BasicTable($header,$data);
/*
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);

$pdf->AddPage();
$pdf->FancyTable($header,$data);
*/
$pdf->Output();
mysqli_close($mConnection);
?>