<?php
/**
 * This file handles the connection and actions for gtcSpreadsheet 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]
 * Sandro Roberto Weisheimer [sandrow@solis.coop.br]
 *
 * @since
 * Class created on 26/09/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 BusinessGnuteca3BusSpreadsheet extends GnutecaBusiness
{
    public $pkeys;
    public $pkeysWhere;
    public $cols;
    public $fullColumns;

    public $category;
    public $level;
    public $field;
    public $required;
    public $repeatFieldRequired;
    public $defaultValue;
    public $menuName;
    public $menuOption;
    public $menuLevel;

    public $categoryS;
    public $levelS;
    public $fieldS;
    public $requiredS;
    public $repeatFieldRequiredS;
    public $defaultValueS;
    public $menuNameS;
    public $menuOptionS;
    public $menuLevelS;


    /**
     * Class constructor
     **/
    function __construct()
    {
        parent::__construct();
        $this->tables  = 'gtcSpreadsheet';
        $this->pkeys   = 'category,
                          level';
        $this->cols    = 'field,
                          required,
                          repeatFieldRequired,
                          defaultValue,
                          menuName,
            			  menuOption,
            			  menuLevel';

        $this->fullColumns = $this->pkeys . ',' . $this->cols;
        $this->pkeysWhere  = 'category = ? AND level = ?';
    }


    /**
     * Insert a new record
     *
     * @return TRUE if succed, otherwise FALSE
     **/
    public function insertSpreadsheet()
    {
        $this->clear();
        $this->setColumns($this->fullColumns);
        $this->setTables($this->tables);
        $sql = $this->insert( $this->associateData( $this->fullColumns ) );
        $rs  = $this->execute($sql);
        return $rs;
    }


    /**
     * Update data from a specific record
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     **/
    public function updateSpreadsheet()
    {
        $this->clear();
        $this->setColumns($this->cols);
        $this->setTables($this->tables);
        $this->setWhere($this->pkeysWhere);
        $sql = $this->update( $this->associateData( $this->cols . ',' . $this->pkeys ) );
        $rs  = $this->execute($sql);
        return $rs;
    }


    /**
     * Delete a record
     *
     * @param $category (String)
     * @param $level (String)
     *
     * @return (boolean): TRUE if succeed, otherwise FALSE
     *
     **/
    public function deleteSpreadsheet($category, $level)
    {
        $this->clear();
        $this->setTables($this->tables);
        $this->setWhere($this->pkeysWhere);
        $sql = $this->delete( array($category, $level) );
        $rs  = $this->execute($sql);
        return $rs;
    }


    /**
     * Return a specific record from the database
     *
     * @param $category (String)
     * @param $level (String)
     *
     * @return (Object): Return an object of the type handled by the class
     **/
    public function getSpreadsheet($category, $level)
    {
        $this->clear();
        $this->setTables($this->tables);
        $this->setColumns($this->fullColumns);
        $this->setWhere("category = '$category' AND level = '$level'");
        $sql = $this->select();
        $rs  = $this->query($sql, TRUE);
        if ($rs)
        {
        	$this->setData($rs[0]);
            return $rs[0];
        }
        else
        {
            return false;
        }
    }


    /**
     * Do a search on the database table handled by the class
     *
     * @return (Array): An array containing the search results
     **/
    public function searchSpreadsheet($toObject = FALSE)
    {
        $this->clear();

        if ($this->categoryS)
        {
            $this->setWhere('category = ?');
            $data[] = $this->categoryS;
        }
        if ($this->levelS)
        {
            $this->setWhere('level = ?');
            $data[] = $this->levelS;
        }
        if ($this->fieldS)
        {
            $this->setWhere('lower(field) LIKE lower(?)');
            $data[] = '%' . $this->fieldS . '%';
        }
        if ($this->requiredS)
        {
            $this->setWhere('lower(required) LIKE lower(?)');
            $data[] = '%' . $this->requiredS . '%';
        }
        if ($this->repeatFieldRequiredS)
        {
            $this->setWhere('lower(repeatFieldRequired) LIKE lower(?)');
            $data[] = '%' . $this->repeatFieldRequiredS . '%';
        }
        if ($this->defaultValueS)
        {
            $this->setWhere('lower(defaultValue) LIKE lower(?)');
            $data[] = '%' . $this->defaultValueS . '%';
        }
        if ($this->menuNameS)
        {
            $this->setWhere('lower(menuName) LIKE lower(?)');
            $data[] = '%' . $this->menuNameS . '%';
        }
        if ($this->menuItemS)
        {
        	$this->setWhere('menuItem = ?');
        	$data[] = $this->menuItemS;
        }

        $this->setTables($this->tables);
        $this->setColumns($this->fullColumns);
        $sql = $this->select($data);
        $rs  = $this->query($sql, $toObject ? TRUE : FALSE);
        return $rs;
    }


    /**
     * List all records from the table handled by the class
     *
     * @return (Array): Return an array with the entire table
     *
     **/
    public function listSpreadsheet()
    {
        $this->clear();
        $this->setColumns($this->fullColumns);
        $this->setTables($this->tables);
        $sql = $this->select();
        $rs  = $this->query($sql);
        return $rs;
    }


    public function getMenus( $category = false , $level = false , $onlyWithMenuName = true )
    {
        $this->clear();
        $this->setColumns("menuname, menuoption, menulevel, category, level");
        $this->setTables($this->tables);
        
        if ( $onlyWithMenuName )
        {
            $this->setWhere(" menuname is not null AND menuoption is not null AND menuname != '' AND menuoption != '' ");
        }

        if ($category)
        {
        	$this->setWhere('category = ?');
        	$args[] = $category;
        }

        if ($level)
        {
            $this->setWhere('level = ?');
            $args[] = $level;
        }

        $this->setOrderBy("menulevel");
        $sql = $this->select( $args );
        $rs  = $this->query($sql, true);
        return $rs;
    }

}
?>
