<?php

class frmLogin extends MForm
{

    public $auth;

    public function frmLogin()
    {
        parent::__construct(_M('Login'));
        $this->setWidth('60%');
        $this->setIcon($this->manager->getUI()->getImage('admin', 'login-16x16.png'));
        if ($this->page->isPostBack())
        {
            $this->eventHandler();
        }
    }

    public function createFields()
    {
        global $MIOLO, $action, $module;
        
        $ui = $MIOLO->getUI();
        
        $this->auth = $this->manager->auth;
        $return_to = $this->getFormValue('return_to', MIOLO::_Request('return_to'));
        
        if (! $return_to)
        {
            $return_to_module = $this->manager->getConf('options.common');
            $return_to = $MIOLO->getActionURL($return_to_module, 'main');
        }
        
        $imgLogin = new MImage('imgLogin', _M('Inform the username and password'), $ui->getImage($module, 'attention.png'));
        
        $fields = array ( MMessage::getMessageContainer(),
                        new MTextField('uid', $this->auth->login->iduser, 'Login', 20), 
                        new MPasswordField('pwd', '', _M('Password'), 20), 
                        new MTextLabel('username', $this->auth->login->user, 'Nome', 40), 
                        new MLink('mail', 'Email para contato', 'mailto:' . $this->manager->getConf('theme.email'), $this->manager->getConf('theme.email')), 
                        new MHiddenField('tries', ''), 
                        new MHiddenField('return_to', $return_to));
        
        $this->setFields($fields);
        
        $this->addButton(new MButton('btnLogin', _M('Login', $module)));
        $this->addButton(new MButton('btnLogout', _M('Logout', $module)));
        //$help = $MIOLO->getActionURL('admin','loginhelp',null,null,'popup.php');
        $helpMsg = _M('You need to inform a valid username and password in order to login.');
        $this->addButton(new MButton('btnHelp', _M('Help', $module), "alert('$helpMsg');"));
        
        $this->setButtonAttr('btnLogin', 'visible', ! $this->isAuthenticated());
        $this->setButtonAttr('btnLogout', 'visible', $this->isAuthenticated());
        $this->setFieldAttr('uid', 'readonly', $this->isAuthenticated());
        $this->setFieldAttr('pwd', 'visible', ! $this->isAuthenticated());
        $this->getField('uid')->setClass('m-text-user-field');
        $this->getField('pwd')->setClass('m-text-passwd-field');
        
        $this->setFieldAttr('username', 'visible', $this->isAuthenticated());
        
        $this->setFocus('uid');
    }

    public function btnLogin_click()
    {
        global $MIOLO;
        
        $this->getData();
        
        // Max login tries
        $max_tries = 3;
        
        // get form data
        $uid = $this->getFormValue('uid');
        $pwd = $this->getFormValue('pwd');
        
        $MIOLO->logMessage('[LOGIN] Validating login information: ' . $uid);
        
        if (! $this->loginPermitted($uid))
        {
            $err = _M('Acess not allowed!', MIOLO::getCurrentModule() );
        }
        else
        {
            // Authentica the user
            if ($this->auth->authenticate($uid, $pwd))
            {
                $return_to = $this->getFormValue('return_to');

                if ($return_to)
                {
                    $url = $return_to;
                }
                else
                {
                    $url = $MIOLO->getActionURL('admin', 'login');
                }
                $this->page->redirect($url);
            }
            else
            {
                $tries = $this->getFormValue('tries');
                
                if ($tries >= $max_tries)
                {
                    $MIOLO->error( _M('Error identifying user!', MIOLO::getCurrentModule()) );
                }
                else
                {
                    $left = $max_tries - $tries;
                    
                    $err  = _M('Error validating user! ');
                    $err .= _M('@1 tries left.',MIOLO::getCurrentModule(), $left);
                    $err .= '<br/>' . $this->auth->errors;

                    $tries ++;
                    $this->setFormValue('tries', $tries);
                    $pwd = null;
                    
//                    if ($err)
//                    {
//                        $this->addError($err);
//                    }
                    
                    new MMessage($err, MMessage::TYPE_ERROR);
                }
            }
        }
    }

    public function btnLogout_click()
    {
        global $MIOLO;
        $this->page->redirect($MIOLO->getActionURL($module, 'logout'));
    }

    public function loginPermitted($uid)
    {
        global $MIOLO;
        
        $ok = true;
        return $ok;
    }

    public function isAuthenticated()
    {
        return $this->auth->isLogged();
    }

}
?>
