<?php
namespace language;

class FLanguage {

  public function loadLang( $language_tmp = 'pl' , $admin = false )
  {
    global $cache , $db , $sys , $config;

    $language_tmp = \language\Flanguage::checkLangCorrection( $language_tmp );

    $key = 'loadLang:' . $language_tmp;

    if ( !$translation = $cache -> fetch( $key ) )
    {
      $query = $db -> prepare( 'SELECT text , ' . $language_tmp . ' FROM pcms_lang_text' );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $translation[ $row['text'] ] = $row[ $language_tmp ];
      }
      $query -> closeCursor();
      $cache -> store( $key , $translation , $config['cache_expire_long' ] );
    }
    $sys -> setSessionVar( 'current_lang' , $language_tmp , $admin );

    $language = new \language\Language( $language_tmp );
    $language -> set_translation( $translation );

    return $language;
  }

  private function checkLangCorrection( $lang_check )
  {
    switch ( $lang_check )
    {
      case 'pl':
        return 'pl';
      break;
      case 'en':
        return 'en';
      break;
      case 'ru':
        return 'ru';
      break;
      case 'de':
        return 'de';
      break;
      case 'uk':
        return 'uk';
      break;
      default:
        return 'pl';
      break;
    }
  }
}
?>
