<?php
$MIOLO->import('modules::example::controls::viewsource');
// the class name is the same as the file name
class FrmCurrency extends MForm
{
        public function __construct()
        {
            parent::__construct( 'Currency Field Sample' );

            // if the form was submitted
            if( $this->page->isPostBack() )
            {
                // calls the events (in this form: btnPost_click)
                $this->eventHandler();
            }

            // view source link
            $this->addField( new ViewSource(__FILE__) );
        }


        // create the form fields
        public function createFields()
        {
            // define some currency fields
            // note: _M() is the Miolo's i18n method
            $fields = array( new MCurrencyField( 'edtMoney1', '', _M('First Value') ),
                             new MCurrencyField( 'edtMoney2', '', _M('Second Value') ),
                             new MTextLabel( 'edtMoney3', '', _M('Sum') ),
                             new MSeparator()
                            );

            // set the fields on the form
            $this->setFields( $fields );

            // define the submit button
            $btn1 = new MButton('btnPost', _M('Submit'));
            // set a javascript hint
            $btn1->setJsHint( _M('Press to sum the values' ) );
            $buttons = array( $btn1 );

            // set the button to the form
            $this->setButtons( $buttons );
        }


        // this method is automatically called when the form is submitted
        // the submit button 
        public function btnPost_click()
        {
            // get the values filled by the user
            $money1 = $this->getFieldValue( 'edtMoney1' );
            $money2 = $this->getFieldValue( 'edtMoney2' );

            // currency formatter
            $cf = new MCurrencyFormatter();
            $money3 = $cf->formatWithSymbol( $cf->toDecimal($money1) + $cf->toDecimal($money2) );

            // add two fields with the submitted values
            $this->addField( new MTextLabel('lblMoney1', $money1, 'GetField 1') );
            $this->addField( new MTextLabel('lblMoney2', $money2, 'GetField 2') );

            // edtMoney3 was created in createFields method, here we
            // update the values
            $this->setFieldValue( 'edtMoney3', $money3 );
        }
}

?>
