BxBaseStudioFormView.php 8.6 KB

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