<?php
if (! $_REQUEST ['record'] || $_REQUEST ['record'] == '')
	die ( 'Brak rekordu' );
else
	$record = $_REQUEST ['record'];
include_once ("include/MPDF57/mpdf.php");

if($_REQUEST['generateFromTeplate'] != ''){
	include_once ("modules/kwota.php");
	$kwota=new KwotaSlownie();

	$focus = new EcmAgreement ();
	$focus->retrieve ( $record );
	$db = $GLOBALS ['db'];
    $q = 'SELECT pdf_text,pdf_template FROM ecmagreements WHERE id="' . $focus->id . '"';
	$rs = $db->query($q);
    $row = $db->fetchByAssoc($rs);
	$pdf_margins_query = "SELECT value6 as m_top,value7 as m_bottom, value8 as m_left, value9 as m_right FROM operating_values WHERE id='" . $row['pdf_template'] ."'";
	$pdf_margins_rs = $db->query($pdf_margins_query);
    $pdf_margins = $db->fetchByAssoc($pdf_margins_rs);
	$p = new mPDF ( '', 'A4', null, 'helvetica',  $pdf_margins['m_left'], $pdf_margins['m_right'], $pdf_margins['m_top'], $pdf_margins['m_bottom'], 5, 5 );
	$p->mirrorMargins = 1;
	
	
	$template = html_entity_decode($row ['pdf_text']);
	
	$p->WriteHTML ( $template );
	$p->Output ();
	return true;
}else if($_REQUEST['generateFromTeplateExample'] != ''){
	include_once ("modules/kwota.php");
	$kwota=new KwotaSlownie();

	$focus = new EcmAgreement ();
	$focus->retrieve ( $record );
	$db = $GLOBALS ['db'];
    $q= 'SELECT text0 as pdf_template,value6 as margin_top, value7 as margin_bottom, value8 as margin_left,value9 as margin_right FROM operating_values WHERE operating_values.name="AgreementTemplates" AND module_name="EcmAgreements" AND id="' . $_REQUEST['generateFromTeplateExample'] . '"';
	$rs = $db->query($q);
    $row = $db->fetchByAssoc($rs);

	$p = new mPDF ( '', 'A4', null, 'helvetica',  $row['margin_left'], $row['margin_right'], $row['margin_top'], $row['margin_bottom'], 5, 5 );
	$p->mirrorMargins = 1;
	$template = html_entity_decode($row ['pdf_template']);
	$p->WriteHTML ($template);
	$p->Output ('pdfexample.pdf', 'F');
	print ('1');
}else{
	include_once ("modules/EcmAgreements/PDFTemplate/helper.php");

	$focus = new EcmAgreement ();
	$focus->retrieve ( $record );

	$user = new User ();
	$user->retrieve ( $focus->assigned_user_id );
	$labels = return_module_language($focus->ecmlanguage, 'EcmAgreements');

	$p = new mPDF ( '', 'A4-L', null, 'helvetica', 10, 10, 20, 25, 5, 5 );
	$mpdf->mirrorMargins = 1;

	$db = $GLOBALS ['db'];
	// document pdf
	// document already exist?
	/*
	$res = $db->query ( "SELECT footer, content FROM ecmagreement_pdf WHERE id='$record'" );
	if ($res->num_rows == 0) {
	*/
		// create and save document
		$positions = formatPDFPositions ( $focus->getPositionList ( true ), $focus );
		// get header
		$header = '';
		include_once ("modules/EcmAgreements/PDFTemplate/header.php");
		$content = '';
		include_once ("modules/EcmAgreements/PDFTemplate/content.php");
		
		global $current_user,$app_list_strings;
		/*
		$db->query ( "INSERT INTO ecmagreement_pdf VALUES ('$record','$current_user->id', NOW(),'" . urlencode ( $header ) . "', '" . urlencode( $content ) . "')" );
	} else {
		$row = $db->fetchByAssoc ( $res );
		$header = urldecode ( $row ['footer'] ); // punk rock!
		$content = urldecode ( $row ['content'] );
	}*/
	//var_dump($positions);

	$p->WriteHTML ( $content );
	//echo $content;
	// draw PDF
	$p->Output ();
}
