<?php
/**
 *
 * Library Unit form
 *
 * @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 29/07/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 FrmLibraryUnit extends GnutecaForm
{
	public $MIOLO;
	public $module;
	public $tables;
    public $businessPrivilegeGroup;
    public $businessLibraryGroup;
    public $busWeekDay;


    public function __construct()
    {
    	$this->MIOLO   = MIOLO::getInstance();
    	$this->module  = MIOLO::getCurrentModule();
        $this->businessPrivilegeGroup = $this->MIOLO->getBusiness($this->module, 'BusPrivilegeGroup');
        $this->businessLibraryGroup   = $this->MIOLO->getBusiness($this->module, 'BusLibraryGroup');
        $this->busWeekDay             = $this->MIOLO->getBusiness($this->module, 'BusWeekDay');

        $this->setAllFunctions('LibraryUnit', null, array('libraryUnitId'), array('libraryName'));
        parent::__construct();

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


    public function createFields()
    {
        $this->tables['libraryUnitIsClosed'] = new GnutecaRepetitiveField('libraryUnitIsClosed', _M('Days library unit is closed', $this->module) );
        $this->tables['group'] = new GnutecaRepetitiveField('group', _M('Free access for the groups', $this->module), NULL, NULL, array('edit', 'remove'));

        if ( $this->function != 'insert' )
        {
            $libraryUnitId = new MTextField('libraryUnitId', '', _M('Code', $this->module), FIELD_ID_SIZE);
            $libraryUnitId->setReadOnly(true);
            $fields[] = $libraryUnitId;
        }

        $libraryName = new MTextField('libraryName', $this->libraryName->value, _M('Name',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[]     = $libraryName;
        $validators[] = new MRequiredValidator('libraryName');

        $lbl = new MLabel(_M('Is restricted', $this->module) . ':');
	    $lbl->setWidth(FIELD_LABEL_SIZE);
        $isRestricted = new MRadioButtonGroup('isRestricted', null, GnutecaUtils::listYesNo(1), 'f', null, MRadioButtonGroup::LAYOUT_HORIZONTAL);
        $fields[] = new MHContainer('hctIsRestricted', array($lbl, $isRestricted));

        $city = new MTextField('city', $this->city->value, _M('City',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[] = $city;

        $zipCode = new MTextField('zipCode', $this->zipCode->value, _M('Zip code',$this->module), FIELD_ID_SIZE);
        $fields[]     = $zipCode;
        $validators[] = new MCepValidator('zipCode');

        $location = new MTextField('location', $this->location->value, _M('Location',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[] = $location;

        $number = new MTextField('number', $this->number->value, _M('Number',$this->module), FIELD_ID_SIZE);
        $fields[] = $number;

        $complement = new MTextField('complement', $this->complement->value, _M('Complement',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[] = $complement;

        $email = new MTextField('email', $this->email->value, _M('E-mail',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[]     = $email;
        $validators[] = new MEmailValidator('email');

        $url = new MTextField('url', $this->url->value, _M('URL',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[] = $url;

        $privilegeOptions = $this->businessPrivilegeGroup->listPrivilegeGroup();
        $privilegeGroupId = new GnutecaSelection('privilegeGroupId', $this->privilegeGroupId->value, _M('Privilege group',$this->module), $privilegeOptions ,false, '','' , false );
        //$privilegeGroupId ->hideDefaultSelect();
        //$privilegeGroupId ->setOptions($privilegeOptions);
        $fields[] = $privilegeGroupId;
        $validators[] = new MRequiredValidator('privilegeGroupId');

        $libraryGroupId = new GnutecaSelection('libraryGroupId', $this->libraryGroupId->value, _M('Library group',$this->module), $this->businessLibraryGroup->listLibraryGroup());
        $fields[] = $libraryGroupId;

        $level = new MTextField('level', null, _M('Level', $this->module), FIELD_ID_SIZE);
        $fields[] = $level;
        $validators[] = new MIntegerValidator('level');

        $fields[] = new MMultiLineField('observation', null, _M('Observation', $this->module), NULL, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);

        //gtcLibraryUnitIsClosed
        $lblWeekDayId = new MLabel(_M('Week day',$this->module) . ':');
        $lblWeekDayId->setWidth(FIELD_LABEL_SIZE);
        $weekDayId = new GnutecaSelection('weekDayId', null, '' , $this->busWeekDay->listWeekDay());
        $fldLibraryUnitIsClosed[] = new MHContainer('hctWeekDay', array($lblWeekDayId, $weekDayId));
        $weekDayId->addAttribute("onchange", 'document.getElementById(\'weekDescription\').value = document.getElementById(\'weekDayId\').options[document.getElementById(\'weekDayId\').selectedIndex].text');

        $fldLibraryUnitIsClosed[] = new MHiddenField('weekDescription');

        $this->tables['libraryUnitIsClosed']->setFields($fldLibraryUnitIsClosed);
        //$tableClosedValidators[] = new MRequiredValidator('weekDayId');
        $tableClosedValidators[] = new MRequiredValidator('weekDescription');
        $tableClosedValidators[] = new MIntegerValidator('weekDayId','','required', _M('Por favor selecione corretamente o dia da semana.', $this->module) );
        $tableClosedValidators[] = new GnutecaUniqueValidator('weekDayId');
        $this->tables['libraryUnitIsClosed']->setValidators( $tableClosedValidators );

        $columns[] = new MGridColumn( _M('Week day code', $this->module), 'left', true, '', false, 'weekDayId');
        $columns[] = new MGridColumn( _M('Week day', $this->module),      'left', true, '', true, 'weekDescription');

        $valids[] = new GnutecaUniqueValidator('weekDayId', _M('Week day', $this->module), 'required');
        $this->tables['libraryUnitIsClosed']->setColumns($columns);
        $this->tables['libraryUnitIsClosed']->setValidators($valids);
        $fields[] = new MSeparator('<br/>');
        $fields[] = $this->tables['libraryUnitIsClosed'];

        //Group
        $linkIdLabel = new MLabel(_M('Link code', $this->module) . ':');
        $linkIdLabel->setWidth(FIELD_LABEL_SIZE);

        //$linkId = new MLookupTextField('linkId', '', '', FIELD_LOOKUPFIELD_SIZE, NULL, NULL, 'linkId, description', $this->module, 'Link');

        $linkId = new MLookupTextField('linkId', '', '', FIELD_LOOKUPFIELD_SIZE);
        $linkId->baseModule = $this->module;
        $linkId->setContext($this->module, $this->module, 'Link', 'filler', 'linkId,description');

        $linkIdDescription = new MTextField('description', $this->description, NULL, FIELD_DESCRIPTION_LOOKUP_SIZE);

        $linkIdDescription->setReadOnly(true);
        $fldGroup[] = new MHcontainer('hctLink', array($linkIdLabel, $linkId, $linkIdDescription));

        $this->tables['group']->setFields( $fldGroup );
        $fields[] = new MSeparator('<br/>');
        $fields[] = $this->tables['group'];

        unset($columns, $valids);
        $columns[] = new MGridColumn( _M('Code',        $this->module), 'left', true, null, true, 'linkId' );
        $columns[] = new MGridColumn( _M('Description', $this->module), 'left', true, null, true, 'description' );
        $valids[]  = new GnutecaUniqueValidator('linkId', _M('Link code', $this->module));
        $valids[]  = new MIntegerValidator('linkId', _M('Link code', $this->module), 'required');
        $this->tables['group']->setValidators($valids);
        $this->tables['group']->setColumns($columns);

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


    public function tbBtnSave_click($sender=NULL)
    {
        $data = $this->getData();
        $data->libraryUnitIsClosed = $this->tables['libraryUnitIsClosed']->getData();
        $data->group               = $this->tables['group']->getData();
        parent::tbBtnSave_click($sender, $data);
    }


    public function loadFields()
    {
	    $this->business->getLibraryUnit1( MIOLO::_REQUEST('libraryUnitId') );
	    $this->setData( $this->business );
	    $this->tables['libraryUnitIsClosed']->setData($this->business->libraryUnitIsClosed);
        $this->tables['group']->setData($this->business->group);
    }
}
?>
