/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
64.1.1 by b11johgu
ExamplesController:
1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3
	class Login extends CI_Controller {
4
		/*
5
		 *	Constructor
6
		 */
7
		function __construct() {
8
			parent::__construct();
9
			
10
			//Load required library
11
			$this->load->model('user', '', TRUE);
12
		}
13
	
14
		
15
		/*
16
		 *	This function runs when the user navigates directly to the login controller
17
		 */
18
		public function index() {
19
			//If user is already logged in
20
			if($this->user->isLoggedIn()) {
21
				//User already logged in
22
				redirect(base_url().'home', 'refresh');
23
			} else {
24
				//Display the login form
25
				$this->drawLoginForm('');
26
			}
27
		}
28
		
29
		
30
		/*
31
		 *	This function validate the user input from login form using the library "form_validation".
32
		 *  NOTICE: This does NOT mean that it validates it against the database.
33
		 */
34
		public function validate() {
35
			//Load required library
36
			$this->load->library('form_validation');
37
			
38
			//Sets validation rules
39
			$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
40
			$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
41
			
42
			//Run validation
43
			if($this->form_validation->run() == FALSE) {
44
				//Field validation failed. Display login form (with error message).
45
				$this->drawLoginForm(validation_errors());
46
			} else {
47
				$username = $this->input->post('username');
48
				$password = $this->input->post('password');
49
				
50
				//Try to login
51
				if ($this->user->login($username, $password)) {
52
					redirect(base_url().'home', 'refresh');
53
				} else {
54
					$this->drawLoginForm('Access denied!');
55
				}
56
			}	
57
		}
58
		
59
	
60
		/*
61
		 *	This function draws the login form.
62
		 */
63
		private function drawLoginForm($errors) {
64
			//Load required library
65
			$this->load->helper(array('form'));
66
			
67
			//Display the view
68
			$data['error'] = $errors;
69
		$headTagData = array(
70
			'cssFiles' => array('header', 'login_view'),
71
			'jsFiles' => array('header', 'login_view', 'userControls')
72
		);
73
		$this->load->view('headTag', array('headTagData' => $headTagData));
74
		$this->load->model('user');
75
		$userInfo = array(
76
			'userType' => $this->user->getUserType(),
77
			'userName' => $this->user->getUserName()
78
		);
79
		
80
		$this->load->view('header', $userInfo);
81
			$this->load->view('login_view', $data);
82
		}
83
	}
84
?>