<?php
/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 */

if (!defined('_VALID_MOS') && !defined('_JEXEC')) die('Direct Access to ' . 
    basename(__FILE__) . ' is not allowed.');   

  
global $mosConfig_lang;
global $langContent;
$database = JFactory::getDBO();


  // load language
  $languagelocale = "";
  $query = "SELECT title, lang_code, sef FROM #__medialibrary_languages";
  $database->setQuery($query);
  $languages = $database->loadObjectList();

  $lang = JFactory::getLanguage();

  foreach ($languages as $language) {
      if ($lang->getTag() == $language->lang_code) {
          $mosConfig_lang = $language->lang_code;
          $languagelocale = $language->lang_code;
          break;
      }
  }  

  if ($languagelocale == '') {
    foreach ($lang->getLocale() as $locale) {

        foreach ($languages as $language) {

            if (strtolower($locale) == strtolower($language->title)
                || strtolower($locale) == strtolower($language->lang_code)
                || strtolower($locale) == strtolower($language->sef)
                || strtolower(str_replace(array('_','-'), '', $locale)) 
                == strtolower((str_replace(array('_','-'), '', $language->lang_code)))
                
                ) {

                $mosConfig_lang = $locale;
                $languagelocale = $language->lang_code;


                break;
            }
        }
    }
  }


  if (isset($_REQUEST['option']) && $_REQUEST['option'] == 'com_installer')
      $languagelocale = "en-GB";

  if ($languagelocale == '')
      $languagelocale = "en-GB";

 

  $query = "SELECT c.const, cl.value_const ";
  $query .= "FROM #__medialibrary_const_languages as cl ";
  $query .= "LEFT JOIN #__medialibrary_languages AS l ON cl.fk_languagesid=l.id ";
  $query .= "LEFT JOIN #__medialibrary_const AS c ON cl.fk_constid=c.id ";
  $query .= "WHERE l.lang_code = '$languagelocale'";
  $database->setQuery($query);
  $langConst = $database->loadObjectList();



  foreach ($langConst as $item) {
      if(!defined($item->const) )  define($item->const, $item->value_const); // $database->quote()
  }



  $query = "SELECT l.lang_code "
          . "FROM #__languages as l " ;
  $database->setQuery($query);
  $languages = $database->loadObjectList();



 foreach ($lang->getLocale() as $locale) {

      foreach ($languages as $language) {


          if (strtolower($locale) == strtolower($language->lang_code)
              || strtolower(str_replace(array('_','-'), '', $locale)) 
              == strtolower((str_replace(array('_','-'), '', $language->lang_code)))
              
              ) {

              $langContent = $language->lang_code;


              break;
          }
      }
  }


  
