BxBaseStudioFormView.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 genRow(&$aInput)
  27. {
  28. $sRow = '';
  29. switch($aInput['type']) {
  30. case 'image_uploader':
  31. $sRow = $this->genRowImageUploader($aInput);
  32. break;
  33. default:
  34. $sRow = parent::genRow($aInput);
  35. }
  36. return $sRow;
  37. }
  38. function genInput(&$aInput)
  39. {
  40. $sInput = '';
  41. switch($aInput['type']) {
  42. case 'text_translatable':
  43. $sInput = $this->genInputTranslatable($aInput, 'text');
  44. break;
  45. case 'textarea_translatable':
  46. $aInput['html'] = 0;
  47. $sInput = $this->genInputTranslatable($aInput, 'textarea');
  48. break;
  49. default:
  50. $sInput = parent::genInput($aInput);
  51. }
  52. return $sInput;
  53. }
  54. function genRowImageUploader(&$aInput)
  55. {
  56. $aTmplVarsPreview = array(
  57. 'bx_if:show_empty' => array(
  58. 'condition' => true,
  59. 'content' => array()
  60. ),
  61. 'bx_if:show_image' => array(
  62. 'condition' => false,
  63. 'content' => array()
  64. )
  65. );
  66. if(($iId = (int)$aInput['value']) != 0) {
  67. $sFileUrl = false;
  68. if (!empty($aInput['transcoder_object'])) {
  69. $oTranscoder = BxDolTranscoderImage::getObjectInstance($aInput['transcoder_object']);
  70. $sFileUrl = $oTranscoder->getFileUrlNotReady($iId);
  71. if (isset($aInput['transcoder_image_width']) && $aInput['transcoder_image_width'] > 0)
  72. $sFileUrl = bx_append_url_params($sFileUrl, array('x' => $aInput['transcoder_image_width']));
  73. if (isset($aInput['transcoder_image_height']) && $aInput['transcoder_image_height'] > 0)
  74. $sFileUrl = bx_append_url_params($sFileUrl, array('y' => $aInput['transcoder_image_height']));
  75. }
  76. else {
  77. $sStorage = isset($aInput['storage_object']) && $aInput['storage_object'] != '' ? $aInput['storage_object'] : BX_DOL_STORAGE_OBJ_IMAGES;
  78. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  79. $sFileUrl = $oStorage->getFileUrlById($iId);
  80. }
  81. if($sFileUrl !== false) {
  82. $aTmplVarsPreview['bx_if:show_empty']['condition'] = false;
  83. $aTmplVarsPreview['bx_if:show_image'] = array(
  84. 'condition' => true,
  85. 'content' => array(
  86. 'url' => $sFileUrl,
  87. 'bx_if:show_action_delete' => array(
  88. 'condition' => isset($aInput['ajax_action_delete']) && $aInput['ajax_action_delete'] != "",
  89. 'content' => array(
  90. 'action' => $aInput['ajax_action_delete']
  91. )
  92. )
  93. )
  94. );
  95. }
  96. }
  97. $sRow = '';
  98. $aInputPreview = array(
  99. 'type' => 'custom',
  100. 'name' => 'preview',
  101. 'caption' => isset($aInput['caption_preview']) ? $aInput['caption_preview'] : _t('_adm_txt_form_view_iu_preview'),
  102. 'content' => $this->oTemplate->parseHtmlByName('form_view_iu_preview.html', $aTmplVarsPreview)
  103. );
  104. $sRow .= $this->genRow($aInputPreview);
  105. $aInput['type'] = 'file';
  106. $sRow .= $this->genRow($aInput);
  107. return $sRow;
  108. }
  109. function genInputTranslatable(&$aInput, $sType = 'text')
  110. {
  111. $iValueLength = 20;
  112. $sInputIdPrefix = 'bx-form-input-';
  113. $aInputMethod = array('text' => 'genInputStandard', 'textarea' => 'genInputTextarea');
  114. $sInput = '';
  115. $sInputName = $aInput['name'];
  116. $aInputAttrs = isset($aInput['attrs']) && is_array($aInput['attrs']) ? $aInput['attrs'] : array();
  117. $oFunctions = BxTemplStudioFunctions::getInstance();
  118. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  119. $sLanguage = $oLanguage->getCurrentLangName(false);
  120. $aLanguages = $oLanguage->getLanguagesInfo();
  121. $aInput['type'] = 'hidden';
  122. $aInput['attrs'] = array_merge($aInputAttrs, array(
  123. 'id' => $sInputIdPrefix . $aInput['name']
  124. ));
  125. $sInput .= $this->genInputStandard($aInput);
  126. $aStrings = !empty($aInput['value']) ? $oLanguage->getLanguageString($aInput['value']) : array();
  127. $aTmplVars = array();
  128. foreach($aLanguages as $aLanguage) {
  129. $bLanguage = $aLanguage['name'] == $sLanguage;
  130. $sValue = '';
  131. if(($bValue = key_exists($aLanguage['id'], $aStrings)) === true)
  132. $sValue = $aStrings[$aLanguage['id']]['string'];
  133. else if(isset($aInput['values'][$aLanguage['name']]))
  134. $sValue = $aInput['values'][$aLanguage['name']];
  135. $aInput['type'] = $sType;
  136. $aInput['name'] = $sInputName . '-' . $aLanguage['name'];
  137. $aInput['value'] = $sValue;
  138. $aInput['attrs'] = array_merge($aInputAttrs, array(
  139. 'id' => $sInputIdPrefix . $aInput['name'],
  140. 'style' => !$bLanguage ? 'display:none;' : ''
  141. ));
  142. $sInput .= $this->{$aInputMethod[$sType]}($aInput);
  143. $aTmplVarValue = array(
  144. 'condition' => $bValue,
  145. 'content' => array(
  146. 'value' => strmaxtextlen($sValue, $iValueLength)
  147. )
  148. );
  149. $aTmplVarMissing = array(
  150. 'condition' => !$bValue || empty($sValue),
  151. 'content' => array()
  152. );
  153. $aTmplVars[] = array(
  154. 'name' => $sInputName,
  155. 'lang_name' => $aLanguage['name'],
  156. 'lang_flag' => $aLanguage['icon'],
  157. 'lang_title' => $aLanguage['title'],
  158. 'bx_if:hide_active' => array(
  159. 'condition' => !$bLanguage,
  160. 'content' => array()
  161. ),
  162. 'bx_if:act_value' => $aTmplVarValue,
  163. 'bx_if:act_missing' => $aTmplVarMissing,
  164. 'bx_if:hide_passive' => array(
  165. 'condition' => $bLanguage,
  166. 'content' => array()
  167. ),
  168. 'bx_if:pas_value' => $aTmplVarValue,
  169. 'bx_if:pas_missing' => $aTmplVarMissing,
  170. );
  171. }
  172. $sPopup = $this->oTemplate->parseHtmlByName('form_view_translator.html', array('bx_repeat:languages' => $aTmplVars));
  173. $sPopup = $oFunctions->transBox('bx-form-field-translator-popup-' . $sInputName, $sPopup, true);
  174. $aLanguage = $oLanguage->getLanguageInfo($sLanguage);
  175. $sControl = $this->oTemplate->parseHtmlByName('form_view_translatable.html', array(
  176. 'flag' => $aLanguage['icon'],
  177. 'title' => $aLanguage['title'],
  178. 'name' => $sInputName,
  179. 'popup' => $sPopup
  180. ));
  181. return $sInput . $sControl;
  182. }
  183. /**
  184. * Generate Reverse Checkbox Set Element
  185. *
  186. * @param array $aInput
  187. * @return string
  188. */
  189. function genInputCheckboxSet(&$aInput)
  190. {
  191. $aInput['value'] = isset($aInput['value']) && $aInput['value'] && is_array($aInput['value']) ? $aInput['value'] : array();
  192. if(!empty($aInput['reverse']))
  193. $aInput['value'] = array_diff(array_keys($aInput['values']), $aInput['value']);
  194. return parent::genInputCheckboxSet($aInput);
  195. }
  196. }
  197. /** @} */