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             oxUtilsView::getInstance()->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->getLanguagePath( 'lang.php', 0, $iBaseId ) );
00354             if ( !$sDir ) {
00355                 //additional check for former templates
00356                 $sDir = dirname( $myConfig->getLanguagePath( 'lang.txt', 0, $iBaseId ) );
00357             }
00358 
00359         if ( empty($sDir) ) {
00360             $oEx = oxNew( "oxExceptionToDisplay" );
00361             $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00362             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00363         }
00364     }
00365 
00373     protected function _checkMultilangFieldsExistsInDb( $sOxId )
00374     {
00375         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00376         $sTable  = getLangTableName('oxarticles', $iBaseId );
00377         $sColumn = 'oxtitle' . oxLang::getInstance()->getLanguageTag( $iBaseId );
00378 
00379         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00380         return $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sColumn, $sTable );
00381     }
00382 
00389     protected function _addNewMultilangFieldsToDb()
00390     {
00391         //creating new multilanguage fields with new id over whole DB
00392         oxDb::startTransaction();
00393 
00394         $oDbMeta = oxNew( "oxDbMetaDataHandler" );
00395 
00396         try {
00397              $oDbMeta->addNewLangToDb();
00398         } catch( Exception $oEx ) {
00399              // if exception, rollBack everything
00400              oxDb::rollbackTransaction();
00401 
00402              //show warning
00403              echo $oEx->getMessage();
00404              $oEx = oxNew( "oxExceptionToDisplay" );
00405              $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
00406              oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00407 
00408              return;
00409         }
00410 
00411         oxDb::commitTransaction();
00412     }
00413 
00421     protected function _checkLangExists( $sAbbr )
00422     {
00423         $myConfig = $this->getConfig();
00424         $aAbbrs = array_keys($this->_aLangData['lang']);
00425 
00426         if ( in_array( $sAbbr, $aAbbrs ) ) {
00427             return true;
00428         }
00429 
00430         return false;
00431     }
00432 
00442     protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00443     {
00444         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00445     }
00446 
00452     protected function _validateInput()
00453     {
00454         $blResult = true;
00455 
00456         $sOxId = $this->getEditObjectId();
00457         $aParams = oxConfig::getParameter( "editval" );
00458 
00459         // if creating new language, checking if language already exists with
00460         // entered language abbervation
00461         if ( $sOxId == -1 ) {
00462             if ( $this->_checkLangExists( $aParams['abbr'] ) ) {
00463                 $oEx = oxNew( 'oxExceptionToDisplay' );
00464                 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00465                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00466                 $blResult = false;
00467             }
00468         }
00469 
00470         // checking if language name is not empty
00471         if ( empty($aParams['desc']) ) {
00472             $oEx = oxNew( 'oxExceptionToDisplay' );
00473             $oEx->setMessage( 'LANGUAGE_EMPTYLANGUAGENAME_ERROR' );
00474             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00475             $blResult = false;
00476         }
00477 
00478         return $blResult;
00479     }
00480 }