<?php
/**
 * Class
 *
 * @author Tiago Gossmann [tiagog@solis.coop.br]
 *
 * $version: $Id$
 *
 * \b Maintainers \n
 * Eduardo Bonfandini [eduardo@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Luiz Gregory Filho [luiz@solis.coop.br]
 * Moises Heberle [moises@solis.coop.br]
 *
 * @since
 * Class created on 02/07/2007
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2008 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPL (for further details read the COPYING file or http://www.gnu.org/copyleft/gpl.html )
 *
 * \b History: \n
 * See history in SVN repository: http://gnuteca.solis.coop.br
 **/

require_once('fpdf/fpdf.php');

class GnutecaPDFLabel extends FPDF
{
	public $MIOLO;
	public $module;
    public $x, $y, $column, $line, $labelLayout, $pdf;
    public $filename;


    public function __construct($labelLayout)
    {
    	$this->MIOLO   = MIOLO::getInstance();
    	$this->module  = MIOLO::getCurrentModule();

        $this->labelLayout = $labelLayout;
        $this->column      = 0;
        $this->line        = 0;

        $pFormat = $this->labelLayout->pageFormat;
        if (strtolower($pFormat) == 'automatic')
        {
        	$pFormat = 'A4';
        }
        $this->pdf = new FPDF('P', 'cm', $pFormat);
        $this->pdf->setAuthor('Gnuteca 3');
        $this->pdf->setCreator('Gnuteca 3');

        //Ajusta a margem esquerda e direita
        $this->pdf->setMargins($this->labelLayout->leftMargin, $this->labelLayout->topMargin, 0);
        $this->pdf->SetAutoPageBreak(false, 0);
        $this->pdf->addPage();
    }


    public function checkBreakLine()
    {
        if ($this->column >= $this->labelLayout->columns)
        {
            $this->addBreakLine();
            return true;
        }
        return false;
    }


    public function addBreakLine()
    {
        $this->column = 0;
        $this->line++;
    }


    public function checkBreakPage()
    {
        if ($this->line >= $this->labelLayout->lines)
        {
            $this->line = 0;
            $this->column = 0;
            $this->pdf->addPage();
            return true;
        }
        return false;
    }


    public function setBeginPositionOfTheLabel()
    {
        $this->x = $this->labelLayout->leftMargin+($this->labelLayout->horizontalSpacing*$this->column);
        $this->y = $this->labelLayout->topMargin+($this->labelLayout->verticalSpacing*$this->line);
        $this->pdf->setXY($this->x, $this->y);

        $this->column++;
    }


    public function setBeginLabel($beginLabel)
    {
        $this->column = (int) ($beginLabel%($this->labelLayout->columns))-1;
        $this->line   = (int) ($beginLabel/($this->labelLayout->columns));
        if ($this->column < 0)
        {
            $this->column = $this->labelLayout->columns-1;
            $this->line--;
        }
    }


    public function lengthLabel()
    {
        $w = $this->labelLayout->labelWidth;
        $h = $this->labelLayout->labelHeight;

        return array($w, $h);
    }


    public function output($filename = null)
    {
        if ($filename == null)
        {
            $filename = $this->getFilename();
        }
        else
        {
            $this->setFilename($filename);
        }
        $pathFile = $this->MIOLO->getModulePath($this->module, "html/files/{$filename}");
        $this->pdf->output($pathFile, 'F');
    }


    public function setFilename($filename)
    {
        $this->filename = $filename;
    }


    public function getFilename()
    {
        return $this->filename;
    }


    public function getDownloadURL()
    {
        $module = MIOLO::getCurrentModule();
        $filename = $this->getFilename();
        return "index.php/{$module}/files/{$filename}";
    }



    public function showDownloadInfo()
    {
        global $module;
        $f = new GnutecaForm();
        $f->information(_M('File generated. Click on OK button to download', $this->module), "window.open('".$this->getDownloadURL()."')", null, true);
        return $lblDownload;
    }
}

?>
