BxBaseStudioFormView.php 5.7 KB

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