<?php
/**
 * SearchFormat 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]
 * Sandro Roberto Weisheimer [sandrow@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
 */


/**
 * Form to manipulate a preference
 **/
class FrmSearchableField extends GnutecaForm
{
	public $MIOLO;
	public $module;
	public $tables;
   // public $businessPrivilegeGroup;
    public $businessGroupAccess;
    public $businessLibraryGroup;
    public $businessUserGroup;

    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->businessUserGroup      = $this->MIOLO->getBusiness($this->module, 'BusUserGroup');
        $this->setAllFunctions('SearchableField', null, array('searchableFieldId'), array('description', 'field', 'identifier', 'isRestricted', 'level'));
        parent::__construct();
        if  ( $this->primeiroAcessoAoForm() && ($this->function != 'update') && strlen(MIOLO::_REQUEST('event')) == 0 )
        {
            $this->tables['group']->clearData();
            $this->setFocus('description');
        }
    }



    public function createFields()
    {
        $this->tables['group'] = new GnutecaRepetitiveField('group', _M('Access for the groups', $this->module), NULL, NULL, array('edit', 'remove'));

        if ( $this->function == 'update' )
        {
            $searchableFieldId = new MTextField('searchableFieldId', null, _M('Code', $this->module), FIELD_ID_SIZE);
            $searchableFieldId->setReadOnly(TRUE);
            $fields[] = $searchableFieldId;
            $validators[] = new MRequiredValidator('searchFormatId');
        }

        $fields[] = new MTextField('description', null, _M('Description', $this->module), FIELD_DESCRIPTION_SIZE);
        $validators[] = new MRequiredValidator('description');
        $fields[] = new MTextField('field', null, _M('Field', $this->module), FIELD_DESCRIPTION_SIZE);
        $validators[] = new MRequiredValidator('field');
        $fields[] = new MTextField('identifier', null, _M('Identifier', $this->module), FIELD_DESCRIPTION_SIZE);
        $validators[] = new MRequiredValidator('identifier');
        $fields[] = new MMultiLineField('observation', null, _M('Observation', $this->module), null, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);

        $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));
        //$validators[] = new MRequiredValidator('isRestricted');
        $fields[] = new MTextField('level', $this->level->value, _M('Level',$this->module), FIELD_ID_SIZE);
        $validators[] = new MIntegerValidator('level', null, 'required');
        $fields[]       = new GnutecaSelection('fieldType', $this->fieldType->value, _M('Field type', $this->module), $this->business->listFieldType(), null, null, null, TRUE);
        $fields[] = new MTextField('helps', $this->help->value, _M('help',$this->module), FIELD_DESCRIPTION_SIZE);

        //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,description1');

        $linkIdDescription = new MTextField('description1', $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'];

        $columns   = null;
        $valids    = null;
        $columns[] = new MGridColumn( _M('Code',        $this->module), 'left', true, null, true, 'linkId' );
        $columns[] = new MGridColumn( _M('Description', $this->module), 'left', true, null, true, 'description1' );
        //$valids[]  = new GnutecaUniqueValidator('linkId', _M('Link code', $this->module));
        //$valids[] = new MRequiredValidator('description1');
        $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 loadFields()
    {
        $this->business->getSearchableField( MIOLO::_REQUEST('searchableFieldId') );
        $this->setData($this->business);
        foreach ($this->business->group as $key=>$grupo)
        {
        	$this->business->group[$key]->description1 = $grupo->description;
        }
        $this->tables['group']->setData($this->business->group);
    }

    public function tbBtnSave_click($sender)
    {
        $data = $this->getData();
        $data->group               = $this->tables['group']->getData();
        if ($data->group)
        {
            foreach ($data->group as $g)
            {
                $linkId = $this->businessUserGroup->getUserGroup($g->linkId)->linkId;
            	if (!$g->linkId || $g->linkId != $linkId)
            	{
            		$errors[] = _M('No group for @1.', $this->module, $g->linkId);
            	}
            }
        }
    	parent::tbBtnSave_click($sender, $data, $errors);
    }

}
?>
