<?php

/**
 *
 * Class Library Association Form
 *
 * @author Luiz Gilberto 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]
 * Sandro Roberto Weisheimer [sandrow@solis.coop.br]
 *
 * @since
 * Class created on 31/jul/08
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2007 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 FrmLibraryAssociation extends GnutecaForm
{
	public $MIOLO;
	public $module;
    public $businessAssociation;
    public $businessLibraryUnit;
    public $tables;


    public function __construct()
    {
    	$this->MIOLO   = MIOLO::getInstance();
    	$this->module  = MIOLO::getCurrentModule();
        $this->businessAssociation  = $this->MIOLO->getBusiness($this->module, 'BusAssociation');
        $this->businessLibraryUnit  = $this->MIOLO->getBusiness($this->module, 'BusLibraryUnit');
        $this->setAllFunctions('Association', null, array('associationId'), array('description'));
        parent::__construct();

        if  ( $this->primeiroAcessoAoForm() && ($this->function != 'update') )
        {
            $this->tables['libraryAssociation']->clearData();
            $this->setFocus('description');
        }
    }


    public function createFields()
    {
        $fields[] = new MTextField("description", null,  _M("Description", $this->module), FIELD_DESCRIPTION_SIZE);
        $validators[] = new MRequiredValidator("description", _M("Description", $this->module) );

        if ($this->function == 'update')
        {
            $fields[] = new MHiddenField("associationId", null, null, 0);
        }

        $label = new MLabel (_M('Library unit', $this->module));
        $label->setWidth(FIELD_LABEL_SIZE);
        $tableFields[] = $label;
        $selection     = new GnutecaSelection('libraryUnitId', null, null, $this->businessLibraryUnit->listLibraryUnit());

        $selection->addAttribute("onchange", 'document.getElementById(\'libraryUnitDescription\').value = document.getElementById(\'libraryUnitId\').options[document.getElementById(\'libraryUnitId\').selectedIndex].text');
        $tableFields[] = $selection;
        $tableFields[] = new MHiddenField('libraryUnitDescription', null, 'uhu', 10);
        $tableFields[] = new MSeparator();

        $this->tables['libraryAssociation'] = new GnutecaRepetitiveField('LibraryUnits', _M('Library', $this->module));
        $this->tables['libraryAssociation']->setFields($tableFields);

        $columns = array
        (
            new MGridColumn( _M('Code',         $this->module), 'right',    true, "10%", true, 'libraryUnitId'              ),
            new MGridColumn( _M('Description',  $this->module), 'left',     true, "74%", true, 'libraryUnitDescription'     ),
        );

        $valids[] = new GnutecaUniqueValidator('libraryUnitId');
        $this->tables['libraryAssociation']->setColumns($columns);
        $this->tables['libraryAssociation']->setValidators($valids);
        $fields[] = $this->tables['libraryAssociation'];

        $this->setFields( $fields );
        $this->setValidators($validators);
    }


    public function tbBtnSave_click($sender = null)
    {
        $data = $this->getData();
        $data->libraryAssociation = $this->tables['libraryAssociation']->getData();
        if (!count($data->libraryAssociation))
        {
            $this->error(_M("Libraries list is empty!", $this->module));
        }

        parent::tbBtnSave_click($sender, $data);
    }


    public function loadFields()
    {
        $this->business->getAssociation( MIOLO::_REQUEST('associationId') );
        $this->setData( $this->business );
        $this->tables['libraryAssociation']->setData( $this->business->libraryAssociation );
    }
}
?>
