<?php
/**
 * A class for forms, extending from MBaseForm.
 * This class acts as a Decorator for MBaseForm, adding a outer box for the form.
 */

class MForm extends MBaseForm
{
/**
 * Attribute Description.
 */
    public $formBox;

/**
 * Attribute Description.
 */
    public $title;

/**
 * Attribute Description.
 */
    public $help;

/**
 * Attribute Description.
 */
    public $footer;

/**
 * Attribute Description.
 */
    public $bgColor;

/**
 * Attribute Description.
 */
    public $align = NULL;

    /**
     * This is the constructor of the Form class. It takes a title and an
     * action url as optional parameters. The action URL is typically 
     * obtained by calling the <code>MIOLO->getActionURL()</code> method.
     *
     * @param $title  (string) the form's title string
     * @param $action (string) the URL for the forms <code>ACTION</code>
     *                attribute.
     */
    public function __construct($title='',$action='',$close='',$icon='')
    {
        $this->formBox = new MBox($title,$close,$icon);
        parent::__construct($action);
        $this->setTitle($title);
    }

    
    /**
     * Obtains the content of this form's title. Observe, that this
     * can be anything other as a simple text string too, such as array of
     * strings and an object implementing the <code>Generate()</code> method.
     *
     * @return (Mixed &) a reference to the title of the form
     */
    public function getTitle()
    {
        return $this->title;
    }
    
    
    /**
     * Set the form's title/caption
     *
     * @param (string) $title Form title
     */
    public function setTitle($title)
    {
        $this->setCaption($title);
    }

    /**
     * Set the form's title/caption
     *
     * @param (string) $title Form title
     */
    public function setCaption($title)
    {
        $this->title = $this->text = $this->caption = $title;
    }

    /**
     * Sets the form's close action
     *
     * @param (string) $action Form action
     */
    public function setClose($action)
    {
        if ($this->formBox->boxTitle != NULL)
        {
           $this->formBox->boxTitle->setClose($action);
        }
    }

    /**
     * Sets the form's icon
     *
     * @param (string) $icon Icon URL
     */
    public function setIcon($icon)
    {
        if ($this->formBox->boxTitle != NULL)
        {
            $this->formBox->boxTitle->setIcon($icon);
        }
    }

    /**
     * Obtains the content title of this form's footer. Observe, that this
     * can be anything other as a simple text string too, such as array of
     * strings and an object implementing the <code>Generate()</code> method.
     *
     * @return (Mixed &) a reference to the footer of the form
     */
    public function getFooter()
    {
        return $this->footer;
    }

    /**
     * Form's footer.
     * Sets the form's footer content.
     *
     * @param $footer (tipo) Footer content
     */
    public function setFooter($footer)
    {
        $this->footer = $footer;
    }

    /**
     * Sets an URL to a help document. This document will be opened via
     * JavaScript in a new window. If this URL is set, the form will display
     * a help button in it's title bar.
     *
     * @param (string) $href URL
     */
    public function setHelp($href)
    {
        $this->help = $href;
    }
    
    /**
     * Set CSS for the field.
     *
     * @param (string) $name
     * @param (integer) $top
     * @param (integer) $left
     * @param (integer) $width
     * @param (string) $position
     */
    public function setFieldCSS($name,$top,$left,$width=NULL, $position='absolute')
    {   
        $field = $this->$name;
//        $top = $this->formBox->top + $top;
//        $left = $this->formBox->left + $left;
        if ($width) 
        {
            $field->width = "{$width}px";
        }
        
        $field->top = "{$top}px";
        $field->left = "{$left}px";
        
        if ($position) 
        {
            $field->position = $position;
        }
        
        $field->formMode = 2;
    }

    /**
     * Set form CSS
     *
     * @param (integer) $height
     * @param (integer) $width
     * @param (integer) $top
     * @param (integer) $left
     * @param (integer) $buttons
     * @param (string) $position
     */
    public function setFormCSS($height=0, $width=0, $top=0, $left=0, $buttons=0, $position='absolute')
    {
        if ($height) 
        {
            $this->formBox->addStyle('height',"{$height}px");
        }
        
        if ($width) 
        {
            $this->formBox->addStyle('width',"{$width}px");
        }
        
        if ($top) 
        {
            $this->formBox->addStyle('top',"{$top}px");
        }
        
        if ($left) 
        {
            $this->formBox->addStyle('left',"{$left}px");
        }
        
        if ($position) 
        {
            $this->formBox->addStyle('position',$position);
        }
        
        $this->cssButtons = "{$buttons}px";
        $this->cssForm = true;
    }
    
    public function setBackgroundColor($bgcolor)
    {
        $this->bgColor = $bgcolor; 
    }

    public function setAlign($value)
    {
        $this->align = $value;
    }
    
    public function setWidth($width=NULL)
    { 
        if ($width) $this->formBox->addStyle('width',"{$width}");
    }
    
    public function setHeight($height=NULL)
    { 
        if ($height) $this->formBox->addStyle('height',"{$height}");
    }

    public function enterSubmit($button)
    {
        $formId = $this->page->getFormId();
        $event = "miolo.doPostBack('{$button}:click','','$formId')";
        $this->page->onLoad("miolo.registerEvent('{$formId}', 'keypress', \"if (event.keyCode==dojo.keys.ENTER) { event.preventDefault();{$event};}\",false);");
    }

/**
 * Brief Description.
 * Complete Description.
 *
 * @returns (tipo) desc
 *
 */
    public function generateFooter()
    {
    }
   
    
    public function generateInner()
    {
        parent::generateInner();
        $body = $this->inner;
        $footer = $this->generateFooter();
        $this->formBox->setCaption($this->caption);
        $this->formBox->setControls(array($body, $footer));
        $this->formBox->setClass("mFormOuter");
        $this->setClass("mForm");
        if (!is_null($this->align))
        {
            $this->addStyle('text-align',$this->align);
        }
        $this->inner = $this->formBox;
    }
}

?>