<?php

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

/**
 * Description of user_register_model
 *
 * @author Igor Ratsun
 */
class user_register_model extends CI_Model
{

    public function add_new()
    {
        // Insert main data
        $ins = $this->prepare_main_data();
        $this->db->insert('acl_user', $ins);

        $id_acl_user = $this->db->insert_id();

        if (empty($id_acl_user)) {

            return FALSE;
        }

        // insert translate
        $ins_tr = $this->prepare_translate_data($id_acl_user);
        $this->db->insert('acl_user_translate', $ins_tr);

        $this->send_confirm_email($id_acl_user);

        return TRUE;
    }

    public function update_current_user()
    {
        $id_user = $this->rt_user->id_user;

        // Update main data
        $main = $this->prepare_main_data(FALSE);
        $this->db
                ->where('id_acl_user', $id_user)
                ->update('acl_user', $main);

        // Update translate
        $trans = $this->prepare_translate_data($id_user);
        $this->db
                ->where('id_acl_user', $id_user)
                ->update('acl_user_translate', $trans);
    }

    public function send_confirm_email($id_user)
    {
        $this->load->model('user/user_model_site');

        $this->load->library('ciext/configurator');
        $config = new configurator('system');
        $this->load->library('email');
        $this->email->from($config->item('email_from'), $config->item('email_from'));
        $this->email->to($this->input->post('email'));
        $this->email->subject($this->lang->line('Confirm account'));

        $link = base_url('ajax/user/action/registration_confirm/' . $this->user_model_site->makeHash($id_user));
        $message = sprintf($this->lang->line('To confirm your registration click on the <a href="%s">link</a>, after which your account will be activated'), $link);
        $this->email->message($message);

        $this->email->send();

        return TRUE;
    }

    public function confirm_registration($id)
    {
        $this->db
                ->where('id_acl_user', $id)
                ->update('acl_user', array('confirmed' => TRUE, 'active' => TRUE));

        return ($this->db->affected_rows() > 0);
    }

    protected function prepare_main_data($set_default_values = TRUE)
    {
        $subscribe = $this->input->post('subscribe');
        $data = array(
            'sex'       => (int) $this->input->post('sex'),
            'phone'     => $this->input->post('phone'),
            'subscribe' => !empty($subscribe)
        );

        if ($set_default_values) {
            $data['login'] = $this->input->post('email');
            $data['email'] = $this->input->post('email');
            $data['password'] = $this->rt_user->password_hash($this->input->post('password'));
            $data['registered'] = date('Y-m-d H:i:s');
            $data['id_acl_role'] = $this->config->item('registered_user_default_role_id');
        }

        return $data;
    }

    protected function prepare_translate_data($id_user)
    {
        $this->load->library('db_lang_lib');
        $id_lan = $this->db_lang_lib->get_current_lang_id();

        $data = array(
            'id_acl_user' => $id_user,
            'name'        => $this->input->post('name'),
            'surname'     => $this->input->post('surname'),
            'id_lan'      => $id_lan
        );

        return $data;
    }

}
