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
00054 $this->_aLangData = $this->_getLanguages();
00055
00056 if ( $sOxId != -1 ) {
00057
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
00096 $this->_aLangData = $this->_getLanguages();
00097
00098 if ( !$this->_validateInput() ) {
00099 return;
00100 }
00101
00102 $blViewError = false;
00103
00104
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
00114 if ( $blNewLanguage = ($sOxId == -1) ) {
00115 $sOxId = $aParams['abbr'];
00116 $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
00117 $this->setEditObjectId( $sOxId );
00118 }
00119
00120
00121 $this->_aLangData['lang'][$sOxId] = $aParams['desc'];
00122
00123
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
00129 if ( $aParams['default'] == '1' ) {
00130 $this->_setDefaultLang( $sOxId );
00131 }
00132
00133
00134 $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00135 $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
00136 $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
00137
00138
00139 $this->_sortLangArraysByBaseId();
00140
00141 $this->_aViewData["updatelist"] = "1";
00142
00143
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
00150
00151 if ($blNewLanguage) {
00152 if (!$this->_checkMultilangFieldsExistsInDb( $sOxId ) ) {
00153 $this->_addNewMultilangFieldsToDb();
00154 } else {
00155 $blViewError = true;
00156 }
00157 }
00158
00159
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
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
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
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 $aLangParams = $this->_aLangData['params'][$sOxId];
00352 $iBaseId = $aLangParams['baseId'];
00353 $iIsActive = $aLangParams['active'];
00354
00355 $sDir = dirname( $myConfig->getDir( 'lang.php', oxLang::getInstance()->getLanguageAbbr( $iBaseId ), 0, $iBaseId ) );
00356
00357 $sDirAdmin = dirname( $myConfig->getDir( 'lang.php', oxLang::getInstance()->getLanguageAbbr( $iBaseId ), 1, $iBaseId ) );
00358
00359 if ( !$sDir ) {
00360
00361 $sDir = dirname( $myConfig->getLanguagePath( 'lang.txt', 0, $iBaseId ) );
00362 }
00363
00364
00365 if ( $iIsActive == 0 && empty($sDirAdmin) ) {
00366 $oEx = oxNew( "oxExceptionToDisplay" );
00367 $oEx->setMessage( 'LANGUAGE_BACKEND_NOTRANSLATIONS_WARNING' );
00368 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00369 }
00370
00371 elseif ( $iIsActive == 1 && empty($sDir) ) {
00372 $oEx = oxNew( "oxExceptionToDisplay" );
00373 $oEx->setMessage( 'LANGUAGE_FRONTEND_NOTRANSLATIONS_WARNING' );
00374 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00375 }
00376 }
00377
00385 protected function _checkMultilangFieldsExistsInDb( $sOxId )
00386 {
00387 $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00388 $sTable = getLangTableName('oxarticles', $iBaseId );
00389 $sColumn = 'oxtitle' . oxLang::getInstance()->getLanguageTag( $iBaseId );
00390
00391 $oDbMetadata = oxNew('oxDbMetaDataHandler');
00392 return $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sColumn, $sTable );
00393 }
00394
00401 protected function _addNewMultilangFieldsToDb()
00402 {
00403
00404 oxDb::startTransaction();
00405
00406 $oDbMeta = oxNew( "oxDbMetaDataHandler" );
00407
00408 try {
00409 $oDbMeta->addNewLangToDb();
00410 } catch( Exception $oEx ) {
00411
00412 oxDb::rollbackTransaction();
00413
00414
00415 echo $oEx->getMessage();
00416 $oEx = oxNew( "oxExceptionToDisplay" );
00417 $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
00418 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00419
00420 return;
00421 }
00422
00423 oxDb::commitTransaction();
00424 }
00425
00433 protected function _checkLangExists( $sAbbr )
00434 {
00435 $myConfig = $this->getConfig();
00436 $aAbbrs = array_keys($this->_aLangData['lang']);
00437
00438 if ( in_array( $sAbbr, $aAbbrs ) ) {
00439 return true;
00440 }
00441
00442 return false;
00443 }
00444
00454 protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
00455 {
00456 return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00457 }
00458
00464 protected function _validateInput()
00465 {
00466 $blResult = true;
00467
00468 $sOxId = $this->getEditObjectId();
00469 $aParams = oxConfig::getParameter( "editval" );
00470
00471
00472
00473 if ( $sOxId == -1 ) {
00474 if ( $this->_checkLangExists( $aParams['abbr'] ) ) {
00475 $oEx = oxNew( 'oxExceptionToDisplay' );
00476 $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
00477 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00478 $blResult = false;
00479 }
00480 }
00481
00482
00483 if ( empty($aParams['desc']) ) {
00484 $oEx = oxNew( 'oxExceptionToDisplay' );
00485 $oEx->setMessage( 'LANGUAGE_EMPTYLANGUAGENAME_ERROR' );
00486 oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00487 $blResult = false;
00488 }
00489
00490 return $blResult;
00491 }
00492 }