<?php
/**
 * DictionaryContent 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 03/12/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 FrmDictionaryContent extends GnutecaForm
{
	public $MIOLO;
	public $module;
	public $busDictionary;
    public $_dictionaryRelatedContent;
    public $busDictionaryRelatedContent;


    public function __construct()
    {
    	$this->MIOLO  = MIOLO::getInstance();
    	$this->module = MIOLO::getCurrentModule();
    	$this->busDictionary = $this->MIOLO->getBusiness($this->module, 'BusDictionary');
        $this->busDictionaryRelatedContent = $this->MIOLO->getBusiness($this->module, 'BusDictionaryRelatedContent');

        $this->function = MIOLO::_REQUEST('function');
        if ( ($this->function == 'search') || ($this->function == '') )
        {
            $this->setAllFunctions('DictionaryContent', array('dictionaryId'));
        }
        else
        {
            $this->setAllFunctions('DictionaryContent', array('dictionaryId'), 'dictionaryContentId', array('dictionaryId'));
        }
        parent::__construct();

        if ( $this->function == 'insert' && $this->getEvent() == 'tbBtnNew:click' )
        {
            $this->_dictionaryRelatedContent->clearData();
        }
    }


    public function createFields()
    {        
        
    	$tag = MIOLO::_REQUEST('openerRelated');
    	$tag = str_replace('_', '.',substr($tag, -5));
    	$dictionaryId = '';
    	if ( strlen($tag) > 0 )
    	{
	    	$dictionary = $this->busDictionary->checkExistsDictionaryForTag($tag);
	    	$dictionaryId = $dictionary->dictionaryId;
	    	//$this->page->onload("dojo.byId('content2').focus();");
	    	//$this->jsSetFocus('content2', false);
	    	$this->setFocus('dictionaryContent');
	    	
    	}
    	
        if ( ( $this->function == 'search' ) || ( $this->function == '' ) )
        {
            $dictionaryContentIdS = new MTextField('dictionaryContentIdS', null, _M('Code', $this->module), FIELD_ID_SIZE);
            $fields[] = $dictionaryContentIdS;
            $this->setFocus('dictionaryContentIdS');
        }

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

        $fields[] = new GnutecaSelection('dictionaryId', $dictionaryId, _M('Dictionary', $this->module), $this->busDictionary->listDictionary());
        $fields[] = new MTextField('dictionaryContent', null, _M('Content', $this->module), FIELD_DESCRIPTION_SIZE);

        if ( ($this->function == 'insert') || ($this->function == 'update') )
        {
            if  ( ($this->primeiroAcessoAoForm()) && (strlen($tag) == 0) )
            {
                $this->setFocus('dictionaryId');
            }
            $validators[] = new MRequiredValidator('dictionaryContent');
            $validators[] = new MRequiredValidator('dictionaryId');

	        $flds[] = new MTextField('relatedContent', null, _M('Related content', $this->module), FIELD_DESCRIPTION_SIZE);
	        $dictionaryRelatedContent = new MTextField('dictionaryRelatedContentId');
            $dictionaryRelatedContent->setVisibility(FALSE);
            $flds[] = $dictionaryRelatedContent;
	        $columns[] = new MGridColumn(_M('Related content', $this->module),                 MGrid::ALIGN_LEFT, true, null, true,  'relatedContent');
	        $columns[] = new MGridColumn(_M('Dictionary related content code', $this->module), MGrid::ALIGN_LEFT, true, null, false, 'dictionaryRelatedContentId');
	        $valids[] = new MRequiredValidator('relatedContent');
	        $this->_dictionaryRelatedContent = new GnutecaRepetitiveField('dictionaryRelatedContent', _M('Related content', $this->module), $columns, $flds);
	        $this->_dictionaryRelatedContent->setValidators($valids);
	        $fields[] = $this->_dictionaryRelatedContent;
        }
        $fields[] = new MHiddenField('relatedEdit');
        $this->setFields($fields);
        if ($validators)
        {
            $this->setValidators($validators);
        }
    }


    public function tbBtnSave_click($sender = NULL)
    {
        $data = $this->getData();
        $data->dictionaryRelatedContent = $this->_dictionaryRelatedContent->getData();

        //Tratamento especifico para quando formulario veio da lookup, na Catalogacao
        $errors = $this->gValidator->validate( $data );
        if (MIOLO::_REQUEST('from') == 'lookup' && !$errors)
        {
            $this->business->setData( $data );
            $insert             = call_user_func(array($this->business, $this->insertFunction)); //true/false
            $codigo             = $this->business->dictionaryContentId;
            $dictionaryContent  = $this->business->getDictionaryContent( $codigo );
            $this->page->onload("
                id = '".MIOLO::_REQUEST('openerRelated')."';
                opener.dojo.byId(id).value = '{$dictionaryContent->dictionaryContent}';
                self.close();
            ");
            $this->setResponse( null, 'divResponse');
            return;
        }

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


    public function loadFields()
    {
        $this->business->getDictionaryContent( MIOLO::_REQUEST('dictionaryContentId') );
        $this->setData( $this->business );
        $this->_dictionaryRelatedContent->setData($this->business->dictionaryRelatedContent);
    }

    public function showRelatedContent()
    {
        $dictionaryContentId = MIOLO::_REQUEST('dictionaryContentId');
        $this->busDictionaryRelatedContent->dictionaryContentId = $dictionaryContentId;
        $search = $this->busDictionaryRelatedContent->searchDictionaryRelatedContent(TRUE);
        if ($search)
        {
            //Cria o array com os dados
            $tbData = array();
            foreach ($search as $v)
            {
                $tbData[] = array(
                    $v->relatedContent
                );
            }

            //Cria as colunas
            $tbColumns = array(
                _M('Related content', $this->module)
            );

	        $tb = new MTableRaw('', $tbData, $tbColumns);
	        $tb->zebra = TRUE;
	        $fields[] = $this->getDefaultDetailDiv($tb);
        }
        else
        {
            $fields[] = new MLabel(_M('No record found.', $this->module));
        }
        $this->setTitle( _M('Related content', $this->module) );
        $this->setFields($fields, FALSE);
        parent::_showDetail($this);
    }


    public function forceAddToTable($args)
    {
        //Verifica se foi uma edicao
        if (MUtil::getBooleanValue($args->relatedEdit))
        {
            $args->dictionaryRelatedContentId = null;
        }
        parent::forceAddToTable($args);
        $this->page->onLoad("document.getElementById('relatedEdit').value = 'f'");
    }


    public function editFromTable($args)
    {
        parent::editFromTable($args);
        $this->page->onLoad("document.getElementById('relatedEdit').value = 't'");
    }
}
?>
