<?php
/**
 *
 * OperatorLibraryUnit business
 *
 * @author Moises Heberle [moises@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 06/01/2009
 *
 * \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
 */
class BusinessGnuteca3BusOperatorLibraryUnit extends GnutecaBusiness
{
    public $operator;
    public $libraryUnitId;
    public $operatorLibrary;
    
    public $operatorS;
    public $libraryUnitIdS;
    

    public function __construct()
    {
        parent::__construct('gtcOperatorLibraryUnit', 'operator', 'libraryUnitId');
    }


    public function getOperatorLibraryUnit($operator)
    {
        $this->clear();
        $this->setTables($this->tables);
        $this->setColumns($this->id);
        $this->setWhere('operator = ?');
        $rs = $this->query($this->select(array($operator)), true);
        $data = $rs[0];
        
        $this->clear();
        $this->setTables('gtcOperatorLibraryUnit A
                LEFT JOIN gtcLibraryUnit         B
                       ON (A.libraryUnitId = B.libraryUnitId)');
        $this->setColumns('A.operator,
                           A.libraryUnitId,
                           B.libraryName');
        $this->setWhere('A.operator = ?');
        $sql = $this->select(array($operator));
        $data->operatorLibrary = $this->query($sql, true);
        
        $this->setData($data);
        return $data;
    }


    public function searchOperatorLibraryUnit()
    {
        $sql = new MSQL('DISTINCT operator', $this->tables);
        if ($this->operatorS)
        {
        	$sql->setWhere('lower(operator) LIKE lower(?)');
        	$args[] = '%' . $this->operatorS . '%';
        }
        if ($this->libraryUnitIdS)
        {
        	$sql->setWhere('libraryUnitId = ?');
        	$args[] = $this->libraryUnitIdS;
        }
        $rs = $this->query( $sql->select((array)$args) );
        if ($rs)
        {
        	foreach ($rs as $val)
        	{
        		$operator = $val[0];
        		$tables = 'gtcOperatorLibraryUnit  A
                 LEFT JOIN gtcLibraryUnit          B
                        ON (A.libraryUnitId = B.libraryUnitId)';
        		$sql = new MSQL('B.libraryName', $tables, 'A.operator = ?');
        		$query = $this->query( $sql->select(array($operator)) );
        		$libraries = array();
        		if ($query)
        		{
        			foreach ($query as $val)
        			{
        				$libraries[] = $val[0];
        			}
        		}
        		$data[] = array($operator, implode("<br/>", $libraries));
        	}
        }
        return (array)$data;
    }


    public function insertOperatorLibraryUnit()
    {
    	$this->clear();
    	$this->setTables($this->tables);
    	$this->setColumns($this->columns);
    	foreach ((array)$this->operatorLibrary as $data)
    	{
    		if ($data->removeData) continue;
    		$sql = $this->insert(array($data->operator, $data->libraryUnitId));
    		$ok  = $this->execute($sql);
    	}
    	return ($ok);
    }


    public function updateOperatorLibraryUnit()
    {
        $this->deleteOperatorLibraryUnit($this->operator);
        return $this->insertOperatorLibraryUnit();
    }


    public function deleteOperatorLibraryUnit($operator)
    {
        return $this->autoDelete($operator);
    }
}
?>
