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->_aLangData = $this->_getLanguages();
00065 
00066         if ( $sOxId != -1 ) {
00067             //checking if translations files exists
00068             $this->_checkLangTranslations( $sOxId );
00069         }
00070 
00071         if ( $sOxId != "-1" && isset( $sOxId)) {
00072             $this->_aViewData["edit"] =  $this->_getLanguageInfo( $sOxId );
00073         }
00074 
00075         return "language_main.tpl";
00076     }
00077 
00083     public function save()
00084     {
00085         $myConfig  = $this->getConfig();
00086 
00087 
00088         $sOxId   = oxConfig::getParameter( "oxid");
00089         $aParams = oxConfig::getParameter( "editval" );
00090 
00091         if ( !isset( $aParams['active'])) {
00092             $aParams['active'] = 0;
00093         }
00094 
00095         if ( !isset( $aParams['default'])) {
00096             $aParams['default'] = false;
00097         }
00098 
00099         if ( empty( $aParams['sort'])) {
00100             $aParams['sort'] = '99999';
00101         }
00102 
00103         //loading languages info from config
00104         $this->_aLangData = $this->_getLanguages();
00105         //checking input errors
00106         if ( !$this->_validateInput() ) {
00107             return;
00108         }
00109 
00110         // if changed language abbervation, updating it for all arrays related with languages
00111         if ( $sOxId != -1 && $sOxId  != $aParams['abbr'] ) {
00112             $this->_updateAbbervation( $sOxId, $aParams['abbr'] );
00113             $sOxId = $aParams['abbr'];
00114             oxSession::setVar( "saved_oxid", $sOxId);
00115         }
00116 
00117         // if adding new language, setting lang id to abbervation
00118         if ( $sOxId == -1 ) {
00119             $sOxId = $aParams['abbr'];
00120             $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00121             oxSession::setVar( "saved_oxid", $sOxId);
00122 
00123             //checking if added language already has created multilang fields
00124             //with new base ID - if not, creating new fields
00125             if ( !$this->_checkMultilangFieldsExistsInDb( $sOxId ) ) {
00126                 $this->_addNewMultilangFieldsToDb();
00127             }
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 
00188     protected function _setLanguages( $aLangData )
00189     {
00190         $this->_aLangData = $aLangData;
00191     }
00192 
00200     protected function _getLanguages()
00201     {
00202         $aLangData['params']  = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00203         $aLangData['lang']    = $this->getConfig()->getConfigParam( 'aLanguages' );
00204         $aLangData['urls']    = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
00205         $aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
00206 
00207         // empty languages parameters array - creating new one with default values
00208         if ( !is_array( $aLangData['params']) ) {
00209             $aLangData['params'] = $this->_assignDefaultLangParams( $aLangData['lang'] );
00210         }
00211 
00212         return $aLangData;
00213     }
00214 
00223     protected function _updateAbbervation( $sOldId, $sNewId )
00224     {
00225         foreach ( array_keys($this->_aLangData) as $sTypeKey ) {
00226 
00227             if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
00228 
00229                 if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
00230                     continue;
00231                 }
00232 
00233                 $aKeys   = array_keys( $this->_aLangData[$sTypeKey] );
00234                 $aValues = array_values( $this->_aLangData[$sTypeKey] );
00235                 //find and replace key
00236                 $iReplaceId = array_search( $sOldId, $aKeys );
00237                 $aKeys[$iReplaceId] = $sNewId;
00238 
00239                 $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
00240             }
00241         }
00242     }
00243 
00250     protected function _sortLangArraysByBaseId()
00251     {
00252         $aUrls      = array();
00253         $aSslUrls   = array();
00254         $aLanguages = array();
00255 
00256         uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
00257 
00258         foreach ( $this->_aLangData['params'] as  $sAbbr => $aParams ) {
00259             $iId = (int)$aParams['baseId'];
00260             $aUrls[$iId]        = $this->_aLangData['urls'][$iId];
00261             $aSslUrls[$iId]     = $this->_aLangData['sslUrls'][$iId];
00262             $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00263         }
00264 
00265         $this->_aLangData['lang']    = $aLanguages;
00266         $this->_aLangData['urls']    = $aUrls;
00267         $this->_aLangData['sslUrls'] = $aSslUrls;
00268     }
00269 
00277     protected function _assignDefaultLangParams( $aLanguages )
00278     {
00279         $aParams = array();
00280         $iBaseId = 0;
00281 
00282         foreach ( array_keys($aLanguages) as $sOxId ) {
00283             $aParams[$sOxId]['baseId']  = $iBaseId;
00284             $aParams[$sOxId]['active']  = 1;
00285             $aParams[$sOxId]['sort']   = $iBaseId + 1;
00286 
00287             $iBaseId++;
00288         }
00289 
00290         return $aParams;
00291     }
00292 
00300     protected function _setDefaultLang( $sOxId )
00301     {
00302         $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
00303         $this->getConfig()->saveShopConfVar( 'str', 'sDefaultLang', $sDefaultId );
00304     }
00305 
00311     protected function _getAvailableLangBaseId()
00312     {
00313         $aBaseId = array();
00314         foreach ( $this->_aLangData['params'] as $aLang ) {
00315             $aBaseId[] = $aLang['baseId'];
00316         }
00317 
00318         $iNewId = 0;
00319         sort( $aBaseId );
00320         $iTotal = count($aBaseId);
00321 
00322         //getting first available id
00323         while ( $iNewId <= $iTotal ) {
00324             if ( $iNewId !== $aBaseId[$iNewId] ) {
00325                 break;
00326             }
00327             $iNewId++;
00328         }
00329 
00330         return $iNewId;
00331     }
00332 
00341     protected function _checkLangTranslations( $sOxId )
00342     {
00343         $myConfig = $this->getConfig();
00344         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00345 
00346         $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', 0, $iBaseId ) );
00347             if ( !$sDir ) {
00348                 //additional check for former templates
00349                 $sDir = dirname( $myConfig->getLanguagePath( 'lang.txt', 0, $iBaseId ) );
00350             }
00351 
00352         if ( empty($sDir) ) {
00353             $oEx = new oxExceptionToDisplay();
00354             $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00355             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00356         }
00357     }
00358 
00366     protected function _checkMultilangFieldsExistsInDb( $sOxId )
00367     {
00368         $oDbMeta = oxNew( "oxDbMetaDataHandler" );
00369         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00370 
00371         $sPrefix = oxLang::getInstance()->getLanguageTag( $iBaseId );
00372         $sMultiLangCol = 'OXTITLE' . $sPrefix;
00373 
00374         if ( !$oDbMeta->fieldExists( $sMultiLangCol, "oxarticles" ) ) {
00375             return false;
00376         }
00377 
00378         return true;
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              $oEx = new oxExceptionToDisplay();
00402              $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
00403              oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00404 
00405              return;
00406         }
00407 
00408         oxDb::commitTransaction();
00409     }
00410 
00418     protected function _checkLangExists( $sAbbr )
00419     {
00420         $myConfig = $this->getConfig();
00421         $aAbbrs = array_keys($this->_aLangData['lang']);
00422 
00423         if ( in_array( $sAbbr, $aAbbrs ) ) {
00424             return true;
00425         }
00426 
00427         return false;
00428     }
00429 
00439     protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00440     {
00441         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00442     }
00443 
00449     protected function _validateInput()
00450     {
00451         $blResult = true;
00452 
00453         $sOxId   = oxConfig::getParameter( "oxid");
00454         $aParams = oxConfig::getParameter( "editval" );
00455 
00456         // if creating new language, checking if language already exists with
00457         // entered language abbervation
00458         if ( $sOxId == -1 ) {
00459             if ( $this->_checkLangExists( $aParams['abbr'] ) ) {
00460                 $oEx = oxNew( 'oxExceptionToDisplay' );
00461                 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00462                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00463                 $blResult = false;
00464             }
00465         }
00466 
00467         // checking if language name is not empty
00468         if ( empty($aParams['desc']) ) {
00469             $oEx = oxNew( 'oxExceptionToDisplay' );
00470             $oEx->setMessage( 'LANGUAGE_EMPTYLANGUAGENAME_ERROR' );
00471             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00472             $blResult = false;
00473         }
00474 
00475         return $blResult;
00476     }
00477 }

Generated by  doxygen 1.6.2