<?php

using('System.Web.UI.HtmlControls');
/**
 * LoginBox
 *
 * @date 21.03.2007
 * @name LoginBox
 * @author Margo Poolak <margo 'dot' poolak 'at' mail 'dot' ee>
 * @copyright Copyright &copy;2006 Margo Poolak. All Rights Reserved.
 * @version 2.0
 * @package System.Web.UI.WebControls
 * @category 
 * 
 * This is UI LoginBox
 */
class LoginBox extends Page
{
	/*
	*
	*/
	public function onInit()
	{
		$this->setPath(dirname(__FILE__));
		/*
		* If user is authenticated then set visible to false
		*/
		if($this->User->isAuthenticated()==true)
		{
			$this->setVisible(false);
		}		
	}
	/*
	*
	*/
	public function onLoad()
	{
		// other variables and text
		$this->set('color','green');		
		$this->set('error_login','');
		
		// if is submited
		if($this->Request->isPostBack()==true)
		{
			$user = $this->Request->getPost('username');
			$pass = $this->Request->getPost('password');
			// check is the parameters set
			if(isset($user) && isset($pass))
			{
				// longer then specified
				if((strlen($user)>1) AND strlen($pass)>2)
				{
					/*
					* We need to use module Login method, check the availability
					*/
					if(method_exists($this->User,'Login') == true)
					{
						// lets try to login
						$valid = $this->User->Login($user,$pass);
						// if valid login
						if($valid == true)
						{
							// redirect to another page
							$url = $this->Request->getGet(REDIRECT);
							if(strlen($url)>0)
							{
								$this->Response->redirect($url);// success redirect
							}
							else
							{
								$this->Response->redirect($this->Request->constructUrl());// success redirect
							}
						}
					}
					else
					{
						trigger_error('No Login method defined in Module:'.get_class($this->User).':Login($user,$password)',E_USER_ERROR);
					}
				}
				$this->set('error_login','failed');
				$this->set('color','red');				
			}
		}
	}
}
?>