<?php
/**
 * Group Right 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 01/08/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 FrmGroupRight extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $businessPrivilegeGroup;
    public $businessMaterialGender;
    public $businessOperation;
    public $businessBond;


    public function __construct()
    {
        $this->setAllFunctions('Right', null, array('privilegeGroupId', 'linkId', 'materialGenderId', 'operationId'), array('privilegeGroupId'));

        $this->MIOLO                  = MIOLO::getInstance();
        $this->module                 = MIOLO::getCurrentModule();
        $this->businessPrivilegeGroup = $this->MIOLO->getBusiness($this->module, 'BusPrivilegeGroup');
        $this->businessBond           = $this->MIOLO->getBusiness($this->module, 'BusBond');
        $this->businessMaterialGender   = $this->MIOLO->getBusiness($this->module, 'BusMaterialGender');
        $this->businessOperation      = $this->MIOLO->getBusiness($this->module, 'BusOperation');

        parent::__construct();
        $this->setFocus('privilegeGroupId');
    }


    public function createFields()
    {
        $privilegeGroupId = new GnutecaSelection('privilegeGroupId', $this->privilegeGroupId->value, _M('Privilege group',$this->module), $this->businessPrivilegeGroup->listPrivilegeGroup());
        $fields[] = $privilegeGroupId;
        $validators[] = new MRequiredValidator('privilegeGroupId');

        $linkList = new MMultiSelection('linkList', array(null), _M('Link',$this->module), $this->businessBond->listBond(), null, null, 5);
        $fields[] = $linkList;
        $validators[] = new MRequiredValidator('linkList');

        $materialGenderList = new MMultiSelection('materialGenderList', array(null), _M('Material gender', $this->module), $this->businessMaterialGender->listMaterialGender(), null, null, 5);
        $fields[] = $materialGenderList;
        $validators[] = new MRequiredValidator('materialGenderList');

        $operationList = new MMultiSelection('operationList', array(null), _M('Operation type', $this->module), $this->businessOperation->listOperation(), null, null, 5);
        $fields[] = $operationList;
        $validators[] = new MRequiredValidator('operationList');

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


    public function tbBtnSave_click($sender=NULL)
    {
        $data = $this->getData();
        if ($this->function == 'insert')
        {
            foreach ($data->linkList as $dataInsert->linkId)
            {
                foreach ($data->materialGenderList as $dataInsert->materialGenderId)
                {
                    foreach ($data->operationList as $dataInsert->operationId)
                    {
                        $dataInsert->privilegeGroupId = $data->privilegeGroupId;
                        $this->business->setData($dataInsert);
                        $this->business->insertRight();
                    }
                }
            }
            //verifica se algum dos campos está em branco
            if (!$dataInsert->linkId || !$dataInsert->materialGenderId || !$dataInsert->operationId)
            {
            	$errors[] = _M('Field is null');
            }
        }
        parent::tbBtnSave_click($sender, $data, $errors);
    }
}
?>
