<?php

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

class user_model_site extends CI_Model
{

    protected $getUserData = NULL;

    /**
     * Return user id by email
     * @param string $email
     * @return int
     */
    public function getIdUserByEmail($email)
    {
        $q = $this->db->
                        select('id_acl_user')->
                        where('email', $email)->
                        where('active', TRUE)->
                        limit(1)->
                        get('acl_user')->row_array();

        return (!empty($q['id_acl_user'])) ? $q['id_acl_user'] : FALSE;
    }

    public function sendRecoveryEmail($id_user = 0)
    {
        $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('Passwordless login'));

        $link = base_url('ajax/user/action/loggin_by_hash/' . $this->makeHash($id_user));
        $message = sprintf($this->lang->line('You can log in by clicking on the <a href="%s">link</a>'), $link);
        $this->email->message($message);

        $this->email->send();

        return TRUE;
    }

    public function makeHash($id_user = 0)
    {
        $q = $this->getUserData($id_user);

        if (empty($q)) {

            return FALSE;
        }

        $solt = date('Y-m-d') . $this->config->item('encryption_key');

        return $id_user . '_' . md5($q['login'] . $q['password'] . $solt);
    }

    /**
     * Return data of current user
     * @param string $key
     * @return string
     */
    public function getCurrentUserData($key)
    {
        $user = $this->getUserData($this->rt_user->id_user);

        return (!empty($user[$key])) ? $user[$key] : '';
    }

    public function change_pass()
    {
        // Check old pass
        if ($this->rt_user->password_hash($this->input->post('oldpass')) != $this->getCurrentUserData('password')) {

            return FALSE;
        }
        $data = array(
            'password' => $this->rt_user->password_hash($this->input->post('password'))
        );

        $this->db
                ->where('id_acl_user', $this->rt_user->id_user)
                ->update('acl_user', $data);

        return TRUE;
    }

    protected function getUserData($id_user = 0)
    {
        if (empty($this->getUserData[$id_user])) {
            $this->load->library('db_lang_lib');
            $id_lan = $this->db_lang_lib->get_current_lang_id();

            $this->getUserData[$id_user] = $this->db->
                            select('u.*, t.*')->
                            join('acl_user_translate t', 't.id_acl_user = u.id_acl_user AND t.id_lan = ' . $id_lan, 'left')->
                            where('u.id_acl_user', (int) $id_user)->
                            get('acl_user u')->row_array();
        }

        return $this->getUserData[$id_user];
    }

}
