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 
00118             if ( $this->_checkLangExists( $sOxId ) ) {
00119                 //language already exist, showing error and skipping saving
00120                 $oEx = new oxExceptionToDisplay();
00121                 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00122                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00123                 return;
00124             }
00125 
00126             $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00127             oxSession::setVar( "saved_oxid", $sOxId);
00128         }
00129 
00130         //updating language description
00131         $this->_aLangData['lang'][$sOxId]  = $aParams['desc'];
00132 
00133         //updating language parameters
00134         $this->_aLangData['params'][$sOxId]['active']  = $aParams['active'];
00135         $this->_aLangData['params'][$sOxId]['default'] = $aParams['default'];
00136         $this->_aLangData['params'][$sOxId]['sort']   = $aParams['sort'];
00137 
00138         //if setting lang as default
00139         if ( $aParams['default'] == '1' ) {
00140             $this->_setDefaultLang( $sOxId );
00141         }
00142 
00143         //updating language urls
00144         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00145         $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
00146         $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
00147 
00148         //sort parameters, urls and languages arrays by language base id
00149         $this->_sortLangArraysByBaseId();
00150 
00151         $this->_aViewData["updatelist"] = "1";
00152 
00153         //saving languages info
00154         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguageParams',  $this->_aLangData['params']  );
00155         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguages',       $this->_aLangData['lang']    );
00156         $this->getConfig()->saveShopConfVar( 'arr',  'aLanguageURLs',    $this->_aLangData['urls']    );
00157         $this->getConfig()->saveShopConfVar( 'arr',  'aLanguageSSLURLs', $this->_aLangData['sslUrls'] );
00158     }
00159 
00167     protected function _getLanguageInfo( $sOxId )
00168     {
00169         $sDefaultLang = $this->getConfig()->getConfigParam( 'sDefaultLang' );
00170 
00171         $aLangData               = $this->_aLangData['params'][$sOxId];
00172         $aLangData['abbr']       = $sOxId;
00173         $aLangData['desc']       = $this->_aLangData['lang'][$sOxId];
00174         $aLangData['baseurl']    = $this->_aLangData['urls'][$aLangData['baseId']];
00175         $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
00176         $aLangData['default']    = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
00177 
00178         return $aLangData;
00179     }
00180 
00187     protected function _loadLanguages()
00188     {
00189         $this->_aLangData['params']  = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00190         $this->_aLangData['lang']    = $this->getConfig()->getConfigParam( 'aLanguages' );
00191         $this->_aLangData['urls']    = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
00192         $this->_aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
00193 
00194         // empty languages parameters array - creating new one with default values
00195         if ( !is_array($this->_aLangData['params']) ) {
00196             $this->_aLangData['params'] = $this->_assignDefaultLangParams();
00197         }
00198     }
00199 
00208     protected function _updateAbbervation( $sOldId, $sNewId )
00209     {
00210         foreach( array_keys($this->_aLangData) as $sTypeKey ) {
00211 
00212             if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
00213 
00214                 if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
00215                     continue;
00216                 }
00217 
00218                 $aKeys   = array_keys( $this->_aLangData[$sTypeKey] );
00219                 $aValues = array_values( $this->_aLangData[$sTypeKey] );
00220                 //find and replace key
00221                 $iReplaceId = array_search( $sOldId, $aKeys );
00222                 $aKeys[$iReplaceId] = $sNewId;
00223 
00224                 $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
00225             }
00226         }
00227     }
00228 
00235     protected function _sortLangArraysByBaseId()
00236     {
00237         $aUrls      = array();
00238         $aSslUrls   = array();
00239         $aLanguages = array();
00240 
00241         uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
00242 
00243         foreach( $this->_aLangData['params'] as  $sAbbr => $aParams ) {
00244             $iId = (int)$aParams['baseId'];
00245             $aUrls[$iId]        = $this->_aLangData['urls'][$iId];
00246             $aSslUrls[$iId]     = $this->_aLangData['sslUrls'][$iId];
00247             $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00248         }
00249 
00250         $this->_aLangData['lang']    = $aLanguages;
00251         $this->_aLangData['urls']    = $aUrls;
00252         $this->_aLangData['sslUrls'] = $aSslUrls;
00253     }
00254 
00260     protected function _assignDefaultLangParams()
00261     {
00262         $aParams = array();
00263         $iBaseId = 0;
00264 
00265         foreach( array_keys($this->_aLangData['lang']) as $sOxId ) {
00266             $aParams[$sOxId]['baseId']  = $iBaseId;
00267             $aParams[$sOxId]['active']  = 1;
00268             $aParams[$sOxId]['sort']   = $iBaseId + 1;
00269 
00270             $iBaseId++;
00271         }
00272 
00273         return $aParams;
00274     }
00275 
00283     protected function _setDefaultLang( $sOxId )
00284     {
00285         $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
00286         $this->getConfig()->saveShopConfVar( 'str',  'sDefaultLang', $sDefaultId );
00287     }
00288 
00294     protected function _getAvailableLangBaseId()
00295     {
00296         $aBaseId = array();
00297         foreach( $this->_aLangData['params'] as $aLang ) {
00298             $aBaseId[] = $aLang['baseId'];
00299         }
00300 
00301         $iNewId = 0;
00302         sort( $aBaseId );
00303         $iTotal = count($aBaseId);
00304 
00305         //getting first available id
00306         while( $iNewId <= $iTotal ) {
00307             if ( $iNewId !== $aBaseId[$iNewId] ) {
00308                 break;
00309             }
00310             $iNewId++;
00311         }
00312 
00313         return $iNewId;
00314     }
00315 
00324     protected function _checkLangTranslations( $sOxId )
00325     {
00326         $myConfig = $this->getConfig();
00327         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00328 
00329         $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', 0, $iBaseId ) );
00330             if ( !$sDir ) {
00331                 //additional check for former templates
00332                 $sDir = dirname( $myConfig->getLanguagePath( 'lang.txt', 0, $iBaseId ) );
00333             }
00334 
00335         if ( empty($sDir) ) {
00336             $oEx = new oxExceptionToDisplay();
00337             $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00338             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00339         }
00340     }
00341 
00350     protected function _checkMultiLangDbFields( $sOxId )
00351     {
00352         $oDbMeta = oxNew( "oxDbMetaDataHandler" );
00353         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00354 
00355         $sPrefix = oxLang::getInstance()->getLanguageTag( $iBaseId );
00356         $sMultiLangCol = 'OXTITLE' . $sPrefix;
00357 
00358         if ( !$oDbMeta->fieldExists( $sMultiLangCol, "oxarticles" ) ) {
00359             //creating new multilanguage fields with new id over whole DB
00360             oxDb::startTransaction();
00361             try {
00362                 $oDbMeta->addNewLangToDb();
00363             } catch( Exception $oEx ) {
00364                 // if exception, rollBack everything
00365                 oxDb::rollbackTransaction();
00366 
00367                 //show warning
00368                 $oEx = new oxExceptionToDisplay();
00369                 $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
00370                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00371 
00372                 return;
00373             }
00374 
00375             oxDb::commitTransaction();
00376         }
00377 
00378         /*
00379         $oDB = oxDb::getDb( true );
00380         $myConfig = $this->getConfig();
00381 
00382 
00383         $sSql = "SHOW COLUMNS FROM oxarticles";
00384         $aFields = $oDB->getAll( $sSql );
00385 
00386         $sPrefix = ( $iBaseId > 0 ) ? '_' . $iBaseId : '';
00387         $sMultiLangCol = 'OXTITLE' . $sPrefix;
00388         $blExists = false;
00389 
00390         foreach ( $aFields as $aCollInfo ) {
00391             if ( $aCollInfo['Field'] == $sMultiLangCol ) {
00392                 $blExists = true;
00393                 break;
00394             }
00395         }
00396 
00397         if ( !$blExists ) {
00398             $oEx = new oxExceptionToDisplay();
00399             $oEx->setMessage( 'LANGUAGE_NODBMULTILANGFIELDS_WARNING' );
00400             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00401         }
00402         */
00403     }
00404 
00412     protected function _checkLangExists( $sAbbr )
00413     {
00414         $myConfig = $this->getConfig();
00415         $aAbbrs = array_keys($this->_aLangData['lang']);
00416 
00417         if ( in_array( $sAbbr, $aAbbrs ) ) {
00418             return true;
00419         }
00420 
00421         return false;
00422     }
00423 
00433     protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00434     {
00435         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00436     }
00437 
00438 }

Generated on Mon Oct 26 20:07:16 2009 for OXID eShop CE by  doxygen 1.5.5