<?php
require('fpdf.php');

class PDF_MC_Table extends FPDF {
var $widths;
var $aligns;
var $fontsize_int;
var $intLinea_int;
var $ragsoc;
var $w1;
var $w2;
var $mezzodifTitle;
var $mezzodif;
var $tipotrasmisTitle;
var $tipotrasmis;
var $emittenteTitle;
var $emittente;
var $descriptionTitle;
var $description;
var $val;
var $margintop;
var $colw;
var $align;
var $headerreg;
var $totw;
var $border;

function Header() {

	$dtot=210;
	$dx=0;
	if($this->orientamento==1) { // Landscape (orizzontale)
		$dtot=297;
		$dx=97;
	}

	if($this->val==5 || $this->val==1 || $this->val==4) {
		//$this->SetFont('Arial','',$this->fontsize_int);
		$this->SetFont('times','',$this->fontsize_int);
		//$this->SetFont('calibri','',$this->fontsize_int);

		//$this->MultiCell(0,$this->intLinea_int, $this->ragsoc, 0,'C');
		/*
		$dtot=210;
		$dx=80;
		if($this->orientamento==1) { // Landscape
			$dtot=297;
			$dx=97;
		}
		*/

		$cella1=$this->w2;
		$cella3=$this->w2;
		$cella2=$dtot-($this->w2*2)-$cella3;

		$this->Cell($cella1,$this->intLinea_int,$this->ragsoc,0,0,'L'); // SX
	    $this->Cell($cella2,$this->intLinea_int,$this->centro_int,0,0,'C'); // CENTRO

		if($this->val!=4) $this->Cell($cella3,$this->intLinea_int,"",0,1,'R'); // DESTRA
		if($this->val==4) $this->Cell($cella3,$this->intLinea_int,"PAG. N ".($this->nrfrom+$this->f) ."/".$this->nrto,0,1,'R'); // DESTRA

		$this->Ln(3);

		$this->Cell($this->w1,$this->intLinea_int,$this->mezzodifTitle.": ".$this->mezzodif,0,0,'L');
		$this->Cell($dx,$this->intLinea_int,$this->tipotrasmisTitle.": ".$this->tipotrasmis,0,1,'R');
        $this->Ln(3);
		//$this->Cell($this->w2);
		$this->Cell($this->w1,$this->intLinea_int,$this->emittenteTitle.": ".$this->emittente,0,0,'L');
		$this->Cell($dx,$this->intLinea_int,$this->descriptionTitle.": ".$this->description,0,1,'R');
		$this->Ln(3);
		//if($this->val!=5) $this->SetAutoPageBreak(true,$this->$marginbottom);
	}

	if($this->val==3) {
		//$this->SetFont('Arial','',$this->fontsize_int);
		$this->SetFont('times','',$this->fontsize_int);
		//$this->SetFont('calibri','',$this->fontsize_int);

/*
		$dtot=210;
		$dx=80;
		if($this->orientamento==1) { // Landscape
			$dtot=297;
			$dx=97;
		}
*/
		$cella1=$this->w2;
		$cella3=$this->w2;
		$cella2=$dtot-($this->w2*2)-$cella3;
		$this->Ln(3);
	}

	if($this->val==5 || $this->val==3) {

		//$this->SetFont('Arial','B',6);
		$this->SetFont('times','B',6);
		//$this->SetFont('calibri','B',6);
		$this->SetWidths($this->colw);
		$this->SetAligns($this->align);
		$this->RowHeader($this->headerreg);
		$this->Cell($this->totw,1,'','T',2);
		$this->Ln(3);
	}
}

function SetBorder($border) {
	$this->setborder=$border;
}
function SetCentroInt($centro_int){
	$this->centro_int=$centro_int;
}
function SetOrientamento($orientamento){
	$this->orientamento=$orientamento;
}
function SetColw($colw){
	$this->colw=$colw;
}
function SetAlign($align){
	$this->align=$align;
}
function SetHeaderReg($headerreg){
	$this->headerreg=$headerreg;
}
function SetTotw($totw){
	$this->totw=$totw;
}
function SetFontSize_int($fontsize_int){
	$this->fontsize_int=$fontsize_int;
}
function SetIntLinea_int($intLinea_int){
	$this->intLinea_int=$intLinea_int;
}
function SetRagSoc($ragsoc){
	$this->ragsoc=$ragsoc;
}
function SetW1($w1){
	$this->w1=$w1;
}
function SetW2($w2){
	$this->w2=$w2;
}
function SetNrTo($nrto){
	$this->nrto=$nrto;
}
function SetNrFrom($nrfrom){
	$this->nrfrom=$nrfrom;
}
function SetF($f){
	$this->f=$f;
}
function SetMezzoDif($mezzodif){
	$this->mezzodif=$mezzodif;
}
function SetMezzoDifTitle($mezzodifTitle){
	$this->mezzodifTitle=$mezzodifTitle;
}
function SetTipoTrasmisTitle($tipotrasmisTitle){
	$this->tipotrasmisTitle=$tipotrasmisTitle;
}
function SetTipoTrasmis($tipotrasmis){
	$this->tipotrasmis=$tipotrasmis;
}
function SetEmittenteTitle($emittenteTitle){
	$this->emittenteTitle=$emittenteTitle;
}
function SetEmittente($emittente){
	$this->emittente=$emittente;
}
function SetDescriptionTitle($descriptionTitle){
	$this->descriptionTitle=$descriptionTitle;
}
function SetDescription($description){
	$this->description=$description;
}
function SetVal($val){
	$this->val=$val;
}
function SetMarginTop($margintop){
	$this->margintop=$margintop;
}
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 SetHeights($h) {
	//Set the array of column widths
	$this->heights=$h;
}

function RowHeader($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]));
    //$h=5*$nb;
	$h=3*$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] : 'C';
        //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,5,$data[$i],0,$a); // Originale
		$this->MultiCell($w,3,$data[$i],'T',$a, false);
        //Put the position to the right of the cell
        $this->SetXY($x+$w,$y);
    }
    //Go to the next line
    $this->Ln($h);
}

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]));
        $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        $h1 = isset($this->heights) ? $this->heights : '5'; // Fey 11/2021 moltiplico il valore di interlinea in setup con num max di righe che trova 
    }

    $h=$h1*$nb; 
    //$h=5*$nb;
	//$h=3*$nb; // fino al 13/01/17
	//$h=2.3*$nb; // default
    //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';
		$bord=isset($this->setborder) ? $this->setborder : 0;
		//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,5,$data[$i],0,$a); // Originale
		$this->MultiCell($w,3,$data[$i],$bord,$a);
        //Put the position to the right of the cell
        $this->SetXY($x+$w,$y);
    }
    //Go to the next line
    $this->Ln($h);
}

    function _Row($data)
    {

        //Calculate the height of the row
        $nb = 0;

        // num max di righe che occuperà il testo
        for ($i = 0; $i < count($data); $i++) {
            //echo $this->NbLines($this->widths[$i], $data[$i]) . "<br/>";
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i])); 
        }

        //echo count($data) . "<br/>";
        //echo $nb . "<br/>";
        $h = 5 * $nb; // xchè 5

        //echo $h . "<br/>";
        //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, 8);
            //Print the text
            $this->MultiCell($w, 5, $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;
}


    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;
    }
}
?>
