<?php
/**
 * Authenticate
 *
 * @author Eduardo Bonfandini [eduardo@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]
 *
 * @since
 * Class created on 01/10/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 FrmAuthenticate extends GnutecaForm
{
    public $MIOLO;
    public $module;
    public $action;
    public $grid;
    public $function;
    public $business;
    public $tryAction;
 
    public function __construct($args)
    {
        $this->tryAction= $args;
        $this->MIOLO    = MIOLO::getInstance();
        $this->module   = MIOLO::getCurrentModule();
        $this->action   = MIOLO::getCurrentAction();
        $this->function = MIOLO::_REQUEST('function');
        $this->business = $this->MIOLO->getBusiness( $this->module, 'BusAuthenticate');

        parent::__construct( _M('Authenticate', $this->module) );
        $this->setFocus('user');
        $this->setIcon($this->MIOLO->getUI()->getImage($this->module, 'myLibrary-16x16.png'));

        if ($this->business->checkAcces())
        {
            $goto = $this->MIOLO->getActionURL($this->module, 'main:myLibrary', null, $opts);
            $this->page->redirect($goto);
        }

        $this->eventHandler();
    }


    public function createFields()
    {
        $this->defaultButton = false;
        $ui = $this->MIOLO->getUI();

        $fields[0] = new MTextField('user', MIOLO::_REQUEST('user'), _M('Code', $this->module), 20);
        $valids[0] = new MIntegerValidator('user');
        $valids[1] = new MRequiredValidator('user');
        $fields[1] = new MPasswordField('password', null, _M('Password', $this->module), 20);
        $fields[1]->addAttribute('onPressEnter', "miolo.doAjax('btnLogin_click', '', frm);");
        $valids[1] = new MRequiredValidator('password');

        $fields[] = new MButton('btnLogin',_M('Login', $this->module));
        $this->setFields( $fields );
        $this->setValidators( $valids );

        $this->setLabelWidth(FIELD_LABEL_SIZE);
        $this->setWidth('80%');

        if ($this->_action == 'main:myLibrary')
        {
        	$_REQUEST['redirect_action'] = null;
        }
    }


    public function btnLogin_click()
    {
        $data   = $this->getData();
        $result = $this->business->authenticate($data->user, $data->password);
        if (!$result)
        {
/*            $label = new MLabel(_M('Not logged. Please verify your data.', $this->module), 'red', true);
            $this->injectContent($label, true, _M('Error', $this->module) );*/
            $this->error( _M('Not logged. Please verify your data.', $this->module ),'javascript:gnuteca.closeAction()', null, false);
        }
        else
        {
            $previousUrl = $this->MIOLO->getPreviousUrl();
            if ($previousUrl && strpos($previousUrl, 'myLibrary') )
            {
                $goto = $previousUrl;
            }
            else
            {
                $goto = $this->MIOLO->getActionURL($this->module, 'main:myLibrary', null, $opts);
            }

            if ($this->tryAction)
            {
            $goto = $this->MIOLO->getActionURL($this->module, $this->tryAction, null, $opts);
            }

            if ($action = MIOLO::_REQUEST('redirect_action'))
            {
            	$goto = $this->MIOLO->getActionURL($this->module, $action);
            }

            $this->page->redirect($goto);
        }
    }
}
?>
