コード例 #1
0
 /**
  * Initializes the languages
  * Note: cannot be executed in constructor
  *       since the class is created before creating the factory
  */
 function initLanguages()
 {
     global $application;
     global $zone;
     // if no mb_string then no language selection is available
     if (!$application->multilang_core->_mb_enabled) {
         return;
     }
     $active_only = $zone == 'CustomerZone';
     // setting the default language
     $this->_default_language = $this->_readDefaultLanguage();
     CTrace::inf('Default language is ' . CTrace::var2str($this->_default_language));
     // selecting the current language
     $this->_language = $this->_default_language;
     CTrace::inf('Current language is ' . CTrace::var2str($this->_language));
     // if customer is registered
     if ($zone == 'CustomerZone' && ($customer = modApiFunc('Customer_Account', 'getCurrentSignedCustomer'))) {
         $lng = modApiFunc('Customer_Account', 'getAccountLanguage', $customer);
         if ($lng && $this->checkLanguage($lng, $active_only)) {
             $this->_language = $lng;
             CTrace::inf('Customer is signed in, change current language to ' . $this->_language . ' because of the account settings.');
         }
     }
     // if admin is registered
     if ($zone == 'AdminZone' && ($adminID = modApiFunc('Users', 'getCurrentUserID'))) {
         $lng = modApiFunc('Users', 'getAccountLanguageById', $adminID);
         if ($lng && $this->checkLanguage($lng, $active_only)) {
             $this->_language = $lng;
             CTrace::inf('Admin is signed in, change current language to ' . $this->_language . ' because of the account settings.');
         }
     }
     // if cookie is set
     if (isset($_COOKIE['current_language_az']) && $zone == 'AdminZone') {
         $lng = $_COOKIE['current_language_az'];
         if ($this->checkLanguage($lng, $active_only)) {
             $this->_language = $lng;
             CTrace::inf('Change current language to ' . $this->_language . ' because of cookies.');
         }
     }
     if (isset($_COOKIE['current_language']) && $zone == 'CustomerZone') {
         $lng = $_COOKIE['current_language'];
         if ($this->checkLanguage($lng, $active_only)) {
             $this->_language = $lng;
             CTrace::inf('Change current language to ' . $this->_language . ' because of cookies.');
         }
     }
     // if language is set in the query string
     if (isset($_GET['current_language']) || isset($_POST['current_language'])) {
         if (isset($_GET['current_language'])) {
             $lng = $_GET['current_language'];
         } else {
             $lng = $_POST['current_language'];
         }
         if ($this->checkLanguage($lng, $active_only)) {
             $this->_language = $lng;
             CTrace::inf('Change current language to ' . $this->_language . ' because of GET params.');
         }
     }
     // checking the language
     if (!$this->checkLanguage($this->_language, $active_only)) {
         $this->_language = $this->_getAnyLanguage($active_only);
         if (!$this->_language) {
             $this->_default_language = '';
             CTrace::inf('Set default language to "".');
         }
     }
     // saving the current language
     // (only in case if the language was not provided in URL)
     if ($this->_language && !isset($_GET['current_language']) && !isset($_POST['current_language'])) {
         // setting the cookie for 30 days
         if ($zone == 'CustomerZone') {
             setcookie('current_language', $this->_language, time() + 2592000);
         } else {
             setcookie('current_language_az', $this->_language, time() + 2592000);
         }
         // updating the customer_account if needed
         if ($zone == 'CustomerZone' && $customer) {
             modApiFunc('Customer_Account', 'setAccountLanguage', $customer, $this->_language);
         }
         // updating the admin_account if needed
         if ($zone == 'AdminZone' && $adminID) {
             modApiFunc('Users', 'updateAccountLanguage', $adminID, $this->_language);
         }
     }
     $this->_default_language_number = $this->_readLanguageNumber($this->_default_language);
     $this->_language_number = $this->_readLanguageNumber($this->_language);
     $this->_resource_language = $this->_language;
     $this->_resource_language_number = $this->_language_number;
     // getting the page language if set
     if ($zone == 'AdminZone' && modApiFunc('Session', 'is_set', 'PageLanguages')) {
         $PageLanguages = modApiFunc('Session', 'get', 'PageLanguages');
         $request =& $application->getInstance('Request');
         $view = $request->getValueByKey('page_view');
         $action = $request->getCurrentAction();
         $pagelanguage = '';
         $script_name = array_pop(explode('/', $_SERVER['SCRIPT_NAME']));
         if (isset($PageLanguages['pages'][$script_name])) {
             $pagelanguage = $PageLanguages['pages'][$script_name];
         } elseif (isset($PageLanguages['views'][$view])) {
             $pagelanguage = $PageLanguages['views'][$view];
         } elseif (isset($PageLanguages['actions'][$action])) {
             $pagelanguage = $PageLanguages['actions'][$action];
         }
         if ($pagelanguage && $this->checkLanguage($pagelanguage, $active_only)) {
             $this->_language = $pagelanguage;
             $this->_language_number = $this->_readLanguageNumber($this->_language);
             CTrace::inf('Change current language to ' . $this->_language . ' because it is page language.');
         }
     }
 }