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