<?php
/**
 * Preference 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 28/07/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
 *
 **/

$MIOLO->uses('classes/GnutecaIntegerField.class.php', $module);

class FrmPreference extends GnutecaForm
{

    function __construct()
    {
    	$this->setAllFunctions('Preference', null, array('moduleConfig','parameter'), 'parameter');
        parent::__construct();
    }


    public function createFields()
    {
    	if ( MIOLO::_REQUEST('function') == 'test')
    	{
    		$tabControl = new ATabControl('tabModules');

    		$list = $this->business->listPreference( true );

    		$fields = array();

    		if ( is_array( $list ) )
    		{
    			foreach ( $list as $line => $preference )
    			{
    				$type = strtoupper( $preference->type );

                    $label = new MLabeL($preference->description);
                    $label->setWidth('500px');

    				if ( $type == 'INT' || $type == 'INTEGER' )
    				{
                        $field = new GnutecaIntegerField( $preference->parameter , $preference->configValue );
    				}
    				else
    				if ( $type == 'VARCHAR' )
    				{
                        $field = new MMultiLineField( $preference->parameter , $preference->configValue ,null, FIELD_MULTILINE_ROWS_SIZE, FIELD_MULTILINE_ROWS_SIZE ,FIELD_MULTILINE_COLS_SIZE);
    				}
    				else
    				if ( $type == 'BOOLEAN' )
    				{
    					$field = new MCheckBox( $preference->parameter , $preference->configValue );
    				}
    				else
    				if ( $type == 'CHAR' || $type == 'TEXT' || !$type)
    				{
                        $field = new MTextField( $preference->parameter , $preference->configValue  );
    				}

    				$fields[ $preference->moduleConfig ][] = new MHContainer(null, array( $label, $field ) );
    			}
    		}


    		foreach ( $fields as $tab => $controls )
    		{
    			$tabControl->addTab( $tab, $tab, $controls );
    		}

    		$this->setFields( array( $tabControl ), false );
    	}
    	else
    	{

	        $moduleConfig = new MTextField('moduleConfig', strtoupper($this->module), _M('Module',$this->module), 20);
	        $moduleConfig->setReadOnly(true);
	        $fields[] = $moduleConfig;
	        $validators[] = new MRequiredValidator('moduleConfig');

	        if ( $this->function != 'update' )
	        {
	            $parameter = new MTextField('parameter', $this->parameter->value, _M('Parameter',$this->module), FIELD_DESCRIPTION_SIZE);
	            $this->setFocus('parameter');
	            $fields[] = $parameter;
	            $validators[] = new MRequiredValidator('parameter', '', 255);
	        }
	        else
	        {
	            $parameter = new MTextField('parameter', $this->parameter->value, _M('Parameter',$this->module), FIELD_DESCRIPTION_SIZE);
	            $this->setFocus('configValue');
	            $parameter->setReadOnly(true);
	            $fields[] = $parameter;
	            $validators[] = new MRequiredValidator('parameter', '', 255);
	        }
	        $configValue = new MMultiLineField('configValue', $this->getFormValue('configValue',$data->value), _M('Value',$this->module), 20, 5, 100);
	        $fields[] = $configValue;

	        $description = new MMultiLineField('description', $this->getFormValue('description',$data->description), _M('Description',$this->module), 40, 5, 100);
	        $fields[] = $description;
	        $validators[] = new MRequiredValidator('description');

	        $type = new GnutecaSelection('type', $this->getFormValue('type',$data->type), _M('Field type',$this->module), GnutecaUtils::listTypes());
	        $fields[] = $type;
	        $validators[] = new MRequiredValidator('type');
	        $this->setFields($fields);

	        if ( isset($validators) )
	        {
	            $this->setValidators($validators);
	        }

    	}
    }
}
?>
