BxDolStudioPolyglotKeys.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. define('BX_DOL_STUDIO_PK_CATEGORY_SYSTEM', 1);
  10. define('BX_DOL_STUDIO_PK_CATEGORY_CUSTOM', 2);
  11. define('BX_DOL_STUDIO_PK_PREVIEW', 100);
  12. class BxDolStudioPolyglotKeys extends BxTemplStudioGrid
  13. {
  14. protected $iPreviewLength;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. parent::__construct ($aOptions, $oTemplate);
  18. $this->oDb = new BxDolStudioPolyglotQuery();
  19. $this->iPreviewLength = BX_DOL_STUDIO_PK_PREVIEW;
  20. }
  21. protected function add(&$oForm)
  22. {
  23. $sKey = $oForm->getCleanValue('key');
  24. $iCategoryId = (int)$oForm->getCleanValue('category_id');
  25. $aLanguages = explode(',', $oForm->getCleanValue('languages'));
  26. if(empty($sKey) || empty($iCategoryId))
  27. return _t('_adm_pgt_err_create_key_empty_fields');
  28. $aStrings = array();
  29. foreach($aLanguages as $iLanguageId)
  30. $aStrings[$iLanguageId] = $oForm->getCleanValue('language_' . $iLanguageId);
  31. $mixedResult = BxDolStudioLanguagesUtils::getInstance()->addLanguageString($sKey, $aStrings, 0, $iCategoryId);
  32. if($mixedResult === false)
  33. return _t('_adm_pgt_err_create_key');
  34. return $mixedResult;
  35. }
  36. protected function edit(&$oForm)
  37. {
  38. $iId = $oForm->getCleanValue('id');
  39. $aLanguages = explode(',', $oForm->getCleanValue('languages'));
  40. if(empty($iId) || empty($aLanguages))
  41. return _t('_adm_pgt_err_save');
  42. $aStrings = array();
  43. foreach($aLanguages as $iLanguageId)
  44. $aStrings[$iLanguageId] = $oForm->getCleanValue('language_' . $iLanguageId);
  45. if(!BxDolStudioLanguagesUtils::getInstance()->updateLanguageStringById($iId, $aStrings))
  46. return _t('_adm_pgt_err_save');
  47. return $iId;
  48. }
  49. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  50. {
  51. $iModule = 0;
  52. if(strpos($sFilter, $this->sParamsDivider) !== false)
  53. list($iModule, $sFilter) = explode($this->sParamsDivider, $sFilter);
  54. $iLanguage = BxDolLanguages::getInstance()->getCurrentLangId();
  55. $aLanguage = array();
  56. $this->oDb->getLanguagesBy(array('type' => 'by_id', 'value' => $iLanguage), $aLanguage, false);
  57. if(empty($aLanguage) || !is_array($aLanguage))
  58. $this->oDb->getLanguagesBy(array('type' => 'default'), $aLanguage, false);
  59. if(empty($aLanguage) || !is_array($aLanguage)) {
  60. $aLanguages = array();
  61. $iLanguages = $this->oDb->getLanguagesBy(array('type' => 'active'), $aLanguages);
  62. if($iLanguages <= 0 || empty($aLanguages))
  63. return array();
  64. $aLanguage = $aLanguages[0];
  65. }
  66. $this->_aOptions['source'] = sprintf($this->_aOptions['source'], $aLanguage['id']);
  67. if((int)$iModule != 0)
  68. $this->_aOptions['source'] .= $this->oDb->prepareAsString(" AND `tlk`.`IDCategory`=?", $iModule);
  69. $aResults = parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  70. foreach($aResults as $iIndex => $aResult) {
  71. $aKey = $this->oDb->getKeyFullInfo($aResult['id'], $this->iPreviewLength);
  72. if(empty($aKey))
  73. continue;
  74. $aResults[$iIndex]['module'] = $aKey['module'];
  75. $aResults[$iIndex]['string'] = $aKey['strings'][$aLanguage['name']]['preview'];
  76. $aResults[$iIndex]['languages'] = implode(', ', array_keys($aKey['strings']));
  77. }
  78. return $aResults;
  79. }
  80. }
  81. /** @} */