<?php
/**
 * This file handles the connection and actions for gtcWeekDay table
 *
 * @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 28/11/2008
 *
 * \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 to manipulate the basConfig table
 **/
class BusinessGnuteca3BusSearchFormatAccess extends GnutecaBusiness
{
    public $searchFormatId;
    public $linkId;
    public $links; //list of links separated by comma used in search

    public $searchFormatIdS;
    public $linkIdS;

    
    /**
     * Class constructor
     **/
    public function __construct()
    {
        $table = 'gtcSearchFormatAccess';
        $pkeys = 'searchFormatId,
                  linkId';
        parent::__construct($table, $pkeys);
    }


    /**
     * Insert a new record
     *
     * @return TRUE if succed, otherwise FALSE
     **/
    public function insertSearchFormatAccess()
    {
        if ($this->removeData)
        {
            $this->deleteSearchFormatAccess($this->searchFormatId, $this->linkId);
            return false;
        }
        if ($this->getSearchFormatAccess($this->searchFormatId, $this->linkId))
        {
            return true;
        }
        return $this->autoInsert();
    }


    /**
     * Update data from a specific record
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     **/
    public function updateSearchFormatAccess()
    {
        return $this->autoUpdate();
    }


    /**
     * Delete a record
     *
     * @param $formatBackOfBookId (integer)
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     *
     **/
    public function deleteSearchFormatAccess($searchFormatId, $linkId)
    {
        return $this->autoDelete($searchFormatId, $linkId);
    }


    /**
     * Return a specific record from the database
     *
     * @param $formatBackOfBookId (integer)
     *
     * @return (Object): Return an object of the type handled by the class
     **/
    public function getSearchFormatAccess($searchFormatId, $linkId)
    {
        return $this->autoGet($searchFormatId, $linkId);
    }


    /**
     * Do a search on the database table handled by the class
     *
     * @param $object (bool): Case TRUE return as Object, otherwise Array
     *
     * @return (Array): An array containing the search results
     **/
    public function searchSearchFormatAccess($object = false)
    {
        $this->clear();
        if ($this->searchFormatId)
        {
        	$this->setWhere('A.searchFormatId = ?');
        	$args[] = $this->searchFormatId;
        }
        if ($this->linkId)
        {
        	$this->setWhere('A.linkId = ?');
        	$args[] = $this->linkId;
        }

        if ($this->links)
        {
            $this->setWhere('A.linkId in ('.$this->links.')');
        	//$args[] = $this->links;
        }
        
        //TODO: Fix bug on GnutecaBusiness
        $tables  = $this->tables;
        $columns = $this->columns;
        
        $this->setTables('gtcSearchFormatAccess A
              LEFT JOIN   basLink               B
                     ON   (A.linkId = B.linkId)
              LEFT JOIN   gtcSearchFormat C
                     ON (A.searchFormatId = C.searchFormatId) ');
        $this->setColumns('A.searchFormatId,
                           A.linkId,
                           B.description AS linkIdDescription,
                           C.description');
        $sql = $this->select($args);
        $rs  = $this->query($sql, $object);
        
        //TODO: Fix bug on GnutecaBusiness
        $this->setTables($tables);
        $this->setColumns($columns);
        
        return $rs;
    }


    /**
     * List all records from the table handled by the class
     *
     * @param None
     *
     * @return (Array): Return an array with the entire table
     *
     **/
    public function listSearchFormatAccess()
    {
        return $this->autoList();
    }
    
    
    public function deleteBySearchFormat($searchFormatId)
    {
    	$sql = new MSQL(null, $this->tables, 'searchFormatId = ?');
    	return $this->execute( $sql->delete($searchFormatId) );
    }
}
?>
