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             // #0004850 preventing changing abbr for main language with base id = 0
00107             if ( (int) $this->_aLangData['params'][$sOxId]['baseId'] == 0 ) {
00108                 $oEx = oxNew( "oxExceptionToDisplay" );
00109                 $oEx->setMessage( 'LANGUAGE_ABBRCHANGEMAINLANG_WARNING' );
00110                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00111                 $aParams['abbr'] = $sOxId;
00112             } else {
00113                 $this->_updateAbbervation( $sOxId, $aParams['abbr'] );
00114                 $sOxId = $aParams['abbr'];
00115                 $this->setEditObjectId( $sOxId );
00116 
00117                 $blViewError = true;
00118             }
00119         }
00120 
00121         // if adding new language, setting lang id to abbervation
00122         if ( $blNewLanguage = ($sOxId == -1) ) {
00123             $sOxId = $aParams['abbr'];
00124             $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00125             $this->setEditObjectId( $sOxId );
00126         }
00127 
00128         //updating language description
00129         $this->_aLangData['lang'][$sOxId]  = $aParams['desc'];
00130 
00131         //updating language parameters
00132         $this->_aLangData['params'][$sOxId]['active']  = $aParams['active'];
00133         $this->_aLangData['params'][$sOxId]['default'] = $aParams['default'];
00134         $this->_aLangData['params'][$sOxId]['sort']   = $aParams['sort'];
00135 
00136         //if setting lang as default
00137         if ( $aParams['default'] == '1' ) {
00138             $this->_setDefaultLang( $sOxId );
00139         }
00140 
00141         //updating language urls
00142         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00143         $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
00144         $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
00145 
00146         //sort parameters, urls and languages arrays by language base id
00147         $this->_sortLangArraysByBaseId();
00148 
00149         $this->_aViewData["updatelist"] = "1";
00150 
00151         //saving languages info
00152         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguageParams', $this->_aLangData['params'] );
00153         $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguages', $this->_aLangData['lang'] );
00154         $this->getConfig()->saveShopConfVar( 'arr', 'aLanguageURLs', $this->_aLangData['urls'] );
00155         $this->getConfig()->saveShopConfVar( 'arr', 'aLanguageSSLURLs', $this->_aLangData['sslUrls'] );
00156 
00157         //checking if added language already has created multilang fields
00158         //with new base ID - if not, creating new fields
00159         if ($blNewLanguage) {
00160             if (!$this->_checkMultilangFieldsExistsInDb( $sOxId ) ) {
00161                 $this->_addNewMultilangFieldsToDb();
00162             } else {
00163                 $blViewError = true;
00164             }
00165         }
00166 
00167         // show message for user to generate views
00168         if ($blViewError) {
00169             $oEx = oxNew( 'oxExceptionToDisplay' );
00170             $oEx->setMessage( 'LANGUAGE_ERRORGENERATEVIEWS' );
00171             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00172         }
00173     }
00174 
00182     protected function _getLanguageInfo( $sOxId )
00183     {
00184         $sDefaultLang = $this->getConfig()->getConfigParam( 'sDefaultLang' );
00185 
00186         $aLangData               = $this->_aLangData['params'][$sOxId];
00187         $aLangData['abbr']       = $sOxId;
00188         $aLangData['desc']       = $this->_aLangData['lang'][$sOxId];
00189         $aLangData['baseurl']    = $this->_aLangData['urls'][$aLangData['baseId']];
00190         $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
00191         $aLangData['default']    = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
00192 
00193         return $aLangData;
00194     }
00195 
00203     protected function _setLanguages( $aLangData )
00204     {
00205         $this->_aLangData = $aLangData;
00206     }
00207 
00215     protected function _getLanguages()
00216     {
00217         $aLangData['params']  = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00218         $aLangData['lang']    = $this->getConfig()->getConfigParam( 'aLanguages' );
00219         $aLangData['urls']    = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
00220         $aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
00221 
00222         // empty languages parameters array - creating new one with default values
00223         if ( !is_array( $aLangData['params']) ) {
00224             $aLangData['params'] = $this->_assignDefaultLangParams( $aLangData['lang'] );
00225         }
00226 
00227         return $aLangData;
00228     }
00229 
00238     protected function _updateAbbervation( $sOldId, $sNewId )
00239     {
00240         foreach ( array_keys($this->_aLangData) as $sTypeKey ) {
00241 
00242             if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
00243 
00244                 if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
00245                     continue;
00246                 }
00247 
00248                 $aKeys   = array_keys( $this->_aLangData[$sTypeKey] );
00249                 $aValues = array_values( $this->_aLangData[$sTypeKey] );
00250                 //find and replace key
00251                 $iReplaceId = array_search( $sOldId, $aKeys );
00252                 $aKeys[$iReplaceId] = $sNewId;
00253 
00254                 $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
00255             }
00256         }
00257     }
00258 
00265     protected function _sortLangArraysByBaseId()
00266     {
00267         $aUrls      = array();
00268         $aSslUrls   = array();
00269         $aLanguages = array();
00270 
00271         uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
00272 
00273         foreach ( $this->_aLangData['params'] as  $sAbbr => $aParams ) {
00274             $iId = (int)$aParams['baseId'];
00275             $aUrls[$iId]        = $this->_aLangData['urls'][$iId];
00276             $aSslUrls[$iId]     = $this->_aLangData['sslUrls'][$iId];
00277             $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00278         }
00279 
00280         $this->_aLangData['lang']    = $aLanguages;
00281         $this->_aLangData['urls']    = $aUrls;
00282         $this->_aLangData['sslUrls'] = $aSslUrls;
00283     }
00284 
00292     protected function _assignDefaultLangParams( $aLanguages )
00293     {
00294         $aParams = array();
00295         $iBaseId = 0;
00296 
00297         foreach ( array_keys($aLanguages) as $sOxId ) {
00298             $aParams[$sOxId]['baseId']  = $iBaseId;
00299             $aParams[$sOxId]['active']  = 1;
00300             $aParams[$sOxId]['sort']   = $iBaseId + 1;
00301 
00302             $iBaseId++;
00303         }
00304 
00305         return $aParams;
00306     }
00307 
00315     protected function _setDefaultLang( $sOxId )
00316     {
00317         $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
00318         $this->getConfig()->saveShopConfVar( 'str', 'sDefaultLang', $sDefaultId );
00319     }
00320 
00326     protected function _getAvailableLangBaseId()
00327     {
00328         $aBaseId = array();
00329         foreach ( $this->_aLangData['params'] as $aLang ) {
00330             $aBaseId[] = $aLang['baseId'];
00331         }
00332 
00333         $iNewId = 0;
00334         sort( $aBaseId );
00335         $iTotal = count($aBaseId);
00336 
00337         //getting first available id
00338         while ( $iNewId <= $iTotal ) {
00339             if ( $iNewId !== $aBaseId[$iNewId] ) {
00340                 break;
00341             }
00342             $iNewId++;
00343         }
00344 
00345         return $iNewId;
00346     }
00347 
00356     protected function _checkLangTranslations( $sOxId )
00357     {
00358         $myConfig = $this->getConfig();
00359 
00360         $sDir = dirname( $myConfig->getTranslationsDir( 'lang.php', $sOxId ) );
00361 
00362         if ( empty($sDir) ) {
00363             $oEx = oxNew( "oxExceptionToDisplay" );
00364             $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00365             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00366         }
00367     }
00368 
00376     protected function _checkMultilangFieldsExistsInDb( $sOxId )
00377     {
00378         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00379         $sTable  = getLangTableName('oxarticles', $iBaseId );
00380         $sColumn = 'oxtitle' . oxRegistry::getLang()->getLanguageTag( $iBaseId );
00381 
00382         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00383         return $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sColumn, $sTable );
00384     }
00385 
00392     protected function _addNewMultilangFieldsToDb()
00393     {
00394         //creating new multilanguage fields with new id over whole DB
00395         oxDb::getDb()->startTransaction();
00396 
00397         $oDbMeta = oxNew( "oxDbMetaDataHandler" );
00398 
00399         try {
00400              $oDbMeta->addNewLangToDb();
00401         } catch( Exception $oEx ) {
00402              // if exception, rollBack everything
00403              oxDb::getDb()->rollbackTransaction();
00404 
00405              //show warning
00406              echo $oEx->getMessage();
00407              $oEx = oxNew( "oxExceptionToDisplay" );
00408              $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
00409              oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00410 
00411              return;
00412         }
00413 
00414         oxDb::getDb()->commitTransaction();
00415     }
00416 
00424     protected function _checkLangExists( $sAbbr )
00425     {
00426         $myConfig = $this->getConfig();
00427         $aAbbrs = array_keys($this->_aLangData['lang']);
00428 
00429         if ( in_array( $sAbbr, $aAbbrs ) ) {
00430             return true;
00431         }
00432 
00433         return false;
00434     }
00435 
00445     protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00446     {
00447         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00448     }
00449 
00455     protected function _validateInput()
00456     {
00457         $blResult = true;
00458 
00459         $sOxId = $this->getEditObjectId();
00460         $aParams = oxConfig::getParameter( "editval" );
00461 
00462         // if creating new language, checking if language already exists with
00463         // entered language abbervation
00464         if ( $sOxId == -1 ) {
00465             if ( $this->_checkLangExists( $aParams['abbr'] ) ) {
00466                 $oEx = oxNew( 'oxExceptionToDisplay' );
00467                 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00468                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00469                 $blResult = false;
00470             }
00471         }
00472 
00473         // checking if language name is not empty
00474         if ( empty($aParams['desc']) ) {
00475             $oEx = oxNew( 'oxExceptionToDisplay' );
00476             $oEx->setMessage( 'LANGUAGE_EMPTYLANGUAGENAME_ERROR' );
00477             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00478             $blResult = false;
00479         }
00480 
00481         return $blResult;
00482     }
00483 }