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
00054 $sSavedID = oxConfig::getParameter( "saved_oxid");
00055 if ( isset( $sSavedID) ) {
00056 $sOxId = $sSavedID;
00057 oxSession::deleteVar( "saved_oxid");
00058 $this->_aViewData["oxid"] = $sOxId;
00059
00060 $this->_aViewData["updatelist"] = "1";
00061 }
00062
00063
00064 $this->_loadLanguages();
00065
00066 if ( $sOxId != -1 ) {
00067
00068 $this->_checkLangTranslations( $sOxId );
00069 $this->_checkMultiLangDbFields( $sOxId );
00070 }
00071
00072 if ( $sOxId != "-1" && isset( $sOxId)) {
00073 $this->_aViewData["edit"] = $this->_getLanguageInfo( $sOxId );
00074 }
00075
00076 return "language_main.tpl";
00077 }
00078
00084 public function save()
00085 {
00086 $myConfig = $this->getConfig();
00087
00088
00089 $sOxId = oxConfig::getParameter( "oxid");
00090 $aParams = oxConfig::getParameter( "editval" );
00091
00092 if ( !isset( $aParams['active'])) {
00093 $aParams['active'] = 0;
00094 }
00095
00096 if ( !isset( $aParams['default'])) {
00097 $aParams['default'] = false;
00098 }
00099
00100 if ( empty( $aParams['sort'])) {
00101 $aParams['sort'] = '99999';
00102 }
00103
00104
00105 $this->_loadLanguages();
00106
00107
00108 if ( $sOxId != -1 && $sOxId != $aParams['abbr'] ) {
00109 $this->_updateAbbervation( $sOxId, $aParams['abbr'] );
00110 $sOxId = $aParams['abbr'];
00111 oxSession::setVar( "saved_oxid", $sOxId);
00112 }
00113
00114
00115 if ( $sOxId == -1 ) {
00116 $sOxId = $aParams['abbr'];
00117
00118 if ( $this->_checkLangExists( $sOxId ) ) {
00119
00120 $oEx = new oxExceptionToDisplay();
00121 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00122 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00123 return;
00124 }
00125
00126 $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00127 oxSession::setVar( "saved_oxid", $sOxId);
00128 }
00129
00130
00131 $this->_aLangData['lang'][$sOxId] = $aParams['desc'];
00132
00133
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
00139 if ( $aParams['default'] == '1' ) {
00140 $this->_setDefaultLang( $sOxId );
00141 }
00142
00143
00144 $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00145 $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
00146 $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
00147
00148
00149 $this->_sortLangArraysByBaseId();
00150
00151 $this->_aViewData["updatelist"] = "1";
00152
00153
00154 $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguageParams', serialize($this->_aLangData['params']) );
00155 $this->getConfig()->saveShopConfVar( 'aarr', 'aLanguages', serialize($this->_aLangData['lang']) );
00156 $this->getConfig()->saveShopConfVar( 'arr', 'aLanguageURLs', serialize($this->_aLangData['urls']) );
00157 $this->getConfig()->saveShopConfVar( 'arr', 'aLanguageSSLURLs', serialize($this->_aLangData['sslUrls']) );
00158
00159
00160 $this->getConfig()->setConfigParam( 'aLanguageParams', $this->_aLangData['params'] );
00161 $this->getConfig()->setConfigParam( 'aLanguages', $this->_aLangData['lang'] );
00162 $this->getConfig()->setConfigParam( 'aLanguageURLs', $this->_aLangData['urls'] );
00163 $this->getConfig()->setConfigParam( 'aLanguageSSLURLs', $this->_aLangData['sslUrls'] );
00164 }
00165
00173 protected function _getLanguageInfo( $sOxId )
00174 {
00175 $sDefaultLang = $this->getConfig()->getConfigParam( 'sDefaultLang' );
00176
00177 $aLangData = $this->_aLangData['params'][$sOxId];
00178 $aLangData['abbr'] = $sOxId;
00179 $aLangData['desc'] = $this->_aLangData['lang'][$sOxId];
00180 $aLangData['baseurl'] = $this->_aLangData['urls'][$aLangData['baseId']];
00181 $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
00182 $aLangData['default'] = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
00183
00184 return $aLangData;
00185 }
00186
00193 protected function _loadLanguages()
00194 {
00195 $this->_aLangData['params'] = $this->getConfig()->getConfigParam( 'aLanguageParams' );
00196 $this->_aLangData['lang'] = $this->getConfig()->getConfigParam( 'aLanguages' );
00197 $this->_aLangData['urls'] = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
00198 $this->_aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
00199
00200
00201 if ( !is_array($this->_aLangData['params']) ) {
00202 $this->_aLangData['params'] = $this->_assignDefaultLangParams();
00203 }
00204 }
00205
00214 protected function _updateAbbervation( $sOldId, $sNewId )
00215 {
00216 foreach( array_keys($this->_aLangData) as $sTypeKey ) {
00217
00218 if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
00219
00220 if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
00221 continue;
00222 }
00223
00224 $aKeys = array_keys( $this->_aLangData[$sTypeKey] );
00225 $aValues = array_values( $this->_aLangData[$sTypeKey] );
00226
00227 $iReplaceId = array_search( $sOldId, $aKeys );
00228 $aKeys[$iReplaceId] = $sNewId;
00229
00230 $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
00231 }
00232 }
00233 }
00234
00241 protected function _sortLangArraysByBaseId()
00242 {
00243 $aUrls = array();
00244 $aSslUrls = array();
00245 $aLanguages = array();
00246
00247 uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
00248
00249 foreach( $this->_aLangData['params'] as $sAbbr => $aParams ) {
00250 $iId = (int)$aParams['baseId'];
00251 $aUrls[$iId] = $this->_aLangData['urls'][$iId];
00252 $aSslUrls[$iId] = $this->_aLangData['sslUrls'][$iId];
00253 $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00254 }
00255
00256 $this->_aLangData['lang'] = $aLanguages;
00257 $this->_aLangData['urls'] = $aUrls;
00258 $this->_aLangData['sslUrls'] = $aSslUrls;
00259 }
00260
00266 protected function _assignDefaultLangParams()
00267 {
00268 $aParams = array();
00269 $iBaseId = 0;
00270
00271 foreach( array_keys($this->_aLangData['lang']) as $sOxId ) {
00272 $aParams[$sOxId]['baseId'] = $iBaseId;
00273 $aParams[$sOxId]['active'] = 1;
00274 $aParams[$sOxId]['sort'] = $iBaseId + 1;
00275
00276 $iBaseId++;
00277 }
00278
00279 return $aParams;
00280 }
00281
00289 protected function _setDefaultLang( $sOxId )
00290 {
00291 $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
00292 $this->getConfig()->saveShopConfVar( 'str', 'sDefaultLang', $sDefaultId );
00293 $this->getConfig()->setConfigParam( 'sDefaultLang', $sDefaultId );
00294 }
00295
00301 protected function _getAvailableLangBaseId()
00302 {
00303 $aBaseId = array();
00304 foreach( $this->_aLangData['params'] as $aLang ) {
00305 $aBaseId[] = $aLang['baseId'];
00306 }
00307
00308 $iNewId = 0;
00309 sort( $aBaseId );
00310 $iTotal = count($aBaseId);
00311
00312
00313 while( $iNewId <= $iTotal ) {
00314 if ( $iNewId !== $aBaseId[$iNewId] ) {
00315 break;
00316 }
00317 $iNewId++;
00318 }
00319
00320 return $iNewId;
00321 }
00322
00331 protected function _checkLangTranslations( $sOxId )
00332 {
00333 $myConfig = $this->getConfig();
00334 $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00335
00336 $sDir = dirname( $myConfig->getLanguagePath( 'lang.php', 0, $iBaseId ) );
00337 if ( !$sDir ) {
00338
00339 $sDir = dirname( $myConfig->getLanguagePath( 'lang.txt', 0, $iBaseId ) );
00340 }
00341
00342 if ( empty($sDir) ) {
00343 $oEx = new oxExceptionToDisplay();
00344 $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
00345 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00346 }
00347 }
00348
00357 protected function _checkMultiLangDbFields( $sOxId )
00358 {
00359 $oDB = oxDb::getDb( true );
00360 $myConfig = $this->getConfig();
00361
00362 $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00363
00364 $sSql = "SHOW COLUMNS FROM oxarticles";
00365 $aFields = $oDB->getAll( $sSql );
00366
00367 $sPrefix = ( $iBaseId > 0 ) ? '_' . $iBaseId : '';
00368 $sMultiLangCol = 'OXTITLE' . $sPrefix;
00369 $blExists = false;
00370
00371 foreach ( $aFields as $aCollInfo ) {
00372 if ( $aCollInfo['Field'] == $sMultiLangCol ) {
00373 $blExists = true;
00374 break;
00375 }
00376 }
00377
00378 if ( !$blExists ) {
00379 $oEx = new oxExceptionToDisplay();
00380 $oEx->setMessage( 'LANGUAGE_NODBMULTILANGFIELDS_WARNING' );
00381 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00382 }
00383
00384 }
00385
00393 protected function _checkLangExists( $sAbbr )
00394 {
00395 $myConfig = $this->getConfig();
00396 $aAbbrs = array_keys($this->_aLangData['lang']);
00397
00398 if ( in_array( $sAbbr, $aAbbrs ) ) {
00399 return true;
00400 }
00401
00402 return false;
00403 }
00404
00414 protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00415 {
00416 return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00417 }
00418
00419 }