<?php
/**
 * News form
 *
 * @author Sandro Roberto Weisheimer [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 20/03/2009
 *
 * \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 FrmNews 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('News', 'newsId', array('newsId', 'place', 'title', 'news', 'date'), array('newsId'));
        parent::__construct();

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



    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' )
        {
            $newsId = new MTextField('newsId', null, _M('Code', $this->module), FIELD_ID_SIZE);
            $newsId->setReadOnly(TRUE);
            $fields[] = $newsId;
        }

        $place          = new GnutecaSelection('place', $this->place->value, _M('Place', $this->module), $this->business->listPlace(), null, null, null, TRUE);
        $fields[]       = $place;

        $title = new MTextField('title1', $this->titleS->value, _M('Title',$this->module), FIELD_DESCRIPTION_SIZE);
        $fields[]    = $title;

        $news           = new MMultiLIneField('news', $this->news->value, _M('News', $this->module), NULL, 5, FIELD_MULTILINE_COLS_SIZE);
        //$validators[]   = new MRequiredValidator('news');
        $fields[]       = $news;

        $fields[]       = new MCalendarField('date', GnutecaDate::now(GnutecaDate::getDbMask('DATE')), _M('Date', $this->module), FIELD_DATE_SIZE );
        $validators[] = new MRequiredValidator('date');

        $beginDate = new MCalendarField('beginDate', $this->beginDate->value, _M('Begin date', $this->module), FIELD_DATE_SIZE, null);
        $validators[] = new MDateDMYValidator('beginDate');
        $fields[] = $beginDate;

        $endDate = new MCalendarField('endDate', $this->endDate->value, _M('End date', $this->module), FIELD_DATE_SIZE, null);
        $validators[] = new MDateDMYValidator('endDate');
        $fields[] = $endDate;

        $operator = new MTextField('operator', GnutecaOperator::getOperatorId(), _M('Operator', $this->module));
        $operator->setReadOnly(true);
        $validators[] = new MRequiredValidator('operator');
        $fields[] = $operator;

        $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));

        $lbla = new MLabel(_M('Is active', $this->module) . ':');
        $lbla->setWidth(FIELD_LABEL_SIZE);
        $isActive = new MRadioButtonGroup('isActive', null, GnutecaUtils::listYesNo(1), 'f', null, MRadioButtonGroup::LAYOUT_HORIZONTAL);
        $fields[] = new MHContainer('hctIsActive', array($lbla, $isActive));

        //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 loadFields()
    {
        $data = $this->business->getNews( MIOLO::_REQUEST('newsId') );
        $data->date = GnutecaDate::explodeDate($data->date);
        $data->date = $data->date[0];
        $data->beginDate = GnutecaDate::explodeDate($data->beginDate);
        $data->beginDate = $data->beginDate[0];
        $data->endDate   = GnutecaDate::explodeDate($data->endDate);
        $data->endDate   = $data->endDate[0];
        $this->setData($data);
        $this->tables['group']->setData($this->business->group);
    }

    public function tbBtnSave_click($sender=NULL)
    {
        $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);
    }
}
?>
