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 = oxRegistry::getConfig()->getRequestParameter("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                 oxRegistry::get("oxUtilsView")->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 
00201     protected function _setLanguages($aLangData)
00202     {
00203         $this->_aLangData = $aLangData;
00204     }
00205 
00213     protected function _getLanguages()
00214     {
00215         $aLangData['params'] = $this->getConfig()->getConfigParam('aLanguageParams');
00216         $aLangData['lang'] = $this->getConfig()->getConfigParam('aLanguages');
00217         $aLangData['urls'] = $this->getConfig()->getConfigParam('aLanguageURLs');
00218         $aLangData['sslUrls'] = $this->getConfig()->getConfigParam('aLanguageSSLURLs');
00219 
00220         // empty languages parameters array - creating new one with default values
00221         if (!is_array($aLangData['params'])) {
00222             $aLangData['params'] = $this->_assignDefaultLangParams($aLangData['lang']);
00223         }
00224 
00225         return $aLangData;
00226     }
00227 
00234     protected function _updateAbbervation($sOldId, $sNewId)
00235     {
00236         foreach (array_keys($this->_aLangData) as $sTypeKey) {
00237 
00238             if (is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0) {
00239 
00240                 if ($sTypeKey == 'urls' || $sTypeKey == 'sslUrls') {
00241                     continue;
00242                 }
00243 
00244                 $aKeys = array_keys($this->_aLangData[$sTypeKey]);
00245                 $aValues = array_values($this->_aLangData[$sTypeKey]);
00246                 //find and replace key
00247                 $iReplaceId = array_search($sOldId, $aKeys);
00248                 $aKeys[$iReplaceId] = $sNewId;
00249 
00250                 $this->_aLangData[$sTypeKey] = array_combine($aKeys, $aValues);
00251             }
00252         }
00253     }
00254 
00259     protected function _sortLangArraysByBaseId()
00260     {
00261         $aUrls = array();
00262         $aSslUrls = array();
00263         $aLanguages = array();
00264 
00265         uasort($this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback'));
00266 
00267         foreach ($this->_aLangData['params'] as $sAbbr => $aParams) {
00268             $iId = (int) $aParams['baseId'];
00269             $aUrls[$iId] = $this->_aLangData['urls'][$iId];
00270             $aSslUrls[$iId] = $this->_aLangData['sslUrls'][$iId];
00271             $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
00272         }
00273 
00274         $this->_aLangData['lang'] = $aLanguages;
00275         $this->_aLangData['urls'] = $aUrls;
00276         $this->_aLangData['sslUrls'] = $aSslUrls;
00277     }
00278 
00286     protected function _assignDefaultLangParams($aLanguages)
00287     {
00288         $aParams = array();
00289         $iBaseId = 0;
00290 
00291         foreach (array_keys($aLanguages) as $sOxId) {
00292             $aParams[$sOxId]['baseId'] = $iBaseId;
00293             $aParams[$sOxId]['active'] = 1;
00294             $aParams[$sOxId]['sort'] = $iBaseId + 1;
00295 
00296             $iBaseId++;
00297         }
00298 
00299         return $aParams;
00300     }
00301 
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         //getting first available id
00330         while ($iNewId <= $iTotal) {
00331             if ($iNewId !== $aBaseId[$iNewId]) {
00332                 break;
00333             }
00334             $iNewId++;
00335         }
00336 
00337         return $iNewId;
00338     }
00339 
00346     protected function _checkLangTranslations($sOxId)
00347     {
00348         $myConfig = $this->getConfig();
00349 
00350         $sDir = dirname($myConfig->getTranslationsDir('lang.php', $sOxId));
00351 
00352         if (empty($sDir)) {
00353             $oEx = oxNew("oxExceptionToDisplay");
00354             $oEx->setMessage('LANGUAGE_NOTRANSLATIONS_WARNING');
00355             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00356         }
00357     }
00358 
00366     protected function _checkMultilangFieldsExistsInDb($sOxId)
00367     {
00368         $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
00369         $sTable = getLangTableName('oxarticles', $iBaseId);
00370         $sColumn = 'oxtitle' . oxRegistry::getLang()->getLanguageTag($iBaseId);
00371 
00372         $oDbMetadata = oxNew('oxDbMetaDataHandler');
00373 
00374         return $oDbMetadata->tableExists($sTable) && $oDbMetadata->fieldExists($sColumn, $sTable);
00375     }
00376 
00383     protected function _addNewMultilangFieldsToDb()
00384     {
00385         //creating new multilanguage fields with new id over whole DB
00386         oxDb::getDb()->startTransaction();
00387 
00388         $oDbMeta = oxNew("oxDbMetaDataHandler");
00389 
00390         try {
00391             $oDbMeta->addNewLangToDb();
00392         } catch (Exception $oEx) {
00393             // if exception, rollBack everything
00394             oxDb::getDb()->rollbackTransaction();
00395 
00396             //show warning
00397             echo $oEx->getMessage();
00398             $oEx = oxNew("oxExceptionToDisplay");
00399             $oEx->setMessage('LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS');
00400             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00401 
00402             return;
00403         }
00404 
00405         oxDb::getDb()->commitTransaction();
00406     }
00407 
00415     protected function _checkLangExists($sAbbr)
00416     {
00417         $myConfig = $this->getConfig();
00418         $aAbbrs = array_keys($this->_aLangData['lang']);
00419 
00420         if (in_array($sAbbr, $aAbbrs)) {
00421             return true;
00422         }
00423 
00424         return false;
00425     }
00426 
00436     protected function _sortLangParamsByBaseIdCallback($oLang1, $oLang2)
00437     {
00438         return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
00439     }
00440 
00446     protected function _validateInput()
00447     {
00448         $blResult = true;
00449 
00450         $sOxId = $this->getEditObjectId();
00451         $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
00452 
00453         // if creating new language, checking if language already exists with
00454         // entered language abbervation
00455         if ($sOxId == -1) {
00456             if ($this->_checkLangExists($aParams['abbr'])) {
00457                 $oEx = oxNew('oxExceptionToDisplay');
00458                 $oEx->setMessage('LANGUAGE_ALREADYEXISTS_ERROR');
00459                 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00460                 $blResult = false;
00461             }
00462         }
00463 
00464         // checking if language name is not empty
00465         if (empty($aParams['desc'])) {
00466             $oEx = oxNew('oxExceptionToDisplay');
00467             $oEx->setMessage('LANGUAGE_EMPTYLANGUAGENAME_ERROR');
00468             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00469             $blResult = false;
00470         }
00471 
00472         return $blResult;
00473     }
00474 }