<?php
/**
 *
 * @author Luiz G Gregory Filho [luiz@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 28/11/2008
 *
 * \b @organization \n
 * SOLIS - Cooperativa de Soluções Livres \n
 * The Gnuteca3 development team
 *
 * \b Copyleft \n
 * Copyleft (L) 2008 - SOLIS - Cooperativa de Soluções 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 CVS repository: http://gnuteca.solis.coop.br
 *
 **/


class BusinessGnuteca3BusMaterialHistory extends GnutecaBusiness
{
    /**
     * Attributes
     */
    public  $MIOLO;

    public  $controlNumber,
            $materialHistoryId,
            $revisionNumber,
            $operator,
            $data,
            $chancesType,
            $fieldId,
            $subFieldId,
            $previousLine,
            $previousIndicator1,
            $previousIndicator2,
            $previousContent,
            $currentLine,
            $currentIndicator1,
            $currentIndicator2,
            $currentContent;

    public  $fullColumns,
            $localColumns;

    /**
     * Constructor Method
     */
    function __construct()
    {
        parent::__construct();

        $this->MIOLO    = MIOLO::getInstance();
        $this->module   = MIOLO::getCurrentModule();

        $this->localColumns = 'controlNumber,
                               revisionNumber,
                               operator,
                               data,
                               chancesType,
                               fieldId,
                               subFieldId,
                               previousLine,
                               previousIndicator1,
                               previousIndicator2,
                               previousContent,
                               currentLine,
                               currentIndicator1,
                               currentIndicator2,
                               currentContent';

        $this->fullColumns =  "materialhistoryid, {$this->localColumns}";

        $this->localColumns = str_replace(array(" ", "\n", "\t"), "", $this->localColumns);
        $this->fullColumns  = str_replace(array(" ", "\n", "\t"), "", $this->fullColumns);

        $this->setData(null);
        $this->setColumns();
        $this->setTables();
    }


    /**
     * Seta as tabelas
     *
     */
    public function setTables()
    {
        parent::setTables("gtcmaterialhistory");
    }


    /**
     * Este método seta as colunas da tabela.
     *
     * @param String
     */
    public function setColumns()
    {
        parent::setColumns($this->fullColumns);
    }


    /**
     * Seta as condições do sql
     *
     * @return string para where da consulta
     */
    public function getWhereCondition()
    {
        $where = "";

        if(!is_null($this->controlNumber))
        {
            $where.= " controlnumber = ? AND ";
        }
        if(!is_null($this->fieldId))
        {
            $where.= " fieldId = ? AND ";
        }
        if(!is_null($this->subFieldId))
        {
            $where.= " subFieldId = ? AND ";
        }

        if(strlen($where))
        {
            $where = substr($where, 0, strlen($where) - 4);
            parent::setWhere($where);
        }
    }


    function getMaterialHistory()
    {
        parent::clear();
        $this->setTables();
        parent::setColumns($this->fullColumns);
        parent::setWhere($this->getWhereCondition());
        $sql = parent::select(array($this->controlNumber, $this->fieldId, $this->subFieldId));
        return parent::query($sql, true);
    }


    /**
     * insere um registro no historico
     *
     */
    public function insertMaterialHistory()
    {
        parent::clear();

        $this->setTables();
        parent::setColumns($this->localColumns);
        $sql = parent::insert($this->associateData());
        return parent::Execute();
    }


    /**
     * anula todos atributos locais.
     *
     */
    public function cleanLocalVars()
    {
        $this->controlNumber        =
        $this->materialHistoryId    =
        $this->revisionNumber       =
        $this->operator             =
        $this->data                 =
        $this->chancesType          =
        $this->fieldId              =
        $this->subFieldId           =
        $this->previousLine         =
        $this->previousIndicator1   =
        $this->previousIndicator2   =
        $this->previousContent      =
        $this->currentLine          =
        $this->currentIndicator1    =
        $this->currentIndicator2    =
        $this->currentContent       = null;
    }

    function clean()
    {
        $this->cleanLocalVars();
    }

    /**
     * retorna o numero da proxima revisao
     *
     * @param integer $controlNumber
     * @return integer
     */
    function getNextRevision($controlNumber = null)
    {
        if(is_null($controlNumber) && !is_null($this->controlNumber))
        {
            $controlNumber = $this->controlNumber;
        }
        if(!$controlNumber)
        {
            return false;
        }

        parent::clear();
        $this->setTables();
        parent::setColumns("MAX(revisionnumber)");
        parent::setWhere("controlNumber = ?");
        $sql = parent::select(array($controlNumber));
        $r = parent::query();

        if(!$r)
        {
            return 1;
        }

        return ($r[0][0] + 1);
    }



    function checkHistory()
    {
        parent::clear();
        $this->setTables();
        parent::setColumns("revisionnumber");
        parent::setWhere($this->getWhereCondition());
        $sql = parent::select(array($this->controlNumber, $this->fieldId, $this->subFieldId));
        return parent::query($sql);
    }

} // final da classe
?>