OXID eShop CE  4.9.5
 All Classes 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 
45  public function render()
46  {
47  $myConfig = $this->getConfig();
48 
49 
51 
52  $sOxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
53  //loading languages info from config
54  $this->_aLangData = $this->_getLanguages();
55 
56  if ($sOxId != -1) {
57  //checking if translations files exists
58  $this->_checkLangTranslations($sOxId);
59  }
60 
61  if ($sOxId != "-1" && isset($sOxId)) {
62  $this->_aViewData["edit"] = $this->_getLanguageInfo($sOxId);
63  }
64 
65  return "language_main.tpl";
66  }
67 
73  public function save()
74  {
75  $myConfig = $this->getConfig();
76 
77 
78  parent::save();
79 
80  $sOxId = $this->getEditObjectId();
81  $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
82 
83  if (!isset($aParams['active'])) {
84  $aParams['active'] = 0;
85  }
86 
87  if (!isset($aParams['default'])) {
88  $aParams['default'] = false;
89  }
90 
91  if (empty($aParams['sort'])) {
92  $aParams['sort'] = '99999';
93  }
94 
95  //loading languages info from config
96  $this->_aLangData = $this->_getLanguages();
97  //checking input errors
98  if (!$this->_validateInput()) {
99  return;
100  }
101 
102  $blViewError = false;
103 
104  // if changed language abbervation, updating it for all arrays related with languages
105  if ($sOxId != -1 && $sOxId != $aParams['abbr']) {
106  // #0004850 preventing changing abbr for main language with base id = 0
107  if ((int) $this->_aLangData['params'][$sOxId]['baseId'] == 0) {
108  $oEx = oxNew("oxExceptionToDisplay");
109  $oEx->setMessage('LANGUAGE_ABBRCHANGEMAINLANG_WARNING');
110  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
111  $aParams['abbr'] = $sOxId;
112  } else {
113  $this->_updateAbbervation($sOxId, $aParams['abbr']);
114  $sOxId = $aParams['abbr'];
115  $this->setEditObjectId($sOxId);
116 
117  $blViewError = true;
118  }
119  }
120 
121  // if adding new language, setting lang id to abbervation
122  if ($blNewLanguage = ($sOxId == -1)) {
123  $sOxId = $aParams['abbr'];
124  $this->_aLangData['params'][$sOxId]['baseId'] = $this->_getAvailableLangBaseId();
125  $this->setEditObjectId($sOxId);
126  }
127 
128  //updating language description
129  $this->_aLangData['lang'][$sOxId] = $aParams['desc'];
130 
131  //updating language parameters
132  $this->_aLangData['params'][$sOxId]['active'] = $aParams['active'];
133  $this->_aLangData['params'][$sOxId]['default'] = $aParams['default'];
134  $this->_aLangData['params'][$sOxId]['sort'] = $aParams['sort'];
135 
136  //if setting lang as default
137  if ($aParams['default'] == '1') {
138  $this->_setDefaultLang($sOxId);
139  }
140 
141  //updating language urls
142  $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
143  $this->_aLangData['urls'][$iBaseId] = $aParams['baseurl'];
144  $this->_aLangData['sslUrls'][$iBaseId] = $aParams['basesslurl'];
145 
146  //sort parameters, urls and languages arrays by language base id
147  $this->_sortLangArraysByBaseId();
148 
149  $this->_aViewData["updatelist"] = "1";
150 
151  //saving languages info
152  $this->getConfig()->saveShopConfVar('aarr', 'aLanguageParams', $this->_aLangData['params']);
153  $this->getConfig()->saveShopConfVar('aarr', 'aLanguages', $this->_aLangData['lang']);
154  $this->getConfig()->saveShopConfVar('arr', 'aLanguageURLs', $this->_aLangData['urls']);
155  $this->getConfig()->saveShopConfVar('arr', 'aLanguageSSLURLs', $this->_aLangData['sslUrls']);
156 
157  //checking if added language already has created multilang fields
158  //with new base ID - if not, creating new fields
159  if ($blNewLanguage) {
160  if (!$this->_checkMultilangFieldsExistsInDb($sOxId)) {
162  } else {
163  $blViewError = true;
164  }
165  }
166 
167  // show message for user to generate views
168  if ($blViewError) {
169  $oEx = oxNew('oxExceptionToDisplay');
170  $oEx->setMessage('LANGUAGE_ERRORGENERATEVIEWS');
171  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
172  }
173  }
174 
182  protected function _getLanguageInfo($sOxId)
183  {
184  $sDefaultLang = $this->getConfig()->getConfigParam('sDefaultLang');
185 
186  $aLangData = $this->_aLangData['params'][$sOxId];
187  $aLangData['abbr'] = $sOxId;
188  $aLangData['desc'] = $this->_aLangData['lang'][$sOxId];
189  $aLangData['baseurl'] = $this->_aLangData['urls'][$aLangData['baseId']];
190  $aLangData['basesslurl'] = $this->_aLangData['sslUrls'][$aLangData['baseId']];
191  $aLangData['default'] = ($this->_aLangData['params'][$sOxId]["baseId"] == $sDefaultLang) ? true : false;
192 
193  return $aLangData;
194  }
195 
201  protected function _setLanguages($aLangData)
202  {
203  $this->_aLangData = $aLangData;
204  }
205 
213  protected function _getLanguages()
214  {
215  $aLangData['params'] = $this->getConfig()->getConfigParam('aLanguageParams');
216  $aLangData['lang'] = $this->getConfig()->getConfigParam('aLanguages');
217  $aLangData['urls'] = $this->getConfig()->getConfigParam('aLanguageURLs');
218  $aLangData['sslUrls'] = $this->getConfig()->getConfigParam('aLanguageSSLURLs');
219 
220  // empty languages parameters array - creating new one with default values
221  if (!is_array($aLangData['params'])) {
222  $aLangData['params'] = $this->_assignDefaultLangParams($aLangData['lang']);
223  }
224 
225  return $aLangData;
226  }
227 
234  protected function _updateAbbervation($sOldId, $sNewId)
235  {
236  foreach (array_keys($this->_aLangData) as $sTypeKey) {
237 
238  if (is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0) {
239 
240  if ($sTypeKey == 'urls' || $sTypeKey == 'sslUrls') {
241  continue;
242  }
243 
244  $aKeys = array_keys($this->_aLangData[$sTypeKey]);
245  $aValues = array_values($this->_aLangData[$sTypeKey]);
246  //find and replace key
247  $iReplaceId = array_search($sOldId, $aKeys);
248  $aKeys[$iReplaceId] = $sNewId;
249 
250  $this->_aLangData[$sTypeKey] = array_combine($aKeys, $aValues);
251  }
252  }
253  }
254 
259  protected function _sortLangArraysByBaseId()
260  {
261  $aUrls = array();
262  $aSslUrls = array();
263  $aLanguages = array();
264 
265  uasort($this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback'));
266 
267  foreach ($this->_aLangData['params'] as $sAbbr => $aParams) {
268  $iId = (int) $aParams['baseId'];
269  $aUrls[$iId] = $this->_aLangData['urls'][$iId];
270  $aSslUrls[$iId] = $this->_aLangData['sslUrls'][$iId];
271  $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
272  }
273 
274  $this->_aLangData['lang'] = $aLanguages;
275  $this->_aLangData['urls'] = $aUrls;
276  $this->_aLangData['sslUrls'] = $aSslUrls;
277  }
278 
287  {
288  $aParams = array();
289  $iBaseId = 0;
290 
291  foreach (array_keys($aLanguages) as $sOxId) {
292  $aParams[$sOxId]['baseId'] = $iBaseId;
293  $aParams[$sOxId]['active'] = 1;
294  $aParams[$sOxId]['sort'] = $iBaseId + 1;
295 
296  $iBaseId++;
297  }
298 
299  return $aParams;
300  }
301 
307  protected function _setDefaultLang($sOxId)
308  {
309  $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
310  $this->getConfig()->saveShopConfVar('str', 'sDefaultLang', $sDefaultId);
311  }
312 
318  protected function _getAvailableLangBaseId()
319  {
320  $aBaseId = array();
321  foreach ($this->_aLangData['params'] as $aLang) {
322  $aBaseId[] = $aLang['baseId'];
323  }
324 
325  $iNewId = 0;
326  sort($aBaseId);
327  $iTotal = count($aBaseId);
328 
329  //getting first available id
330  while ($iNewId <= $iTotal) {
331  if ($iNewId !== $aBaseId[$iNewId]) {
332  break;
333  }
334  $iNewId++;
335  }
336 
337  return $iNewId;
338  }
339 
346  protected function _checkLangTranslations($sOxId)
347  {
348  $myConfig = $this->getConfig();
349 
350  $sDir = dirname($myConfig->getTranslationsDir('lang.php', $sOxId));
351 
352  if (empty($sDir)) {
353  $oEx = oxNew("oxExceptionToDisplay");
354  $oEx->setMessage('LANGUAGE_NOTRANSLATIONS_WARNING');
355  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
356  }
357  }
358 
366  protected function _checkMultilangFieldsExistsInDb($sOxId)
367  {
368  $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
369  $sTable = getLangTableName('oxarticles', $iBaseId);
370  $sColumn = 'oxtitle' . oxRegistry::getLang()->getLanguageTag($iBaseId);
371 
372  $oDbMetadata = oxNew('oxDbMetaDataHandler');
373 
374  return $oDbMetadata->tableExists($sTable) && $oDbMetadata->fieldExists($sColumn, $sTable);
375  }
376 
383  protected function _addNewMultilangFieldsToDb()
384  {
385  //creating new multilanguage fields with new id over whole DB
386  oxDb::getDb()->startTransaction();
387 
388  $oDbMeta = oxNew("oxDbMetaDataHandler");
389 
390  try {
391  $oDbMeta->addNewLangToDb();
392  } catch (Exception $oEx) {
393  // if exception, rollBack everything
394  oxDb::getDb()->rollbackTransaction();
395 
396  //show warning
397  echo $oEx->getMessage();
398  $oEx = oxNew("oxExceptionToDisplay");
399  $oEx->setMessage('LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS');
400  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
401 
402  return;
403  }
404 
405  oxDb::getDb()->commitTransaction();
406  }
407 
415  protected function _checkLangExists($sAbbr)
416  {
417  $myConfig = $this->getConfig();
418  $aAbbrs = array_keys($this->_aLangData['lang']);
419 
420  if (in_array($sAbbr, $aAbbrs)) {
421  return true;
422  }
423 
424  return false;
425  }
426 
436  protected function _sortLangParamsByBaseIdCallback($oLang1, $oLang2)
437  {
438  return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
439  }
440 
446  protected function _validateInput()
447  {
448  $blResult = true;
449 
450  $sOxId = $this->getEditObjectId();
451  $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
452 
453  // if creating new language, checking if language already exists with
454  // entered language abbervation
455  if ($sOxId == -1) {
456  if ($this->_checkLangExists($aParams['abbr'])) {
457  $oEx = oxNew('oxExceptionToDisplay');
458  $oEx->setMessage('LANGUAGE_ALREADYEXISTS_ERROR');
459  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
460  $blResult = false;
461  }
462  }
463 
464  // checking if language name is not empty
465  if (empty($aParams['desc'])) {
466  $oEx = oxNew('oxExceptionToDisplay');
467  $oEx->setMessage('LANGUAGE_EMPTYLANGUAGENAME_ERROR');
468  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
469  $blResult = false;
470  }
471 
472  return $blResult;
473  }
474 }