<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides login capability
 *
 * @package    lnApp
 * @subpackage Page/Login
 * @category   Controllers
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 * @also       [logout]
 */
class Controller_Login extends Controller_TemplateDefault {
	public function action_index() {
		// If user already signed-in
		if (Auth::instance()->logged_in()!= 0) {
			// Redirect to the user account
			Request::instance()->redirect('welcome/index');
		}

		// If there is a post and $_POST is not empty
		if ($_POST) {
			// Store our details in a session key
			Session::instance()->set('admin_name',$_POST['admin_name']);
			Session::instance()->set('password',$_POST['password']);

			// Instantiate a new user
			$user = ORM::factory('account');

			// Check Auth
			$status = $user->login($_POST);

			// If the post data validates using the rules setup in the user model
			if ($status) {
				// Redirect to the user account
				if ($redir = Session::instance()->get('afterlogin')) {
					Session::instance()->delete('afterlogin');
					Request::instance()->redirect($redir);
					
				} else
					Request::instance()->redirect('welcome/index');

			} else {
				SystemMessage::add(array(
					'title'=>_('Invalid username or password'), 
					'type'=>'error',
					'body'=>_('The username or password was invalid.')
				));
			}
		}

		Block::add(array(
			'title'=>_('Login to server'),
			'body'=>View::factory('login'),
			'style'=>array('css/login.css'=>'screen'),
		));

		$this->template->control = HTML::anchor($this->request->uri(),'Login',array('id'=>'ajxbody'));
		$this->template->content = Block::factory();

		Script::add(array('type'=>'stdin','data'=>'
			$(document).ready(function() {
				$("#ajxbody").click(function() {$("#ajBODY").load("'.$this->request->uri().'/"); return false;});
			});'
		));
	}

	public function action_register() {
		// If user already signed-in
		if (Auth::instance()->logged_in()!= 0) {
			// Redirect to the user account
			Request::instance()->redirect('welcome/index');
		}

		// Instantiate a new user
		$account = ORM::factory('account');

		// If there is a post and $_POST is not empty
		if ($_POST) {
			// Check Auth
			$status = $account->values($_POST)->check();

			if (! $status) {
				foreach ($account->validate()->errors() as $f=>$r) {
					// $r[0] has our reason for validation failure
					switch ($r[0]) {
						// Generic validation reason
						default:
							SystemMessage::add(array(
								'title'=>_('Validation failed'), 
								'type'=>'error',
								'body'=>sprintf(_('The defaults on your submission were not valid for field %s (%s).'),$f,$r[0])
							));
					}
				}
			}

			$ido = ORM::factory('module')
				->where('name','=','account')
				->find();

			$account->id = $ido->record_id->next_id($ido->id);
			// Save the user details
			if ($account->save()) {}

		}

		SystemMessage::add(array(
			'title'=>_('Already have an account?'), 
			'type'=>'info',
			'body'=>_('If you already have an account, please login..')
		));

		Block::add(array(
			'title'=>_('Register'),
			'body'=>View::factory('bregister')
				->set('account',$account)
				->set('errors',$account->validate()->errors()),
			'style'=>array('css/bregister.css'=>'screen'),
		));

		$this->template->control = HTML::anchor($this->request->uri(),'Register',array('id'=>'ajxbody'));
		$this->template->content = Block::factory();
		$this->template->left = HTML::anchor('login','Login').'...';
	}

	public function action_noaccess() {
		$this->template->content = '&nbsp;';

		SystemMessage::add(array(
			'title'=>_('No access to requested resource'), 
			'type'=>'error',
			'body'=>_('You do not have access to the requested resource, please contact your administrator.')
		));
	}
}
?>
