<?php
/* ****************************************
Header Master Sertifikat
Edit By: ardiansyah@fmipa.unila.ac.id
Created Date: 28/03/2020
Last Modification: 29/03/2020
* *****************************************/

//include_once "function.php";
require('../fpdf182/fpdf.php');
include "db.php";




class PDF extends FPDF
{
    var $bgSertifikat;
    var $stateSertifikat;
    
    function setBgSertifikat($path, $state)
    {
        $this->bgSertifikat = $path;   
        $this->stateSertifikat = $state;
    }
    
    // Page header
    function Header()
    {
        if($this->stateSertifikat == 0) {
            $this->Image($this->bgSertifikat,0,0,210,297);
        } else {
            $this->Image($this->bgSertifikat,0,0,210,170);
        }
    }
    
    //Page Footer Edit
    function Footer()
    {
        
    }



    // Wrapping Cell
    var $widths;
    var $aligns;
    var $spacing;
    var $bold_font;
    
    function SetSpacing($spc)
    {
        $this->spacing = $spc;
    }
    
    function SetWidths($w)
    {
        //Set the array of column widths
        $this->widths=$w;
    }
    
    function SetAligns($a)
    {
        //Set the array of column alignments
        $this->aligns=$a;
    }
    
    function SetBoldFont($a)
    {
        $this->bold_font = $a;
    }
    
    function SubHeader($data)
    {
        for($i=0;$i<count($data);$i++)
        {
            $this->Cell($this->widths[$i], 8, $data[$i], 1, 0, 'C', true);
        }
        $this->Ln();
    }
    
    function SubHeaderNoBack($data)
    {
        $this->SetFont('','B');
        for($i=0;$i<count($data);$i++)
        {
            $this->Cell($this->widths[$i], 8, $data[$i], 1, 0, 'C', false);
        }
        $this->Ln();
    }
    
    function Row($data)
    {
        //Calculate the height of the row
        $nb=0;
        for($i=0;$i<count($data);$i++)
            $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        $space = $a=isset($this->spacing) ? $this->spacing : 5;
        $h=$space*$nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for($i=0;$i<count($data);$i++)
        {
            $w=$this->widths[$i];
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            $style=isset($this->bold_font[$i]) ? $this->bold_font[$i] : '';
            //Save the current position
            $x=$this->GetX();
            $y=$this->GetY();
            //Draw the border
            $this->Rect($x,$y,$w,$h);
            //Print the text
            $this->SetFont('', $style);
            $this->MultiCell($w,$space,$data[$i],0,$a);
            //Put the position to the right of the cell
            $this->SetXY($x+$w,$y);
        }
        //Go to the next line
        $this->Ln($h);
    }
    
    function RowNoBorder($data)
    {
        //Calculate the height of the row
        $nb=0;
        for($i=0;$i<count($data);$i++)
            $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        $space = $a=isset($this->spacing) ? $this->spacing : 5;
        $h=$space*$nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for($i=0;$i<count($data);$i++)
        {
            $w=$this->widths[$i];
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            //Save the current position
            $x=$this->GetX();
            $y=$this->GetY();
            //Draw the border
            //$this->Rect($x,$y,$w,$h);
            //Print the text
            $this->MultiCell($w,$space,$data[$i],0,$a);
            //Put the position to the right of the cell
            $this->SetXY($x+$w,$y);
        }
        //Go to the next line
        $this->Ln($h);
    }
    
