<?php

class Lib_Lang
{
	
    public function Lang()
    {			
		$db = Zend_Registry::get('db');
		$lang_sel = new Zend_Session_Namespace('language');
	
		$lang = LANGDEF;
		
		
		$url = (@$_SERVER['HTTPS'] === null ? 'http' : 'https') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
		
		
		$requestLang = Zend_Controller_Request_Http::getPost('changeLang');
		if( isset($requestLang) ){
			/* sprawdzenie czy język istnieje */			
			$Lang = new Lang();
			$select = $Lang->select();		
			$select->where('lang_suffix = ?', $requestLang);				
			$row = $Lang->fetchRow($select);
			if( isset($row) ){
				$lang = $requestLang;
				$lang_sel->select = $requestLang;
			}
			header("Location: ".$url);
		}
		
		if( $lang_sel->select ) $lang = $lang_sel->select;
	
	/*
		$dataLang = $db->query("SELECT * FROM `lang_".$lang."`")->fetchAll();
		
		foreach( $dataLang as $key => $value ){
			//runkit_constant_remove('LANG_'.$value['language_variable']);
			define('LANG_'.$value['language_variable'], $value['language_value']);		
		}		
	*/
		//runkit_constant_remove('LANG');
		define('LANG',$lang);
	}
	
    public function Language()
    {			
		$db = Zend_Registry::get('db');		
		return $db->query("SELECT * FROM `lang`")->fetchAll();			
	}
	
    public function translate($var)
    {			
		$db = Zend_Registry::get('db');

		$ask = "SELECT language_value FROM lang_".LANG." WHERE language_variable = '".$var."' LIMIT 1";
		$result = $db->query( $ask )->fetchAll();
		return $result[0]['language_value'];		
	//	return $ask; 
	}
	
	function consta($const)
	{
		$var = (defined($const)) ? constant($const): $const;
		return Lib_Lang::translate($var);
	}
	
}

