BxBaseStudioFormView.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. $oFunctions = BxTemplStudioFunctions::getInstance();
  56. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  57. if(!empty($aInput['error']) && is_string($aInput['error'])) {
  58. $sLanguage = $aInput['error'];
  59. $aInput['error'] = isset($aInput['checker']['error']) ? $aInput['checker']['error'] : '';
  60. $aInput['error_updated'] = true;
  61. }
  62. else
  63. $sLanguage = $oLanguage->getCurrentLangName(false);
  64. $aLanguages = $oLanguage->getLanguagesInfo();
  65. $aInput['type'] = 'hidden';
  66. $aInput['attrs'] = array_merge($aInputAttrs, array(
  67. 'id' => $sInputIdPrefix . $aInput['name']
  68. ));
  69. $sInput .= $this->genInputStandard($aInput);
  70. $aStrings = !empty($aInput['value']) ? $oLanguage->getLanguageString($aInput['value']) : array();
  71. $aTmplVars = array();
  72. foreach($aLanguages as $aLanguage) {
  73. $bLanguage = $aLanguage['name'] == $sLanguage;
  74. $sValue = '';
  75. if(key_exists($aLanguage['id'], $aStrings))
  76. $sValue = $aStrings[$aLanguage['id']]['string'];
  77. if(!$sValue && isset($aInput['values'][$aLanguage['name']]))
  78. $sValue = $aInput['values'][$aLanguage['name']];
  79. $bValue = !empty($sValue);
  80. $aInput['type'] = $sType;
  81. $aInput['name'] = $sInputName . '-' . $aLanguage['name'];
  82. $aInput['value'] = $sValue;
  83. $aInput['attrs'] = array_merge($aInputAttrs, array(
  84. 'id' => $sInputIdPrefix . $aInput['name']
  85. ));
  86. $sInput .= $this->oTemplate->parseHtmlByName('form_input_translation.html', array(
  87. 'class' => ' bx-form-input-translation-' . $aInput['name'],
  88. 'attrs' => bx_convert_array2attrs(array(
  89. 'style' => !$bLanguage ? 'display:none;' : ''
  90. )),
  91. 'content' => $this->{$aInputMethod[$sType]}($aInput)
  92. ));
  93. $aTmplVarValue = array(
  94. 'condition' => $bValue,
  95. 'content' => array(
  96. 'value' => strmaxtextlen($sValue, $iValueLength)
  97. )
  98. );
  99. $aTmplVarMissing = array(
  100. 'condition' => !$bValue || empty($sValue),
  101. 'content' => array()
  102. );
  103. $aTmplVars[] = array(
  104. 'name' => $sInputName,
  105. 'lang_name' => $aLanguage['name'],
  106. 'lang_flag' => $aLanguage['icon'],
  107. 'lang_title' => $aLanguage['title'],
  108. 'bx_if:hide_active' => array(
  109. 'condition' => !$bLanguage,
  110. 'content' => array()
  111. ),
  112. 'bx_if:act_value' => $aTmplVarValue,
  113. 'bx_if:act_missing' => $aTmplVarMissing,
  114. 'bx_if:hide_passive' => array(
  115. 'condition' => $bLanguage,
  116. 'content' => array()
  117. ),
  118. 'bx_if:pas_value' => $aTmplVarValue,
  119. 'bx_if:pas_missing' => $aTmplVarMissing,
  120. );
  121. }
  122. $sPopup = $this->oTemplate->parseHtmlByName('form_view_translator.html', array('bx_repeat:languages' => $aTmplVars));
  123. $sPopup = $oFunctions->transBox('bx-form-field-translator-popup-' . $sInputName, $sPopup, true);
  124. $aLanguage = $oLanguage->getLanguageInfo($sLanguage);
  125. $sControl = $this->oTemplate->parseHtmlByName('form_view_translatable.html', array(
  126. 'flag' => $aLanguage['icon'],
  127. 'title' => $aLanguage['title'],
  128. 'name' => $sInputName,
  129. 'popup' => $sPopup
  130. ));
  131. return $sInput . $sControl;
  132. }
  133. /**
  134. * Generate Reverse Checkbox Set Element
  135. *
  136. * @param array $aInput
  137. * @return string
  138. */
  139. function genInputCheckboxSet(&$aInput)
  140. {
  141. $aInput['value'] = isset($aInput['value']) && $aInput['value'] && is_array($aInput['value']) ? $aInput['value'] : array();
  142. if(!empty($aInput['reverse']))
  143. $aInput['value'] = array_diff(array_keys($aInput['values']), $aInput['value']);
  144. return parent::genInputCheckboxSet($aInput);
  145. }
  146. }
  147. /** @} */