<?php
/**
 *
 * Tag 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 R. Weisheimer [sandrow@solis.coop.br] 
 *
 * @since
 * Class created on 25/09/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 FrmTag extends GnutecaForm
{
    public $tables;


    function __construct()
    {
        $this->setAllFunctions('Tag', null, array('fieldId', MARC_SPACE), array('fieldId'));
        parent::__construct();

        if (($this->function == 'insert') && ($this->getEvent() == 'tbBtnNew:click'))
        {
            $this->tables['tag']->clearData();
           	$this->setFocus('fieldId');
        }
    }


    public function createFields()
    {   
        $this->tables['tag'] = new GnutecaRepetitiveField('tag', _M('Tags', $this->module), NULL, NULL, array('edit', 'remove'));

        $fieldId = new MTextField('fieldId', $this->fieldId->value, _M('Field code', $this->module), 3);
        $fields[] = $fieldId;
        $validators[] = new MRequiredValidator('fieldId', null, 3);

        $fields[] = new MHiddenField('subfieldId', MARC_SPACE);

        if ($this->function == 'update')
        {
            $fieldId->setReadOnly(TRUE);
        }

        $fields[] = new MTextField('description', $this->description->value, _M('Description', $this->module), FIELD_DESCRIPTION_SIZE);
        $validators[] = new MRequiredValidator('description', null, 100);

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


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

        $lbl = new MLabel(_M('Has subfield', $this->module) . ':');
        $lbl->setWidth(FIELD_LABEL_SIZE);
        $hasSubfield = new MRadioButtonGroup('hasSubfield', null, GnutecaUtils::listYesNo(1), 'f', null, MRadioButtonGroup::LAYOUT_HORIZONTAL);
        $fields[] = new MHContainer('hctHasSubfield', array($lbl, $hasSubfield));

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

        $lbl = new MLabel(_M('In demonstration', $this->module) . ':');
        $lbl->setWidth(FIELD_LABEL_SIZE);
        $inDemonstration = new MRadioButtonGroup('inDemonstration', null, GnutecaUtils::listYesNo(1), 'f', null, MRadioButtonGroup::LAYOUT_HORIZONTAL);
        $fields[] = new MHContainer('hctInDemonstration', array($lbl, $inDemonstration));

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


        $fields[] = new MMultiLIneField('helpX', null,  _M('Help', $this->module), null, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);

        //Tag
        $lblSubfieldId = new MLabel(_M('Subfield', $this->module) . ':');
        $lblSubfieldId->setWidth(FIELD_LABEL_SIZE);
        $subfieldId = new MTextField('TAG_subfieldId', $this->TAG_subfieldId->value, null, 1);
        $fldTag[] = new MHContainer('hctSubfieldTAG', array($lblSubfieldId, $subfieldId));

        $lblDescription = new MLabel(_M('Description', $this->module) . ':');
        $lblDescription->setWidth(FIELD_LABEL_SIZE);
        $description = new MTextField('TAG_description', $this->TAG_description->value, null, FIELD_DESCRIPTION_SIZE);
        $fldTag[] = new MHContainer('hctDescriptionTAG', array($lblDescription, $description));

        $lblObservation = new MLabel(_M('Observation', $this->module) . ':');
        $lblObservation->setWidth(FIELD_LABEL_SIZE);
        $observation = new MMultiLIneField('TAG_observation', $this->TAG_observation->value,  '', null, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);
        $fldTag[] = new MHContainer('hctObservationTAG', array($lblObservation, $observation));

        $lblIsRepetitive = new MLabel(_M('Is repetitive', $this->module) . ':');
        $lblIsRepetitive->setWidth(FIELD_LABEL_SIZE);
        $isRepetitive = new GnutecaSelection('TAG_isRepetitive', $this->TAG_isRepetitive->value, '', GnutecaUtils::listYesNo());
        $fldTag[] = new MHContainer('hctIsRepetitiveTAG', array($lblIsRepetitive, $isRepetitive));

        $lblHasSubfield = new MLabel(_M('Has subfield', $this->module) . ':');
        $lblHasSubfield->setWidth(FIELD_LABEL_SIZE);
        $hasSubfield = new GnutecaSelection('TAG_hasSubfield', $this->TAG_hasSubfield->value, '', GnutecaUtils::listYesNo());
        $fldTag[] = new MHContainer('hctHasSubfieldTAG', array($lblHasSubfield, $hasSubfield));

        $lblIsActive = new MLabel(_M('Is active', $this->module));
        $lblIsActive->setWidth(FIELD_LABEL_SIZE);
        $isActive = new GnutecaSelection('TAG_isActive', $this->TAG_isActive->value, '', GnutecaUtils::listYesNo());
        $fldTag[] = new MHContainer('hctIsActiveTAG', array($lblIsActive, $isActive));

        $lblInDemonstration = new MLabel(_M('In demonstration', $this->module) . ':');
        $lblInDemonstration->setWidth(FIELD_LABEL_SIZE);
        $inDemonstration = new GnutecaSelection('TAG_inDemonstration', $this->TAG_inDemonstration->value, '', GnutecaUtils::listYesNo());
        $fldTag[] = new MHContainer('hctInDemonstrationTAG', array($lblInDemonstration, $inDemonstration));

        $lblIsObsolete = new MLabel(_M('Is obsolete', $this->module) . ':');
        $lblIsObsolete->setWidth(FIELD_LABEL_SIZE);
        $isObsolete = new GnutecaSelection('TAG_isObsolete', $this->TAG_isObsolete->value, '', GnutecaUtils::listYesNo());
        $fldTag[] = new MHContainer('hctIsObsoleteTAG', array($lblIsObsolete, $isObsolete));

        $lblHelp = new MLabel(_M('Help', $this->module) . ':');
        $lblHelp->setWidth(FIELD_LABEL_SIZE);
        $help = new MMultiLIneField('TAG_help', $this->TAG_help->value, '', null, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_COLS_SIZE);
        $fldTag[] = new MHContainer('hctHelpTAG', array($lblHelp, $help));

        $vctTag[] = new MVContainer('vctTag', $fldTag);
        $this->tables['tag']->setFields( $vctTag );
        $fields[] = $this->tables['tag'];

        $columns = null;
        $yn = GnutecaUtils::getYesNo();
        $columns[] = new MGridColumn( _M('Subfield',    $this->module),      'left', true, null, true, 'TAG_subfieldId' );
        $columns[] = new MGridColumn( _M('Description', $this->module),      'left', true, null, true, 'TAG_description' );
        $columns[] = new MGridColumn( _M('Observation', $this->module),      'left', true, null, true, 'TAG_observation' );
        $columns[] = new MGridColumn( _M('Is repetitive', $this->module),    'left', true, null, true, 'TAG_isRepetitiveF' );
        $columns[] = new MGridColumn( _M('Has subfield', $this->module),     'left', true, null, true, 'TAG_hasSubfieldF' );
        $columns[] = new MGridColumn( _M('Is active', $this->module),        'left', true, null, true, 'TAG_isActiveF' );
        $columns[] = new MGridColumn( _M('In demonstration', $this->module), 'left', true, null, true, 'TAG_inDemonstrationF' );
        $columns[] = new MGridColumn( _M('Is obsolete', $this->module),      'left', true, null, true, 'TAG_isObsoleteF' );
        $columns[] = new MGridColumn( _M('Help', $this->module),             'left', true, null, true, 'TAG_help' );
        $this->tables['tag']->setColumns($columns);

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


    public function tbBtnDelete_confirm($sender=NULL)
    {
        try
        {
            $ok = $this->business->deleteTag( MIOLO::_REQUEST('fieldId'), MARC_SPACE );
            $goto = $this->MIOLO->getActionURL($this->module, $this->_action);

            if ( $ok )
            {
                $goto = $this->MIOLO->getActionURL($this->module, $this->action);
                $this->information(MSG_RECORD_DELETED, $goto);
            }
            else
            {
                $goto = $this->MIOLO->getActionURL($this->module, $this->action);
                $this->error(MSG_RECORD_ERROR, $goto);
            }
        }
        catch( EDatabaseException $e )
        {
            $this->error( $e->getMessage() );
        }
    }


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

        //Tag
        $tagData = $this->tables['tag']->getData();
        for ($i=0, $c=count($tagData); $i < $c; $i++)
        {
            $tagData[$i]->subfieldId   = $tagData[$i]->TAG_subfieldId;
            $tagData[$i]->description  = $tagData[$i]->TAG_description;
            $tagData[$i]->observation  = $tagData[$i]->TAG_observation;
            $tagData[$i]->isRepetitive = $tagData[$i]->TAG_isRepetitive;
            $tagData[$i]->hasSubfield  = $tagData[$i]->TAG_hasSubfield;
            $tagData[$i]->isActive     = $tagData[$i]->TAG_isActive;
            $tagData[$i]->inDemonstration = $tagData[$i]->TAG_inDemonstration;
            $tagData[$i]->isObsolete   = $tagData[$i]->TAG_isObsolete;
            $tagData[$i]->help         = $tagData[$i]->TAG_help;
        }
        $data->tag = $tagData;
        parent::tbBtnSave_click($sender, $data);
    }


    public function loadFields()
    {
        try
        {
            $data = $this->business->getTag( MIOLO::_REQUEST('fieldId'), MARC_SPACE );
            $this->business->helpX = $data->help;
            $this->setData( $this->business );

            for ($i=0, $c=count($data->tag); $i < $c; $i++)
            {
                $data->tag[$i]->TAG_subfieldId   = $data->tag[$i]->subfieldId;
                $data->tag[$i]->TAG_description  = $data->tag[$i]->description;
                $data->tag[$i]->TAG_observation  = $data->tag[$i]->observation;
                $data->tag[$i]->TAG_isRepetitive = $data->tag[$i]->isRepetitive;
                $data->tag[$i]->TAG_hasSubfield  = $data->tag[$i]->hasSubfield;
                $data->tag[$i]->TAG_isActive     = $data->tag[$i]->isActive;
                $data->tag[$i]->TAG_inDemonstration = $data->tag[$i]->inDemonstration;
                $data->tag[$i]->TAG_isObsolete   = $data->tag[$i]->isObsolete;
                $data->tag[$i]->TAG_help         = $data->tag[$i]->help;
            }
            $this->tables['tag']->setData( $this->parseTag($data->tag) );
        }
        catch( EDatabaseException $e )
        {
            $this->error( $e->getMessage() );
        }
    }


    public function addToTable($args)
    {
    	$args = $this->parseTag($args);
    	parent::addToTable($args);
    }


    public function parseTag($data)
    {
        if (is_array($data))
        {
            $out[] = array();
            foreach ($data as $value)
            {
                $out[] = $this->parseTag($value);
            }
            return $out;
        }
        else if ($data)
        {
            ($data->TAG_isRepetitive)    ? $data->TAG_isRepetitiveF    = GnutecaUtils::getYesNo($data->TAG_isRepetitive) : null;
            ($data->TAG_hasSubfield)     ? $data->TAG_hasSubfieldF     = GnutecaUtils::getYesNo($data->TAG_hasSubfield) : null;
            ($data->TAG_isActive)        ? $data->TAG_isActiveF        = GnutecaUtils::getYesNo($data->TAG_isActive) : null;
            ($data->TAG_inDemonstration) ? $data->TAG_inDemonstrationF = GnutecaUtils::getYesNo($data->TAG_inDemonstration) : null;
            ($data->TAG_isObsolete)      ? $data->TAG_isObsoleteF      = GnutecaUtils::getYesNo($data->TAG_isObsolete) : null;
            return $data;
        }
    }
}
?>
