<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* User class.
*
* @extends CI_Controller
*/
class User extends CI_Controller {
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
parent::__construct();
$this->load->library(array('session'));
$this->load->helper(array('url'));
$this->load->model('user_model');
}
public function index() {
//If no session, redirect to login page
redirect('home/');
}
/**
* register function.
*
* @access public
* @return void
*/
public function register() {
// create the data object
$data = new stdClass();
// load form helper and validation library
$this->load->helper('form');
$this->load->library('form_validation');
// set validation rules
$this->form_validation->set_rules('pseudo', 'pseudo', 'trim|required|alpha_numeric|min_length[4]|is_unique[accounts.pseudo]', array('is_unique' => 'Ce pseudo est déjà choisis. Veuillez en choisir un nouveau.'));
$this->form_validation->set_rules('mail', 'mail', 'trim|required|valid_email|is_unique[accounts.email]',
array('is_unique' => 'L\'adresse mail est déjà utilisé. Veuillez en choisir une nouvelle.'));
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]',
array('min_length' => 'Le mot de passe doit comporter au moins 6 caractères.'));
$this->form_validation->set_rules('conf_password', 'conf_password', 'trim|required|matches[password]',
array('matches' => 'Les mots de passes ne sont pas identiques.'));
if ($this->form_validation->run() === false) {
// validation not ok, send validation errors to the view
$this->load->view('layout/header');
$this->load->view('user/register', $data);
$this->load->view('layout/footer');
} else {
// set variables from the form
$username = $this->input->post('pseudo');
$email = $this->input->post('mail');
$password = $this->input->post('password');
if ($this->user_model->create_user($username, $email, $password)) {
// user creation ok
$this->load->view('layout/header');
$this->load->view('user/register_success', $data);
$this->load->view('layout/footer');
} else {
// user creation failed, this should never happen
$data->error = 'Un problème est surveneu lors de la création de votre compte. Veuillez réessayer.';
// send error to the view
$this->load->view('layout/header');
$this->load->view('user/register', $data);
$this->load->view('layout/footer');
}
}
}
/**
* login function.
*
* @access public
* @return void
*/
public function login() {
// create the data object
$data = new stdClass();
// load form helper and validation library
$this->load->helper('form');
$this->load->library('form_validation');
// set validation rules
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == false) {
// validation not ok, send validation errors to the view
$this->load->view('layout/header');
$this->load->view('user/login');
$this->load->view('layout/footer');
} else {
// set variables from the form
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user_model->resolve_user_login($username, $password)) {
$user_id = $this->user_model->get_user_id_from_username($username);
$user = $this->user_model->get_user($user_id);
// set session user datas
$_SESSION['user_id'] = (int)$user->guid;
$_SESSION['username'] = (string)$user->pseudo;
$_SESSION['email'] = (string)$user->email;
$_SESSION['pseudo_dofus'] = (string)$user->pseudo_dofus;
$_SESSION['pseudo_youtube'] = (string)$user->pseudo_youtube;
$_SESSION['sexe'] = (string)$user->sexe;
$_SESSION['travail'] = (string)$user->travail;
$_SESSION['url_dofus'] = (string)$user->url_dofus;
$_SESSION['logged_in'] = (bool)true;
// user login ok
$this->load->view('layout/header');
$this->load->view('user/login_success', $data);
$this->load->view('layout/footer');
} else {
// login failed
$data->error = 'Mauvais nom d\'utlisateur ou mot de passe !';
// send error to the view
$this->load->view('layout/header');
$this->load->view('user/login', $data);
$this->load->view('layout/footer');
}
}
}
/**
* logout function.
*
* @access public
* @return void
*/
public function logout() {
// create the data object
$data = new stdClass();
if(!$this->session->has_userdata('guid')){
// remove session datas
foreach ($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
// user logout ok
$this->load->view('layout/header');
$this->load->view('user/logout_success', $data);
$this->load->view('layout/footer');
} else {
// there user was not logged in, we cannot logged him out,
// redirect him to site root
redirect('/');
}
}
/**
* account function.
*
* @access public
* @return void
*/
public function account($id)
{
$data['accounts'] = $this->user_model->get_account($id);
$this->load->view('layout/header');
$this->load->view('user/account',$data);
$this->load->view('layout/footer');
}
/**
* account function.
*
* @access public
* @return void
*/
public function account_edit() {
// load form helper and validation library
$this->load->helper('form');
$this->load->library('form_validation');
// set validation rules
$this->form_validation->set_rules('pseudo', 'pseudo', 'required|alpha_numeric');
$this->form_validation->set_rules('pseudo_dofus', 'pseudo_dofus', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('pseudo_youtube', 'pseudo_youtube', 'required');
$this->form_validation->set_rules('sexe', 'sexe', 'required|alpha_numeric');
$this->form_validation->set_rules('travail', 'travail', 'required');
$this->form_validation->set_rules('url_dofus', 'url_dofus', 'required');
if ($this->form_validation->run() == false) {
// validation not ok, send validation errors to the view
$this->load->view('layout/header');
$this->load->view('user/account');
$this->load->view('layout/footer');
} else {
// set variables from the form
$pseudo = $this->input->post('pseudo');
$pseudo_dofus = $this->input->post('pseudo_dofus');
$email = $this->input->post('email');
$pseudo_youtube = $this->input->post('pseudo_youtube');
$sexe = $this->input->post('sexe');
$travail = $this->input->post('travail');
$url_dofus = $this->input->post('url_dofus');
if ($this->user_model->update_user($pseudo, $pseudo_dofus, $email, $pseudo_youtube, $sexe, $travail, $url_dofus)) {
// user account update
$this->load->view('layout/header');
$this->load->view('user/account_update');
$this->load->view('layout/footer');
} else {
// there user was not logged in, we cannot logged him out,
// redirect him to site root
redirect('/');
}
}
}
}