    function CheckPageBreak($h)
    {
        //If the height h would cause an overflow, add a new page immediately
        if($this->GetY()+$h>$this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }
    
    function NbLines($w,$txt)
    {
        //Computes the number of lines a MultiCell of width w will take
        $cw=&$this->CurrentFont['cw'];
        if($w==0)
            $w=$this->w-$this->rMargin-$this->x;
        $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
        $s=str_replace("\r",'',$txt);
        $nb=strlen($s);
        if($nb>0 and $s[$nb-1]=="\n")
            $nb--;
        $sep=-1;
        $i=0;
        $j=0;
        $l=0;
        $nl=1;
        while($i<$nb)
        {
            $c=$s[$i];
            if($c=="\n")
            {
                $i++;
                $sep=-1;
                $j=$i;
                $l=0;
                $nl++;
                continue;
            }
            if($c==' ')
                $sep=$i;
            $l+=$cw[$c];
            if($l>$wmax)
            {
                if($sep==-1)
                {
                    if($i==$j)
                        $i++;
                }
                else
                    $i=$sep+1;
                $sep=-1;
                $j=$i;
                $l=0;
                $nl++;
            }
            else
                $i++;
        }
        return $nl;
    }
    

    // SUB Write
    function subWrite($h, $txt, $link='', $subFontSize=12, $subOffset=0)
    {
        // resize font
        $subFontSizeold = $this->FontSizePt;
        $this->SetFontSize($subFontSize);
        
        // reposition y
        $subOffset = ((($subFontSize - $subFontSizeold) / $this->k) * 0.3) + ($subOffset / $this->k);
        $subX        = $this->x;
        $subY        = $this->y;
        $this->SetXY($subX, $subY - $subOffset);
    
        //Output text
        $this->Write($h, $txt, $link);
    
        // restore y position
        $subX        = $this->x;
        $subY        = $this->y;
        $this->SetXY($subX,  $subY + $subOffset);
    
        // restore font size
        $this->SetFontSize($subFontSizeold);
    }
    
    
    // Transparancy
    protected $extgstates = array();

    // alpha: real value from 0 (transparent) to 1 (opaque)
    // bm:    blend mode, one of the following:
    //          Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn,
    //          HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
    function SetAlpha($alpha, $bm='Normal')
    {
        // set alpha for stroking (CA) and non-stroking (ca) operations
        $gs = $this->AddExtGState(array('ca'=>$alpha, 'CA'=>$alpha, 'BM'=>'/'.$bm));
        $this->SetExtGState($gs);
    }

    function AddExtGState($parms)
    {
        $n = count($this->extgstates)+1;
        $this->extgstates[$n]['parms'] = $parms;
        return $n;
    }

    function SetExtGState($gs)
    {
        $this->_out(sprintf('/GS%d gs', $gs));
    }

    function _enddoc()
    {
        if(!empty($this->extgstates) && $this->PDFVersion<'1.4')
            $this->PDFVersion='1.4';
        parent::_enddoc();
    }

    function _putextgstates()
    {
        for ($i = 1; $i <= count($this->extgstates); $i++)
        {
            $this->_newobj();
            $this->extgstates[$i]['n'] = $this->n;
            $this->_put('<</Type /ExtGState');
            $parms = $this->extgstates[$i]['parms'];
            $this->_put(sprintf('/ca %.3F', $parms['ca']));
            $this->_put(sprintf('/CA %.3F', $parms['CA']));
            $this->_put('/BM '.$parms['BM']);
            $this->_put('>>');
            $this->_put('endobj');
        }
    }

    function _putresourcedict()
    {
        parent::_putresourcedict();
        $this->_put('/ExtGState <<');
        foreach($this->extgstates as $k=>$extgstate)
            $this->_put('/GS'.$k.' '.$extgstate['n'].' 0 R');
        $this->_put('>>');
    }

    function _putresources()
    {
        $this->_putextgstates();
        parent::_putresources();
    }

}

function create_qrcode($txt, $filepath)
{
    include("phpqrcode/qrlib.php");
    $logopath ="/home/uptbahasa/public_html/eSertifikat/download/images/logo.png";
    
    //ob_start("callback");
    //$debugLog = ob_get_contents();
    //ob_end_clean();
    
    QRcode::png($txt, $filepath, QR_ECLEVEL_H, 3, true );
    // Start DRAWING LOGO IN QRCODE

    $QR = imagecreatefrompng($filepath);

        // START TO DRAW THE IMAGE ON THE QR CODE
        $logo = imagecreatefromstring(file_get_contents($logopath));
        $QR_width = imagesx($QR);
        $QR_height = imagesy($QR);

        $logo_width = imagesx($logo);
        $logo_height = imagesy($logo);

        // Scale logo to fit in the QR Code
        $logo_qr_width = $QR_width/3;
        $scale = $logo_width/$logo_qr_width;
        $logo_qr_height = $logo_height/$scale;

        imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

        // Save QR code again, but with logo on it
        imagepng($QR,$filepath);

}

function ordinal($number) {
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if ((($number % 100) >= 11) && (($number%100) <= 13))
        return 'th';
    else
        return $ends[$number % 10];
}

/* GET DATA for PRINT */

$ID = $_GET['id'];
$date = $_GET['tgl'];

$conn = connect_db();
$person = get_data_person($conn, $ID);
$nilai = get_nilai_by_ID($conn, $ID, $date);


// QR Code
$pathname = "/home/uptbahasa/public_html/eSertifikat/download/images/qrcode/".$ID.".png";
$txt = "https://uptbahasa.unila.ac.id/eSertifikat/verify.php?code=".$nilai['verif_code'];
create_qrcode($txt, $pathname);


// Personal Data
$ID = $person['ID'];
$nama = $person['nama'];
$tgl_lahir = $person['normal_tgl_lahir'];

// Sertifikat
$tgl_tes = $nilai['tes_tgl'];
$lis = $nilai['lis'];
$str = $nilai['str'];
$rv = $nilai['rv'];
$skor = $nilai['skor'];
$tgl_ttd = $nilai['tgl_ttd'];
$tes_ke = $nilai['tes_ke'];
$nama_ka_upt = $nilai['nama'];
$nip_ka_upt = $nilai['nip'];
$sertifikat_no = $nilai['sertifikat_NO'];
$sertifikat_bln = $nilai['sertifikat_BLN'];

$date_tes = date_create($tgl_tes);
$date_ttd = date_create($tgl_ttd);
$sertifikat_number = $sertifikat_no."-".$sertifikat_bln."/UN26.33/TU.00.08/".date_format($date_tes, "Y");
