load->model('user_model');
		if(!$this->user_model->authorize(99)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
		$data['results'] = $this->user_model->users();
		$this->load->view('layout/header');
		$this->load->view('user/main', $data);
		$this->load->view('layout/footer');
	}
	function add() {
		$this->load->model('user_model');
		if(!$this->user_model->authorize(99)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('user_name', 'Username', 'required');
		$this->form_validation->set_rules('user_email', 'E-mail', 'required');
		$this->form_validation->set_rules('user_password', 'Password', 'required');
		$this->form_validation->set_rules('user_type', 'Type', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('layout/header');
			if($this->input->post('user_name'))
			{
				$data['user_name'] = $this->input->post('user_name');
				$data['user_email'] = $this->input->post('user_email');
				$data['user_password'] = $this->input->post('user_password');
				$data['user_type'] = $this->input->post('user_type');
				$this->load->view('user/add', $data);
			} else {
				$this->load->view('user/add');
			}
			$this->load->view('layout/footer');
		}
		else
		{
			switch($this->user_model->add($this->input->post('user_name'), $this->input->post('user_password'), $this->input->post('user_email'), $this->input->post('user_type'))) {
				// Check for errors
				case EUSERNAMEEXISTS:
					$data['username_error'] = 'Username '.$this->input->post('user_name').' already in use!';
					break;
				case EEMAILEXISTS:
					$data['email_error'] = 'E-mail address '.$this->input->post('user_email').' already in use!';
					break;
				case EPASSWORDINVALID:
					$data['password_error'] = 'Invalid password!';
					break;
				// All okay, return to user screen
				case OK:
					$this->session->set_flashdata('notice', 'User '.$this->input->post('user_name').' added');
					redirect('user');
					return;
			}
			$this->load->view('layout/header');
			$data['user_name'] = $this->input->post('user_name');
			$data['user_email'] = $this->input->post('user_email');
			$data['user_password'] = $this->input->post('user_password');
			$data['user_type'] = $this->input->post('user_type');
			$this->load->view('user/add', $data);
			$this->load->view('layout/footer');
		}
	}
	function edit() {
		$this->load->model('user_model');
		if(!$this->user_model->authorize(99)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
		$query = $this->user_model->get_by_id($this->uri->segment(3));
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('user_name', 'Username', 'required');
		$this->form_validation->set_rules('user_email', 'E-mail', 'required');
		$this->form_validation->set_rules('user_type', 'Type', 'required');
		$data = $query->row();
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('layout/header');
			if($this->input->post('user_name'))
			{
				$data['user_name'] = $this->input->post('user_name');
				$data['user_email'] = $this->input->post('user_email');
				$data['user_password'] = $this->input->post('user_password');
				$data['user_type'] = $this->input->post('user_type');
			}
			$this->load->view('user/edit', $data);
			$this->load->view('layout/footer');
		}
		else
		{
			unset($data);
			switch($this->user_model->edit($this->input->post('id'), $this->input->post('user_name'), $this->input->post('user_password'), $this->input->post('user_email'), $this->input->post('user_type'))) {
				// Check for errors
				case EUSERNAMEEXISTS:
					$data['username_error'] = 'Username '.$this->input->post('user_name').' already in use!';
					break;
				case EEMAILEXISTS:
					$data['email_error'] = 'E-mail address '.$this->input->post('user_email').' already in use!';
					break;
				case EPASSWORDINVALID:
					$data['password_error'] = 'Invalid password!';
					break;
				// All okay, return to user screen
				case OK:
					$this->session->set_flashdata('notice', 'User '.$this->input->post('user_name').' edited');
					redirect('user');
					return;
			}
			$this->load->view('layout/header');
			$data['user_name'] = $this->input->post('user_name');
			$data['user_email'] = $this->input->post('user_email');
			$data['user_password'] = $this->input->post('user_password');
			$data['user_type'] = $this->input->post('user_type');
			$this->load->view('user/edit', $data);
			$this->load->view('layout/footer');
		}
	}
	function delete() {
		$this->load->model('user_model');
		if(!$this->user_model->authorize(99)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); }
		$query = $this->user_model->get_by_id($this->uri->segment(3));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 'user_id', 'required');
		$data = $query->row();
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('layout/header');
			$this->load->view('user/delete', $data);
			$this->load->view('layout/footer');
		}
		else
		{
			if($this->user_model->delete($data->user_id))
			{
				$this->session->set_flashdata('notice', 'User deleted');
				redirect('user');
			} else {
				$this->session->set_flashdata('notice', 'Database error: Could not delete user!');
				redirect('user');
			}
		}
	}
	function login() {
		$this->load->model('user_model');
		$query = $this->user_model->get($this->input->post('user_name'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('user_name', 'Username', 'required');
		$this->form_validation->set_rules('user_password', 'Password', 'required');
		$data = $query->row();
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('layout/header');
			$this->load->view('user/login', $data);
			$this->load->view('layout/footer');
		}
		else
		{
			if($this->user_model->login() == 1) {
				$this->session->set_flashdata('notice', 'User logged in');
				$this->user_model->update_session($data->user_id);
				redirect('dashboard');
			} else {
				$this->session->set_flashdata('notice', 'Incorrect username or password!');
				redirect('user/login');
			}
		}
	}
	function logout() {
		$this->load->model('user_model');
		$user_name = $this->session->userdata('user_name');
		$this->user_model->clear_session();
		$this->session->set_flashdata('notice', 'User '.$user_name.' logged out.');
		redirect('dashboard');
	}
}