oxlang.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxLang extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     protected $_sName = 'lang';
00021 
00027     protected $_iBaseLanguageId = null;
00028 
00034     protected $_iTplLanguageId = null;
00035 
00041     protected $_iEditLanguageId = null;
00042 
00048     protected $_aLangCache = array();
00049 
00055     protected $_aAdminTplLanguageArray = null;
00056 
00062     protected $_aLangAbbr = null;
00063 
00069     protected $_aAdditionalLangFiles = array();
00070 
00076     protected $_aLangMap = array();
00077 
00083     protected $_aActiveModuleInfo = null;
00084 
00090     protected $_aDisabledModuleInfo = null;
00091 
00099     public static function getInstance()
00100     {
00101         return oxRegistry::getLang();
00102     }
00103 
00110     public function resetBaseLanguage()
00111     {
00112         $this->_iBaseLanguageId = null;
00113     }
00114 
00120     public function getBaseLanguage()
00121     {
00122         if ( $this->_iBaseLanguageId === null ) {
00123 
00124             $myConfig = $this->getConfig();
00125             $blAdmin = $this->isAdmin();
00126 
00127             // languages and search engines
00128             if ( $blAdmin && ( ( $iSeLang = oxConfig::getParameter( 'changelang' ) ) !== null ) ) {
00129                 $this->_iBaseLanguageId = $iSeLang;
00130             }
00131 
00132             if ( is_null( $this->_iBaseLanguageId ) ) {
00133                 $this->_iBaseLanguageId = oxConfig::getParameter( 'lang' );
00134             }
00135 
00136             //or determining by domain
00137             $aLanguageUrls = $myConfig->getConfigParam( 'aLanguageURLs' );
00138 
00139             if ( !$blAdmin && is_array( $aLanguageUrls ) ) {
00140                 foreach ( $aLanguageUrls as $iId => $sUrl ) {
00141                     if ( $sUrl && $myConfig->isCurrentUrl( $sUrl ) ) {
00142                         $this->_iBaseLanguageId = $iId;
00143                         break;
00144                     }
00145                 }
00146             }
00147 
00148             if ( is_null( $this->_iBaseLanguageId ) ) {
00149                 $this->_iBaseLanguageId = oxConfig::getParameter( 'language' );
00150                 if (!isset($this->_iBaseLanguageId)) {
00151                     $this->_iBaseLanguageId = oxSession::getVar('language');
00152                 }
00153             }
00154 
00155             // if language still not set and not search engine browsing,
00156             // getting language from browser
00157             if ( is_null( $this->_iBaseLanguageId ) && !$blAdmin && !oxRegistry::getUtils()->isSearchEngine() ) {
00158 
00159                 // getting from cookie
00160                 $this->_iBaseLanguageId = oxRegistry::get("oxUtilsServer")->getOxCookie( 'language' );
00161 
00162                 // getting from browser
00163                 if ( is_null( $this->_iBaseLanguageId ) ) {
00164                     $this->_iBaseLanguageId = $this->detectLanguageByBrowser();
00165                 }
00166             }
00167 
00168             if ( is_null( $this->_iBaseLanguageId ) ) {
00169                 $this->_iBaseLanguageId = $myConfig->getConfigParam( 'sDefaultLang' );
00170             }
00171 
00172             $this->_iBaseLanguageId = (int) $this->_iBaseLanguageId;
00173 
00174             // validating language
00175             $this->_iBaseLanguageId = $this->validateLanguage( $this->_iBaseLanguageId );
00176 
00177             oxRegistry::get("oxUtilsServer")->setOxCookie( 'language', $this->_iBaseLanguageId );
00178         }
00179 
00180         return $this->_iBaseLanguageId;
00181     }
00182 
00188     public function getObjectTplLanguage()
00189     {
00190         if ( $this->_iObjectTplLanguageId === null ) {
00191             $this->_iObjectTplLanguageId = $this->getTplLanguage();
00192             $aLanguages = $this->getAdminTplLanguageArray();
00193             if ( !isset( $aLanguages[$this->_iObjectTplLanguageId] ) ||
00194                  $aLanguages[$this->_iObjectTplLanguageId]->active == 0 ) {
00195                 $this->_iObjectTplLanguageId = key( $aLanguages );
00196             }
00197         }
00198         return $this->_iObjectTplLanguageId;
00199     }
00200 
00208     public function getTplLanguage()
00209     {
00210         if ( $this->_iTplLanguageId === null ) {
00211             $iSessLang = oxSession::getVar( 'tpllanguage' );
00212             $this->_iTplLanguageId = $this->isAdmin() ? $this->setTplLanguage( $iSessLang ) : $this->getBaseLanguage();
00213         }
00214         return $this->_iTplLanguageId;
00215     }
00216 
00222     public function getEditLanguage()
00223     {
00224         if ( $this->_iEditLanguageId === null ) {
00225 
00226             if ( !$this->isAdmin() ) {
00227                 $this->_iEditLanguageId = $this->getBaseLanguage();
00228             } else {
00229 
00230                 $iLang = null;
00231                 // choosing language ident
00232                 // check if we really need to set the new language
00233                 if ( "saveinnlang" == $this->getConfig()->getActiveView()->getFncName() ) {
00234                     $iLang = oxConfig::getParameter( "new_lang");
00235                 }
00236                 $iLang = ( $iLang === null ) ? oxConfig::getParameter( 'editlanguage' ) : $iLang;
00237                 $iLang = ( $iLang === null ) ? oxSession::getVar( 'editlanguage' ) : $iLang;
00238                 $iLang = ( $iLang === null ) ? $this->getBaseLanguage() : $iLang;
00239 
00240                 // validating language
00241                 $this->_iEditLanguageId = $this->validateLanguage( $iLang );
00242 
00243                 // writing to session
00244                 oxSession::setVar( 'editlanguage', $this->_iEditLanguageId );
00245             }
00246         }
00247         return $this->_iEditLanguageId;
00248     }
00249 
00259     public function getLanguageArray( $iLanguage = null, $blOnlyActive = false, $blSort = false )
00260     {
00261         $myConfig = $this->getConfig();
00262 
00263         if ( is_null($iLanguage) ) {
00264             $iLanguage = $this->_iBaseLanguageId;
00265         }
00266 
00267         $aLanguages = array();
00268         $aConfLanguages = $myConfig->getConfigParam( 'aLanguages' );
00269         $aLangParams    = $myConfig->getConfigParam( 'aLanguageParams' );
00270 
00271         if ( is_array( $aConfLanguages ) ) {
00272             $i = 0;
00273             reset( $aConfLanguages );
00274             while ( list( $key, $val ) = each( $aConfLanguages ) ) {
00275 
00276                 if ( $blOnlyActive && is_array($aLangParams) ) {
00277                     //skipping non active languages
00278                     if ( !$aLangParams[$key]['active'] ) {
00279                         $i++;
00280                         continue;
00281                     }
00282                 }
00283 
00284                 if ( $val ) {
00285                     $oLang = new stdClass();
00286                     $oLang->id   = isset($aLangParams[$key]['baseId']) ? $aLangParams[$key]['baseId'] : $i;
00287                     $oLang->oxid = $key;
00288                     $oLang->abbr = $key;
00289                     $oLang->name = $val;
00290 
00291                     if ( is_array( $aLangParams ) ) {
00292                         $oLang->active = $aLangParams[$key]['active'];
00293                         $oLang->sort   = $aLangParams[$key]['sort'];
00294                     }
00295 
00296                     $oLang->selected = ( isset( $iLanguage ) && $oLang->id == $iLanguage ) ? 1 : 0;
00297                     $aLanguages[$oLang->id] = $oLang;
00298                 }
00299                 ++$i;
00300             }
00301         }
00302 
00303         if ( $blSort && is_array($aLangParams) ) {
00304             uasort( $aLanguages, array($this, '_sortLanguagesCallback') );
00305         }
00306         return $aLanguages;
00307     }
00308 
00314     public function getAdminTplLanguageArray()
00315     {
00316         if ( $this->_aAdminTplLanguageArray === null ) {
00317             $myConfig = $this->getConfig();
00318 
00319             $aLangArray  = $this->getLanguageArray();
00320             $this->_aAdminTplLanguageArray = array();
00321 
00322             $sSourceDir = $myConfig->getAppDir() . 'views/admin/';
00323             foreach ( $aLangArray as $iLangKey => $oLang ) {
00324                 $sFilePath = "{$sSourceDir}{$oLang->abbr}/lang.php";
00325                 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00326                     $this->_aAdminTplLanguageArray[$iLangKey] = $oLang;
00327                 }
00328             }
00329         }
00330 
00331         // moving pointer to beginning
00332         reset( $this->_aAdminTplLanguageArray );
00333         return $this->_aAdminTplLanguageArray;
00334     }
00335 
00343     public function getLanguageAbbr( $iLanguage = null )
00344     {
00345         if ( $this->_aLangAbbr === null ) {
00346             $this->_aLangAbbr = $this->getLanguageIds();
00347         }
00348 
00349         $iLanguage = isset( $iLanguage ) ? (int) $iLanguage : $this->getBaseLanguage();
00350         if ( isset( $this->_aLangAbbr[$iLanguage] ) ) {
00351             $iLanguage = $this->_aLangAbbr[$iLanguage];
00352         }
00353 
00354         return $iLanguage;
00355     }
00356 
00363     public function getLanguageNames()
00364     {
00365         $aConfLanguages = $this->getConfig()->getConfigParam( 'aLanguages' );
00366         $aLangIds = $this->getLanguageIds();
00367         $aLanguages = array();
00368         foreach ( $aLangIds as $iId => $sValue ) {
00369             $aLanguages[$iId] = $aConfLanguages[$sValue];
00370         }
00371         return $aLanguages;
00372     }
00373 
00386     public function translateString( $sStringToTranslate, $iLang = null, $blAdminMode = null )
00387     {
00388         // checking if in cache exist
00389         $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode );
00390         if ( isset( $aLang[$sStringToTranslate] ) ) {
00391             return $aLang[$sStringToTranslate];
00392         }
00393 
00394         // checking if in map exist
00395         $aMap = $this->_getLanguageMap( $iLang, $blAdminMode );
00396         if ( isset( $aLang[$aMap[$sStringToTranslate]] ) ) {
00397             return $aLang[$aMap[$sStringToTranslate]];
00398         }
00399 
00400         // checking if in theme options exist
00401         if (count($this->_aAdditionalLangFiles)) {
00402             $aLang = $this->_getLangTranslationArray( $iLang, $blAdminMode, $this->_aAdditionalLangFiles);
00403             if (isset( $aLang[$sStringToTranslate] )) {
00404                 return $aLang[$sStringToTranslate];
00405             }
00406         }
00407 
00408         return $sStringToTranslate;
00409     }
00410 
00421     protected function _collectSimilar( $aData, $sKey, $aCollection = array() )
00422     {
00423         foreach ( $aData as $sValKey => $sValue ) {
00424             if ( strpos( $sValKey, $sKey ) === 0 ) {
00425                 $aCollection[$sValKey] = $sValue;
00426             }
00427         }
00428 
00429         return $aCollection;
00430     }
00431 
00442     public function getSimilarByKey( $sKey, $iLang = null, $blAdmin = null )
00443     {
00444         startProfile("getSimilarByKey");
00445 
00446         $iLang   = isset( $iLang ) ? $iLang : $this->getTplLanguage();
00447         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00448 
00449         // checking if exists in cache
00450         $aLang = $this->_getLangTranslationArray( $iLang, $blAdmin );
00451         $aSimilarConst = $this->_collectSimilar( $aLang, $sKey );
00452 
00453         // checking if in map exist
00454         $aMap = $this->_getLanguageMap( $iLang, $blAdmin );
00455         $aSimilarConst = $this->_collectSimilar( $aMap, $sKey, $aSimilarConst );
00456 
00457         // checking if in theme options exist
00458         if ( count( $this->_aAdditionalLangFiles ) ) {
00459             $aLang = $this->_getLangTranslationArray( $iLang, $blAdmin, $this->_aAdditionalLangFiles);
00460             $aSimilarConst = $this->_collectSimilar( $aLang, $sKey, $aSimilarConst );
00461         }
00462 
00463         stopProfile("getSimilarByKey");
00464 
00465         return $aSimilarConst;
00466     }
00467 
00476     public function formatCurrency( $dValue, $oActCur = null )
00477     {
00478         if ( !$oActCur ) {
00479             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00480         }
00481         $sValue = oxRegistry::getUtils()->fRound( $dValue, $oActCur );
00482         return number_format( (double)$sValue, $oActCur->decimal, $oActCur->dec, $oActCur->thousand );
00483     }
00484 
00493     public function formatVat( $dValue, $oActCur = null )
00494     {
00495         $iDecPos = 0;
00496         $sValue  = ( string ) $dValue;
00497         $oStr = getStr();
00498         if ( ( $iDotPos = $oStr->strpos( $sValue, '.' ) ) !== false ) {
00499             $iDecPos = $oStr->strlen( $oStr->substr( $sValue, $iDotPos + 1 ) );
00500         }
00501 
00502         $oActCur = $oActCur ? $oActCur : $this->getConfig()->getActShopCurrencyObject();
00503         $iDecPos = ( $iDecPos < $oActCur->decimal ) ? $iDecPos : $oActCur->decimal;
00504         return number_format( (double)$dValue, $iDecPos, $oActCur->dec, $oActCur->thousand );
00505     }
00506 
00514     public function getLanguageTag( $iLanguage = null)
00515     {
00516         if ( !isset( $iLanguage ) ) {
00517             $iLanguage = $this->getBaseLanguage();
00518         }
00519 
00520         $iLanguage = (int) $iLanguage;
00521 
00522         return ( ( $iLanguage )?"_$iLanguage":"" );
00523     }
00524 
00532     public function validateLanguage( $iLang = null )
00533     {
00534         $iLang = (int) $iLang;
00535 
00536         // checking if this language is valid
00537         $aLanguages = $this->getLanguageArray( null, !$this->isAdmin() );
00538         if ( !isset( $aLanguages[$iLang] ) && is_array( $aLanguages ) ) {
00539             $oLang = current( $aLanguages );
00540             if (isset($oLang->id)) {
00541                 $iLang = $oLang->id;
00542             }
00543         }
00544 
00545         return $iLang;
00546     }
00547 
00555     public function setBaseLanguage( $iLang = null )
00556     {
00557         if ( is_null($iLang) ) {
00558             $iLang = $this->getBaseLanguage();
00559         } else {
00560             $this->_iBaseLanguageId = (int) $iLang;
00561         }
00562 
00563         if ( defined( 'OXID_PHP_UNIT' ) ) {
00564             modSession::getInstance();
00565         }
00566 
00567         oxSession::setVar( 'language', $iLang );
00568     }
00569 
00577     public function setTplLanguage( $iLang = null )
00578     {
00579         $this->_iTplLanguageId = isset( $iLang ) ? (int) $iLang : $this->getBaseLanguage();
00580         if ( $this->isAdmin() ) {
00581             $aLanguages = $this->getAdminTplLanguageArray();
00582             if ( !isset( $aLanguages[$this->_iTplLanguageId] ) ) {
00583                 $this->_iTplLanguageId = key( $aLanguages );
00584             }
00585         }
00586 
00587         if ( defined( 'OXID_PHP_UNIT' ) ) {
00588             modSession::getInstance();
00589         }
00590 
00591         oxSession::setVar( 'tpllanguage', $this->_iTplLanguageId );
00592         return $this->_iTplLanguageId;
00593     }
00594 
00604     protected function _recodeLangArray( $aLangArray, $sCharset, $blRecodeKeys = false )
00605     {
00606         $aLangs = array();
00607         foreach ( $aLangArray as $sKey => $sValue ) {
00608             $sItemKey = $sKey;
00609             if ($blRecodeKeys === true) {
00610                 $sItemKey = iconv($sCharset, 'UTF-8', $sItemKey);
00611             }
00612 
00613             $aLangs[$sItemKey] = iconv( $sCharset, 'UTF-8', $sValue );
00614             unset($aLangArray[$sKey]);
00615         }
00616 
00617         return $aLangs;
00618     }
00619 
00627     protected function _getLangFilesPathArray( $iLang )
00628     {
00629         $oConfig = $this->getConfig();
00630         $aLangFiles = array();
00631 
00632         $sAppDir        = $oConfig->getAppDir();
00633         $sLang          = oxRegistry::getLang()->getLanguageAbbr( $iLang );
00634         $sTheme         = $oConfig->getConfigParam( "sTheme" );
00635         $sCustomTheme   = $oConfig->getConfigParam( "sCustomTheme" );
00636         $sShopId        = $oConfig->getShopId();
00637         $aModulePaths   = $this->_getActiveModuleInfo();
00638 
00639         //get generic lang files
00640         $sGenericPath = $sAppDir . 'translations/' . $sLang;
00641         if ( $sGenericPath ) {
00642             $aLangFiles[] = $sGenericPath . "/lang.php";
00643             $aLangFiles = $this->_appendLangFile( $aLangFiles, $sGenericPath );
00644         }
00645 
00646         //get theme lang files
00647         if ( $sTheme ) {
00648             $sThemePath = $sAppDir . 'views/' . $sTheme .'/' . $sLang;
00649             $aLangFiles[] = $sThemePath . "/lang.php";
00650             $aLangFiles = $this->_appendLangFile( $aLangFiles, $sThemePath );
00651         }
00652 
00653         //get custom theme lang files
00654         if ( $sCustomTheme ) {
00655             $sCustPath = $sAppDir . 'views/' . $sCustomTheme .'/' . $sLang;
00656             $aLangFiles[] = $sCustPath . "/lang.php";
00657             $aLangFiles = $this->_appendLangFile( $aLangFiles, $sCustPath );
00658         }
00659 
00660 
00661         // custom theme shop languages
00662 
00663         // modules language files
00664         $aLangFiles = $this->_appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang );
00665 
00666         // custom language files
00667         $aLangFiles = $this->_appendCustomLangFiles( $aLangFiles, $sLang );
00668 
00669         return count( $aLangFiles ) ? $aLangFiles : false;
00670     }
00671 
00679     protected function _getAdminLangFilesPathArray( $iLang )
00680     {
00681         $oConfig = $this->getConfig();
00682         $aLangFiles = array();
00683 
00684         $sAppDir        = $oConfig->getAppDir();
00685         $sLang          = oxRegistry::getLang()->getLanguageAbbr( $iLang );
00686 
00687         $aModulePaths = array();
00688         $aModulePaths = array_merge( $aModulePaths, $this->_getActiveModuleInfo() );
00689         $aModulePaths = array_merge( $aModulePaths, $this->_getDisabledModuleInfo() );
00690 
00691         // admin lang files
00692         $sAdminPath = $sAppDir . 'views/admin/' . $sLang;
00693         $aLangFiles[] = $sAdminPath . "/lang.php";
00694         $aLangFiles[] = $sAppDir . 'translations/' . $sLang . '/translit_lang.php';
00695         $aLangFiles = $this->_appendLangFile( $aLangFiles, $sAdminPath );
00696 
00697         // themes options lang files
00698         $sThemePath = $sAppDir . 'views/*/' . $sLang;
00699         $aLangFiles = $this->_appendLangFile( $aLangFiles, $sThemePath, "options" );
00700 
00701         // module language files
00702         $aLangFiles = $this->_appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang, true );
00703 
00704         // custom language files
00705         $aLangFiles = $this->_appendCustomLangFiles( $aLangFiles, $sLang, true );
00706 
00707         return count( $aLangFiles ) ? $aLangFiles : false;
00708     }
00709 
00719     protected function _appendLangFile( $aLangFiles, $sFullPath, $sFilePattern = "lang" )
00720     {
00721         $aFiles = glob( $sFullPath . "/*_{$sFilePattern}.php" );
00722         if ( is_array( $aFiles ) && count( $aFiles ) ) {
00723             foreach ( $aFiles as $sFile ) {
00724                 if ( !strpos( $sFile, 'cust_lang.php' ) ) {
00725                     $aLangFiles[] = $sFile;
00726                 }
00727             }
00728         }
00729         return $aLangFiles;
00730     }
00731 
00741     protected function _appendCustomLangFiles( $aLangFiles, $sLang, $blForAdmin = false  )
00742     {
00743         $oConfig      = $this->getConfig();
00744         $sAppDir      = $oConfig->getAppDir();
00745         $sTheme       = $oConfig->getConfigParam( "sTheme" );
00746         $sCustomTheme = $oConfig->getConfigParam( "sCustomTheme" );
00747 
00748         if ( $blForAdmin ) {
00749             $aLangFiles[] = $sAppDir . 'views/admin/' . $sLang . '/cust_lang.php';
00750         } else {
00751             if ( $sTheme ) {
00752                 $aLangFiles[] = $sAppDir . 'views/' . $sTheme . '/' . $sLang . '/cust_lang.php';
00753             }
00754             if ( $sCustomTheme ) {
00755                 $aLangFiles[] = $sAppDir . 'views/' . $sCustomTheme . '/' . $sLang . '/cust_lang.php';
00756             }
00757         }
00758 
00759         return $aLangFiles;
00760     }
00761 
00772     protected function _appendModuleLangFiles( $aLangFiles, $aModulePaths, $sLang, $blForAdmin = false )
00773     {
00774         if ( is_array( $aModulePaths ) ) {
00775 
00776             $oConfig = $this->getConfig();
00777 
00778             foreach ( $aModulePaths as $sPath ) {
00779                 $sFullPath  = $oConfig->getModulesDir() . $sPath;
00780                 $sFullPath .= ($blForAdmin) ? '/views/admin/' : '/translations/';
00781                 $sFullPath .= $sLang;
00782                 //@deprecated since v4.7.1/5.0.1 (2012-11-20); for < 4.6 modules, since 4.7/5.0 translation files should be in modules/modulepath/views/admin/ dir
00783                 if ( !is_dir( $sFullPath ) ) {
00784                     $sFullPath = $oConfig->getModulesDir() . $sPath;
00785                     $sFullPath .= ($blForAdmin) ? '/out/admin/' : '/out/lang/';
00786                     $sFullPath .= $sLang;
00787                 }
00788                 // END deprecated
00789                 $aLangFiles = $this->_appendLangFile( $aLangFiles, $sFullPath );
00790                 //load admin modules options lang files
00791                 if ( $blForAdmin ) {
00792                     $aLangFiles[] = $sFullPath . '/module_options.php';
00793                 }
00794             }
00795         }
00796 
00797         return $aLangFiles;
00798     }
00799 
00809     protected function _getLangFileCacheName( $blAdmin, $iLang, $aLangFiles = null )
00810     {
00811         $myConfig = $this->getConfig();
00812         $sLangFilesIdent = '_default';
00813         if (is_array($aLangFiles) && $aLangFiles) {
00814             $sLangFilesIdent = '_'.md5(implode('+', $aLangFiles));
00815         }
00816         return "langcache_" . ( (int) $blAdmin ) . "_{$iLang}_" . $myConfig->getShopId() . "_" . $myConfig->getConfigParam( 'sTheme' ).$sLangFilesIdent;
00817     }
00818 
00828     protected function _getLanguageFileData( $blAdmin = false, $iLang = 0, $aLangFiles = null )
00829     {
00830         $myConfig = $this->getConfig();
00831         $myUtils  = oxRegistry::getUtils();
00832 
00833         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00834         $aLangCache = $myUtils->getLangCache( $sCacheName );
00835         if ( !$aLangCache && $aLangFiles === null ) {
00836             if ( $blAdmin ) {
00837                 $aLangFiles = $this->_getAdminLangFilesPathArray( $iLang );
00838             } else {
00839                 $aLangFiles = $this->_getLangFilesPathArray( $iLang );
00840             }
00841         }
00842         if ( !$aLangCache && $aLangFiles ) {
00843             $aLangCache = array();
00844             $sBaseCharset = false;
00845             $aLang = array();
00846             $aLangSeoReplaceChars = array();
00847             foreach ( $aLangFiles as $sLangFile ) {
00848 
00849                 if ( file_exists( $sLangFile ) && is_readable( $sLangFile ) ) {
00850                     $aSeoReplaceChars = array();
00851                     include $sLangFile;
00852 
00853                     // including only (!) those, which has charset defined
00854                     if ( isset( $aLang['charset'] ) ) {
00855 
00856                         // recoding only in utf
00857                         if ( $myConfig->isUtf() ) {
00858                             $aLang = $this->_recodeLangArray( $aLang, $aLang['charset'] );
00859 
00860                             if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00861                                 $aSeoReplaceChars = $this->_recodeLangArray( $aSeoReplaceChars, $aLang['charset'], true );
00862                             }
00863 
00864                             // overriding charset
00865                             $aLang['charset'] = 'UTF-8';
00866                         }
00867 
00868                         if (isset($aSeoReplaceChars) && is_array($aSeoReplaceChars)) {
00869                             $aLangSeoReplaceChars = array_merge($aLangSeoReplaceChars, $aSeoReplaceChars);
00870                         }
00871 
00872                         if ( !$sBaseCharset ) {
00873                             $sBaseCharset = $aLang['charset'];
00874                         }
00875 
00876                         $aLangCache = array_merge( $aLangCache, $aLang );
00877                     }
00878                 }
00879             }
00880 
00881             // setting base charset
00882             if ( $sBaseCharset ) {
00883                 $aLangCache['charset'] = $sBaseCharset;
00884             }
00885 
00886             // special character replacement list
00887             $aLangCache['_aSeoReplaceChars'] = $aLangSeoReplaceChars;
00888 
00889             //save to cache
00890             $myUtils->setLangCache( $sCacheName, $aLangCache );
00891         }
00892 
00893         return $aLangCache;
00894     }
00895 
00904     protected function _getLanguageMap( $iLang, $blAdmin = null )
00905     {
00906         $blAdmin = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00907         $sKey = $iLang . ( (int) $blAdmin );
00908         if ( !isset( $this->_aLangMap[$sKey] ) ) {
00909             $this->_aLangMap[$sKey] = array();
00910             $myConfig = $this->getConfig();
00911 
00912             $sMapFile = '';
00913             $sParentMapFile = $myConfig->getAppDir() . '/views/' .  ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sTheme" ) ) .'/' . oxRegistry::getLang()->getLanguageAbbr( $iLang ) . '/map.php';
00914             $sCustomThemeMapFile = $myConfig->getAppDir() . '/views/' .  ( $blAdmin ? 'admin' : $myConfig->getConfigParam( "sCustomTheme" ) ) .'/' . oxRegistry::getLang()->getLanguageAbbr( $iLang ) . '/map.php';
00915 
00916             if ( file_exists( $sCustomThemeMapFile ) && is_readable( $sCustomThemeMapFile ) ) {
00917                 $sMapFile = $sCustomThemeMapFile;
00918             } elseif ( file_exists( $sParentMapFile ) && is_readable( $sParentMapFile ) ) {
00919                 $sMapFile = $sParentMapFile;
00920             }
00921 
00922             if ( $sMapFile ) {
00923                 include $sMapFile;
00924                 $this->_aLangMap[$sKey] = $aMap;
00925             }
00926 
00927         }
00928 
00929         return $this->_aLangMap[$sKey];
00930     }
00931 
00940     protected function _getCacheLanguageId( $blAdmin, $iLang = null )
00941     {
00942         $iLang = ( $iLang === null && $blAdmin ) ? $this->getTplLanguage() : $iLang;
00943         if ( !isset( $iLang ) ) {
00944             $iLang = $this->getBaseLanguage();
00945             if ( !isset( $iLang ) ) {
00946                 $iLang = 0;
00947             }
00948         }
00949 
00950         return (int) $iLang;
00951     }
00952 
00962     protected function _getLangTranslationArray( $iLang = null, $blAdmin = null, $aLangFiles = null )
00963     {
00964         startProfile("_getLangTranslationArray");
00965 
00966         $blAdmin    = isset( $blAdmin ) ? $blAdmin : $this->isAdmin();
00967         $iLang      = $this->_getCacheLanguageId( $blAdmin, $iLang );
00968         $sCacheName = $this->_getLangFileCacheName( $blAdmin, $iLang, $aLangFiles );
00969 
00970         if ( !isset( $this->_aLangCache[$sCacheName] ) ) {
00971             $this->_aLangCache[$sCacheName] = array();
00972         }
00973         if ( !isset( $this->_aLangCache[$sCacheName][$iLang] ) ) {
00974             // loading main lang files data
00975             $this->_aLangCache[$sCacheName][$iLang] = $this->_getLanguageFileData( $blAdmin, $iLang, $aLangFiles );
00976         }
00977 
00978         stopProfile("_getLangTranslationArray");
00979 
00980         // if language array exists ..
00981         return ( isset( $this->_aLangCache[$sCacheName][$iLang] ) ? $this->_aLangCache[$sCacheName][$iLang] : array() );
00982     }
00983 
00992     protected function _sortLanguagesCallback( $a1, $a2 )
00993     {
00994         return ($a1->sort > $a2->sort);
00995     }
00996 
01002     public function getName()
01003     {
01004         return $this->_sName;
01005     }
01006 
01012     public function getFormLang()
01013     {
01014         $sLang = null;
01015         if ( !$this->isAdmin()) {
01016             $sLang = "<input type=\"hidden\" name=\"".$this->getName()."\" value=\"". $this->getBaseLanguage() . "\" />";
01017         }
01018         return $sLang;
01019     }
01020 
01028     public function getUrlLang( $iLang = null )
01029     {
01030         $sLang = null;
01031         if ( !$this->isAdmin()) {
01032             $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01033             $sLang = $this->getName()."=". $iLang;
01034         }
01035         return $sLang;
01036     }
01037 
01050     public function processUrl( $sUrl, $iLang = null )
01051     {
01052         $iLang = isset( $iLang ) ? $iLang : $this->getBaseLanguage();
01053         $oStr = getStr();
01054 
01055         if ( !$this->isAdmin() ) {
01056             $sParam = $this->getUrlLang( $iLang );
01057             if ( !$oStr->preg_match('/(\?|&(amp;)?)lang=[0-9]+/', $sUrl)  && ($iLang != oxRegistry::getConfig()->getConfigParam( 'sDefaultLang' ))) {
01058                 if ( $sUrl ) {
01059                     if ($oStr->strpos( $sUrl, '?') === false) {
01060                         $sUrl .= "?";
01061                     } elseif ( !$oStr->preg_match('/(\?|&(amp;)?)$/', $sUrl ) ) {
01062                         $sUrl .= "&amp;";
01063                     }
01064                 }
01065                 $sUrl .= $sParam."&amp;";
01066             } else {
01067                 $sUrl = getStr()->preg_replace('/(\?|&(amp;)?)lang=[0-9]+/', '\1'.$sParam, $sUrl);
01068             }
01069         }
01070 
01071         return $sUrl;
01072     }
01073 
01080     public function detectLanguageByBrowser()
01081     {
01082         $sBrowserLang = strtolower( substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 ) );
01083 
01084         if ( !$sBrowserLang ) {
01085             return;
01086         }
01087 
01088         $aLangs = $this->getLanguageArray(null, true );
01089 
01090         foreach ( $aLangs as $oLang ) {
01091             if ( $oLang->abbr == $sBrowserLang ) {
01092                 return (int) $oLang->id;
01093             }
01094         }
01095     }
01096 
01102     public function getMultiLangTables()
01103     {
01104         $aTables = array( "oxarticles", "oxartextends", "oxattribute",
01105                           "oxcategories", "oxcontents", "oxcountry",
01106                           "oxdelivery", "oxdiscount", "oxgroups",
01107                           "oxlinks", "oxnews", "oxobject2attribute",
01108                           "oxpayments", "oxselectlist", "oxshops",
01109                           "oxactions", "oxwrapping", "oxdeliveryset",
01110                           "oxvendor", "oxmanufacturers", "oxmediaurls",
01111                           "oxstates" );
01112 
01113 
01114         $aMultiLangTables = $this->getConfig()->getConfigParam( 'aMultiLangTables' );
01115 
01116         if ( is_array( $aMultiLangTables ) ) {
01117             $aTables = array_merge($aTables, $aMultiLangTables);
01118         }
01119 
01120         return $aTables;
01121     }
01122 
01130     public function getSeoReplaceChars($iLang)
01131     {
01132         // get language replace chars
01133         $aSeoReplaceChars = $this->translateString('_aSeoReplaceChars', $iLang);
01134         if (!is_array($aSeoReplaceChars)) {
01135             $aSeoReplaceChars = array();
01136         }
01137 
01138         return $aSeoReplaceChars;
01139     }
01140 
01146     protected function _getActiveModuleInfo()
01147     {
01148         if ($this->_aActiveModuleInfo === null) {
01149             $oModuleList = oxNew('oxModuleList');
01150             $this->_aActiveModuleInfo = $oModuleList->getActiveModuleInfo();
01151         }
01152         return $this->_aActiveModuleInfo;
01153     }
01154 
01160     protected function _getDisabledModuleInfo()
01161     {
01162         if ($this->_aDisabledModuleInfo === null) {
01163             $oModuleList = oxNew('oxModuleList');
01164             $this->_aDisabledModuleInfo = $oModuleList->getDisabledModuleInfo();
01165         }
01166         return $this->_aDisabledModuleInfo;
01167     }
01168 
01174     public function getAllShopLanguageIds()
01175     {
01176         $aLanguages = $this->_getLanguageIdsFromDatabase();
01177 
01178         return $aLanguages;
01179     }
01180 
01188     public function getLanguageIds( $iShopId = null )
01189     {
01190         if ( empty( $iShopId ) || $iShopId == $this->getConfig()->getShopId() ) {
01191             $aLanguages = $this->getActiveShopLanguageIds();
01192         } else {
01193             $aLanguages = $this->_getLanguageIdsFromDatabase( $iShopId );
01194         }
01195 
01196         return $aLanguages;
01197     }
01198 
01204     public function getActiveShopLanguageIds()
01205     {
01206         $oConfig = $this->getConfig();
01207 
01208         //if exists language parameters array, extract lang id's from there
01209         $aLangParams = $oConfig->getConfigParam( 'aLanguageParams' );
01210         if ( is_array( $aLangParams ) ) {
01211             $aIds = $this->_getLanguageIdsFromLanguageParamsArray( $aLangParams );
01212         } else {
01213             $aIds = $this->_getLanguageIdsFromLanguagesArray( $oConfig->getConfigParam( 'aLanguages' ) );
01214         }
01215 
01216         return $aIds;
01217     }
01218 
01226     protected function _getLanguageIdsFromDatabase( $iShopId = null )
01227     {
01228             $aLanguages = $this->getLanguageIds();
01229 
01230 
01231         return $aLanguages;
01232     }
01233 
01242     protected function _getConfigLanguageValues( $sLanguageParameterName, $iShopId = null )
01243     {
01244         $aConfigDecodedValues = array();
01245         $aConfigValues = $this->_selectLanguageParamValues( $sLanguageParameterName, $iShopId );
01246 
01247         foreach ( $aConfigValues as $sConfigValue )
01248         {
01249             $aConfigLanguages = unserialize( $sConfigValue['oxvarvalue'] );
01250 
01251             $aLanguages = array();
01252             if ( $sLanguageParameterName == 'aLanguageParams' ) {
01253                 $aLanguages = $this->_getLanguageIdsFromLanguageParamsArray( $aConfigLanguages );
01254             } elseif ( $sLanguageParameterName == 'aLanguages' ) {
01255                 $aLanguages = $this->_getLanguageIdsFromLanguagesArray( $aConfigLanguages );
01256             }
01257 
01258             $aConfigDecodedValues = array_unique( array_merge( $aConfigDecodedValues, $aLanguages ) );
01259         }
01260 
01261         return $aConfigDecodedValues;
01262     }
01263 
01272     protected function _selectLanguageParamValues( $sParamName, $sShopId = null )
01273     {
01274         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01275         $oConfig = oxRegistry::getConfig();
01276 
01277         $sQuery = "
01278             select ".$oConfig->getDecodeValueQuery()." as oxvarvalue
01279             from oxconfig
01280             where oxvarname = '{$sParamName}' ";
01281 
01282         if ( !empty( $sShopId ) ) {
01283             $sQuery .= " and oxshopid = '{$sShopId}' limit 1";
01284         }
01285 
01286         return $oDb->getArray( $sQuery );
01287     }
01288 
01289 
01297     protected function _getLanguageIdsFromLanguageParamsArray( $aLanguageParams )
01298     {
01299         $aLanguages = array();
01300         foreach ( $aLanguageParams as $sAbbr => $aValue ) {
01301             $iBaseId = (int) $aValue[ 'baseId' ];
01302             $aLanguages[ $iBaseId ] = $sAbbr;
01303         }
01304 
01305         return $aLanguages;
01306     }
01307 
01315     protected function _getLanguageIdsFromLanguagesArray( $aLanguages )
01316     {
01317         return array_keys( $aLanguages );
01318     }
01319 
01320 }