<?php
/**
 * Open Source - Content Managment System
 * Next Generation v2
 * @Coder:  Mutzii
 * @copyright  Copyright (c) 2012-2013 by Mutzii
 * File: language.api.php
 */

class language extends API
{
    protected $lang_list;
	protected $lang_default = 2;
	
	public function Init()
	{
	   $this->lang_list = $this->getInstance()->getErrorList();
	}
	
   
    public function setUserLanguage($name,$id)
	{
       if($this->existsUser($name)){
	      $SQL   = $this->getInstance()->getSQL();
	      $query = $SQL->updateint("accounts","account_language",$id,"account_user",$name);
		  $SQL->query($query);
		  return true;
	   }
	   
	   return false;
	}
	
	 public function getLoginUser()
	{
	   /* Hier wird der Name ausgegeben, von dem Spieler der gerade Eingeloggt ist. */
	   return $this->getInstance()->getLogin()->PrintUser();
	}
	
	
	public function existsUser($name)
	{
	  $SQL       = $this->getInstance()->getSQL();
	  $query     = $SQL->select("accounts");
	  $result    = $SQL->query($query);
	  $userfound = false;
	  
	  
	  while($daten = mysql_fetch_object($result)){
	  
			if(strcmp($daten->account_user,$name) == false){
					$userfound = true;
					break;
			}
	  
	  }
	
	  return $userfound;
	}
   
   
	public function getUserIdLanguage($name)
	{
	 if($this->getInstance()->getLogin()->Login()){
	 
       $SQL    = $this->getInstance()->getSQL();
	   $query  = $SQL->select("accounts");
	   $result = $SQL->query($query);
	   
	   $language_id = null;
	   
	   while($daten = mysql_fetch_object($result)){
	   
	      if(strcmp($daten->account_user,$name) == false){	  
		      $language_id = $daten->account_language;
		      break;
		  } 
	   } 
       
       return $language_id;	
	   
     }else{
	   return $this->lang_default;
	 }

	}
	
	public function getUserLanguage($language_id)
	{
	   $SQL    = $this->getInstance()->getSQL();
	   $query  = $SQL->select("language_list");
	   $result = $SQL->query($query);
	   
	   $lang_shortcut = null;
	   
	   while($daten = mysql_fetch_object($result)){
	   
	      if(strcmp($daten->index,$language_id) == false){	  
		      $lang_shortcut = $daten->shortcuts;
		      break;
		  } 
	   } 
	
	   return $lang_shortcut;
	}
	
	
	public function Fill_Message_List($user_lang_shortcut)
	{
	   $SQL    = $this->getInstance()->getSQL();
	   $query  = $SQL->select("language");
	   $result = $SQL->query($query);
	   
	   while($daten = mysql_fetch_object($result)){
	
	    $this->lang_list->addError($daten->holder,$daten->$user_lang_shortcut);

	   }
	}

	public function getLangList()
	{
	  return $this->lang_list;
	}
	
	public function setPlaceHolders()
	{
	  $array = array();
	  $list = $this->lang_list->getIndexList();
	  $size = $this->lang_list->getSize();
	  
	  for($x = 0; $x < $size; $x++){    
		  $array = array_merge($array, array('{'.$list[$x].'}' => $this->lang_list->getMessage($list[$x])));
	  }
	  
	  $this->getInstance()->getPlaceHolder()->fill($array);
	}
	
	
}




?>