BxBaseStudioFormView.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioFormView extends BxDolStudioForm
  11. {
  12. function __construct($aInfo, $oTemplate)
  13. {
  14. parent::__construct($aInfo, $oTemplate);
  15. }
  16. function addCssJs()
  17. {
  18. parent::addCssJs();
  19. $this->oTemplate->addJs(array(
  20. 'forms.js'
  21. ));
  22. $this->oTemplate->addCss(array(
  23. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flag-icon-css/css/|flag-icon.min.css'
  24. ));
  25. }
  26. function genRowStandard(&$aInput, $isOneLine = false)
  27. {
  28. $aInput['attrs_wrapper']['class'] ??= '';
  29. $aInput['attrs_wrapper']['class'] .= ' bx-form-input-translatable';
  30. return parent::genRowStandard($aInput, $isOneLine);
  31. }
  32. function genInput(&$aInput)
  33. {
  34. $sInput = '';
  35. switch($aInput['type']) {
  36. case 'text_translatable':
  37. $sInput = $this->genInputTranslatable($aInput, 'text');
  38. break;
  39. case 'textarea_translatable':
  40. $sInput = $this->genInputTranslatable($aInput, 'textarea');
  41. break;
  42. default:
  43. $sInput = parent::genInput($aInput);
  44. }
  45. return $sInput;
  46. }
  47. function genInputTranslatable(&$aInput, $sType = 'text')
  48. {
  49. $iValueLength = 20;
  50. $sInputIdPrefix = 'bx-form-input-';
  51. $aInputMethod = array('text' => 'genInputStandard', 'textarea' => 'genInputTextarea');
  52. $sInput = '';
  53. $sInputName = $aInput['name'];
  54. $aInputAttrs = isset($aInput['attrs']) && is_array($aInput['attrs']) ? $aInput['attrs'] : array();
  55. $aInputLang = $aInput;
  56. $oFunctions = BxTemplStudioFunctions::getInstance();
  57. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  58. if(!empty($aInput['error']) && is_string($aInput['error'])) {
  59. $sLanguage = $aInput['error'];
  60. $aInput['error'] = isset($aInput['checker']['error']) ? $aInput['checker']['error'] : '';
  61. $aInput['error_updated'] = true;
  62. }
  63. else
  64. $sLanguage = $oLanguage->getCurrentLangName(false);
  65. $aLanguages = $oLanguage->getLanguagesInfo();
  66. if(!$this->_bIsApi)
  67. $aInput['type'] = 'hidden';
  68. $aInput['translations'] = [];
  69. $aInput['attrs'] = array_merge($aInputAttrs, array(
  70. 'id' => $sInputIdPrefix . $aInput['name']
  71. ));
  72. $sInput .= $this->genInputStandard($aInput);
  73. $aStrings = !empty($aInput['value']) ? $oLanguage->getLanguageString($aInput['value']) : array();
  74. $aTmplVars = [];
  75. foreach($aLanguages as $aLanguage) {
  76. $bLanguage = $aLanguage['name'] == $sLanguage;
  77. $sInputLangName = $sInputName . '-' . $aLanguage['name'];
  78. $aInput['translations'][] = [
  79. 'title' => $aLanguage['title'],
  80. 'name' => $sInputLangName
  81. ];
  82. if($this->_bIsApi)
  83. continue;
  84. $sValue = '';
  85. if(key_exists($aLanguage['id'], $aStrings))
  86. $sValue = $aStrings[$aLanguage['id']]['string'];
  87. if(!$sValue && isset($aInput['values'][$aLanguage['name']]))
  88. $sValue = $aInput['values'][$aLanguage['name']];
  89. $bValue = !empty($sValue);
  90. $aInputLang['type'] = $sType;
  91. $aInputLang['name'] = $sInputLangName;
  92. $aInputLang['value'] = $sValue;
  93. $aInputLang['attrs'] = array_merge($aInputAttrs, array(
  94. 'id' => $sInputIdPrefix . $aInputLang['name']
  95. ));
  96. $sInput .= $this->oTemplate->parseHtmlByName('form_input_translation.html', array(
  97. 'class' => ' bx-form-input-translation-' . $aInputLang['name'],
  98. 'attrs' => bx_convert_array2attrs(array(
  99. 'style' => !$bLanguage ? 'display:none;' : ''
  100. )),
  101. 'content' => $this->{$aInputMethod[$sType]}($aInputLang)
  102. ));
  103. $aTmplVarValue = array(
  104. 'condition' => $bValue,
  105. 'content' => array(
  106. 'value' => strmaxtextlen($sValue, $iValueLength)
  107. )
  108. );
  109. $aTmplVarMissing = array(
  110. 'condition' => !$bValue || empty($sValue),
  111. 'content' => array()
  112. );
  113. $aTmplVars[] = array(
  114. 'name' => $sInputName,
  115. 'lang_name' => $aLanguage['name'],
  116. 'lang_flag' => $aLanguage['icon'],
  117. 'lang_title' => $aLanguage['title'],
  118. 'bx_if:hide_active' => array(
  119. 'condition' => !$bLanguage,
  120. 'content' => array()
  121. ),
  122. 'bx_if:act_value' => $aTmplVarValue,
  123. 'bx_if:act_missing' => $aTmplVarMissing,
  124. 'bx_if:hide_passive' => array(
  125. 'condition' => $bLanguage,
  126. 'content' => array()
  127. ),
  128. 'bx_if:pas_value' => $aTmplVarValue,
  129. 'bx_if:pas_missing' => $aTmplVarMissing,
  130. );
  131. }
  132. $sPopup = $this->oTemplate->parseHtmlByName('form_view_translator.html', array('bx_repeat:languages' => $aTmplVars));
  133. $sPopup = $oFunctions->transBox('bx-form-field-translator-popup-' . $sInputName, $sPopup, true);
  134. $aLanguage = $oLanguage->getLanguageInfo($sLanguage);
  135. $sControl = $this->oTemplate->parseHtmlByName('form_view_translatable.html', array(
  136. 'flag' => $aLanguage['icon'],
  137. 'title' => $aLanguage['title'],
  138. 'name' => $sInputName,
  139. 'popup' => $sPopup
  140. ));
  141. return $sInput . $sControl;
  142. }
  143. /**
  144. * Generate Reverse Checkbox Set Element
  145. *
  146. * @param array $aInput
  147. * @return string
  148. */
  149. function genInputCheckboxSet(&$aInput)
  150. {
  151. $aInput['value'] = isset($aInput['value']) && $aInput['value'] && is_array($aInput['value']) ? $aInput['value'] : array();
  152. if(!empty($aInput['reverse']))
  153. $aInput['value'] = array_diff(array_keys($aInput['values']), $aInput['value']);
  154. return parent::genInputCheckboxSet($aInput);
  155. }
  156. }
  157. /** @} */