OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
language_main.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
15  protected $_aLangData = null;
16 
22  protected $_aLangParams = null;
23 
29  protected $_aLanguagesUrls = null;
30 
36  protected $_aLanguagesSslUrls = null;
37 
40 
48  public function render()
49  {
50  $myConfig = $this->getConfig();
51 
52 
54 
55  $sOxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
56  //loading languages info from config
57  $this->_aLangData = $this->_getLanguages();
58 
59  if ($sOxId != -1) {
60  //checking if translations files exists
61  $this->_checkLangTranslations($sOxId);
62  }
63 
64  if ($sOxId != "-1" && isset($sOxId)) {
65  $this->_aViewData["edit"] = $this->_getLanguageInfo($sOxId);
66  }
67 
68  return "language_main.tpl";
69  }
70 
76  public function save()
77  {
78  $myConfig = $this->getConfig();
79 
80 
81  parent::save();
82 
83  $sOxId = $this->getEditObjectId();
84  $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
85 
86  if (!isset($aParams['active'])) {
87  $aParams['active'] = 0;
88  }
89 
90  if (!isset($aParams['default'])) {
91  $aParams['default'] = false;
92  }
93 
94  if (empty($aParams['sort'])) {
95  $aParams['sort'] = '99999';
96  }
97 
98  //loading languages info from config
99  $this->_aLangData = $this->_getLanguages();
100  //checking input errors
101  if (!$this->_validateInput()) {
102  return;
103  }
104 
105  $blViewError = false;
106 
107  // if changed language abbervation, updating it for all arrays related with languages
108  if ($sOxId != -1 && $sOxId != $aParams['abbr']) {
109  // #0004850 preventing changing abbr for main language with base id = 0
110  if ((int) $this->_aLangData['params'][$sOxId]['baseId'] == 0) {
111  $oEx = oxNew("oxExceptionToDisplay");
112  $oEx->setMessage('LANGUAGE_ABBRCHANGEMAINLANG_WARNING');
113  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
114  $aParams['abbr'] = $sOxId;
115  } else {
116  $this->_updateAbbervation($sOxId, $aParams['abbr']);
117  $sOxId = $aParams['abbr'];
118  $this->setEditObjectId($sOxId);
119 
120  $blViewError = true;
121  }
122  }
123 
124  // if adding new language, setting lang id to abbervation
125  if ($blNewLanguage = ($sOxId == -1)) {
126  $sOxId = $aParams['abbr'];
127  $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
128  $this->setEditObjectId($sOxId);
129  }
130 
131  //updating language description
132  $this->_aLangData['lang'][$sOxId] = $aParams['desc'];
133 
134  //updating language parameters
135  $this->_aLangData['params'][$sOxId]['active'] = $aParams['active'];
136  $this->_aLangData['params'][$sOxId]['default'] = $aParams['default'];
137  $this->_aLangData['params'][$sOxId]['sort'] = $aParams['sort'];
138 
139  //if setting lang as default
140  if ($aParams['default'] == '1') {
141  $this->_setDefaultLang($sOxId);
142  }
143 
144  //updating language urls
145  $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
146  $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
147  $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
148 
149  //sort parameters, urls and languages arrays by language base id
150  $this->_sortLangArraysByBaseId();
151 
152  $this->_aViewData["updatelist"] = "1";
153 
154  if ($this->isValidLanguageData($this->_aLangData)) {
155  //saving languages info
156  $this->getConfig()->saveShopConfVar('aarr', 'aLanguageParams', $this->_aLangData['params']);
157  $this->getConfig()->saveShopConfVar('aarr', 'aLanguages', $this->_aLangData['lang']);
158  $this->getConfig()->saveShopConfVar('arr', 'aLanguageURLs', $this->_aLangData['urls']);
159  $this->getConfig()->saveShopConfVar('arr', 'aLanguageSSLURLs', $this->_aLangData['sslUrls']);
160  //checking if added language already has created multilang fields
161  //with new base ID - if not, creating new fields
162  if ($blNewLanguage) {
163  if (!$this->_checkMultilangFieldsExistsInDb($sOxId)) {
165  } else {
166  $blViewError = true;
167  }
168  }
169  // show message for user to generate views
170  if ($blViewError) {
171  $oEx = oxNew('oxExceptionToDisplay');
172  $oEx->setMessage('LANGUAGE_ERRORGENERATEVIEWS');
173  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
174  }
175  }
176  }
177 
185  protected function _getLanguageInfo($sOxId)
186  {
187  $sDefaultLang = $this->getConfig()->getConfigParam('sDefaultLang');
188 
189  $aLangData = $this->_aLangData['params'][$sOxId];
190  $aLangData['abbr'] = $sOxId;
191  $aLangData['desc'] = $this->_aLangData['lang'][$sOxId];
192  $aLangData['baseurl'] = $this->_aLangData['urls'][$aLangData['baseId']];
193  $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
194  $aLangData['default'] = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
195 
196  return $aLangData;
197  }
198 
204  protected function _setLanguages($aLangData)
205  {
206  $this->_aLangData = $aLangData;
207  }
208 
216  protected function _getLanguages()
217  {
218  $aLangData['params'] = $this->getConfig()->getConfigParam('aLanguageParams');
219  $aLangData['lang'] = $this->getConfig()->getConfigParam('aLanguages');
220  $aLangData['urls'] = $this->getConfig()->getConfigParam('aLanguageURLs');
221  $aLangData['sslUrls'] = $this->getConfig()->getConfigParam('aLanguageSSLURLs');
222 
223  // empty languages parameters array - creating new one with default values
224  if (!is_array($aLangData['params'])) {
225  $aLangData['params'] = $this->_assignDefaultLangParams($aLangData['lang']);
226  }
227 
228  return $aLangData;
229  }
230 
237  protected function _updateAbbervation($sOldId, $sNewId)
238  {
239  foreach (array_keys($this->_aLangData) as $sTypeKey) {
240 
241  if (is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0) {
242 
243  if ($sTypeKey == 'urls' || $sTypeKey == 'sslUrls') {
244  continue;
245  }
246 
247  $aKeys = array_keys($this->_aLangData[$sTypeKey]);
248  $aValues = array_values($this->_aLangData[$sTypeKey]);
249  //find and replace key
250  $iReplaceId = array_search($sOldId, $aKeys);
251  $aKeys[$iReplaceId] = $sNewId;
252 
253  $this->_aLangData[$sTypeKey] = array_combine($aKeys, $aValues);
254  }
255  }
256  }
257 
262  protected function _sortLangArraysByBaseId()
263  {
264  $aUrls = array();
265  $aSslUrls = array();
266  $aLanguages = array();
267 
268  uasort($this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback'));
269 
270  foreach ($this->_aLangData['params'] as $sAbbr => $aParams) {
271  $iId = (int) $aParams['baseId'];
272  $aUrls[$iId] = $this->_aLangData['urls'][$iId];
273  $aSslUrls[$iId] = $this->_aLangData['sslUrls'][$iId];
274  $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
275  }
276 
277  $this->_aLangData['lang'] = $aLanguages;
278  $this->_aLangData['urls'] = $aUrls;
279  $this->_aLangData['sslUrls'] = $aSslUrls;
280  }
281 
290  {
291  $aParams = array();
292  $iBaseId = 0;
293 
294  foreach (array_keys($aLanguages) as $sOxId) {
295  $aParams[$sOxId]['baseId'] = $iBaseId;
296  $aParams[$sOxId]['active'] = 1;
297  $aParams[$sOxId]['sort'] = $iBaseId + 1;
298 
299  $iBaseId++;
300  }
301 
302  return $aParams;
303  }
304 
310  protected function _setDefaultLang($sOxId)
311  {
312  $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
313  $this->getConfig()->saveShopConfVar('str', 'sDefaultLang', $sDefaultId);
314  }
315 
321  protected function _getAvailableLangBaseId()
322  {
323  $aBaseId = array();
324  foreach ($this->_aLangData['params'] as $aLang) {
325  $aBaseId[] = $aLang['baseId'];
326  }
327 
328  $iNewId = 0;
329  sort($aBaseId);
330  $iTotal = count($aBaseId);
331 
332  //getting first available id
333  while ($iNewId <= $iTotal) {
334  if ($iNewId !== $aBaseId[$iNewId]) {
335  break;
336  }
337  $iNewId++;
338  }
339 
340  return $iNewId;
341  }
342 
349  protected function _checkLangTranslations($sOxId)
350  {
351  $myConfig = $this->getConfig();
352 
353  $sDir = dirname($myConfig->getTranslationsDir('lang.php', $sOxId));
354 
355  if (empty($sDir)) {
356  $oEx = oxNew("oxExceptionToDisplay");
357  $oEx->setMessage('LANGUAGE_NOTRANSLATIONS_WARNING');
358  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
359  }
360  }
361 
369  protected function _checkMultilangFieldsExistsInDb($sOxId)
370  {
371  $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
372  $sTable = getLangTableName('oxarticles', $iBaseId);
373  $sColumn = 'oxtitle' . oxRegistry::getLang()->getLanguageTag($iBaseId);
374 
375  $oDbMetadata = oxNew('oxDbMetaDataHandler');
376 
377  return $oDbMetadata->tableExists($sTable) && $oDbMetadata->fieldExists($sColumn, $sTable);
378  }
379 
386  protected function _addNewMultilangFieldsToDb()
387  {
388  //creating new multilanguage fields with new id over whole DB
389  oxDb::getDb()->startTransaction();
390 
391  $oDbMeta = oxNew("oxDbMetaDataHandler");
392 
393  try {
394  $oDbMeta->addNewLangToDb();
395  } catch (Exception $oEx) {
396  // if exception, rollBack everything
397  oxDb::getDb()->rollbackTransaction();
398 
399  //show warning
400  echo $oEx->getMessage();
401  $oEx = oxNew("oxExceptionToDisplay");
402  $oEx->setMessage('LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS');
403  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
404 
405  return;
406  }
407 
408  oxDb::getDb()->commitTransaction();
409  }
410 
418  protected function _checkLangExists($sAbbr)
419  {
420  $myConfig = $this->getConfig();
421  $aAbbrs = array_keys($this->_aLangData['lang']);
422 
423  if (in_array($sAbbr, $aAbbrs)) {
424  return true;
425  }
426 
427  return false;
428  }
429 
439  protected function _sortLangParamsByBaseIdCallback($oLang1, $oLang2)
440  {
441  return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
442  }
443 
449  protected function _validateInput()
450  {
451  $blResult = true;
452 
453  $sOxId = $this->getEditObjectId();
454  $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
455 
456  // if creating new language, checking if language already exists with
457  // entered language abbreviation
458  if ($sOxId == -1) {
459  if ($this->_checkLangExists($aParams['abbr'])) {
460  $oEx = oxNew('oxExceptionToDisplay');
461  $oEx->setMessage('LANGUAGE_ALREADYEXISTS_ERROR');
462  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
463  $blResult = false;
464  }
465  }
466 
467  // As the abbreviation is used in database view creation, check for allowed characters
468  if (!$this->checkAbbreviationAllowedCharacters($aParams['abbr'])) {
469  $oEx = oxNew('oxExceptionToDisplay');
470  $oEx->setMessage('LANGUAGE_ABBREVIATION_INVALID_ERROR');
471  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
472  $blResult = false;
473  }
474 
475  // checking if language name is not empty
476  if (empty($aParams['desc'])) {
477  $oEx = oxNew('oxExceptionToDisplay');
478  $oEx->setMessage('LANGUAGE_EMPTYLANGUAGENAME_ERROR');
479  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
480  $blResult = false;
481  }
482 
483  return $blResult;
484  }
485 
496  protected function checkAbbreviationAllowedCharacters($sAbbreviation)
497  {
498  $blReturn = false;
499  if (preg_match('/^[a-zA-Z0-9_]*$/', $sAbbreviation)) {
500  $blReturn = true;
501  }
502  return $blReturn;
503  }
504 
512  protected function isValidLanguageData($aLanguageData)
513  {
514  $blValid = true;
515  $configValidator = $this->getNoJsValidator();
516  foreach ($aLanguageData as $mLanguageDataParameters) {
517  if (is_array($mLanguageDataParameters)) {
518  // Recursion till we gonna have a string.
519  $blDeepResult = $this->isValidLanguageData($mLanguageDataParameters);
520  $blValid = $blDeepResult === false ? $blDeepResult : $blValid;
521  } elseif (!$configValidator->isValid($mLanguageDataParameters)) {
522  $blValid = false;
523  $error = oxNew('oxDisplayError');
524  $error->setFormatParameters(htmlspecialchars($mLanguageDataParameters));
525  $error->setMessage("SHOP_CONFIG_ERROR_INVALID_VALUE");
526  oxRegistry::get("oxUtilsView")->addErrorToDisplay($error);
527  }
528  }
529 
530  return $blValid;
531  }
532 
536  protected function getNoJsValidator()
537  {
538  if (is_null($this->oConfigValidator)) {
539  $this->oConfigValidator = oxNew('oxNoJsValidator');
540  }
541 
543  }
544 }