language_main.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class Language_Main extends oxAdminDetails
00008 {
00009 
00015     protected $_aLangData = null;
00016 
00022     protected $_aLangParams = null;
00023 
00029     protected $_aLanguagesUrls = null;
00030 
00036     protected $_aLanguagesSslUrls = null;
00037 
00045     public function render()
00046     {
00047         $myConfig = $this->getConfig();
00048 
00049 
00050         parent::render();
00051 
00052         $sOxId = oxConfig::getParameter( "oxid");
00053         // check if we right now saved a new entry
00054         $sSavedID = oxConfig::getParameter( "saved_oxid");
00055         if ( isset( $sSavedID) ) {
00056             $sOxId = $sSavedID;
00057             oxSession::deleteVar( "saved_oxid");
00058             $this->_aViewData["oxid"] =  $sOxId;
00059             // for reloading upper frame
00060             $this->_aViewData["updatelist"] =  "1";
00061         }
00062 
00063         //loading languages info from config
00064         $this->_loadLanguages();
00065 
00066         if ( $sOxId != -1 ) {
00067             //checking if translations files and DB multilanguage fields exists
00068             $this->_checkLangTranslations( $sOxId );
00069             $this->_checkMultiLangDbFields( $sOxId );
00070         }
00071 
00072         if ( $sOxId != "-1" && isset( $sOxId)) {
00073             $this->_aViewData["edit"] =  $this->_getLanguageInfo( $sOxId );
00074         }
00075 
00076         return "language_main.tpl";
00077     }
00078 
00084     public function save()
00085     {
00086         $myConfig  = $this->getConfig();
00087 
00088 
00089         $sOxId   = oxConfig::getParameter( "oxid");
00090         $aParams = oxConfig::getParameter( "editval" );
00091 
00092         if ( !isset( $aParams['active'])) {
00093             $aParams['active'] = 0;
00094         }
00095 
00096         if ( !isset( $aParams['default'])) {
00097             $aParams['default'] = false;
00098         }
00099 
00100         if ( empty( $aParams['sort'])) {
00101             $aParams['sort'] = '99999';
00102         }
00103 
00104         //loading languages info from config
00105         $this->_loadLanguages();
00106 
00107         // if changed language abbervation, updating it for all arrays related with languages
00108         if ( $sOxId != -1 && $sOxId  != $aParams['abbr'] ) {
00109             $this->_updateAbbervation( $sOxId, $aParams['abbr'] );
00110             $sOxId = $aParams['abbr'];
00111             oxSession::setVar( "saved_oxid", $sOxId);
00112         }
00113 
00114         // if adding new language, setting lang id to abbervation
00115         if ( $sOxId == -1 ) {
00116             $sOxId = $aParams['abbr'];
00117             $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00118              oxSession::setVar( "saved_oxid", $sOxId);
00119         }
00120 
00121         //updating language description
00122         $this->_aLangData['lang'][$sOxId]  = $aParams['desc'];
00123 
00124         //updating language parameters
00125         $this->_aLangData['params'][$sOxId]['active']  = $aParams['active'];
00126         $this->_aLangData['params'][$sOxId]['default'] = $aParams['default'];
00127         $this->_aLangData['params'][$sOxId]['sort']   = $aParams['sort'];
00128 
00129         //if setting lang as default
00130         if ( $aParams['default'] == '1' ) {
00131             $this->_setDefaultLang( $sOxId );
00132         }
00133 
00134         //updating language urls
00135         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00136         $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
00137         $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
00138 
00139         //sort parameters, urls and languages arrays by language base id
00140         $this->_sortLangArraysByBaseId();
00141 
00142         $this->_aViewData["updatelist"] = "1";
00143 
00144         //saving languages info
00145         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguageParams', serialize($this->_aLangData['params']) );
00146         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguages', serialize($this->_aLangData['lang']) );
00147         $this->getConfig()->saveShopConfVar( 'arr',  'aLanguageURLs', serialize($this->_aLangData['urls']) );
00148         $this->getConfig()->saveShopConfVar( 'arr',  'aLanguageSSLURLs', serialize($this->_aLangData['sslUrls']) );
00149 
00150         //updating oConfig object
00151         $this->getConfig()->setConfigParam( 'aLanguageParams', $this->_aLangData['params'] );
00152         $this->getConfig()->setConfigParam( 'aLanguages', $this->_aLangData['lang'] );
00153         $this->getConfig()->setConfigParam( 'aLanguageURLs', $this->_aLangData['urls'] );
00154         $this->getConfig()->setConfigParam( 'aLanguageSSLURLs', $this->_aLangData['sslUrls'] );
00155     }
00156 
00164     protected function _getLanguageInfo( $sOxId )
00165     {
00166         $sDefaultLang = $this->getConfig()->getConfigParam( 'sDefaultLang' );
00167 
00168         $aLangData               = $this->_aLangData['params'][$sOxId];
00169         $aLangData['abbr']       = $sOxId;
00170         $aLangData['desc']       = $this->_aLangData['lang'][$sOxId];
00171         $aLangData['baseurl']    = $this->_aLangData['urls'][$aLangData['baseId']];
00172         $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
00173         $aLangData['default']    = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
00174 
00175         return $aLangData;
00176     }
00177 
00184     protected function _loadLanguages()
00185     {
00186         $this->_aLangData['params']  = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00187         $this->_aLangData['lang']    = $this->getConfig()->getConfigParam( 'aLanguages' );
00188         $this->_aLangData['urls']    = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
00189         $this->_aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
00190 
00191         // empty languages parameters array - creating new one with default values
00192         if ( !is_array($this->_aLangData['params']) ) {
00193             $this->_aLangData['params'] = $this->_assignDefaultLangParams();
00194         }
00195     }
00196 
00205     protected function _updateAbbervation( $sOldId, $sNewId )
00206     {
00207         foreach( array_keys($this->_aLangData) as $sTypeKey ) {
00208 
00209             if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
00210 
00211                 if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
00212                     continue;
00213                 }
00214 
00215                 $aKeys   = array_keys( $this->_aLangData[$sTypeKey] );
00216                 $aValues = array_values( $this->_aLangData[$sTypeKey] );
00217                 //find and replace key
00218                 $iReplaceId = array_search( $sOldId, $aKeys );
00219                 $aKeys[$iReplaceId] = $sNewId;
00220 
00221                 $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
00222             }
00223         }
00224     }
00225 
00232     protected function _sortLangArraysByBaseId()
00233     {
00234         $aUrls      = array();
00235         $aSslUrls   = array();
00236         $aLanguages = array();
00237 
00238         uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
00239 
00240         foreach( $this->_aLangData['params'] as  $sAbbr => $aParams ) {
00241             $iId = (int)$aParams['baseId'];
00242             $aUrls[$iId]        = $this->_aLangData['urls'][$iId];
00243             $aSslUrls[$iId]     = $this->_aLangData['sslUrls'][$iId];
00244             $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00245         }
00246 
00247         $this->_aLangData['lang']    = $aLanguages;
00248         $this->_aLangData['urls']    = $aUrls;
00249         $this->_aLangData['sslUrls'] = $aSslUrls;
00250     }
00251 
00257     protected function _assignDefaultLangParams()
00258     {
00259         $aParams = array();
00260         $iBaseId = 0;
00261 
00262         foreach( array_keys($this->_aLangData['lang']) as $sOxId ) {
00263             $aParams[$sOxId]['baseId']  = $iBaseId;
00264             $aParams[$sOxId]['active']  = 1;
00265             $aParams[$sOxId]['sort']   = $iBaseId + 1;
00266 
00267             $iBaseId++;
00268         }
00269 
00270         return $aParams;
00271     }
00272 
00280     protected function _setDefaultLang( $sOxId )
00281     {
00282         $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
00283         $this->getConfig()->saveShopConfVar( 'str',  'sDefaultLang', $sDefaultId );
00284         $this->getConfig()->setConfigParam( 'sDefaultLang', $sDefaultId );
00285     }
00286 
00292     protected function _getAvailableLangBaseId()
00293     {
00294         $aBaseId = array();
00295         foreach( $this->_aLangData['params'] as $aLang ) {
00296             $aBaseId[] = $aLang['baseId'];
00297         }
00298 
00299         $iNewId = 0;
00300         sort( $aBaseId );
00301         $iTotal = count($aBaseId);
00302 
00303         //getting first available id
00304         while( $iNewId <= $iTotal ) {
00305             if ( $iNewId !== $aBaseId[$iNewId] ) {
00306                 break;
00307             }
00308             $iNewId++;
00309         }
00310 
00311         return $iNewId;
00312     }
00313 
00322     protected function _checkLangTranslations( $sOxId )
00323     {
00324         $myConfig = $this->getConfig();
00325         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00326 
00327         $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', 0, $iBaseId ) );
00328 
00329         if ( empty($sDir) ) {
00330             $oEx = new oxExceptionToDisplay();
00331             $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00332             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00333         }
00334     }
00335 
00344     protected function _checkMultiLangDbFields( $sOxId )
00345     {
00346         $oDB = oxDb::getDb( true );
00347         $myConfig = $this->getConfig();
00348 
00349         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00350 
00351         $sSql = "SHOW COLUMNS FROM oxarticles";
00352         $aFields = $oDB->getAll( $sSql );
00353 
00354         $sPrefix = ( $iBaseId > 0 ) ? '_' . $iBaseId : '';
00355         $sMultiLangCol = 'OXTITLE' . $sPrefix;
00356         $blExists = false;
00357 
00358         foreach ( $aFields as $aCollInfo ) {
00359             if ( $aCollInfo['Field'] == $sMultiLangCol ) {
00360                 $blExists = true;
00361                 break;
00362             }
00363         }
00364 
00365         if ( !$blExists ) {
00366             $oEx = new oxExceptionToDisplay();
00367             $oEx->setMessage( 'LANGUAGE_NODBMULTILANGFIELDS_WARNING' );
00368             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00369         }
00370 
00371     }
00372 
00382     protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00383     {
00384         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00385     }
00386 
00387 }

Generated on Tue Apr 21 15:45:44 2009 for OXID eShop CE by  doxygen 1.5.5