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 = $this->_aViewData["oxid"] = $this->getEditObjectId();
00053         //loading languages info from config
00054         $this->_aLangData = $this->_getLanguages();
00055 
00056         if ( $sOxId != -1 ) {
00057             //checking if translations files exists
00058             $this->_checkLangTranslations( $sOxId );
00059         }
00060 
00061         if ( $sOxId != "-1" && isset( $sOxId)) {
00062             $this->_aViewData["edit"] =  $this->_getLanguageInfo( $sOxId );
00063         }
00064 
00065         return "language_main.tpl";
00066     }
00067 
00073     public function save()
00074     {
00075         $myConfig  = $this->getConfig();
00076 
00077 
00078         parent::save();
00079 
00080         $sOxId = $this->getEditObjectId();
00081         $aParams = oxConfig::getParameter( "editval" );
00082 
00083         if ( !isset( $aParams['active'])) {
00084             $aParams['active'] = 0;
00085         }
00086 
00087         if ( !isset( $aParams['default'])) {
00088             $aParams['default'] = false;
00089         }
00090 
00091         if ( empty( $aParams['sort'])) {
00092             $aParams['sort'] = '99999';
00093         }
00094 
00095         //loading languages info from config
00096         $this->_aLangData = $this->_getLanguages();
00097         //checking input errors
00098         if ( !$this->_validateInput() ) {
00099             return;
00100         }
00101 
00102         $blViewError = false;
00103 
00104         // if changed language abbervation, updating it for all arrays related with languages
00105         if ( $sOxId != -1 && $sOxId  != $aParams['abbr'] ) {
00106             $this->_updateAbbervation( $sOxId, $aParams['abbr'] );
00107             $sOxId = $aParams['abbr'];
00108             $this->setEditObjectId( $sOxId );
00109 
00110             $blViewError = true;
00111         }
00112 
00113         // if adding new language, setting lang id to abbervation
00114         if ( $blNewLanguage = ($sOxId == -1) ) {
00115             $sOxId = $aParams['abbr'];
00116             $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00117             $this->setEditObjectId( $sOxId );
00118         }
00119 
00120         //updating language description
00121         $this->_aLangData['lang'][$sOxId]  = $aParams['desc'];
00122 
00123         //updating language parameters
00124         $this->_aLangData['params'][$sOxId]['active']  = $aParams['active'];
00125         $this->_aLangData['params'][$sOxId]['default'] = $aParams['default'];
00126         $this->_aLangData['params'][$sOxId]['sort']   = $aParams['sort'];
00127 
00128         //if setting lang as default
00129         if ( $aParams['default'] == '1' ) {
00130             $this->_setDefaultLang( $sOxId );
00131         }
00132 
00133         //updating language urls
00134         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00135         $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
00136         $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
00137 
00138         //sort parameters, urls and languages arrays by language base id
00139         $this->_sortLangArraysByBaseId();
00140 
00141         $this->_aViewData["updatelist"] = "1";
00142 
00143         //saving languages info
00144         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguageParams', $this->_aLangData['params'] );
00145         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguages', $this->_aLangData['lang'] );
00146         $this->getConfig()->saveShopConfVar( 'arr', 'aLanguageURLs', $this->_aLangData['urls'] );
00147         $this->getConfig()->saveShopConfVar( 'arr', 'aLanguageSSLURLs', $this->_aLangData['sslUrls'] );
00148 
00149         //checking if added language already has created multilang fields
00150         //with new base ID - if not, creating new fields
00151         if ($blNewLanguage) {
00152             if (!$this->_checkMultilangFieldsExistsInDb( $sOxId ) ) {
00153                 $this->_addNewMultilangFieldsToDb();
00154             } else {
00155                 $blViewError = true;
00156             }
00157         }
00158 
00159         // show message for user to generate views
00160         if ($blViewError) {
00161             $oEx = oxNew( 'oxExceptionToDisplay' );
00162             $oEx->setMessage( 'LANGUAGE_ERRORGENERATEVIEWS' );
00163             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00164         }
00165     }
00166 
00174     protected function _getLanguageInfo( $sOxId )
00175     {
00176         $sDefaultLang = $this->getConfig()->getConfigParam( 'sDefaultLang' );
00177 
00178         $aLangData               = $this->_aLangData['params'][$sOxId];
00179         $aLangData['abbr']       = $sOxId;
00180         $aLangData['desc']       = $this->_aLangData['lang'][$sOxId];
00181         $aLangData['baseurl']    = $this->_aLangData['urls'][$aLangData['baseId']];
00182         $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
00183         $aLangData['default']    = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
00184 
00185         return $aLangData;
00186     }
00187 
00195     protected function _setLanguages( $aLangData )
00196     {
00197         $this->_aLangData = $aLangData;
00198     }
00199 
00207     protected function _getLanguages()
00208     {
00209         $aLangData['params']  = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00210         $aLangData['lang']    = $this->getConfig()->getConfigParam( 'aLanguages' );
00211         $aLangData['urls']    = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
00212         $aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
00213 
00214         // empty languages parameters array - creating new one with default values
00215         if ( !is_array( $aLangData['params']) ) {
00216             $aLangData['params'] = $this->_assignDefaultLangParams( $aLangData['lang'] );
00217         }
00218 
00219         return $aLangData;
00220     }
00221 
00230     protected function _updateAbbervation( $sOldId, $sNewId )
00231     {
00232         foreach ( array_keys($this->_aLangData) as $sTypeKey ) {
00233 
00234             if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
00235 
00236                 if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
00237                     continue;
00238                 }
00239 
00240                 $aKeys   = array_keys( $this->_aLangData[$sTypeKey] );
00241                 $aValues = array_values( $this->_aLangData[$sTypeKey] );
00242                 //find and replace key
00243                 $iReplaceId = array_search( $sOldId, $aKeys );
00244                 $aKeys[$iReplaceId] = $sNewId;
00245 
00246                 $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
00247             }
00248         }
00249     }
00250 
00257     protected function _sortLangArraysByBaseId()
00258     {
00259         $aUrls      = array();
00260         $aSslUrls   = array();
00261         $aLanguages = array();
00262 
00263         uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
00264 
00265         foreach ( $this->_aLangData['params'] as  $sAbbr => $aParams ) {
00266             $iId = (int)$aParams['baseId'];
00267             $aUrls[$iId]        = $this->_aLangData['urls'][$iId];
00268             $aSslUrls[$iId]     = $this->_aLangData['sslUrls'][$iId];
00269             $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00270         }
00271 
00272         $this->_aLangData['lang']    = $aLanguages;
00273         $this->_aLangData['urls']    = $aUrls;
00274         $this->_aLangData['sslUrls'] = $aSslUrls;
00275     }
00276 
00284     protected function _assignDefaultLangParams( $aLanguages )
00285     {
00286         $aParams = array();
00287         $iBaseId = 0;
00288 
00289         foreach ( array_keys($aLanguages) as $sOxId ) {
00290             $aParams[$sOxId]['baseId']  = $iBaseId;
00291             $aParams[$sOxId]['active']  = 1;
00292             $aParams[$sOxId]['sort']   = $iBaseId + 1;
00293 
00294             $iBaseId++;
00295         }
00296 
00297         return $aParams;
00298     }
00299 
00307     protected function _setDefaultLang( $sOxId )
00308     {
00309         $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
00310         $this->getConfig()->saveShopConfVar( 'str', 'sDefaultLang', $sDefaultId );
00311     }
00312 
00318     protected function _getAvailableLangBaseId()
00319     {
00320         $aBaseId = array();
00321         foreach ( $this->_aLangData['params'] as $aLang ) {
00322             $aBaseId[] = $aLang['baseId'];
00323         }
00324 
00325         $iNewId = 0;
00326         sort( $aBaseId );
00327         $iTotal = count($aBaseId);
00328 
00329         //getting first available id
00330         while ( $iNewId <= $iTotal ) {
00331             if ( $iNewId !== $aBaseId[$iNewId] ) {
00332                 break;
00333             }
00334             $iNewId++;
00335         }
00336 
00337         return $iNewId;
00338     }
00339 
00348     protected function _checkLangTranslations( $sOxId )
00349     {
00350         $myConfig = $this->getConfig();
00351         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00352 
00353         $sDir = dirname( $myConfig->getTranslationsDir( 'lang.php', oxRegistry::getLang()->getLanguageAbbr( $iBaseId ) ) );
00354 
00355         if ( empty($sDir) ) {
00356             $oEx = oxNew( "oxExceptionToDisplay" );
00357             $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00358             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00359         }
00360     }
00361 
00369     protected function _checkMultilangFieldsExistsInDb( $sOxId )
00370     {
00371         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00372         $sTable  = getLangTableName('oxarticles', $iBaseId );
00373         $sColumn = 'oxtitle' . oxRegistry::getLang()->getLanguageTag( $iBaseId );
00374 
00375         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00376         return $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sColumn, $sTable );
00377     }
00378 
00385     protected function _addNewMultilangFieldsToDb()
00386     {
00387         //creating new multilanguage fields with new id over whole DB
00388         oxDb::getDb()->startTransaction();
00389 
00390         $oDbMeta = oxNew( "oxDbMetaDataHandler" );
00391 
00392         try {
00393              $oDbMeta->addNewLangToDb();
00394         } catch( Exception $oEx ) {
00395              // if exception, rollBack everything
00396              oxDb::getDb()->rollbackTransaction();
00397 
00398              //show warning
00399              echo $oEx->getMessage();
00400              $oEx = oxNew( "oxExceptionToDisplay" );
00401              $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
00402              oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00403 
00404              return;
00405         }
00406 
00407         oxDb::getDb()->commitTransaction();
00408     }
00409 
00417     protected function _checkLangExists( $sAbbr )
00418     {
00419         $myConfig = $this->getConfig();
00420         $aAbbrs = array_keys($this->_aLangData['lang']);
00421 
00422         if ( in_array( $sAbbr, $aAbbrs ) ) {
00423             return true;
00424         }
00425 
00426         return false;
00427     }
00428 
00438     protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00439     {
00440         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00441     }
00442 
00448     protected function _validateInput()
00449     {
00450         $blResult = true;
00451 
00452         $sOxId = $this->getEditObjectId();
00453         $aParams = oxConfig::getParameter( "editval" );
00454 
00455         // if creating new language, checking if language already exists with
00456         // entered language abbervation
00457         if ( $sOxId == -1 ) {
00458             if ( $this->_checkLangExists( $aParams['abbr'] ) ) {
00459                 $oEx = oxNew( 'oxExceptionToDisplay' );
00460                 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00461                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00462                 $blResult = false;
00463             }
00464         }
00465 
00466         // checking if language name is not empty
00467         if ( empty($aParams['desc']) ) {
00468             $oEx = oxNew( 'oxExceptionToDisplay' );
00469             $oEx->setMessage( 'LANGUAGE_EMPTYLANGUAGENAME_ERROR' );
00470             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00471             $blResult = false;
00472         }
00473 
00474         return $blResult;
00475     }
00476 }