<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class user_site extends MX_Controller
{

    public function __construct()
    {
        parent::__construct();

        if (!$this->input->post($this->security->get_csrf_token_name())) {
            $this->security->csrf_set_cookie();
        }
    }
    
    public function login()
    {
        if ($this->rt_user->id_user > 0) {
            redirect($this->config->item('logged_in_redirect_page'));
        }
        
        $content = array(
            'status' => $this->session->flashdata('login_widget_dialog_status'),
            'login' => $this->session->userdata('login_widget_dialog_login'),
        );
        
        return $this->parser->parse('user/login', $content, TRUE);
    }

    public function login_widget()
    {
        $content = array(
            'status' => $this->session->flashdata('login_widget_dialog_status'),
            'login' => $this->session->userdata('login_widget_dialog_login'),
            'loggedIn' => ($this->rt_user->id_user > 0)
        );

        return $this->parser->parse('user/login_widget', $content, TRUE);
    }

    public function forgot_password()
    {
        $content = array(
            'message' => $this->session->flashdata('forgot_password_message'),
            'error' => $this->session->flashdata('forgot_password_error'),
            'email' => $this->session->flashdata('forgot_password_email'),
        );

        return $this->parser->parse('user/forgot_pass', $content, TRUE);
    }

    public function register()
    {
        if ($this->session->flashdata('registration_confirm')) {

            return $this->parser->parse('user/register_confirm', array('message' => $this->lang->line('Ваш аккаунт успешно активирован')), TRUE);
        }

        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', $this->lang->line('E-mail'), 'required|valid_email|is_unique[acl_user.email]');
        $this->form_validation->set_rules('password', $this->lang->line('Password'), 'required|min_length[6]|matches[passconf]');
        $this->form_validation->set_rules('passconf', $this->lang->line('Password again'), 'required|min_length[6]');

        if ($this->input->post()) {
            $this->lang->load('user/register');
            $this->load->model('user/user_register_model');
            if ($this->form_validation->run() == TRUE && $this->user_register_model->add_new()) {

                return $this->parser->parse('user/register_confirm', array('message' => $this->lang->line('Check your email for activate account')), TRUE);
            }
        }

        return $this->parser->parse('user/register', array(), TRUE);
    }

    public function user_info()
    {
        $id_user = $this->rt_user->id_user;
        if (empty($id_user)) {

            return $this->lang->line('Please, sign in');
        }

        $this->load->model('user/user_model_site');

        return $this->load->view('user/user_info', array('user' => $this->user_model_site), TRUE);
    }

    public function changepass()
    {
        $id_user = $this->rt_user->id_user;
        if (empty($id_user)) {

            return $this->lang->line('Please, sign in');
        }
        
        $content = array(
            'message' => ''
        );
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('password', $this->lang->line('New password'), 'required|min_length[6]|matches[passconf]');
            $this->form_validation->set_rules('passconf', $this->lang->line('New password again'), 'required|min_length[6]');

            $this->lang->load('user/register');
            $this->load->model('user/user_model_site');
            if ($this->form_validation->run() == TRUE) {
                $content['message'] = ($this->user_model_site->change_pass()) ?
                        $this->lang->line('Your password successfully changed') : $this->lang->line('Error, try again');
            }
        }

        return $this->parser->parse('user/changepass', $content, TRUE);
    }

}
