oxlang.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxLang extends oxSuperCfg
00007 {
00008 
00014     protected $_sName = 'lang';
00015 
00021     protected $_iBaseLanguageId = null;
00022 
00028     protected $_iTplLanguageId = null;
00029 
00035     protected $_iEditLanguageId = null;
00036 
00042     protected $_aLangCache = array();
00043 
00049     protected $_aAdminTplLanguageArray = null;
00050 
00056     protected $_aLangAbbr = null;
00057 
00063     protected $_aAdditionalLangFiles = array();
00064 
00070     protected $_aLangMap = array();
00071 
00077     protected $_aActiveModuleInfo = null;
00078 
00084     protected $_aDisabledModuleInfo = null;
00085 
00091     protected $_blIsTranslated = true;
00092 
00098     protected $_iObjectTplLanguageId = null;
00099 
00105     public function setIsTranslated($blIsTranslated = true)
00106     {
00107         $this->_blIsTranslated = $blIsTranslated;
00108     }
00109 
00115     public function isTranslated()
00116     {
00117         return $this->_blIsTranslated;
00118     }
00119 
00125     public function resetBaseLanguage()
00126     {
00127         $this->_iBaseLanguageId = null;
00128     }
00129 
00135     public function getBaseLanguage()
00136     {
00137         if ($this->_iBaseLanguageId === null) {
00138 
00139             $myConfig = $this->getConfig();
00140             $blAdmin = $this->isAdmin();
00141 
00142             // languages and search engines
00143             if ($blAdmin && (($iSeLang = oxRegistry::getConfig()->getRequestParameter('changelang')) !== null)) {
00144                 $this->_iBaseLanguageId = $iSeLang;
00145             }
00146 
00147             if (is_null($this->_iBaseLanguageId)) {
00148                 $this->_iBaseLanguageId = oxRegistry::getConfig()->getRequestParameter('lang');
00149             }
00150 
00151             //or determining by domain
00152             $aLanguageUrls = $myConfig->getConfigParam('aLanguageURLs');
00153 
00154             if (!$blAdmin && is_array($aLanguageUrls)) {
00155                 foreach ($aLanguageUrls as $iId => $sUrl) {
00156                     if ($sUrl && $myConfig->isCurrentUrl($sUrl)) {
00157                         $this->_iBaseLanguageId = $iId;
00158                         break;
00159                     }
00160                 }
00161             }
00162 
00163             if (is_null($this->_iBaseLanguageId)) {
00164                 $this->_iBaseLanguageId = oxRegistry::getConfig()->getRequestParameter('language');
00165                 if (!isset($this->_iBaseLanguageId)) {
00166                     $this->_iBaseLanguageId = oxRegistry::getSession()->getVariable('language');
00167                 }
00168             }
00169 
00170             // if language still not set and not search engine browsing,
00171             // getting language from browser
00172             if (is_null($this->_iBaseLanguageId) && !$blAdmin && !oxRegistry::getUtils()->isSearchEngine()) {
00173 
00174                 // getting from cookie
00175                 $this->_iBaseLanguageId = oxRegistry::get("oxUtilsServer")->getOxCookie('language');
00176 
00177                 // getting from browser
00178                 if (is_null($this->_iBaseLanguageId)) {
00179                     $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00180                 }
00181             }
00182 
00183             if (is_null($this->_iBaseLanguageId)) {
00184                 $this->_iBaseLanguageId = $myConfig->getConfigParam('sDefaultLang');
00185             }
00186 
00187             $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00188 
00189             // validating language
00190             $this->_iBaseLanguageId = $this->validateLanguage($this->_iBaseLanguageId);
00191 
00192             oxRegistry::get("oxUtilsServer")->setOxCookie('language', $this->_iBaseLanguageId);
00193         }
00194 
00195         return $this->_iBaseLanguageId;
00196     }
00197 
00203     public function getObjectTplLanguage()
00204     {
00205         if ($this->_iObjectTplLanguageId === null) {
00206             $this->_iObjectTplLanguageId = $this->getTplLanguage();
00207             $aLanguages = $this->getAdminTplLanguageArray();
00208             if (!isset($aLanguages[$this->_iObjectTplLanguageId]) ||
00209                 $aLanguages[$this->_iObjectTplLanguageId]->active == 0
00210             ) {
00211                 $this->_iObjectTplLanguageId = key($aLanguages);
00212             }
00213         }
00214 
00215         return $this->_iObjectTplLanguageId;
00216     }
00217 
00225     public function getTplLanguage()
00226     {
00227         if ($this->_iTplLanguageId === null) {
00228             $iSessLang = oxRegistry::getSession()->getVariable('tpllanguage');
00229             $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage($iSessLang) : $this->getBaseLanguage();
00230         }
00231 
00232         return $this->_iTplLanguageId;
00233     }
00234 
00240     public function getEditLanguage()
00241     {
00242         if ($this->_iEditLanguageId === null) {
00243 
00244             if (!$this->isAdmin()) {
00245                 $this->_iEditLanguageId = $this->getBaseLanguage();
00246             } else {
00247 
00248                 $iLang = null;
00249                 // choosing language ident
00250                 // check if we really need to set the new language
00251                 if ("saveinnlang" == $this->getConfig()->getActiveView()->getFncName()) {
00252                     $iLang = oxRegistry::getConfig()->getRequestParameter("new_lang");
00253                 }
00254                 $iLang = ($iLang === null) ? oxRegistry::getConfig()->getRequestParameter('editlanguage') : $iLang;
00255                 $iLang = ($iLang === null) ? oxRegistry::getSession()->getVariable('editlanguage') : $iLang;
00256                 $iLang = ($iLang === null) ? $this->getBaseLanguage() : $iLang;
00257 
00258                 // validating language
00259                 $this->_iEditLanguageId = $this->validateLanguage($iLang);
00260 
00261                 // writing to session
00262                 oxRegistry::getSession()->setVariable('editlanguage', $this->_iEditLanguageId);
00263             }
00264         }
00265 
00266         return $this->_iEditLanguageId;
00267     }
00268 
00278     public function getLanguageArray($iLanguage = null, $blOnlyActive = false, $blSort = false)
00279     {
00280         $myConfig = $this->getConfig();
00281 
00282         if (is_null($iLanguage)) {
00283             $iLanguage = $this->_iBaseLanguageId;
00284         }
00285 
00286         $aLanguages = array();
00287         $aConfLanguages = $myConfig->getConfigParam('aLanguages');
00288         $aLangParams = $myConfig->getConfigParam('aLanguageParams');
00289 
00290         if (is_array($aConfLanguages)) {
00291             $i = 0;
00292             reset($aConfLanguages);
00293             while (list($key, $val) = each($aConfLanguages)) {
00294 
00295                 if ($blOnlyActive && is_array($aLangParams)) {
00296                     //skipping non active languages
00297                     if (!$aLangParams[$key]['active']) {
00298                         $i++;
00299                         continue;
00300                     }
00301                 }
00302 
00303                 if ($val) {
00304                     $oLang = new stdClass();
00305                     $oLang->id = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00306                     $oLang->oxid = $key;
00307                     $oLang->abbr = $key;
00308                     $oLang->name = $val;
00309 
00310                     if (is_array($aLangParams)) {
00311                         $oLang->active = $aLangParams[$key]['active'];
00312                         $oLang->sort = $aLangParams[$key]['sort'];
00313                     }
00314 
00315                     $oLang->selected = (isset($iLanguage) && $oLang->id == $iLanguage) ? 1 : 0;
00316                     $aLanguages[$oLang->id] = $oLang;
00317                 }
00318                 ++$i;
00319             }
00320         }
00321 
00322         if ($blSort && is_array($aLangParams)) {
00323             uasort($aLanguages, array($this, '_sortLanguagesCallback'));
00324         }
00325 
00326         return $aLanguages;
00327     }
00328 
00334     public function getAdminTplLanguageArray()
00335     {
00336         if ($this->_aAdminTplLanguageArray === null) {
00337             $myConfig = $this->getConfig();
00338 
00339             $aLangArray = $this->getLanguageArray();
00340             $this->_aAdminTplLanguageArray = array();
00341 
00342             $sSourceDir = $myConfig->getAppDir() . 'views/admin/';
00343             foreach ($aLangArray as $iLangKey => $oLang) {
00344                 $sFilePath = "{$sSourceDir}{$oLang->abbr}/lang.php";
00345                 if (file_exists($sFilePath) && is_readable($sFilePath)) {
00346                     $this->_aAdminTplLanguageArray[$iLangKey] = $oLang;
00347                 }
00348             }
00349         }
00350 
00351         // moving pointer to beginning
00352         reset($this->_aAdminTplLanguageArray);
00353 
00354         return $this->_aAdminTplLanguageArray;
00355     }
00356 
00364     public function getLanguageAbbr($iLanguage = null)
00365     {
00366         if ($this->_aLangAbbr === null) {
00367             $this->_aLangAbbr = $this->getLanguageIds();
00368         }
00369 
00370         $iLanguage = isset($iLanguage) ? (int) $iLanguage : $this->getBaseLanguage();
00371         if (isset($this->_aLangAbbr[$iLanguage])) {
00372             $iLanguage = $this->_aLangAbbr[$iLanguage];
00373         }
00374 
00375         return $iLanguage;
00376     }
00377 
00384     public function getLanguageNames()
00385     {
00386         $aConfLanguages = $this->getConfig()->getConfigParam('aLanguages');
00387         $aLangIds = $this->getLanguageIds();
00388         $aLanguages = array();
00389         foreach ($aLangIds as $iId => $sValue) {
00390             $aLanguages[$iId] = $aConfLanguages[$sValue];
00391         }
00392 
00393         return $aLanguages;
00394     }
00395 
00408     public function translateString($sStringToTranslate, $iLang = null, $blAdminMode = null)
00409     {
00410         $this->setIsTranslated();
00411         // checking if in cache exist
00412         $aLang = $this->_getLangTranslationArray($iLang, $blAdminMode);
00413         if (isset($aLang[$sStringToTranslate])) {
00414             return $aLang[$sStringToTranslate];
00415         }
00416 
00417         // checking if in map exist
00418         $aMap = $this->_getLanguageMap($iLang, $blAdminMode);
00419         if (isset($aLang[$aMap[$sStringToTranslate]])) {
00420             return $aLang[$aMap[$sStringToTranslate]];
00421         }
00422 
00423         // checking if in theme options exist
00424         if (count($this->_aAdditionalLangFiles)) {
00425             $aLang = $this->_getLangTranslationArray($iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00426             if (isset($aLang[$sStringToTranslate])) {
00427                 return $aLang[$sStringToTranslate];
00428             }
00429         }
00430 
00431         $this->setIsTranslated(false);
00432 
00433         return $sStringToTranslate;
00434     }
00435 
00446     protected function _collectSimilar($aData, $sKey, $aCollection = array())
00447     {
00448         foreach ($aData as $sValKey => $sValue) {
00449             if (strpos($sValKey, $sKey) === 0) {
00450                 $aCollection[$sValKey] = $sValue;
00451             }
00452         }
00453 
00454         return $aCollection;
00455     }
00456 
00467     public function getSimilarByKey($sKey, $iLang = null, $blAdmin = null)
00468     {
00469         startProfile("getSimilarByKey");
00470 
00471         $iLang = isset($iLang) ? $iLang : $this->getTplLanguage();
00472         $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
00473 
00474         // checking if exists in cache
00475         $aLang = $this->_getLangTranslationArray($iLang, $blAdmin);
00476         $aSimilarConst = $this->_collectSimilar($aLang, $sKey);
00477 
00478         // checking if in map exist
00479         $aMap = $this->_getLanguageMap($iLang, $blAdmin);
00480         $aSimilarConst = $this->_collectSimilar($aMap, $sKey, $aSimilarConst);
00481 
00482         // checking if in theme options exist
00483         if (count($this->_aAdditionalLangFiles)) {
00484             $aLang = $this->_getLangTranslationArray($iLang, $blAdmin, $this->_aAdditionalLangFiles);
00485             $aSimilarConst = $this->_collectSimilar($aLang, $sKey, $aSimilarConst);
00486         }
00487 
00488         stopProfile("getSimilarByKey");
00489 
00490         return $aSimilarConst;
00491     }
00492 
00501     public function formatCurrency($dValue, $oActCur = null)
00502     {
00503         if (!$oActCur) {
00504             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00505         }
00506         $sValue = oxRegistry::getUtils()->fRound($dValue, $oActCur);
00507 
00508         return number_format((double) $sValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand);
00509     }
00510 
00519     public function formatVat($dValue, $oActCur = null)
00520     {
00521         $iDecPos = 0;
00522         $sValue = ( string ) $dValue;
00524         $oStr = getStr();
00525         if (($iDotPos = $oStr->strpos($sValue, '.')) !== false) {
00526             $iDecPos = $oStr->strlen($oStr->substr($sValue, $iDotPos + 1));
00527         }
00528 
00529         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00530         $iDecPos = ($iDecPos < $oActCur->decimal) ? $iDecPos : $oActCur->decimal;
00531 
00532         return number_format((double) $dValue, $iDecPos, $oActCur->dec, $oActCur->thousand);
00533     }
00534 
00542     public function getLanguageTag($iLanguage = null)
00543     {
00544         if (!isset($iLanguage)) {
00545             $iLanguage = $this->getBaseLanguage();
00546         }
00547 
00548         $iLanguage = (int) $iLanguage;
00549 
00550         return (($iLanguage) ? "_$iLanguage" : "");
00551     }
00552 
00560     public function validateLanguage($iLang = null)
00561     {
00562         $iLang = (int) $iLang;
00563 
00564         // checking if this language is valid
00565         $aLanguages = $this->getLanguageArray(null, !$this->isAdmin());
00566         if (!isset($aLanguages[$iLang]) && is_array($aLanguages)) {
00567             $oLang = current($aLanguages);
00568             if (isset($oLang->id)) {
00569                 $iLang = $oLang->id;
00570             }
00571         }
00572 
00573         return $iLang;
00574     }
00575 
00581     public function setBaseLanguage($iLang = null)
00582     {
00583         if (is_null($iLang)) {
00584             $iLang = $this->getBaseLanguage();
00585         } else {
00586             $this->_iBaseLanguageId = (int) $iLang;
00587         }
00588 
00589         if (defined('OXID_PHP_UNIT')) {
00590             modSession::getInstance();
00591         }
00592 
00593         oxRegistry::getSession()->setVariable('language', $iLang);
00594     }
00595 
00603     public function setTplLanguage($iLang = null)
00604     {
00605         $this->_iTplLanguageId = isset($iLang) ? (int) $iLang : $this->getBaseLanguage();
00606         if ($this->isAdmin()) {
00607             $aLanguages = $this->getAdminTplLanguageArray();
00608             if (!isset($aLanguages[$this->_iTplLanguageId])) {
00609                 $this->_iTplLanguageId = key($aLanguages);
00610             }
00611         }
00612 
00613         if (defined('OXID_PHP_UNIT')) {
00614             modSession::getInstance();
00615         }
00616 
00617         oxRegistry::getSession()->setVariable('tpllanguage', $this->_iTplLanguageId);
00618 
00619         return $this->_iTplLanguageId;
00620     }
00621 
00631     protected function _recodeLangArray($aLangArray, $sCharset, $blRecodeKeys = false)
00632     {
00633         $aLangs = array();
00634         foreach ($aLangArray as $sKey => $sValue) {
00635             $sItemKey = $sKey;
00636             if ($blRecodeKeys === true) {
00637                 $sItemKey = iconv($sCharset, 'UTF-8', $sItemKey);
00638             }
00639 
00640             $aLangs[$sItemKey] = iconv($sCharset, 'UTF-8', $sValue);
00641             unset($aLangArray[$sKey]);
00642         }
00643 
00644         return $aLangs;
00645     }
00646 
00654     protected function _getLangFilesPathArray($iLang)
00655     {
00656         $oConfig = $this->getConfig();
00657         $aLangFiles = array();
00658 
00659         $sAppDir = $oConfig->getAppDir();
00660         $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
00661         $sTheme = $oConfig->getConfigParam("sTheme");
00662         $sCustomTheme = $oConfig->getConfigParam("sCustomTheme");
00663         $sShopId = $oConfig->getShopId();
00664         $aModulePaths = $this->_getActiveModuleInfo();
00665 
00666         //get generic lang files
00667         $sGenericPath = $sAppDir . 'translations/' . $sLang;
00668         if ($sGenericPath) {
00669             $aLangFiles[] = $sGenericPath . "/lang.php";
00670             $aLangFiles = $this->_appendLangFile($aLangFiles, $sGenericPath);
00671         }
00672 
00673         //get theme lang files
00674         if ($sTheme) {
00675             $sThemePath = $sAppDir . 'views/' . $sTheme . '/' . $sLang;
00676             $aLangFiles[] = $sThemePath . "/lang.php";
00677             $aLangFiles = $this->_appendLangFile($aLangFiles, $sThemePath);
00678         }
00679 
00680         //get custom theme lang files
00681         if ($sCustomTheme) {
00682             $sCustPath = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang;
00683             $aLangFiles[] = $sCustPath . "/lang.php";
00684             $aLangFiles = $this->_appendLangFile($aLangFiles, $sCustPath);
00685         }
00686 
00687 
00688         // custom theme shop languages
00689 
00690         // modules language files
00691         $aLangFiles = $this->_appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang);
00692 
00693         // custom language files
00694         $aLangFiles = $this->_appendCustomLangFiles($aLangFiles, $sLang);
00695 
00696         return count($aLangFiles) ? $aLangFiles : false;
00697     }
00698 
00706     protected function _getAdminLangFilesPathArray($iLang)
00707     {
00708         $oConfig = $this->getConfig();
00709         $aLangFiles = array();
00710 
00711         $sAppDir = $oConfig->getAppDir();
00712         $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
00713 
00714         $aModulePaths = array();
00715         $aModulePaths = array_merge($aModulePaths, $this->_getActiveModuleInfo());
00716         $aModulePaths = array_merge($aModulePaths, $this->_getDisabledModuleInfo());
00717 
00718         // admin lang files
00719         $sAdminPath = $sAppDir . 'views/admin/' . $sLang;
00720         $aLangFiles[] = $sAdminPath . "/lang.php";
00721         $aLangFiles[] = $sAppDir . 'translations/' . $sLang . '/translit_lang.php';
00722         $aLangFiles = $this->_appendLangFile($aLangFiles, $sAdminPath);
00723 
00724         // themes options lang files
00725         $sThemePath = $sAppDir . 'views/*/' . $sLang;
00726         $aLangFiles = $this->_appendLangFile($aLangFiles, $sThemePath, "options");
00727 
00728         // module language files
00729         $aLangFiles = $this->_appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang, true);
00730 
00731         // custom language files
00732         $aLangFiles = $this->_appendCustomLangFiles($aLangFiles, $sLang, true);
00733 
00734         return count($aLangFiles) ? $aLangFiles : false;
00735     }
00736 
00746     protected function _appendLangFile($aLangFiles, $sFullPath, $sFilePattern = "lang")
00747     {
00748         $aFiles = glob($sFullPath . "/*_{$sFilePattern}.php");
00749         if (is_array($aFiles) && count($aFiles)) {
00750             foreach ($aFiles as $sFile) {
00751                 if (!strpos($sFile, 'cust_lang.php')) {
00752                     $aLangFiles[] = $sFile;
00753                 }
00754             }
00755         }
00756 
00757         return $aLangFiles;
00758     }
00759 
00769     protected function _appendCustomLangFiles($aLangFiles, $sLang, $blForAdmin = false)
00770     {
00771         $oConfig = $this->getConfig();
00772         $sAppDir = $oConfig->getAppDir();
00773         $sTheme = $oConfig->getConfigParam("sTheme");
00774         $sCustomTheme = $oConfig->getConfigParam("sCustomTheme");
00775 
00776         if ($blForAdmin) {
00777             $aLangFiles[] = $sAppDir . 'views/admin/' . $sLang . '/cust_lang.php';
00778         } else {
00779             if ($sTheme) {
00780                 $aLangFiles[] = $sAppDir . 'views/' . $sTheme . '/' . $sLang . '/cust_lang.php';
00781             }
00782             if ($sCustomTheme) {
00783                 $aLangFiles[] = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang . '/cust_lang.php';
00784             }
00785         }
00786 
00787         return $aLangFiles;
00788     }
00789 
00800     protected function _appendModuleLangFiles($aLangFiles, $aModulePaths, $sLang, $blForAdmin = false)
00801     {
00802         if (is_array($aModulePaths)) {
00803             $oConfig = $this->getConfig();
00804             foreach ($aModulePaths as $sPath) {
00805                 $sFullPath = $oConfig->getModulesDir() . $sPath;
00806                 if (file_exists($sFullPath . '/application/')) {
00807                     $sFullPath .= '/application';
00808                 }
00809                 $sFullPath .= ($blForAdmin) ? '/views/admin/' : '/translations/';
00810                 $sFullPath .= $sLang;
00811                 $aLangFiles = $this->_appendLangFile($aLangFiles, $sFullPath);
00812                 //load admin modules options lang files
00813                 if ($blForAdmin) {
00814                     $aLangFiles[] = $sFullPath . '/module_options.php';
00815                 }
00816             }
00817         }
00818 
00819         return $aLangFiles;
00820     }
00821 
00831     protected function _getLangFileCacheName($blAdmin, $iLang, $aLangFiles = null)
00832     {
00833         $myConfig = $this->getConfig();
00834         $sLangFilesIdent = '_default';
00835         if (is_array($aLangFiles) && $aLangFiles) {
00836             $sLangFilesIdent = '_' . md5(implode('+', $aLangFiles));
00837         }
00838 
00839         return "langcache_" . ((int) $blAdmin) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam('sTheme') . $sLangFilesIdent;
00840     }
00841 
00851     protected function _getLanguageFileData($blAdmin = false, $iLang = 0, $aLangFiles = null)
00852     {
00853         $myConfig = $this->getConfig();
00854         $myUtils = oxRegistry::getUtils();
00855 
00856         $sCacheName = $this->_getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
00857         $aLangCache = $myUtils->getLangCache($sCacheName);
00858         if (!$aLangCache && $aLangFiles === null) {
00859             if ($blAdmin) {
00860                 $aLangFiles = $this->_getAdminLangFilesPathArray($iLang);
00861             } else {
00862                 $aLangFiles = $this->_getLangFilesPathArray($iLang);
00863             }
00864         }
00865         if (!$aLangCache && $aLangFiles) {
00866             $aLangCache = array();
00867             $sBaseCharset = false;
00868             $aLang = array();
00869             $aLangSeoReplaceChars = array();
00870             foreach ($aLangFiles as $sLangFile) {
00871 
00872                 if (file_exists($sLangFile) && is_readable($sLangFile)) {
00873                     $aSeoReplaceChars = array();
00874                     include $sLangFile;
00875 
00876                     // including only (!) those, which has charset defined
00877                     if (isset($aLang['charset'])) {
00878 
00879                         // recoding only in utf
00880                         if ($myConfig->isUtf()) {
00881                             $aLang = $this->_recodeLangArray($aLang, $aLang['charset']);
00882 
00883                             if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00884                                 $aSeoReplaceChars = $this->_recodeLangArray($aSeoReplaceChars, $aLang['charset'], true);
00885                             }
00886 
00887                             // overriding charset
00888                             $aLang['charset'] = 'UTF-8';
00889                         }
00890 
00891                         if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00892                             $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
00893                         }
00894 
00895                         if (!$sBaseCharset) {
00896                             $sBaseCharset = $aLang['charset'];
00897                         }
00898 
00899                         $aLangCache = array_merge($aLangCache, $aLang);
00900                     }
00901                 }
00902             }
00903 
00904             // setting base charset
00905             if ($sBaseCharset) {
00906                 $aLangCache['charset'] = $sBaseCharset;
00907             }
00908 
00909             // special character replacement list
00910             $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
00911 
00912             //save to cache
00913             $myUtils->setLangCache($sCacheName, $aLangCache);
00914         }
00915 
00916         return $aLangCache;
00917     }
00918 
00927     protected function _getLanguageMap($iLang, $blAdmin = null)
00928     {
00929         $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
00930         $sKey = $iLang . ((int) $blAdmin);
00931         if (!isset($this->_aLangMap[$sKey])) {
00932             $this->_aLangMap[$sKey] = array();
00933             $myConfig = $this->getConfig();
00934 
00935             $sMapFile = '';
00936             $sParentMapFile = $myConfig->getAppDir() . '/views/' . ($blAdmin ? 'admin' : $myConfig->getConfigParam("sTheme")) . '/' . oxRegistry::getLang()->getLanguageAbbr($iLang) . '/map.php';
00937             $sCustomThemeMapFile = $myConfig->getAppDir() . '/views/' . ($blAdmin ? 'admin' : $myConfig->getConfigParam("sCustomTheme")) . '/' . oxRegistry::getLang()->getLanguageAbbr($iLang) . '/map.php';
00938 
00939             if (file_exists($sCustomThemeMapFile) && is_readable($sCustomThemeMapFile)) {
00940                 $sMapFile = $sCustomThemeMapFile;
00941             } elseif (file_exists($sParentMapFile) && is_readable($sParentMapFile)) {
00942                 $sMapFile = $sParentMapFile;
00943             }
00944 
00945             if ($sMapFile) {
00946                 include $sMapFile;
00947                 $this->_aLangMap[$sKey] = $aMap;
00948             }
00949 
00950         }
00951 
00952         return $this->_aLangMap[$sKey];
00953     }
00954 
00963     protected function _getCacheLanguageId($blAdmin, $iLang = null)
00964     {
00965         $iLang = ($iLang === null && $blAdmin) ? $this->getTplLanguage() : $iLang;
00966         if (!isset($iLang)) {
00967             $iLang = $this->getBaseLanguage();
00968             if (!isset($iLang)) {
00969                 $iLang = 0;
00970             }
00971         }
00972 
00973         return (int) $iLang;
00974     }
00975 
00985     protected function _getLangTranslationArray($iLang = null, $blAdmin = null, $aLangFiles = null)
00986     {
00987         startProfile("_getLangTranslationArray");
00988 
00989         $blAdmin = isset($blAdmin) ? $blAdmin : $this->isAdmin();
00990         $iLang = $this->_getCacheLanguageId($blAdmin, $iLang);
00991         $sCacheName = $this->_getLangFileCacheName($blAdmin, $iLang, $aLangFiles);
00992 
00993         if (!isset($this->_aLangCache[$sCacheName])) {
00994             $this->_aLangCache[$sCacheName] = array();
00995         }
00996         if (!isset($this->_aLangCache[$sCacheName][$iLang])) {
00997             // loading main lang files data
00998             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData($blAdmin, $iLang, $aLangFiles);
00999         }
01000 
01001         stopProfile("_getLangTranslationArray");
01002 
01003         // if language array exists ..
01004         return (isset($this->_aLangCache[$sCacheName][$iLang]) ? $this->_aLangCache[$sCacheName][$iLang] : array());
01005     }
01006 
01015     protected function _sortLanguagesCallback($a1, $a2)
01016     {
01017         return ($a1->sort > $a2->sort);
01018     }
01019 
01025     public function getName()
01026     {
01027         return $this->_sName;
01028     }
01029 
01035     public function getFormLang()
01036     {
01037         $sLang = null;
01038         if (!$this->isAdmin()) {
01039             $sLang = "<input type=\"hidden\" name=\"" . $this->getName() . "\" value=\"" . $this->getBaseLanguage() . "\" />";
01040         }
01041 
01042         return $sLang;
01043     }
01044 
01052     public function getUrlLang($iLang = null)
01053     {
01054         $sLang = null;
01055         if (!$this->isAdmin()) {
01056             $iLang = isset($iLang) ? $iLang : $this->getBaseLanguage();
01057             $sLang = $this->getName() . "=" . $iLang;
01058         }
01059 
01060         return $sLang;
01061     }
01062 
01075     public function processUrl($sUrl, $iLang = null)
01076     {
01077         $iLang = isset($iLang) ? $iLang : $this->getBaseLanguage();
01079         $oStr = getStr();
01080 
01081         if (!$this->isAdmin()) {
01082             $sParam = $this->getUrlLang($iLang);
01083             if (!$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl) && ($iLang != oxRegistry::getConfig()->getConfigParam('sDefaultLang'))) {
01084                 if ($sUrl) {
01085                     if ($oStr->strpos($sUrl, '?') === false) {
01086                         $sUrl .= "?";
01087                     } elseif (!$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl)) {
01088                         $sUrl .= "&amp;";
01089                     }
01090                 }
01091                 $sUrl .= $sParam . "&amp;";
01092             } else {
01093                 $sUrl = $oStr->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1' . $sParam, $sUrl);
01094             }
01095         }
01096 
01097         return $sUrl;
01098     }
01099 
01106     public function detectLanguageByBrowser()
01107     {
01108         $sBrowserLanguage = $this->_getBrowserLanguage();
01109 
01110         if (!is_null($sBrowserLanguage)) {
01111 
01112             $aLanguages = $this->getLanguageArray(null, true);
01113             foreach ($aLanguages as $oLang) {
01114                 if ($oLang->abbr == $sBrowserLanguage) {
01115                     return $oLang->id;
01116                 }
01117             }
01118         }
01119     }
01120 
01126     public function getMultiLangTables()
01127     {
01128         $aTables = array("oxarticles", "oxartextends", "oxattribute",
01129                          "oxcategories", "oxcontents", "oxcountry",
01130                          "oxdelivery", "oxdiscount", "oxgroups",
01131                          "oxlinks", "oxnews", "oxobject2attribute",
01132                          "oxpayments", "oxselectlist", "oxshops",
01133                          "oxactions", "oxwrapping", "oxdeliveryset",
01134                          "oxvendor", "oxmanufacturers", "oxmediaurls",
01135                          "oxstates");
01136 
01137 
01138         $aMultiLangTables = $this->getConfig()->getConfigParam('aMultiLangTables');
01139 
01140         if (is_array($aMultiLangTables)) {
01141             $aTables = array_merge($aTables, $aMultiLangTables);
01142         }
01143 
01144         return $aTables;
01145     }
01146 
01154     public function getSeoReplaceChars($iLang)
01155     {
01156         // get language replace chars
01157         $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
01158         if (!is_array($aSeoReplaceChars)) {
01159             $aSeoReplaceChars = array();
01160         }
01161 
01162         return $aSeoReplaceChars;
01163     }
01164 
01170     protected function _getActiveModuleInfo()
01171     {
01172         if ($this->_aActiveModuleInfo === null) {
01173             $oModuleList = oxNew('oxModuleList');
01174             $this->_aActiveModuleInfo = $oModuleList->getActiveModuleInfo();
01175         }
01176 
01177         return $this->_aActiveModuleInfo;
01178     }
01179 
01185     protected function _getDisabledModuleInfo()
01186     {
01187         if ($this->_aDisabledModuleInfo === null) {
01188             $oModuleList = oxNew('oxModuleList');
01189             $this->_aDisabledModuleInfo = $oModuleList->getDisabledModuleInfo();
01190         }
01191 
01192         return $this->_aDisabledModuleInfo;
01193     }
01194 
01200     protected function _getBrowserLanguage()
01201     {
01202         $sBrowserLang = null;
01203         if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE']) {
01204             $sBrowserLang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
01205         }
01206 
01207         return $sBrowserLang;
01208     }
01209 
01215     public function getAllShopLanguageIds()
01216     {
01217         $aLanguages = $this->_getLanguageIdsFromDatabase();
01218 
01219         return $aLanguages;
01220     }
01221 
01229     public function getLanguageIds($iShopId = null)
01230     {
01231         if (empty($iShopId) || $iShopId == $this->getConfig()->getShopId()) {
01232             $aLanguages = $this->getActiveShopLanguageIds();
01233         } else {
01234             $aLanguages = $this->_getLanguageIdsFromDatabase($iShopId);
01235         }
01236 
01237         return $aLanguages;
01238     }
01239 
01245     public function getActiveShopLanguageIds()
01246     {
01247         $oConfig = $this->getConfig();
01248 
01249         //if exists language parameters array, extract lang id's from there
01250         $aLangParams = $oConfig->getConfigParam('aLanguageParams');
01251         if (is_array($aLangParams)) {
01252             $aIds = $this->_getLanguageIdsFromLanguageParamsArray($aLangParams);
01253         } else {
01254             $aIds = $this->_getLanguageIdsFromLanguagesArray($oConfig->getConfigParam('aLanguages'));
01255         }
01256 
01257         return $aIds;
01258     }
01259 
01267     protected function _getLanguageIdsFromDatabase($iShopId = null)
01268     {
01269         $aLanguages = $this->getLanguageIds();
01270 
01271 
01272         return $aLanguages;
01273     }
01274 
01283     protected function _getConfigLanguageValues($sLanguageParameterName, $iShopId = null)
01284     {
01285         $aConfigDecodedValues = array();
01286         $aConfigValues = $this->_selectLanguageParamValues($sLanguageParameterName, $iShopId);
01287 
01288         foreach ($aConfigValues as $sConfigValue) {
01289             $aConfigLanguages = unserialize($sConfigValue['oxvarvalue']);
01290 
01291             $aLanguages = array();
01292             if ($sLanguageParameterName == 'aLanguageParams') {
01293                 $aLanguages = $this->_getLanguageIdsFromLanguageParamsArray($aConfigLanguages);
01294             } elseif ($sLanguageParameterName == 'aLanguages') {
01295                 $aLanguages = $this->_getLanguageIdsFromLanguagesArray($aConfigLanguages);
01296             }
01297 
01298             $aConfigDecodedValues = array_unique(array_merge($aConfigDecodedValues, $aLanguages));
01299         }
01300 
01301         return $aConfigDecodedValues;
01302     }
01303 
01312     protected function _selectLanguageParamValues($sParamName, $sShopId = null)
01313     {
01314         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
01315         $oConfig = oxRegistry::getConfig();
01316 
01317         $sQuery = "
01318             select " . $oConfig->getDecodeValueQuery() . " as oxvarvalue
01319             from oxconfig
01320             where oxvarname = '{$sParamName}' ";
01321 
01322         if (!empty($sShopId)) {
01323             $sQuery .= " and oxshopid = '{$sShopId}' limit 1";
01324         }
01325 
01326         return $oDb->getArray($sQuery);
01327     }
01328 
01329 
01337     protected function _getLanguageIdsFromLanguageParamsArray($aLanguageParams)
01338     {
01339         $aLanguages = array();
01340         foreach ($aLanguageParams as $sAbbr => $aValue) {
01341             $iBaseId = (int) $aValue['baseId'];
01342             $aLanguages[$iBaseId] = $sAbbr;
01343         }
01344 
01345         return $aLanguages;
01346     }
01347 
01355     protected function _getLanguageIdsFromLanguagesArray($aLanguages)
01356     {
01357         return array_keys($aLanguages);
01358     }
01359 }