OXID eShop CE  4.8.12
 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 
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 = oxConfig::getParameter( "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  oxUtilsView::getInstance()->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 
206  protected function _setLanguages( $aLangData )
207  {
208  $this->_aLangData = $aLangData;
209  }
210 
218  protected function _getLanguages()
219  {
220  $aLangData['params'] = $this->getConfig()->getConfigParam( 'aLanguageParams' );
221  $aLangData['lang'] = $this->getConfig()->getConfigParam( 'aLanguages' );
222  $aLangData['urls'] = $this->getConfig()->getConfigParam( 'aLanguageURLs' );
223  $aLangData['sslUrls'] = $this->getConfig()->getConfigParam( 'aLanguageSSLURLs' );
224 
225  // empty languages parameters array - creating new one with default values
226  if ( !is_array( $aLangData['params']) ) {
227  $aLangData['params'] = $this->_assignDefaultLangParams( $aLangData['lang'] );
228  }
229 
230  return $aLangData;
231  }
232 
241  protected function _updateAbbervation( $sOldId, $sNewId )
242  {
243  foreach ( array_keys($this->_aLangData) as $sTypeKey ) {
244 
245  if ( is_array($this->_aLangData[$sTypeKey]) && count($this->_aLangData[$sTypeKey]) > 0 ) {
246 
247  if ( $sTypeKey == 'urls' || $sTypeKey == 'sslUrls' ) {
248  continue;
249  }
250 
251  $aKeys = array_keys( $this->_aLangData[$sTypeKey] );
252  $aValues = array_values( $this->_aLangData[$sTypeKey] );
253  //find and replace key
254  $iReplaceId = array_search( $sOldId, $aKeys );
255  $aKeys[$iReplaceId] = $sNewId;
256 
257  $this->_aLangData[$sTypeKey] = array_combine( $aKeys, $aValues );
258  }
259  }
260  }
261 
268  protected function _sortLangArraysByBaseId()
269  {
270  $aUrls = array();
271  $aSslUrls = array();
272  $aLanguages = array();
273 
274  uasort( $this->_aLangData['params'], array($this, '_sortLangParamsByBaseIdCallback') );
275 
276  foreach ( $this->_aLangData['params'] as $sAbbr => $aParams ) {
277  $iId = (int)$aParams['baseId'];
278  $aUrls[$iId] = $this->_aLangData['urls'][$iId];
279  $aSslUrls[$iId] = $this->_aLangData['sslUrls'][$iId];
280  $aLanguages[$sAbbr] = $this->_aLangData['lang'][$sAbbr];
281  }
282 
283  $this->_aLangData['lang'] = $aLanguages;
284  $this->_aLangData['urls'] = $aUrls;
285  $this->_aLangData['sslUrls'] = $aSslUrls;
286  }
287 
295  protected function _assignDefaultLangParams( $aLanguages )
296  {
297  $aParams = array();
298  $iBaseId = 0;
299 
300  foreach ( array_keys($aLanguages) as $sOxId ) {
301  $aParams[$sOxId]['baseId'] = $iBaseId;
302  $aParams[$sOxId]['active'] = 1;
303  $aParams[$sOxId]['sort'] = $iBaseId + 1;
304 
305  $iBaseId++;
306  }
307 
308  return $aParams;
309  }
310 
318  protected function _setDefaultLang( $sOxId )
319  {
320  $sDefaultId = $this->_aLangData['params'][$sOxId]['baseId'];
321  $this->getConfig()->saveShopConfVar( 'str', 'sDefaultLang', $sDefaultId );
322  }
323 
329  protected function _getAvailableLangBaseId()
330  {
331  $aBaseId = array();
332  foreach ( $this->_aLangData['params'] as $aLang ) {
333  $aBaseId[] = $aLang['baseId'];
334  }
335 
336  $iNewId = 0;
337  sort( $aBaseId );
338  $iTotal = count($aBaseId);
339 
340  //getting first available id
341  while ( $iNewId <= $iTotal ) {
342  if ( $iNewId !== $aBaseId[$iNewId] ) {
343  break;
344  }
345  $iNewId++;
346  }
347 
348  return $iNewId;
349  }
350 
359  protected function _checkLangTranslations( $sOxId )
360  {
361  $myConfig = $this->getConfig();
362 
363  $sDir = dirname( $myConfig->getTranslationsDir( 'lang.php', $sOxId ) );
364 
365  if ( empty($sDir) ) {
366  $oEx = oxNew( "oxExceptionToDisplay" );
367  $oEx->setMessage( 'LANGUAGE_NOTRANSLATIONS_WARNING' );
368  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
369  }
370  }
371 
379  protected function _checkMultilangFieldsExistsInDb( $sOxId )
380  {
381  $iBaseId = $this->_aLangData['params'][$sOxId]['baseId'];
382  $sTable = getLangTableName('oxarticles', $iBaseId );
383  $sColumn = 'oxtitle' . oxRegistry::getLang()->getLanguageTag( $iBaseId );
384 
385  $oDbMetadata = oxNew('oxDbMetaDataHandler');
386  return $oDbMetadata->tableExists( $sTable ) && $oDbMetadata->fieldExists( $sColumn, $sTable );
387  }
388 
395  protected function _addNewMultilangFieldsToDb()
396  {
397  //creating new multilanguage fields with new id over whole DB
398  oxDb::getDb()->startTransaction();
399 
400  $oDbMeta = oxNew( "oxDbMetaDataHandler" );
401 
402  try {
403  $oDbMeta->addNewLangToDb();
404  } catch( Exception $oEx ) {
405  // if exception, rollBack everything
406  oxDb::getDb()->rollbackTransaction();
407 
408  //show warning
409  echo $oEx->getMessage();
410  $oEx = oxNew( "oxExceptionToDisplay" );
411  $oEx->setMessage( 'LANGUAGE_ERROR_ADDING_MULTILANG_FIELDS' );
412  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
413 
414  return;
415  }
416 
417  oxDb::getDb()->commitTransaction();
418  }
419 
427  protected function _checkLangExists( $sAbbr )
428  {
429  $myConfig = $this->getConfig();
430  $aAbbrs = array_keys($this->_aLangData['lang']);
431 
432  if ( in_array( $sAbbr, $aAbbrs ) ) {
433  return true;
434  }
435 
436  return false;
437  }
438 
448  protected function _sortLangParamsByBaseIdCallback( $oLang1, $oLang2 )
449  {
450  return ($oLang1['baseId'] < $oLang2['baseId']) ? -1 : 1;
451  }
452 
458  protected function _validateInput()
459  {
460  $blResult = true;
461 
462  $sOxId = $this->getEditObjectId();
463  $aParams = oxConfig::getParameter( "editval" );
464 
465  // if creating new language, checking if language already exists with
466  // entered language abbreviation
467  if ( $sOxId == -1 ) {
468  if ( $this->_checkLangExists( $aParams['abbr'] ) ) {
469  $oEx = oxNew( 'oxExceptionToDisplay' );
470  $oEx->setMessage( 'LANGUAGE_ALREADYEXISTS_ERROR' );
471  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
472  $blResult = false;
473  }
474  }
475 
476  // As the abbreviation is used in database view creation, check for allowed characters
477  if (!$this->checkAbbreviationAllowedCharacters($aParams['abbr'])) {
478  $oEx = oxNew('oxExceptionToDisplay');
479  $oEx->setMessage('LANGUAGE_ABBREVIATION_INVALID_ERROR');
480  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
481  $blResult = false;
482  }
483 
484  // checking if language name is not empty
485  if ( empty($aParams['desc']) ) {
486  $oEx = oxNew( 'oxExceptionToDisplay' );
487  $oEx->setMessage( 'LANGUAGE_EMPTYLANGUAGENAME_ERROR' );
488  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
489  $blResult = false;
490  }
491 
492  return $blResult;
493  }
494 
505  protected function checkAbbreviationAllowedCharacters($sAbbreviation)
506  {
507  $blReturn = false;
508  if (preg_match('/^[a-zA-Z0-9_]*$/', $sAbbreviation)) {
509  $blReturn = true;
510  }
511  return $blReturn;
512  }
513 
521  protected function isValidLanguageData($aLanguageData)
522  {
523  $blValid = true;
524  $configValidator = $this->getNoJsValidator();
525  foreach ($aLanguageData as $mLanguageDataParameters) {
526  if (is_array($mLanguageDataParameters)) {
527  // Recursion till we gonna have a string.
528  $blDeepResult = $this->isValidLanguageData($mLanguageDataParameters);
529  $blValid = $blDeepResult === false ? $blDeepResult : $blValid;
530  } elseif (!$configValidator->isValid($mLanguageDataParameters)) {
531  $blValid = false;
532  $error = oxNew('oxDisplayError');
533  $error->setFormatParameters(htmlspecialchars($mLanguageDataParameters));
534  $error->setMessage("SHOP_CONFIG_ERROR_INVALID_VALUE");
535  oxRegistry::get("oxUtilsView")->addErrorToDisplay($error);
536  }
537  }
538 
539  return $blValid;
540  }
541 
545  protected function getNoJsValidator()
546  {
547  if (is_null($this->oConfigValidator)) {
548  $this->oConfigValidator = oxNew('oxNoJsValidator');
549  }
550 
552  }
553 }