<?php

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

/**
 * Description of userinfo_model
 *
 * @author Igor Ratsun
 */
class userinfo_model extends CI_Model {
    
    public function get_user_name() {
        $rs = $this->db->
                select('u.name, u.surname')->
                from('acl_user_translate u, lan')->
                where('lan.short_name', $this->config->item('language_short'))->
                where('u.id_lan = lan.id_lan')->
                where('u.id_acl_user', $this->rt_user->id_user)->
                get()->row_array();
        
        return !empty($rs['name']) ? trim($rs['name'] . ' ' . $rs['surname']) : '';
    }

    public function get_data($user_id = 0) {
        $user_id = intval($user_id);
        $main_data = $this->db->
                select('id_acl_user, photo, email, birth_date, id_lan as def_lan')->
                where('id_acl_user = ', (!empty($user_id)) ? $user_id : $this->rt_user->id_user)->
                get('acl_user')->row_array();
        
        if (empty($main_data['id_acl_user'])) {
            show_404();
        }
        
        $ext_data = $this->db->
                        select('lan.id_lan, lan.name as language, uit.name, uit.surname, uit.about')->
                        where('lan.active = 1')->
                        join('acl_user_translate uit', 'lan.id_lan = uit.id_lan AND uit.id_acl_user = ' . $main_data['id_acl_user'], 'left')->
                        get('lan')->result_array();
        
        $main_data['birth_date'] = date('d.m.Y', strtotime($main_data['birth_date']));
        
        if (empty($main_data['photo'])) {
            $main_data['photo'] = $this->config->item('no_photo');
        }

        return array_merge($main_data, $this->prepare_array($ext_data));
    }
    
    protected function prepare_array($array) {
        $rs = array();
        foreach ($array as $value) {
            $rs['name'][] = array('id_lan' => $value['id_lan'], 'value' => $value['name'], 'lang' => $value['language']);
            $rs['surname'][] = array('id_lan' => $value['id_lan'], 'value' => $value['surname'], 'lang' => $value['language']);
            $rs['about'][] = array('id_lan' => $value['id_lan'], 'value' => $value['about'], 'lang' => $value['language']);
        }
        
        return$rs;
    }
    
    public function save($user_id = 0) {
        $id_acl_user = (!empty($user_id)) ? intval($user_id) : $this->rt_user->id_user;
        
        // Clear old data
        if (!empty($id_acl_user)) {
            $this->db->delete('acl_user_translate', array('id_acl_user' => $id_acl_user));
        }
        
        // Update data
        $this->db->update('acl_user', array(
            'photo' => $this->input->post('photo', true),
            'email' => $this->input->post('email', true),
            'id_lan' => (int) $this->input->post('lan'),
            'birth_date' => date('Y-m-d', strtotime($this->input->post('birth_date')))
        ), array(
            'id_acl_user' => $id_acl_user
        ));
        
        $name = $this->input->post('name', true);
        $surname = $this->input->post('surname', true);
        $about = $this->input->post('about', true);
        
        foreach ($name as $k => $v) {
            $this->db->insert('acl_user_translate', array(
                'id_acl_user' => $id_acl_user,
                'name' => $v,
                'surname' => $surname[$k],
                'about' => $about[$k],
                'id_lan' => $k
            ));
        }
        
    }
    
    public function get_lan_items()
    {
        $q = $this->db->
                select('id_lan, name')->
                where('active', TRUE)->
                get('lan')->result_array();
        
        if (empty($q)) {
            
            return array();
        }
        $rs = array('0' => '---');
        foreach ($q as $v) {
            $rs[$v['id_lan']] = $v['name'];
        }
        
        return $rs;
    }

}

