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

/**
 * Description of db_lang_lib
 *
 * @author Igor Ratsun
 */
class Db_lang_lib {
    
    protected $_get_current_lang_id;
    protected $_get_languages;
    
    /**
     * Get id of current language
     * @return int 
     */
    public function get_current_lang_id() {
        $CI = & get_instance();
        $short_name = $CI->uri->current_language;
        if (empty($this->_get_current_lang_id[$short_name])) {
            $db = clone $CI->db;
            $res = $db->select('id_lan')->
                            where(array(
                                'active' => '1',
                                'short_name' => $short_name
                            ))->get('lan')->row_array();
            $this->_get_current_lang_id[$short_name] = (!empty($res['id_lan'])) ? $res['id_lan'] : 0;
        }

        return $this->_get_current_lang_id[$short_name];
    }

    /**
     * Get date of active language
     * @param int $id_lan
     * @return mixed - object or array of objects
     */
    public function get_lang_by_id($id_lan = 0) {
        if (empty($this->_get_languages[$id_lan])) {
            $CI = & get_instance();
            $db = clone $CI->db;
            if ($id_lan != 0) {
                $db->where('id_lan', $id_lan);
            }
            $res = $db->select('id_lan, short_name, name')->
                            where('active', '1')->
                            get('lan')->result();
            foreach ($res as $lang) {
                $this->_get_languages[$lang->id_lan] = $lang;
            }
            
            if ($id_lan == 0) {
                $this->_get_languages[0] = $res;
            }
        }

        return $this->_get_languages[$id_lan];
    }
}

