1
0

BxBaseFormView.php 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxBaseFormView extends BxDolForm
  10. {
  11. protected static $_isToggleJsAdded = false;
  12. protected static $_isCssJsAdded = false;
  13. protected static $_isCssJsUiAdded = false;
  14. protected static $_isCssJsUiSortableAdded = false;
  15. protected static $_isCssJsMinicolorsAdded = false;
  16. protected static $_isCssJsCodeMirrorAdded = false;
  17. protected static $_isCssJsLabelsAdded = false;
  18. protected static $_isCssJsTimepickerAdded = false;
  19. protected static $_isCssJsAddedViewMode = false;
  20. /**
  21. * Enable or disable error message displaying
  22. */
  23. protected $bEnableErrorIcon = true;
  24. /**
  25. * HTML Code of this form
  26. */
  27. protected $sCode;
  28. /**
  29. * Code which will be added to the beginning of the form.
  30. * For example, hidden inputs.
  31. * For internal use only
  32. */
  33. protected $_sCodeAdd = '';
  34. /**
  35. * for internal use only
  36. */
  37. protected $_isSectionOpened = false;
  38. /**
  39. * Default divider for several inputs
  40. */
  41. protected $_sDivider = '<span class="bx-def-margin-left"></span>';
  42. /**
  43. * Alternative divider for several inputs
  44. */
  45. protected $_sDividerAlt = '<div class="bx-form-input-dv-nl"></div>';
  46. /**
  47. * Form is added dynamically.
  48. */
  49. protected $_bDynamicMode = false;
  50. /**
  51. * Form is submitted dynamically (using Ajax Submit).
  52. */
  53. protected $_bAjaxMode = false;
  54. /**
  55. * Use absolute Action URL which is needed in Ajax Mode.
  56. */
  57. protected $_bAbsoluteActionUrl = false;
  58. /**
  59. * Form is displayed in view mode.
  60. */
  61. protected $_bViewMode = false;
  62. /**
  63. * Show or not sections which have no fileds
  64. */
  65. protected $_bShowEmptySections = false;
  66. /**
  67. * Function name for generation close form section HTML.
  68. */
  69. protected $_sSectionClose = 'getCloseSection';
  70. /**
  71. * Function name for generation open form section HTML.
  72. */
  73. protected $_sSectionOpen = 'getOpenSection';
  74. /**
  75. * JS files list for form
  76. */
  77. protected $_aJs = array();
  78. /**
  79. * CSS files list for form
  80. */
  81. protected $_aCss = array();
  82. protected $_sJsClassName;
  83. protected $_sJsObjectName;
  84. protected $_aHtmlIds;
  85. /**
  86. * Constructor
  87. *
  88. * @param array $aInfo Form contents
  89. *
  90. * $aInfo['params'] = array(
  91. * 'remove_form' => true|false,
  92. * );
  93. *
  94. * @return BxBaseFormView
  95. */
  96. function __construct($aInfo, $oTemplate)
  97. {
  98. parent::__construct($aInfo, $oTemplate);
  99. $this->_bAjaxMode = isset($this->aParams['ajax_mode']) && $this->aParams['ajax_mode'];
  100. $this->_bViewMode = isset($this->aParams['view_mode']) && $this->aParams['view_mode'];
  101. if($this->_bViewMode) {
  102. $this->_sSectionClose = 'getCloseSectionViewMode';
  103. $this->_sSectionOpen = 'getOpenSectionViewMode';
  104. }
  105. $this->_sJsClassName = 'BxDolForm';
  106. $sName = !empty($aInfo['params']['display']) ? $aInfo['params']['display'] : '';
  107. if(empty($sName) && !empty($aInfo['params']['object']))
  108. $sName = $aInfo['params']['object'];
  109. if(empty($sName) && !empty($aInfo['form_attrs']['id']))
  110. $sName = $aInfo['form_attrs']['id'];
  111. $this->_sJsObjectName = 'oForm' . bx_gen_method_name($sName, array('_' , '-'));
  112. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sName);
  113. $this->_aHtmlIds = array(
  114. 'help_popup' => $sHtmlId . '-help-popup-',
  115. 'pgc' => $sHtmlId . '-pgc-',
  116. 'pgc_popup' => $sHtmlId . '-pgc-popup-',
  117. 'pgc_form' => $sHtmlId . '-pgc-form-',
  118. );
  119. }
  120. public function performActionGetHelp()
  121. {
  122. $sInput = bx_process_input(bx_get('input'));
  123. if(empty($sInput) || empty($this->aInputs[$sInput]['help']))
  124. return;
  125. echo $this->oTemplate->parseHtmlByName('form_field_help_popup.html', array(
  126. 'content' => _t($this->aInputs[$sInput]['help']),
  127. 'bx_if:show_edit' => array(
  128. 'condition' => isAdmin(),
  129. 'content' => array(
  130. 'link' => BX_DOL_URL_STUDIO . bx_append_url_params('builder_forms.php', array(
  131. 'page' => 'fields',
  132. 'module' => $this->aParams['module'],
  133. 'object' => $this->aParams['object'],
  134. 'display' => $this->aParams['display']
  135. ))
  136. )
  137. )
  138. ));
  139. }
  140. public function performActionChangePrivacyGroup()
  141. {
  142. $iAuthorId = (int)bx_get_logged_profile_id();
  143. if(empty($iAuthorId))
  144. return echoJson(array());
  145. $iInputId = bx_process_input(bx_get('input_id'), BX_DATA_INT);
  146. if(empty($iInputId))
  147. return echoJson(array());
  148. $sPrivacyObject = bx_process_input(bx_get('privacy_object'));
  149. $sPrivacyField = BxDolPrivacy::getFieldName($sPrivacyObject);
  150. if(empty($sPrivacyField))
  151. return echoJson(array());
  152. $sPrivacyGroup = bx_process_input(bx_get($sPrivacyField));
  153. if(empty($sPrivacyField))
  154. return echoJson(array());
  155. if(!BxDolFormQuery::setInputPrivacy($iInputId, $iAuthorId, $sPrivacyField, $sPrivacyGroup))
  156. return echoJson(array());
  157. return echoJson(array(
  158. 'code' => 0,
  159. 'form_id' => $this->getId(),
  160. 'chooser_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  161. 'icon' => $this->_getPrivacyIcon($sPrivacyGroup),
  162. 'eval' => $this->getJsObjectName() . '.pgcOnSelectGroup(oData);'
  163. ));
  164. }
  165. public function performActionGetPrivacyGroupChooser()
  166. {
  167. $iInputId = bx_process_input(bx_get('input_id'), BX_DATA_INT);
  168. $sPrivacyObject = bx_process_input(bx_get('privacy_object'));
  169. if(empty($iInputId) || empty($sPrivacyObject))
  170. return '';
  171. $sFormId = $this->_aHtmlIds['pgc_form'] . $iInputId;
  172. $aFormActionParams = array();
  173. if(!empty($this->aParams['object']) && !empty($this->aParams['display']))
  174. $aFormActionParams = array(
  175. 'o' => $this->aParams['object'],
  176. 'd' => $this->aParams['display']
  177. );
  178. $aFormActionParams['a'] = 'change_privacy_group';
  179. $iAuthorId = bx_get_logged_profile_id();
  180. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  181. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  182. $aInputPgc = $oPrivacy->getGroupChooser($sPrivacyObject, $iAuthorId);
  183. $aInputPgc['value'] = $mixedPrivacyGroup;
  184. if(!isset($aInputPgc['content']))
  185. $aInputPgc['content'] = '';
  186. $aInputPgc['content'] .= $oPrivacy->addCssJs(true);
  187. $aInputPgc['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iAuthorId, array(
  188. 'content_id' => $iInputId,
  189. 'group_id' => $mixedPrivacyGroup,
  190. 'html_ids' => array(
  191. 'form' => $sFormId
  192. )
  193. ));
  194. $aFormInfo = array(
  195. 'form_attrs' => array(
  196. 'id' => $sFormId,
  197. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('form.php', $aFormActionParams),
  198. 'method' => 'post'
  199. ),
  200. 'inputs' => array (
  201. 'input_id' => array(
  202. 'type' => 'hidden',
  203. 'name' => 'input_id',
  204. 'value' => $iInputId,
  205. 'db' => array (
  206. 'pass' => 'Int',
  207. ),
  208. ),
  209. 'privacy_object' => array(
  210. 'type' => 'hidden',
  211. 'name' => 'privacy_object',
  212. 'value' => $sPrivacyObject,
  213. 'db' => array (
  214. 'pass' => 'Xss',
  215. ),
  216. ),
  217. 'privacy_group_chooser' => $aInputPgc,
  218. 'controls' => array(
  219. 'name' => 'controls',
  220. 'type' => 'input_set',
  221. array(
  222. 'type' => 'button',
  223. 'name' => 'do_submit',
  224. 'value' => _t('_sys_submit'),
  225. 'attrs' => array(
  226. 'onclick' => "$('#" . $sFormId . "').submit()",
  227. ),
  228. ),
  229. array (
  230. 'type' => 'reset',
  231. 'name' => 'do_cancel',
  232. 'value' => _t('_Cancel'),
  233. 'attrs' => array(
  234. 'onclick' => "$(this).parents('.bx-popup-applied:visible:first').dolPopupHide()",
  235. 'class' => 'bx-def-margin-sec-left',
  236. ),
  237. )
  238. )
  239. )
  240. );
  241. $oForm = new BxTemplFormView($aFormInfo);
  242. echo $this->oTemplate->parseHtmlByName('form_field_privacy_popup.html', array(
  243. 'form_id' => $oForm->getId(),
  244. 'form' => $oForm->getCode(true),
  245. ));
  246. }
  247. function setShowEmptySections($b)
  248. {
  249. $this->_bShowEmptySections = $b;
  250. }
  251. function setAjaxMode($bAjaxMode)
  252. {
  253. $this->_bAjaxMode = (bool)$bAjaxMode;
  254. }
  255. function isAjaxMode()
  256. {
  257. return $this->_bAjaxMode;
  258. }
  259. function isViewMode()
  260. {
  261. return (bool)($this->aParams['view_mode'] ?? 0);
  262. }
  263. function setAbsoluteActionUrl($sUrl)
  264. {
  265. if(empty($sUrl))
  266. return;
  267. $this->aFormAttrs['action'] = $sUrl;
  268. $this->_bAbsoluteActionUrl = true;
  269. }
  270. /**
  271. * Return Form code
  272. * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup.
  273. * @return string
  274. */
  275. function getCode($bDynamicMode = false)
  276. {
  277. if(!$bDynamicMode && bx_is_dynamic_request())
  278. $bDynamicMode = true;
  279. $this->_bDynamicMode = $bDynamicMode;
  280. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  281. $sInclude = '';
  282. $this->sCode = false;
  283. /**
  284. * @hooks
  285. * @hookdef hook-system-form_output 'system', 'form_output' - hook to override form object and/or code to be output
  286. * - $unit_name - equals `system`
  287. * - $action - equals `form_output`
  288. * - $object_id - not used
  289. * - $sender_id - not used
  290. * - $extra_params - array of additional params with the following array keys:
  291. * - `dynamic` - [boolean] is dynamic mode
  292. * - `object` - [object] by ref, an instance of form class, @see BxDolForm, can be overridden in hook processing
  293. * - `code` - [boolean] or [string] by ref, when false the default generation mechanism will be used, can be overridden in hook processing
  294. * - `include` - [string] by ref, additional data to be attached to output, can be overridden in hook processing
  295. * @hook @ref hook-system-form_output
  296. */
  297. bx_alert('system', 'form_output', 0, 0, [
  298. 'dynamic' => $this->_bDynamicMode,
  299. 'object' => &$this,
  300. 'code' => &$this->sCode,
  301. 'include' => &$sInclude,
  302. 'code_ref' => &$this->sCode,
  303. 'include_ref' => &$sInclude,
  304. 'form_inputs_ref' => &$this->aInputs,
  305. ]);
  306. if($this->sCode === false)
  307. $this->sCode = $this->genForm();
  308. $this->addCssJs();
  309. $sDynamicCssJs = $this->_processCssJs();
  310. return $sInclude . $sDynamicCssJs . $this->sCode;
  311. }
  312. function getCodeAPI()
  313. {
  314. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  315. $this->sCode = false;
  316. /**
  317. * @hooks
  318. * @hookdef hook-system-form_output_api 'system', 'form_output_api' - hook to override form object and/or code to be output. Is used in API calls.
  319. * - $unit_name - equals `system`
  320. * - $action - equals `form_output_api`
  321. * - $object_id - not used
  322. * - $sender_id - not used
  323. * - $extra_params - array of additional params with the following array keys:
  324. * - `dynamic` - [boolean] is dynamic mode
  325. * - `object` - [object] by ref, an instance of form class, @see BxDolForm, can be overridden in hook processing
  326. * - `code` - [boolean] by ref, when false the default generation mechanism will be used, can be overridden in hook processing
  327. * @hook @ref hook-system-form_output_api
  328. */
  329. bx_alert('system', 'form_output_api', 0, 0, [
  330. 'dynamic' => $this->_bDynamicMode,
  331. 'object' => &$this,
  332. 'code' => &$this->sCode,
  333. 'code_ref' => &$this->sCode,
  334. 'form_inputs_ref' => &$this->aInputs,
  335. ]);
  336. if($this->sCode === false) {
  337. if(!$this->genForm() && (bool)($this->aParams['view_mode'] ?? 0))
  338. return [];
  339. }
  340. $oIconset = BxDolIconset::getObjectInstance();
  341. $oFunctions = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate);
  342. // TODO: process inputs to translate titles, alerts, etc
  343. $aInputs = [];
  344. foreach($this->aInputs as $sKey => $aInput) {
  345. if(isset($aInput['visible_for_levels']) && !self::isVisible($aInput))
  346. continue;
  347. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aInput['icon'] ?? '');
  348. $aInput['icon'] = $sIcon ? $oIconset->getIcon($sIcon) : ($sIconHtml ? $sIconHtml : '');
  349. if(!empty($aInput['info']))
  350. $aInput['info'] = strip_tags($aInput['info']);
  351. if(!empty($aInput['help']))
  352. $aInput['help'] = strip_tags($aInput['help']);
  353. if(!empty($aInput['error']))
  354. $aInput['error'] = strip_tags($aInput['error']);
  355. if (isset($aInput['type']) && 'files' == $aInput['type']){
  356. $oStorage = BxDolStorage::getObjectInstance($aInput['storage_object']);
  357. $aInput['ext_allow'] = $oStorage->getObjectData()['ext_allow'];
  358. $aInput['ext_deny'] = $oStorage->getObjectData()['ext_deny'];
  359. $aInput['ghost_template'] = '';
  360. $aInput['value'] = '';
  361. $aInput['values'] = '';
  362. $aInput['values_src'] = '';
  363. if(!empty($aInput['content_id']) && ($sUpr = reset($aInput['uploaders'])) && ($sStg = $aInput['storage_object'] ?? '') && ($oUploader = BxDolUploader::getObjectInstance($sUpr, $sStg, genRndPwd(8))) !== false) {
  364. $oUploader->setMultiple($aInput['multiple'] ?? true);
  365. if(($sMethod = 'setSubmitted') && method_exists($oUploader, $sMethod))
  366. $oUploader->$sMethod($this->isSubmitted());
  367. $sData = $oUploader->getGhostsWithOrder((int)bx_get_logged_profile_id(), 'json', $aInput['images_transcoder'], $aInput['content_id']);
  368. if($sData && ($aData = json_decode($sData, true)) && !empty($aData['g']) && is_array($aData['g'])) {
  369. $aInput['values_src'] = array_values($aData['g']);
  370. $aGhostIds = [];
  371. array_walk($aData['g'], function($aGhost) use (&$aGhostIds) {
  372. $aGhostIds[] = $aGhost['file_id'];
  373. });
  374. $aInput['value'] = implode(',', $aGhostIds);
  375. }
  376. }
  377. }
  378. if (isset($aInput['type']) && 'custom' == $aInput['type']){
  379. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  380. if (method_exists($this, $sCustomMethod))
  381. $aInput = $this->$sCustomMethod($aInput);
  382. }
  383. if (isset($aInput['type']) && 'block_header' == $aInput['type']){
  384. $aInput['name'] = $sKey;
  385. }
  386. if (isset($aInput['type']) && 'block_header' == $aInput['type']){
  387. $aInput['name'] = $sKey;
  388. }
  389. if (isset($aInput['type']) && 'location' == $aInput['type']){
  390. $oLocation = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  391. $aVars = [];
  392. foreach(BxDolForm::$LOCATION_INDEXES as $sKey)
  393. $aVars[$sKey] = $oLocation->getLocationVal($aInput, $sKey, $this);
  394. if(($sLocationString = bx_api_get_location_string($aVars)))
  395. $aVars['location_string'] = $sLocationString;
  396. $aInput['value'] = $aVars;
  397. }
  398. if (isset($aInput['type']) && 'textarea' == $aInput['type']){
  399. if (isset($aInput['value'], $aInput['html']) && (int)$aInput['html'] == 0)
  400. $aInput['value'] = strip_tags($aInput['value']);
  401. }
  402. /**
  403. * Everytime when [key => value] is used it should be converted to [key => ..., value => ...]
  404. */
  405. if (($sVt = $aInput['type'] ?? false) && in_array($sVt, ['select', 'select_multiple', 'checkbox_set', 'radio_set']) && is_array(($aVv = $aInput[($sKv = 'values')] ?? false)) && !(is_array(($aVv1 = reset($aVv))) && isset($aVv1['key'], $aVv1['value']))) {
  406. $aInput[$sKv] = array_map(function($sKey) use($aInput, $sKv) {
  407. return ['key' => $sKey, 'value' => $aInput[$sKv][$sKey]];
  408. }, array_keys($aInput[$sKv]));
  409. }
  410. if(isset($aInput['name']) && in_array($aInput['name'], ['allow_view_to', 'object_privacy_view'])) {
  411. $aParams = bx_get('params');
  412. if(empty($aParams) || !is_array($aParams))
  413. $aParams = [];
  414. if(!isset($aParams['context_id']) || ($iContextId = (int)$aParams['context_id']) >= 0) {
  415. $oProfile = BxDolProfile::getInstance();
  416. foreach($aInput['values'] as $aValue) {
  417. //--- Selected friends
  418. if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_FRIENDS_SELECTED) {
  419. $aIds = BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContent($oProfile->id(), true, 0, 20);
  420. if(!empty($aIds) && is_array($aIds)) {
  421. $aInput['values_friends'] = [];
  422. foreach($aIds as $iId)
  423. $aInput['values_friends'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)];
  424. }
  425. }
  426. //--- Selected relations
  427. if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_RELATIONS_SELECTED) {
  428. $aIds = BxDolConnection::getObjectInstance('sys_profiles_relations')->getConnectedContent($oProfile->id(), true, 0, 20);
  429. if(!empty($aIds) && is_array($aIds)) {
  430. $aInput['values_relations'] = [];
  431. foreach($aIds as $iId)
  432. $aInput['values_relations'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)];
  433. }
  434. }
  435. //--- Selected memberships
  436. if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_MEMBERSHIPS_SELECTED) {
  437. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true);
  438. if(!empty($aLevels) && is_array($aLevels)) {
  439. $aInput['values_memberships'] = [];
  440. foreach($aLevels as $iId => $sTitle)
  441. $aInput['values_memberships'][] = ['key' => $iId, 'value' => $sTitle];
  442. }
  443. }
  444. }
  445. }
  446. else
  447. $aInput = array_merge($aInput, [
  448. 'type' => 'hidden',
  449. 'value' => $iContextId,
  450. 'owner_info' => BxDolProfile::getData(abs($iContextId))
  451. ]);
  452. }
  453. $aInputs[$sKey] = $aInput;
  454. }
  455. return [
  456. 'inputs' => $aInputs,
  457. 'attrs' => $this->aFormAttrs,
  458. 'params' => $this->aParams
  459. ];
  460. }
  461. public function getJsClassName()
  462. {
  463. return $this->_sJsClassName;
  464. }
  465. public function getJsObjectName()
  466. {
  467. return $this->_sJsObjectName;
  468. }
  469. public function getJsScript($bWrap = false)
  470. {
  471. $sJsObjName = $this->getJsObjectName();
  472. $sJsObjClass = $this->getJsClassName();
  473. $sCode = "if(window['" . $sJsObjName . "'] == undefined) window['" . $sJsObjName . "'] = new " . $sJsObjClass . "(" . json_encode(array(
  474. 'sObjName' => $sJsObjName,
  475. 'sId' => $this->getId(),
  476. 'sName' => $this->getName(),
  477. 'sObject' => isset($this->aParams['object']) ? $this->aParams['object'] : '',
  478. 'sDisplay' => isset($this->aParams['display']) ? $this->aParams['display'] : '',
  479. 'sRootUrl' => BX_DOL_URL_ROOT,
  480. 'aHtmlIds' => $this->_aHtmlIds,
  481. 'bInitCodeMirror' => self::$_isCssJsCodeMirrorAdded === true,
  482. 'bLeavePageConfirmation' => getParam('sys_form_lpc_enable') == 'on',
  483. 'sTxtLeavePageConfirmation' => _t('_sys_leave_page_confirmation')
  484. )) . "); window['" . $sJsObjName . "'].init();";
  485. return $bWrap ? $this->oTemplate->_wrapInTagJsCode($sCode) : $sCode;
  486. }
  487. /**
  488. * Generate the whole form
  489. *
  490. * @return string
  491. */
  492. function genForm()
  493. {
  494. $this->_sCodeAdd = '';
  495. $bViewMode = $this->isViewMode();
  496. $sTable = $this->genRows();
  497. if($bViewMode && !$sTable)
  498. return '';
  499. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  500. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  501. if (!empty($this->aParams['remove_form']) || $bViewMode) {
  502. $sForm = <<<BLAH
  503. $sHtmlBefore
  504. {$this->_sCodeAdd}
  505. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  506. $sTable
  507. </div>
  508. $sHtmlAfter
  509. BLAH;
  510. } else {
  511. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  512. $sAjaxFormJs = '';
  513. if($this->_bAjaxMode)
  514. $sAjaxFormJs = <<<BLAH
  515. $("form#{$this->id}").ajaxForm({
  516. dataType: "json",
  517. beforeSubmit: function (formData, jqForm, options) {
  518. bx_loading($("form#{$this->id}"), true);
  519. },
  520. success: function (oData) {
  521. bx_loading($("form#{$this->id}"), false);
  522. processJsonData(oData);
  523. }
  524. });
  525. BLAH;
  526. $sJsCodeDynamic = '';
  527. if($this->_bDynamicMode)
  528. $sJsCodeDynamic = 'function() {' . $this->getJsScript() . '}';
  529. $sForm = <<<BLAH
  530. $sHtmlBefore
  531. <form $sFormAttrs>
  532. {$this->_sCodeAdd}
  533. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  534. $sTable
  535. </div>
  536. </form>
  537. <script>
  538. $(document).ready(function() {
  539. $(this).addWebForms($sJsCodeDynamic);
  540. });
  541. $sAjaxFormJs
  542. </script>
  543. $sHtmlAfter
  544. BLAH;
  545. }
  546. if(!$this->_bDynamicMode)
  547. $sForm = $sForm . $this->getJsScript(true);
  548. return $sForm;
  549. }
  550. /**
  551. * Generate Table HTML code
  552. *
  553. * @return string
  554. */
  555. function genRows()
  556. {
  557. // add CSRF token if it's needed.
  558. if (!(isset($this->aParams['view_mode']) && $this->aParams['view_mode']) && getParam('sys_security_form_token_enable') == 'on' && (!isset($this->aParams['csrf']['disable']) || (isset($this->aParams['csrf']['disable']) && $this->aParams['csrf']['disable'] !== true)) && ($mixedCsrfToken = BxDolForm::getCsrfToken()) !== false) {
  559. $this->aInputs['csrf_token'] = array(
  560. 'type' => 'hidden',
  561. 'name' => 'csrf_token',
  562. 'value' => $mixedCsrfToken,
  563. 'db' => array ('pass' => 'Xss'),
  564. 'visible_for_levels' => PHP_INT_MAX,
  565. );
  566. }
  567. // add 'Ajax Mode' flag
  568. if($this->_bAjaxMode)
  569. $this->aInputs['ajax_mode'] = array(
  570. 'type' => 'hidden',
  571. 'name' => 'ajax_mode',
  572. 'value' => 1,
  573. 'db' => array ('pass' => 'Int'),
  574. 'visible_for_levels' => PHP_INT_MAX,
  575. );
  576. // add 'Absolute Action Url' flag
  577. if($this->_bAbsoluteActionUrl)
  578. $this->aInputs['absolute_action_url'] = array(
  579. 'type' => 'hidden',
  580. 'name' => 'absolute_action_url',
  581. 'value' => 1,
  582. 'db' => array ('pass' => 'Int'),
  583. 'visible_for_levels' => PHP_INT_MAX,
  584. );
  585. // check if we need to generate open section clause
  586. $sOpenSection = '';
  587. foreach ($this->aInputs as $aInput) {
  588. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  589. continue;
  590. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  591. $sOpenSection = $this->{$this->_sSectionOpen}();
  592. break;
  593. }
  594. // generate rows contents
  595. $sCont = '';
  596. $sContHeader = '';
  597. $sContFields = '';
  598. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  599. foreach ($this->aInputs as &$aInput) {
  600. if (!isset($aInput['visible_for_levels']) || self::isVisible($aInput)) {
  601. if ((isset($aInput['type']) && 'block_header' == $aInput['type']) && !$this->_bShowEmptySections) {
  602. // don't show section with no fields or with empty fields
  603. if ($sContHeader) {
  604. if ($sContFields)
  605. $sCont .= $sContHeader . $sContFields;
  606. else
  607. $sContHeader = '';
  608. $sContFields = '';
  609. }
  610. else {
  611. $sCont .= $sContFields;
  612. $sContFields = '';
  613. }
  614. $sContHeader = $this->$sFuncGenRow($aInput);
  615. } else {
  616. $sContFields .= $this->$sFuncGenRow($aInput);
  617. }
  618. }
  619. }
  620. $sCont .= $sContHeader . $sContFields;
  621. $sCloseSection = $this->{$this->_sSectionClose}();
  622. return $this->isViewMode() && !$sCont ? '' : $sOpenSection . $sCont . $sCloseSection;
  623. }
  624. /**
  625. * Generate single Table Row
  626. *
  627. * @param array $aInput
  628. * @return string
  629. */
  630. function genRow(&$aInput)
  631. {
  632. if (!isset($aInput['type']))
  633. $aInput['type'] = false;
  634. if (!empty($aInput['name'])) {
  635. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  636. if (method_exists($this, $sCustomMethod))
  637. return $this->$sCustomMethod($aInput);
  638. }
  639. switch ($aInput['type']) {
  640. case 'block_header':
  641. $sRow = $this->genRowBlockHeader($aInput);
  642. break;
  643. case 'block_end':
  644. $sRow = $this->genBlockEnd($aInput);
  645. break;
  646. case 'hidden':
  647. // do not generate row for hidden inputs
  648. $sRow = '';
  649. $this->_sCodeAdd .= $this->genInput($aInput);
  650. break;
  651. case 'select_box':
  652. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  653. break;
  654. case 'files':
  655. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  656. break;
  657. case 'switcher':
  658. case 'checkbox':
  659. $sRow = $this->genRowStandard($aInput, true);
  660. break;
  661. default:
  662. $sRow = $this->genRowStandard($aInput);
  663. }
  664. return $sRow;
  665. }
  666. public function isInputVisible($mixedInput)
  667. {
  668. if(!is_array($mixedInput) && isset($this->aInputs[$mixedInput]))
  669. $mixedInput = $this->aInputs[$mixedInput];
  670. if(empty($mixedInput) || !is_array($mixedInput))
  671. $mixedInput = BxDolFormQuery::getInputByName($this->aParams['object'], $mixedInput);
  672. if(empty($mixedInput) || !is_array($mixedInput))
  673. return false;
  674. if(!empty($mixedInput['privacy']) && !empty($this->_iAuthorId) && !$this->_isInputVisibleByPrivacy($mixedInput))
  675. return false;
  676. return true;
  677. }
  678. protected function _isInputVisibleByPrivacy($aInput)
  679. {
  680. $mixedResult = checkActionModule($this->_iAuthorId, 'set form fields privacy', 'system');
  681. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  682. return true;
  683. $aInputPrivacy = BxDolFormQuery::getInputPrivacy($aInput['id'], $this->_iAuthorId);
  684. if(empty($aInputPrivacy) || !is_array($aInputPrivacy))
  685. if(BxDolFormQuery::setInputPrivacy($aInput['id'], $this->_iAuthorId, BxDolPrivacy::getFieldName($this->_sPrivacyObjectView), $this->_sPrivacyGroupDefault))
  686. $aInputPrivacy = BxDolFormQuery::getInputPrivacy($aInput['id'], $this->_iAuthorId);
  687. if((empty($aInputPrivacy) || !is_array($aInputPrivacy)) && $this->_sPrivacyGroupDefault != BX_DOL_PG_ALL)
  688. return false;
  689. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_sPrivacyObjectView);
  690. if($oPrivacy && !$oPrivacy->check($aInputPrivacy['id']))
  691. return false;
  692. return true;
  693. }
  694. /**
  695. * Generate single Table Row for view mode
  696. *
  697. * @param array $aInput
  698. * @return string
  699. */
  700. function genViewRow(&$aInput)
  701. {
  702. if (!isset($aInput['type']))
  703. $aInput['type'] = false;
  704. if(!$this->isInputVisible($aInput))
  705. return '';
  706. if (!empty($aInput['name'])) {
  707. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  708. if (method_exists($this, $sCustomMethod))
  709. return $this->$sCustomMethod($aInput);
  710. }
  711. switch ($aInput['type']) {
  712. case 'block_header':
  713. $sRow = $this->genRowBlockHeader($aInput);
  714. break;
  715. case 'block_end':
  716. $sRow = $this->genBlockEnd($aInput);
  717. break;
  718. case 'nested_form':
  719. $sRow = $this->genNestedForm($aInput);
  720. break;
  721. case 'textarea':
  722. $sRow = $this->{'genViewRowWrapped' . ($aInput['html'] != 0 ? 'Html' : '')}($aInput);
  723. break;
  724. default:
  725. $sRow = $this->genViewRowWrapped($aInput);
  726. }
  727. return $sRow;
  728. }
  729. /**
  730. * Generate complete wrapped row for view mode form
  731. *
  732. * @param array $aInput
  733. * @return string
  734. */
  735. function genViewRowWrapped(&$aInput)
  736. {
  737. return $this->_genViewRowWrapped($aInput);
  738. }
  739. function genViewRowWrappedHtml(&$aInput)
  740. {
  741. return $this->_genViewRowWrapped($aInput, [
  742. 'class_value' => 'bx-def-vanilla-html max-w-none'
  743. ]);
  744. }
  745. protected function _genViewRowWrapped(&$aInput, $aParams = [])
  746. {
  747. $sValue = $this->genViewRowValue($aInput);
  748. if (null === $sValue)
  749. return '';
  750. $sClass = !empty($aParams['class']) ? $aParams['class'] : '';
  751. $sClassValue = !empty($aParams['class_value']) ? $aParams['class_value'] : '';
  752. $aTmplVarsIcon = [];
  753. if(!empty($aInput['icon']))
  754. $aTmplVarsIcon = [
  755. 'icon' => $this->genIcon($aInput['icon'])
  756. ];
  757. return $this->oTemplate->parseHtmlByName('form_view_row.html', [
  758. 'type' => $aInput['type'],
  759. 'class' => $sClass,
  760. 'bx_if:show_icon' => [
  761. 'condition' => !empty($aTmplVarsIcon),
  762. 'content' => $aTmplVarsIcon
  763. ],
  764. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  765. 'class_value' => $sClassValue,
  766. 'value' => $sValue
  767. ]);
  768. }
  769. /**
  770. * Generate value for view mode row
  771. *
  772. * @param array $aInput
  773. * @return string
  774. */
  775. function genViewRowValue(&$aInput)
  776. {
  777. if (!empty($aInput['name'])) {
  778. $sCustomMethod = 'genCustomViewRowValue' . $this->_genMethodName($aInput['name']);
  779. if (method_exists($this, $sCustomMethod))
  780. return $this->$sCustomMethod($aInput);
  781. }
  782. switch ($aInput['type']) {
  783. case 'hidden':
  784. $sValue = null;
  785. break;
  786. case 'select':
  787. case 'radio_set':
  788. $sValue = $this->genViewRowValueForSelect($aInput);
  789. break;
  790. case 'dateselect':
  791. case 'datepicker':
  792. $sValue = null;
  793. if (empty($aInput['value']) || !$aInput['value'] || '0000-00-00' == $aInput['value'])
  794. break;
  795. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass']) && !is_numeric($aInput['value']))
  796. $sValue = BxTemplFunctions::getInstance()->timeForJsFullDate($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format']: BX_FORMAT_DATE, true, true);
  797. else
  798. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  799. break;
  800. case 'date_time':
  801. case 'datetime':
  802. $sValue = null;
  803. if(empty($aInput['value']) || !$aInput['value'] || '0000-00-00 00:00:00' == $aInput['value'] || '0000-00-00 00:00' == $aInput['value'])
  804. break;
  805. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass']) && !is_numeric($aInput['value']))
  806. $sValue = BxTemplFunctions::getInstance()->timeForJsFullDate($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true, true);
  807. else
  808. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  809. break;
  810. case 'checkbox_set':
  811. case 'select_multiple':
  812. $sValue = null;
  813. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  814. $sValue = '';
  815. foreach ($aInput['value'] as $sVal)
  816. $sValue .= $aInput['values'][$sVal] . ', ';
  817. $sValue = trim ($sValue, ', ');
  818. }
  819. break;
  820. case 'checkbox':
  821. case 'switcher':
  822. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  823. break;
  824. case 'textarea':
  825. if (isset($aInput['value']) && '' !== $aInput['value'])
  826. $sValue = (isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : bx_process_output($aInput['value'], BX_DATA_TEXT_MULTILINE);
  827. else
  828. $sValue = null;
  829. break;
  830. case 'text':
  831. if(isset($aInput['value']) && '' !== $aInput['value'])
  832. $sValue = bx_linkify(bx_process_output($aInput['value']));
  833. else
  834. $sValue = null;
  835. break;
  836. case 'price':
  837. if(isset($aInput['value']) && $aInput['value'] !== '')
  838. $sValue = _t_format_currency(bx_process_output($aInput['value']));
  839. else
  840. $sValue = null;
  841. break;
  842. default:
  843. if(isset($aInput['value']) && '' !== $aInput['value']) {
  844. $sValue = $aInput['value'];
  845. if(get_mb_substr($sValue, 0, 1) == '_')
  846. $sValue = _t($sValue);
  847. $sValue = bx_process_output($sValue);
  848. }
  849. else
  850. $sValue = null;
  851. }
  852. return $sValue;
  853. }
  854. function genViewRowValueForSelect(&$aInput)
  855. {
  856. if (!isset($aInput['value']) || !$aInput['value'])
  857. return null;
  858. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  859. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])) !== false)
  860. return $oCategory->getCategoryLink($s, $aInput['value']);
  861. return $s;
  862. }
  863. protected function genCustomRowBirthday(&$aInput)
  864. {
  865. if(!$this->_bViewMode)
  866. return $this->genRowStandard($aInput);
  867. $sTxtAge = '_sys_form_input_age';
  868. $aInput = array_merge($aInput, [
  869. 'caption_src' => $sTxtAge,
  870. 'caption' => _t($sTxtAge),
  871. ]);
  872. if(bx_is_api())
  873. $aInput = array_merge($aInput, [
  874. 'type' => 'text',
  875. 'value' => bx_birthday2age($aInput['value'])
  876. ]);
  877. return $this->genViewRowWrapped($aInput);
  878. }
  879. protected function genCustomRowCf(&$aInput)
  880. {
  881. $aInput = BxDolContentFilter::getInstance()->getInput($aInput);
  882. if($aInput['type'] == 'hidden') {
  883. $this->_sCodeAdd .= $this->genInput($aInput);
  884. return '';
  885. }
  886. return $this->genRowStandard($aInput);
  887. }
  888. protected function genCustomViewRowValueBirthday(&$aInput)
  889. {
  890. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00', '0000-00-00Z', '0000-00-00 00:00:00Z')))
  891. return null;
  892. return bx_birthday2age($aInput['value']);
  893. }
  894. /**
  895. * Generate standard row
  896. *
  897. * @param array $aInput
  898. * @return string
  899. */
  900. function genRowStandard(&$aInput, $isOneLine = false)
  901. {
  902. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  903. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  904. $sHelp = !empty($aInput['help']) ? ' <a href="javascript:void(0)" onclick="javascript:' . $this->getJsObjectName() . '.showHelp(this, \'' . $aInput['name'] . '\')"><i class="sys-icon question-circle"></i></a> ' : '';
  905. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  906. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  907. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  908. $sClassWrapper = 'bx-form-element-wrapper';
  909. if($isOneLine)
  910. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  911. $sClassWrapper .= ' bx-def-margin-top-auto';
  912. if (isset($aInput['name'])) {
  913. if (!isset($aInput['tr_attrs']) || !is_array($aInput['tr_attrs']))
  914. $aInput['tr_attrs'] = [];
  915. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  916. }
  917. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  918. $sClassOneLineCaption = '';
  919. $sClassOneLineValue = '';
  920. if ($isOneLine) {
  921. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' ltr:ml-4 rtl:mr-4';
  922. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  923. if (!isset($aInput['attrs']) || !is_array($aInput['attrs']))
  924. $aInput['attrs'] = [];
  925. $aInput['attrs']['id'] = $this->getInputId($aInput);
  926. if ($sCaption)
  927. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  928. }
  929. $sInput = $this->genInput($aInput);
  930. if(isset($aInput['error_updated']) && $aInput['error_updated'] === true)
  931. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  932. if(empty($sErrorIcon))
  933. $sErrorIcon = '';
  934. $aTmplVarsRow = array(
  935. 'bx_if:show_caption' => array(
  936. 'condition' => !empty($sCaption),
  937. 'content' => array(
  938. 'class_caption' => $sClassOneLineCaption,
  939. 'caption' => $sCaption,
  940. 'required' => $sRequired,
  941. 'help' => $sHelp,
  942. )
  943. ),
  944. 'class_value' => $sClassAdd . $sClassOneLineValue,
  945. 'value' => $this->genWrapperInput($aInput, $sInput),
  946. 'info' => $sInfoIcon,
  947. 'error' => $sErrorIcon,
  948. );
  949. return $this->oTemplate->parseHtmlByName('form_row_standard.html', array(
  950. 'tr_attrs' => $sTrAttrs,
  951. 'bx_if:show_one_line' => array(
  952. 'condition' => $isOneLine,
  953. 'content' => $aTmplVarsRow
  954. ),
  955. 'bx_if:show_lined' => array(
  956. 'condition' => !$isOneLine,
  957. 'content' => $aTmplVarsRow
  958. ),
  959. 'info' => $sInfoIcon,
  960. 'error' => $sErrorIcon
  961. ));
  962. }
  963. function genWrapperInput($aInput, $sContent)
  964. {
  965. $aAttrs = $this->_genWrapperInputAttrs($aInput);
  966. $sClass = "bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}";
  967. if(isset($aInput['html']) && $aInput['html'] && $this->isHtmlEditor($aInput['html'], $aInput))
  968. $sClass .= ' bx-form-input-wrapper-html';
  969. return $this->oTemplate->parseHtmlByName('form_input_wrapper.html', array(
  970. 'attrs' => bx_convert_array2attrs($aAttrs, $sClass),
  971. 'content' => $sContent
  972. ));
  973. }
  974. protected function _genWrapperInputAttrs(&$aInput)
  975. {
  976. return isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? $aInput['attrs_wrapper'] : [];
  977. }
  978. /**
  979. * Generate custom row
  980. *
  981. * @param array $aInput
  982. * @param string $sCustomMethod custom method to generate code for input
  983. * @return string
  984. */
  985. function genRowCustom(&$aInput, $sCustomMethod)
  986. {
  987. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  988. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  989. $sHelp = !empty($aInput['help']) ? ' <a href="javascript:void(0)" onclick="javascript:' . $this->getJsObjectName() . '.showHelp(this, \'' . $aInput['name'] . '\')"><i class="sys-icon question-circle"></i></a> ' : '';
  990. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  991. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  992. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  993. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  994. if (isset($aInput['name'])) {
  995. if (!isset($aInput['tr_attrs']) || !is_array($aInput['tr_attrs']))
  996. $aInput['tr_attrs'] = [];
  997. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  998. }
  999. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  1000. return $this->oTemplate->parseHtmlByName('form_row_custom.html', array(
  1001. 'tr_attrs' => $sTrAttrs,
  1002. 'bx_if:show_caption' => array(
  1003. 'condition' => !empty($sCaption),
  1004. 'content' => array(
  1005. 'class_caption' => '',
  1006. 'caption' => $sCaption,
  1007. 'required' => $sRequired,
  1008. 'help' => $sHelp
  1009. )
  1010. ),
  1011. 'class_value' => $sClassAdd,
  1012. 'value' => $sInput,
  1013. ));
  1014. }
  1015. /**
  1016. * Generate Block Headers row
  1017. *
  1018. * @param array $aInput
  1019. * @return string
  1020. */
  1021. function genRowBlockHeader(&$aInput)
  1022. {
  1023. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  1024. // if there is no caption - show divider only
  1025. if (empty($aInput['caption']))
  1026. return $this->{$this->_sSectionOpen}($aAttrs);
  1027. // if section is collapsed by default, add necessary code
  1028. $sClassAddCollapsable = 'bx-form-collapsable';
  1029. if(isset($aInput['collapsed']) && $aInput['collapsed'])
  1030. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  1031. // display section with caption
  1032. if(empty($aAttrs))
  1033. $aAttrs = ['class' => $sClassAddCollapsable];
  1034. else
  1035. $aAttrs['class'] .= ' ' . $sClassAddCollapsable;
  1036. $bViewMode = !empty($this->aParams['view_mode']);
  1037. $sCaption = bx_process_output($aInput['caption'], BX_DATA_HTML);
  1038. $sInfo = !empty($aInput['info']) ? bx_process_output($aInput['info']) : '';
  1039. $bInfo = !empty($sInfo);
  1040. $sTitle = $this->oTemplate->parseHtmlByName('form_field_section_title.html', [
  1041. 'bx_if:show_view' => [
  1042. 'condition' => $bViewMode,
  1043. 'content' => [
  1044. 'caption' => $sCaption,
  1045. 'bx_if:show_view_info' => [
  1046. 'condition' => $bInfo,
  1047. 'content' => [
  1048. 'info' => $sInfo
  1049. ]
  1050. ]
  1051. ]
  1052. ],
  1053. 'bx_if:show_default' => [
  1054. 'condition' => !$bViewMode,
  1055. 'content' => [
  1056. 'caption' => $sCaption,
  1057. 'bx_if:show_default_info' => [
  1058. 'condition' => $bInfo,
  1059. 'content' => [
  1060. 'info' => $sInfo
  1061. ]
  1062. ]
  1063. ]
  1064. ]
  1065. ]);
  1066. if (isset($aInput['name'])) {
  1067. if (!isset($aInput['tr_attrs']) || !is_array($aInput['tr_attrs']))
  1068. $aInput['tr_attrs'] = [];
  1069. $aInput['tr_attrs']['id'] = "bx-form-section-" . $aInput['name'];
  1070. }
  1071. return $this->{$this->_sSectionOpen}($aAttrs, $sTitle, !empty($aInput['tr_attrs']) ? $aInput['tr_attrs'] : []);
  1072. }
  1073. function genBlockEnd()
  1074. {
  1075. return $this->{$this->_sSectionClose}();
  1076. }
  1077. /**
  1078. * Generate HTML Input Element
  1079. *
  1080. * @param array $aInput
  1081. * @return string Output HTML Code
  1082. */
  1083. function genInput(&$aInput)
  1084. {
  1085. if (!empty($aInput['name'])) {
  1086. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  1087. if (method_exists($this, $sCustomMethod))
  1088. return $this->$sCustomMethod($aInput);
  1089. }
  1090. switch ($aInput['type']) {
  1091. case 'datepicker':
  1092. if (!isset($aInput['attrs']))
  1093. $aInput['attrs'] = array();
  1094. $aInput['attrs']['autosuggestion'] = 'off';
  1095. $sInput = $this->genInputStandard($aInput);
  1096. break;
  1097. // standard inputs (and non-standard, interpreted as standard)
  1098. case 'text':
  1099. case 'datepicker':
  1100. case 'dateselect':
  1101. case 'date_time':
  1102. case 'datetime':
  1103. case 'number':
  1104. case 'time':
  1105. case 'checkbox':
  1106. case 'radio':
  1107. case 'image':
  1108. case 'slider':
  1109. case 'doublerange':
  1110. case 'hidden':
  1111. $sInput = $this->genInputStandard($aInput);
  1112. break;
  1113. case 'phone':
  1114. $aInput['type'] = 'text';
  1115. $sInput = $this->genInputStandard($aInput);
  1116. break;
  1117. case 'password':
  1118. $sInput = $this->genInputPassword($aInput);
  1119. break;
  1120. case 'price':
  1121. $sInput = $this->genInputPrice($aInput);
  1122. break;
  1123. case 'file':
  1124. $sInput = $this->genInputFile($aInput);
  1125. break;
  1126. case 'rgb':
  1127. case 'rgba':
  1128. $sClass = 'bx-form-input-' . $aInput['type'];
  1129. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  1130. $aInput['type'] = 'text';
  1131. $sInput = $this->genInputStandard($aInput);
  1132. break;
  1133. case 'rgb-list':
  1134. $sInput = $this->genInputSelectRgb($aInput);
  1135. break;
  1136. case 'switcher':
  1137. $sInput = $this->genInputSwitcher($aInput);
  1138. break;
  1139. case 'button':
  1140. case 'reset':
  1141. case 'submit':
  1142. $sInput = $this->genInputButton($aInput);
  1143. break;
  1144. case 'textarea':
  1145. $sInput = $this->genInputTextarea($aInput);
  1146. break;
  1147. case 'select':
  1148. $sInput = $this->genInputSelect($aInput);
  1149. break;
  1150. case 'select_multiple':
  1151. $sInput = $this->genInputSelectMultiple($aInput);
  1152. break;
  1153. case 'checkbox_set':
  1154. $sInput = $this->genInputCheckboxSet($aInput);
  1155. break;
  1156. case 'radio_set':
  1157. $sInput = $this->genInputRadioSet($aInput);
  1158. break;
  1159. case 'input_set': // numeric array of inputs
  1160. $sInput = '';
  1161. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  1162. foreach ($aInput as $iKey => $aSubInput) {
  1163. if (!is_int($iKey) or !$aSubInput)
  1164. continue; // parse only integer keys and existing values
  1165. $sInput .= $this->genInput($aSubInput); // recursive call
  1166. $sInput .= $sDivider;
  1167. }
  1168. $sInput .= '<div class="bx-clear"></div>';
  1169. break;
  1170. case 'custom':
  1171. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  1172. break;
  1173. case 'captcha':
  1174. $sInput = $this->genInputCaptcha($aInput);
  1175. break;
  1176. case 'location':
  1177. $sInput = $this->genInputLocation($aInput);
  1178. break;
  1179. case 'nested_form':
  1180. $sInput = $this->genInputNestedForm($aInput);
  1181. break;
  1182. case 'value':
  1183. $sInput = '';
  1184. if(!isset($aInput['value']))
  1185. break;
  1186. $sInput = $aInput['value'];
  1187. if(get_mb_substr($sInput, 0, 1) == '_')
  1188. $sInput = _t($sInput);
  1189. break;
  1190. default:
  1191. //unknown control type
  1192. $sInput = 'Unknown control type';
  1193. }
  1194. // create input label
  1195. $sInput .= $this->genLabel($aInput);
  1196. // create input privacy group chooser
  1197. $sInput .= $this->genPrivacyGroupChooser($aInput);
  1198. return $sInput;
  1199. }
  1200. /**
  1201. * Generate new Input Element id
  1202. *
  1203. * @param array $aInput
  1204. * @return string
  1205. */
  1206. function getInputId(&$aInput)
  1207. {
  1208. if (isset($aInput['id']))
  1209. return $aInput['id'];
  1210. $sName = md5($aInput['name']);
  1211. $sID = $this->id . '_input_' . $sName;
  1212. if ( // multiple elements cause identical id's
  1213. (
  1214. (
  1215. $aInput['type'] == 'checkbox' and
  1216. substr($aInput['name'], -2) == '[]' // it is multiple element
  1217. ) or
  1218. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  1219. ) and
  1220. isset($aInput['value']) // if we can make difference
  1221. ) {
  1222. $sValue = md5($aInput['value']);
  1223. // add value
  1224. $sID .= '_' . $sValue;
  1225. }
  1226. $sID = trim($sID, '_');
  1227. $aInput['id'] = $sID; // just for repeated calls
  1228. return $sID;
  1229. }
  1230. /**
  1231. * Generate standard Input Element
  1232. *
  1233. * @param array $aInput
  1234. * @return string
  1235. */
  1236. function genInputStandard(&$aInput)
  1237. {
  1238. $aAttrs = $this->_genInputStandardAttrs($aInput);
  1239. return $this->oTemplate->parseHtmlByName('form_field_standard.html', [
  1240. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}")
  1241. ]);
  1242. }
  1243. protected function _genInputStandardAttrs(&$aInput)
  1244. {
  1245. // clone attributes for system use ;)
  1246. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1247. // add default className to attributes
  1248. $aAttrs['type'] = $aInput['type'];
  1249. if ('datetime' == $aAttrs['type'])
  1250. $aAttrs['type'] = 'date_time';
  1251. switch($aAttrs['type']) {
  1252. case 'datepicker':
  1253. case 'date_time':
  1254. $this->addCssJsTimepicker ();
  1255. break;
  1256. case 'slider':
  1257. case 'doublerange':
  1258. $this->addCssJsUi ();
  1259. break;
  1260. case 'dateselect':
  1261. if(!empty($aInput['value']) && strpos($aInput['value'], ' ') !== false)
  1262. list($aInput['value']) = explode(' ', $aInput['value']);
  1263. $this->_addJs(array(
  1264. 'combodate/combodate.js',
  1265. ), "'undefined' === typeof($.combodate)");
  1266. break;
  1267. case 'text':
  1268. if (isset($aAttrs['class']) && false !== strpos($aAttrs['class'], 'bx-form-input-rgb'))
  1269. $this->addCssJsMinicolors ();
  1270. break;
  1271. }
  1272. if ('doublerange' == $aAttrs['type'] && (!isset($aInput['value']) || !$aInput['value']) && !empty($aAttrs['min']) && !empty($aAttrs['max'])) {
  1273. $aInput['value'] = $aAttrs['min'] . '-' . $aAttrs['max'];
  1274. }
  1275. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  1276. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  1277. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  1278. if (!isset($aAttrs['data-format-24h']))
  1279. $aAttrs['data-frmt-24h'] = getParam('sys_format_input_24h');
  1280. if (!isset($aAttrs['data-format-date']))
  1281. $aAttrs['data-frmt-date'] = getParam('sys_format_date');
  1282. if (!isset($aAttrs['data-format-datetime']))
  1283. $aAttrs['data-frmt-datetime'] = getParam('sys_format_datetime');
  1284. // for inputs with labels generate id
  1285. if (isset($aInput['label']))
  1286. $aAttrs['id'] = $this->getInputId($aInput);
  1287. // for checkboxes
  1288. if (isset($aInput['checked']) and $aInput['checked'])
  1289. $aAttrs['checked'] = 'checked';
  1290. return $aAttrs;
  1291. }
  1292. /**
  1293. * Generate Switcher Input Element (based on checkbox)
  1294. *
  1295. * @param array $aInput
  1296. * @return string
  1297. */
  1298. function genInputSwitcher(&$aInput)
  1299. {
  1300. $aInput['type'] = 'checkbox';
  1301. $sCheckbox = $this->genInputStandard($aInput);
  1302. $aInput['type'] = 'switcher';
  1303. $sClass = 'off';
  1304. if(isset($aInput['checked']) && $aInput['checked'])
  1305. $sClass = 'on';
  1306. return $this->oTemplate->parseHtmlByName('form_field_switcher.html', [
  1307. 'class' => $sClass,
  1308. 'checkbox' => $sCheckbox
  1309. ]);
  1310. }
  1311. function genInputCheckbox(&$aInput, $bWrapped = false)
  1312. {
  1313. $sInput = $this->genInputStandard($aInput);
  1314. return $bWrapped ? $this->genWrapperInput($aInput, $sInput) : $sInput;
  1315. }
  1316. /**
  1317. * Generate Button Input Element
  1318. *
  1319. * @param array $aInput
  1320. * @return string
  1321. */
  1322. function genInputButton(&$aInput)
  1323. {
  1324. $aAttrs = $this->_genInputButtonAttrs($aInput);
  1325. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn";
  1326. if($aInput['type'] == 'submit')
  1327. $sClassAdd .= ' bx-btn-primary';
  1328. $aTmplVarsIcon = [];
  1329. if(isset($aInput['icon']) && ($sIcon = $this->genIcon($aInput['icon'])))
  1330. $aTmplVarsIcon = ['icon' => $sIcon];
  1331. return $this->oTemplate->parseHtmlByName('form_field_button.html', [
  1332. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1333. 'bx_if:show_icon' => [
  1334. 'condition' => !empty($aTmplVarsIcon),
  1335. 'content' => $aTmplVarsIcon
  1336. ],
  1337. 'value' => $aInput['value']
  1338. ]);
  1339. }
  1340. protected function _genInputButtonAttrs(&$aInput)
  1341. {
  1342. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1343. // add default className to attributes
  1344. $aAttrs['type'] = $aInput['type'];
  1345. if(isset($aInput['value']))
  1346. $aAttrs['value'] = $aInput['value'];
  1347. if(isset($aInput['name']))
  1348. $aAttrs['name'] = $aInput['name'];
  1349. return $aAttrs;
  1350. }
  1351. /**
  1352. * Generate Textarea Element
  1353. *
  1354. * @param array $aInput
  1355. * @return string
  1356. */
  1357. function genInputTextarea(&$aInput)
  1358. {
  1359. $aAttrs = $this->_genInputTextareaAttrs($aInput);
  1360. $sUniq = genRndPwd(10, false);
  1361. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']}";
  1362. $bHtml = $bHtmlEditor = false;
  1363. if(($bHtmlEditor = ($bHtml = (bool)($aInput['html'] ?? false)) && $this->addHtmlEditor($aInput['html'], $aInput, $sUniq))) {
  1364. $sClassAdd .= ' bx-form-input-html ' . $sUniq;
  1365. }
  1366. $bCode = $bCodeEditor = false;
  1367. if(($bCodeEditor = ($bCode = (bool)($aInput['code'] ?? false)) && $this->addCodeEditor($aInput['code'], $aInput, $sUniq))) {
  1368. $sClassAdd .= ' bx-form-input-code ' . $sUniq;
  1369. }
  1370. $aTmplVarsSwitcher = [];
  1371. if(($aInput['html_toggle'] ?? false) && $bHtmlEditor && ($aHtmlEditor = $this->getHtmlEditorInfo($aInput['html'], $aInput, $sUniq))) {
  1372. if(($sJsInit = ($aHtmlEditor['js_call_init'] ?? false)) && ($sJsDestroy = ($aHtmlEditor['js_call_destroy'] ?? false))) {
  1373. $aInputSwitcher = [
  1374. 'type' => 'switcher',
  1375. 'name' => $aInput['name'] . '_switcher',
  1376. 'checked' => true,
  1377. 'attrs' => [
  1378. 'onchange' => "if($(this).prop('checked')) " . $sJsInit . "; else " . $sJsDestroy . ";",
  1379. ],
  1380. ];
  1381. $aTmplVarsSwitcher = [
  1382. 'class' => 'bx-form-input-' . $aInput['type'] . '-switcher',
  1383. 'title' => _t('_sys_form_input_textarea_editor'),
  1384. 'switcher' => $this->genInputSwitcher($aInputSwitcher)
  1385. ];
  1386. }
  1387. }
  1388. $sValue = isset($aInput['value']) ? bx_process_output($bHtml || $bCode ? $aInput['value'] : strip_tags($aInput['value']), BX_DATA_TEXT, ['no_process_macros']) : '';
  1389. return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
  1390. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1391. 'value' => $sValue,
  1392. 'bx_if:show_switcher' => [
  1393. 'condition' => !empty($aTmplVarsSwitcher),
  1394. 'content' => $aTmplVarsSwitcher
  1395. ]
  1396. ]);
  1397. }
  1398. protected function _genInputTextareaAttrs(&$aInput)
  1399. {
  1400. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1401. $aAttrs['name'] = $aInput['name'];
  1402. // for inputs with labels generate id
  1403. if (isset($aInput['label']))
  1404. $aAttrs['id'] = $this->getInputId($aInput);
  1405. return $aAttrs;
  1406. }
  1407. function isHtmlEditor($iViewMode, &$aInput)
  1408. {
  1409. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  1410. }
  1411. function addHtmlEditor($iViewMode, &$aInput, $sUniq)
  1412. {
  1413. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1414. if(!$oEditor)
  1415. return false;
  1416. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name=' . $aInput['name'] . ']', $iViewMode, $this->_bDynamicMode, [
  1417. 'form_id' => $this->aFormAttrs['id'],
  1418. 'element_name' => $aInput['name'],
  1419. 'query_params' => $this->getHtmlEditorQueryParams($aInput),
  1420. 'uniq' => $sUniq
  1421. ]);
  1422. return true;
  1423. }
  1424. function getHtmlEditorInfo($iViewMode, &$aInput, $sUniq)
  1425. {
  1426. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1427. if(!$oEditor)
  1428. return false;
  1429. return $oEditor->getEditorInfo('#' . $this->aFormAttrs['id'] . ' [name=' . $aInput['name'] . ']', $iViewMode, $this->_bDynamicMode, [
  1430. 'form_id' => $this->aFormAttrs['id'],
  1431. 'element_name' => $aInput['name'],
  1432. 'uniq' => $sUniq
  1433. ]);
  1434. }
  1435. function addCodeEditor($iViewMode, &$aInput, $sUniq)
  1436. {
  1437. $this->addCssJsCodeMirror();
  1438. return true;
  1439. }
  1440. function getHtmlEditorQueryParams($aInput)
  1441. {
  1442. $aQueryParams = ['i' => $aInput['name'], 'f' => $this->aFormAttrs['id'], 'fi' => ''];
  1443. /**
  1444. * @hooks
  1445. * @hookdef hook-system-editor_query_params 'system', 'editor_query_params' - hook to override http(s) request's query params, which is used in HTML editor
  1446. * - $unit_name - equals `system`
  1447. * - $action - equals `editor_query_params`
  1448. * - $object_id - not used
  1449. * - $sender_id - not used
  1450. * - $extra_params - array of additional params with the following array keys:
  1451. * - `form` - [object] an instance of form, @see BxDolForm
  1452. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  1453. * @hook @ref hook-system-editor_query_params
  1454. */
  1455. bx_alert('system', 'editor_query_params', 0, 0, [
  1456. 'form' => $this,
  1457. 'override_result' => &$aQueryParams
  1458. ]);
  1459. return $aQueryParams;
  1460. }
  1461. /**
  1462. * Generate Select Box Element
  1463. *
  1464. * @param array $aInput
  1465. * @return string
  1466. */
  1467. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  1468. {
  1469. $aNewInput = $aInput;
  1470. $aNewInput['type'] = 'select';
  1471. $aNewInput['name'] .= '[]';
  1472. $sInput = $this->genInput($aNewInput);
  1473. return <<<BLAH
  1474. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  1475. $sInput
  1476. </div>
  1477. $sInfo
  1478. $sError
  1479. BLAH;
  1480. }
  1481. /**
  1482. * Generate Browse File Element
  1483. *
  1484. * @param array $aInput
  1485. * @return string
  1486. */
  1487. function genInputFile(&$aInput)
  1488. {
  1489. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  1490. if(!empty($aInput['attrs']['onchange']))
  1491. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  1492. else
  1493. $aInput['attrs']['onchange'] = $sOnChange;
  1494. $sInput = $this->genInputStandard($aInput);
  1495. return '<label class="bx-btn">' . $sInput . '<span class="bx-fif-label">' . _t('_sys_form_txt_select_file') . '</span></label><span class="bx-fif-value bx-def-margin-thd-left"></span>';
  1496. }
  1497. /**
  1498. * Generate Select Box Element
  1499. *
  1500. * @param array $aInput
  1501. * @return string
  1502. */
  1503. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  1504. {
  1505. if(empty($aInput['uploaders']) || !is_array($aInput['uploaders']))
  1506. return '';
  1507. $sUniqId = !empty($aInput['uploaders_id']) ? $aInput['uploaders_id'] : genRndPwd (8, false);
  1508. $bMultiple = $aInput['multiple'] ?? true;
  1509. $bInitGhosts = isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1;
  1510. $bInitReordering = empty($aInput['init_reordering']) ? 0 : 1;
  1511. $oUploader = null;
  1512. $sUploadersButtons = $sUploadersJs = '';
  1513. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1514. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  1515. if(!$oUploader)
  1516. continue;
  1517. $oUploader->setMultiple($bMultiple);
  1518. //--- Get Button code.
  1519. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1520. if(empty($aInput['attrs']['disabled']))
  1521. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  1522. $aParamsButtons = [
  1523. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1524. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1525. 'attrs' => bx_convert_array2attrs($aAttrs),
  1526. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  1527. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  1528. ];
  1529. $sUploadersButtons .= $oUploader->getUploaderButton($aParamsButtons);
  1530. //--- Get JS code.
  1531. $sGhostTemplate = $this->genGhostTemplate($aInput);
  1532. $aParamsJs = array_merge($oUploader->getUploaderJsParams(),
  1533. [
  1534. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1535. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1536. 'is_init_ghosts' => $bInitGhosts,
  1537. 'is_init_reordering' => $bInitReordering
  1538. ]
  1539. );
  1540. if(isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  1541. $aParamsJs['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  1542. $sUploadersJs .= $oUploader->getUploaderJs($sGhostTemplate, $bMultiple, $aParamsJs, $this->_bDynamicMode);
  1543. }
  1544. if(!$oUploader)
  1545. return '';
  1546. if($bInitReordering) {
  1547. $this->addCssJsUi();
  1548. $this->addCssJsUiSortable();
  1549. }
  1550. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  1551. 'uploaders_buttons' => $sUploadersButtons,
  1552. 'info' => $sInfo,
  1553. 'error' => $sError,
  1554. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  1555. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  1556. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  1557. 'uploaders_js' => $sUploadersJs,
  1558. ));
  1559. }
  1560. protected function genGhostTemplate(&$aInput)
  1561. {
  1562. $sGhostTemplate = false;
  1563. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  1564. $oFormNested = $aInput['ghost_template'];
  1565. if ($oFormNested instanceof BxDolFormNestedGhost)
  1566. $sGhostTemplate = $oFormNested->genForm();
  1567. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && isset($aInput['ghost_template']['inputs'])) { // form is not submitted and ghost template is form array
  1568. $oFormNested = new BxDolFormNestedGhost($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  1569. $sGhostTemplate = $oFormNested->getCode();
  1570. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  1571. $sGhostTemplate = array ();
  1572. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  1573. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNestedGhost)
  1574. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  1575. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  1576. $sGhostTemplate = $aInput['ghost_template'];
  1577. }
  1578. return $sGhostTemplate;
  1579. }
  1580. protected function genCustomInputUsernamesSuggestions ($aInput)
  1581. {
  1582. $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
  1583. $aAttrs = $this->_genCustomInputUsernamesSuggestionsAttrs($aInput, $bDisabled);
  1584. $aTmplVarsVals = [];
  1585. $sValue = '';
  1586. if(!empty($aInput['value'])) {
  1587. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']) . 'Value';
  1588. if(method_exists($this, $sCustomMethod))
  1589. $aTmplVarsVals = $this->$sCustomMethod($aInput);
  1590. else if(is_array($aInput['value']) || (is_numeric($aInput['value']))) {
  1591. if (!is_array($aInput['value']))
  1592. $aInput['value'] = [$aInput['value']];
  1593. foreach($aInput['value'] as $sVal) {
  1594. if(!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  1595. continue;
  1596. $aTmplVarsVals[] = [
  1597. 'item_unit' => $oProfile->getUnit(0, ['template' => ['name' => 'unit_wo_info', 'size' => 'icon']]),
  1598. 'item_name' => $oProfile->getDisplayName(),
  1599. 'name' => $aInput['name'] . (isset($aInput['custom']['only_once']) && $aInput['custom']['only_once'] == 1 ? '' : '[]'),
  1600. 'value' => $sVal
  1601. ];
  1602. }
  1603. }
  1604. else if(is_string($aInput['value']))
  1605. $sValue = $aInput['value'];
  1606. }
  1607. $aTmplVarsInputText = [];
  1608. if(!$bDisabled) {
  1609. $aInputText = $aInput;
  1610. $aInputText['type'] = 'text';
  1611. $aInputText['value'] = '';
  1612. $aInputText['attrs'] = $this->_genCustomInputUsernamesSuggestionsTextAttrs($aInputText, $bDisabled);
  1613. unset($aInputText['name']);
  1614. $aTmplVarsInputText['input'] = $this->genInputStandard($aInputText);
  1615. }
  1616. if (bx_is_api()){
  1617. $aInput['type'] = 'initial_members';
  1618. return $aInput;
  1619. }
  1620. $this->addCssJsUi();
  1621. $sJsCode = '';
  1622. if(!$bDisabled) {
  1623. $sJsCode = $this->oTemplate->parseHtmlByName('form_field_custom_suggestions_js.html', [
  1624. 'id' => $aAttrs['id'],
  1625. 'name' => $aInput['name'],
  1626. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  1627. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  1628. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  1629. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  1630. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  1631. ]);
  1632. if($this->_bDynamicMode)
  1633. $sJsCode = $this->oTemplate->addJsPreloadedWrapped([
  1634. 'jquery-ui/jquery-ui.min.js'
  1635. ], $sJsCode);
  1636. else
  1637. $sJsCode = $this->oTemplate->addJsCodeOnLoadWrapped($sJsCode);
  1638. }
  1639. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  1640. 'name' => $aInput['name'],
  1641. 'attrs' => bx_convert_array2attrs($aAttrs),
  1642. 'bx_repeat:vals' => $aTmplVarsVals,
  1643. 'value' => $sValue,
  1644. 'bx_if:input' => array(
  1645. 'condition' => !$bDisabled,
  1646. 'content' => $aTmplVarsInputText
  1647. ),
  1648. 'js_code' => $sJsCode
  1649. ));
  1650. }
  1651. protected function _genCustomInputUsernamesSuggestionsAttrs (&$aInput, $bDisabled = false)
  1652. {
  1653. $aAttrs = ['id' => $aInput['name'] . time() . mt_rand(0, 100)];
  1654. $aAttrs['class'] = 'bx-form-input-autotoken bx-form-input-text bx-def-font-inputs';
  1655. if($bDisabled)
  1656. $aAttrs['class'] .= ' bx-form-input-disabled';
  1657. if(!empty($aInput['attrs']['class']))
  1658. $aAttrs['class'] .= ' ' . $aInput['attrs']['class'];
  1659. return $aAttrs;
  1660. }
  1661. protected function _genCustomInputUsernamesSuggestionsTextAttrs (&$aInput, $bDisabled = false)
  1662. {
  1663. $aAttrs = ['value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off'];
  1664. if(!empty($aInput['attrs']) && is_array($aInput['attrs'])) {
  1665. if(isset($aInput['attrs']['class']))
  1666. unset($aInput['attrs']['class']);
  1667. $aAttrs = array_merge($aAttrs, $aInput['attrs']);
  1668. }
  1669. return $aAttrs;
  1670. }
  1671. protected function genCustomViewRowValueLabels ($aInput)
  1672. {
  1673. if (empty($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  1674. return null;
  1675. if (!empty($aInput['value']) && !is_array($aInput['value']))
  1676. $aInput['value'] = unserialize($aInput['value']);
  1677. $s = '';
  1678. foreach ($aInput['value'] as $sLabel)
  1679. $s .= '<a href="' . $oMetatags->keywordsGetHashTagUrl($sLabel, $aInput['content_id']) . '"><b class="bx-def-label val">' . trim($sLabel) . '</b></a>';
  1680. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  1681. 'values' => $s,
  1682. ));
  1683. }
  1684. protected function genCustomInputLabels (&$aInput)
  1685. {
  1686. $oLabel = BxDolLabel::getInstance();
  1687. $sName = !empty($aInput['name']) ? $aInput['name'] : 'labels';
  1688. $sValue = '';
  1689. if(!empty($aInput['value'])) {
  1690. if(!is_array($aInput['value']))
  1691. $aInput['value'] = unserialize($aInput['value']);
  1692. if(is_array($aInput['value']))
  1693. foreach($aInput['value'] as $sLabel)
  1694. $sValue .= $oLabel->getLabel($sName, $sLabel);
  1695. }
  1696. $sKeyPlaceholder = $aInput['caption_src'] . '_placeholder';
  1697. if(strcmp($sKeyPlaceholder, _t($sKeyPlaceholder)) != 0)
  1698. $sValue .= $oLabel->getLabelPlaceholder($sKeyPlaceholder);
  1699. $aInputLabels = array(
  1700. 'type' => 'custom',
  1701. 'name' => $sName,
  1702. 'caption' => '',
  1703. 'value' => $sValue,
  1704. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . bx_append_url_params('label.php', array(
  1705. 'action' => 'labels_list',
  1706. )),
  1707. 'attrs' => array(
  1708. 'class' => 'bx-form-input-labels',
  1709. 'disabled' => 'disabled'
  1710. )
  1711. );
  1712. if (bx_is_api()) {
  1713. return array_merge($aInput, [
  1714. 'type' => 'labels',
  1715. 'values' => bx_srv('system', 'get_labels', [], 'TemplLabelsServices')
  1716. ]);
  1717. }
  1718. return $this->oTemplate->parseHtmlByName('label_select_field.html', array(
  1719. 'js_object' => $oLabel->getJsObjectName(),
  1720. 'js_code' => $oLabel->getJsCodeForm(),
  1721. 'html_id' => $oLabel->getFormFieldId($aInput),
  1722. 'name' => $sName,
  1723. 'input_labels' => $this->genCustomInputUsernamesSuggestions($aInputLabels)
  1724. ));
  1725. }
  1726. /**
  1727. * Generate Select Element
  1728. *
  1729. * @param array $aInput
  1730. * @return string
  1731. */
  1732. function genInputSelect(&$aInput)
  1733. {
  1734. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1735. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1736. }
  1737. /**
  1738. * Generate Select RGB Element
  1739. *
  1740. * @param array $aInput
  1741. * @return string
  1742. */
  1743. function genInputSelectRgb(&$aInput)
  1744. {
  1745. $aInput['type'] = 'select';
  1746. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1747. $aInput['values_list_name'] = 'sys_colors';
  1748. $aInput['values'] = self::getDataItems('sys_colors', isset(self::$TYPES_SET[$aInput['type']]));
  1749. $aInput['attrs'] = ['class' => 'bx-form-input-rgb-list'];
  1750. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1751. $aInput['values'][$sOptValue] = ['key' => $sOptValue, 'value' => $sOptTitle, 'class' => 'bg-' . $sOptValue, 'style' => 'color:#fff'];
  1752. }
  1753. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1754. }
  1755. /**
  1756. * Generate Multiple Select Element
  1757. *
  1758. * @param array $aInput
  1759. * @return string
  1760. */
  1761. function genInputSelectMultiple(&$aInput)
  1762. {
  1763. $aCurValues = array();
  1764. if (isset($aInput['value']) && $aInput['value'])
  1765. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1766. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  1767. }
  1768. /**
  1769. * Generate Checkbox Set Element
  1770. *
  1771. * @param array $aInput
  1772. * @return string
  1773. */
  1774. function genInputCheckboxSet(&$aInput)
  1775. {
  1776. $aCurValues = array();
  1777. if (isset($aInput['value']) && $aInput['value'])
  1778. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1779. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  1780. }
  1781. /**
  1782. * Generate Radiobuttons Set Element
  1783. *
  1784. * @param array $aInput
  1785. * @return string
  1786. */
  1787. function genInputRadioSet(&$aInput)
  1788. {
  1789. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1790. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  1791. }
  1792. function _isSelected ($sValue, $sCurValue)
  1793. {
  1794. return ((string)$sValue === (string)$sCurValue);
  1795. }
  1796. function _isSelectedMultiple ($sValue, $aCurValues)
  1797. {
  1798. return in_array($sValue, $aCurValues);
  1799. }
  1800. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  1801. {
  1802. $aOptions = [];
  1803. if(isset($aInput['values']) && is_array($aInput['values'])) {
  1804. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1805. $aOption = array(
  1806. 'bx_if:show_option_group_begin' => [
  1807. 'condition' => false,
  1808. 'content' => []
  1809. ],
  1810. 'bx_if:show_option_group_end' => [
  1811. 'condition' => false,
  1812. 'content' => []
  1813. ],
  1814. 'bx_if:show_option' => [
  1815. 'condition' => false,
  1816. 'content' => []
  1817. ]
  1818. );
  1819. $sOptAttrs = "";
  1820. if(is_array($sOptTitle)) {
  1821. if(isset($sOptTitle['type'])) {
  1822. switch($sOptTitle['type']) {
  1823. case 'group_header':
  1824. $aOption = array_merge($aOption, [
  1825. 'bx_if:show_option_group_begin' => [
  1826. 'condition' => true,
  1827. 'content' => [
  1828. 'opt_label' => bx_process_output($sOptTitle['value'])
  1829. ]
  1830. ]
  1831. ]);
  1832. $aOptions[] = $aOption;
  1833. break;
  1834. case 'group_end':
  1835. $aOption = array_merge($aOption, [
  1836. 'bx_if:show_option_group_end' => [
  1837. 'condition' => true,
  1838. 'content' => []
  1839. ]
  1840. ]);
  1841. $aOptions[] = $aOption;
  1842. break;
  1843. }
  1844. continue;
  1845. }
  1846. $aOptAttrs = $this->_genInputSelectOptionAttrs($sOptTitle);
  1847. if(!empty($aOptAttrs))
  1848. $sOptAttrs = bx_convert_array2attrs($aOptAttrs);
  1849. $sOptValue = $sOptTitle['key'];
  1850. $sOptTitle = $sOptTitle['value'];
  1851. }
  1852. $aOption = array_merge($aOption, [
  1853. 'bx_if:show_option' => [
  1854. 'condition' => true,
  1855. 'content' => [
  1856. 'opt_title' => bx_process_output($sOptTitle),
  1857. 'opt_value' => !empty($sOptValue) ? bx_html_attribute($sOptValue) : '',
  1858. 'opt_attrs' => $sOptAttrs,
  1859. 'bx_if:show_selected' => [
  1860. 'condition' => $this->$sIsSelectedFunc($sOptValue, $mixedCurrentVal),
  1861. 'content' => []
  1862. ]
  1863. ]
  1864. ]
  1865. ]);
  1866. $aOptions[] = $aOption;
  1867. }
  1868. }
  1869. $aAttrs = $this->_genInputSelectAttrs($aInput, $isMultiple);
  1870. return $this->_parseInputSelect('form_field_select.html', [
  1871. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
  1872. 'bx_repeat:options' => $aOptions,
  1873. 'content' => !empty($aInput['content']) ? $aInput['content'] : ''
  1874. ]);
  1875. }
  1876. protected function _genInputSelectAttrs(&$aInput, $isMultiple)
  1877. {
  1878. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1879. $aAttrs['name'] = $aInput['name'];
  1880. if ($isMultiple) {
  1881. $aAttrs['name'] .= '[]';
  1882. $aAttrs['multiple'] = 'multiple';
  1883. }
  1884. // for inputs with labels generate id
  1885. if (isset($aInput['label']))
  1886. $aAttrs['id'] = $this->getInputId($aInput);
  1887. return $aAttrs;
  1888. }
  1889. protected function _genInputSelectOptionAttrs(&$aOption)
  1890. {
  1891. $aAttrs = [];
  1892. if(!empty($aOption['attrs']) && is_array($aOption['attrs']))
  1893. $aAttrs = $aOption['attrs'];
  1894. if(isset($aOption['class']))
  1895. $aAttrs['class'] = !empty($aAttrs['class']) ? $aAttrs['class'] . ' ' . $aOption['class'] : $aOption['class'];
  1896. if(isset($aOption['style']))
  1897. $aAttrs['style'] = !empty($aAttrs['style']) ? $aAttrs['style'] . ' ' . $aOption['style'] : $aOption['style'];
  1898. return $aAttrs;
  1899. }
  1900. protected function _parseInputSelect($sTmplName, $aTmplVars)
  1901. {
  1902. return $this->oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  1903. }
  1904. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  1905. {
  1906. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  1907. $aAttrs['name'] = $aInput['name'];
  1908. // for inputs with labels generate id
  1909. if (isset($aInput['label']))
  1910. $aAttrs['id'] = $this->getInputId($aInput);
  1911. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1912. // generate options
  1913. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1914. $iDividerThreshold = !empty($aInput['dv_thd']) ? (int)$aInput['dv_thd'] : 3;
  1915. $sOptions = '';
  1916. $aAdditionalFieldInfo = [];
  1917. if (in_array($aInput['type'], ['radio_set', 'checkbox_set']) && !empty($aInput['values_list_name']))
  1918. $aAdditionalFieldInfo = BxDolFormQuery::getDataItems($aInput['values_list_name'], false, BX_DATA_VALUES_ALL);
  1919. if (isset($aInput['values']) && is_array($aInput['values'])) {
  1920. if (count($aInput['values']) > $iDividerThreshold && $sDivider == $this->_sDivider)
  1921. $sDivider = $this->_sDividerAlt;
  1922. // generate complex input using simple standard inputs
  1923. foreach ($aInput['values'] as $sValue => $sLabel) {
  1924. if(is_array($sLabel)) {
  1925. $sValue = $sLabel['key'];
  1926. $sLabel = $sLabel['value'];
  1927. }
  1928. // create new simple input
  1929. $aNewInput = array(
  1930. 'type' => $sType,
  1931. 'name' => $aInput['name'] . $sNameAppend,
  1932. 'value' => $sValue,
  1933. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1934. 'label' => $sLabel,
  1935. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1936. );
  1937. if (isset($aInput['label_as_html']))
  1938. $aNewInput['label_as_html'] = $aInput['label_as_html'];
  1939. $sNewInput = $this->genInputCheckbox($aNewInput, true) . $this->genLabel($aNewInput);
  1940. // add additional info for the field
  1941. if (isset($aAdditionalFieldInfo[$sValue]) && !empty($aAdditionalFieldInfo[$sValue]['LKey2']))
  1942. $sNewInput = $this->genFiledItemInfoWrapper($sNewInput, _t($aAdditionalFieldInfo[$sValue]['LKey2']));
  1943. // attach new input to complex
  1944. $sOptions .= ($sNewInput . $sDivider);
  1945. }
  1946. }
  1947. // generate element
  1948. $sCode = <<<BLAH
  1949. <div $sAttrs>
  1950. $sOptions
  1951. </div>
  1952. BLAH;
  1953. return $sCode;
  1954. }
  1955. function genInputLocation(&$aInput)
  1956. {
  1957. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1958. if (!$o)
  1959. return _t('_sys_txt_error_occured');
  1960. return $o->genInputLocation($aInput, $this);
  1961. }
  1962. public function setLocationVals ($aInput, $aVals)
  1963. {
  1964. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1965. if (!$o)
  1966. return false;
  1967. return $o->setLocationVals ($aInput, $aVals, $this);
  1968. }
  1969. public function setLocationVal ($aInput, $sIndex, $sVal)
  1970. {
  1971. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1972. if (!$o)
  1973. return false;
  1974. return $o->setLocationVal ($aInput, $sIndex, $sVal, $this);
  1975. }
  1976. protected function getLocationVal ($aInput, $sIndex)
  1977. {
  1978. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1979. if (!$o)
  1980. return false;
  1981. return $o->getLocationVal ($aInput, $sIndex, $this);
  1982. }
  1983. function genInputPassword(&$aInput)
  1984. {
  1985. return $this->oTemplate->parseHtmlByName('form_field_password.html', array('input' => $this->genInputStandard($aInput)));
  1986. }
  1987. function genInputPrice(&$aInput)
  1988. {
  1989. $sCurrency = '';
  1990. if(!isset($aInput['value_currency'])) {
  1991. $sCurrency = BxDolPayments::getInstance()->getOption('default_currency_code');
  1992. if(empty($sCurrency))
  1993. $sCurrency = getParam('currency_code');
  1994. }
  1995. else
  1996. $sCurrency = $aInput['value_currency'];
  1997. return $this->oTemplate->parseHtmlByName('form_field_price.html', array(
  1998. 'bx_if:show_currency' => [
  1999. 'condition' => !empty($sCurrency),
  2000. 'content' => [
  2001. 'currency' => $sCurrency,
  2002. ]
  2003. ],
  2004. 'input' => $this->genInputStandard($aInput)
  2005. ));
  2006. }
  2007. function genInputNestedForm(&$aInput)
  2008. {
  2009. return '';
  2010. }
  2011. function genNestedForm(&$aInput)
  2012. {
  2013. return '';
  2014. }
  2015. function genInputCaptcha(&$aInput)
  2016. {
  2017. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  2018. // for inputs with labels generate id
  2019. if (isset($aInput['label']))
  2020. $aAttrs['id'] = $this->getInputId($aInput);
  2021. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  2022. $oCaptcha = BxDolCaptcha::getObjectInstance();
  2023. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  2024. }
  2025. /**
  2026. * Generate Label Element
  2027. *
  2028. * @param string $aInput input array
  2029. * @return string HTML code
  2030. */
  2031. function genLabel(&$aInput)
  2032. {
  2033. if (!isset($aInput['label']) || empty($aInput['label']))
  2034. return '';
  2035. $sInputID = $this->getInputId($aInput);
  2036. $sLabel = bx_process_output($aInput['label']);
  2037. if (isset($aInput['label_as_html']) && $aInput['label_as_html'] == true)
  2038. $sLabel = $aInput['label'];
  2039. return '<label for="' . $sInputID . '">' . $sLabel . '</label>';
  2040. }
  2041. function genPrivacyGroupChooser(&$aInput, $sPrivacyObject = '')
  2042. {
  2043. if(!isset($aInput['privacy']) || empty($aInput['privacy']))
  2044. return '';
  2045. $iAuthorId = bx_get_logged_profile_id();
  2046. $mixedResult = checkActionModule($iAuthorId, 'set form fields privacy', 'system');
  2047. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  2048. return '';
  2049. if(empty($sPrivacyObject))
  2050. $sPrivacyObject = $this->_sPrivacyObjectView;
  2051. $iInputId = (int)$aInput['id'];
  2052. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  2053. if(!isset($aInput['attrs_wrapper']['class']))
  2054. $aInput['attrs_wrapper']['class'] = '';
  2055. $aInput['attrs_wrapper']['class'] .= ' bx-form-input-wrapper-pgc';
  2056. return $this->oTemplate->parseHtmlByName('form_field_privacy.html', array(
  2057. 'js_object' => $this->getJsObjectName(),
  2058. 'html_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  2059. 'id' => $iInputId,
  2060. 'privacy_object' => $sPrivacyObject,
  2061. 'icon' => $this->_getPrivacyIcon($mixedPrivacyGroup)
  2062. ));
  2063. }
  2064. function genFiledItemInfoWrapper($sInput, $sInfoValue)
  2065. {
  2066. $sInfo = $this->genInfoIcon($sInfoValue) ;
  2067. return $this->oTemplate->parseHtmlByName('form_field_option_info.html', [
  2068. 'input' => $sInput,
  2069. 'info' => $sInfo
  2070. ]);
  2071. }
  2072. function genIcon($sIcon)
  2073. {
  2074. if(!$sIcon)
  2075. return '';
  2076. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate)->getIcon($sIcon);
  2077. return $this->oTemplate->parseHtmlByName('form_input_icon.html', [
  2078. 'bx_if:icon' => [
  2079. 'condition' => (bool)$sIcon,
  2080. 'content' => ['icon' => $sIcon],
  2081. ],
  2082. 'bx_if:icon-html' => [
  2083. 'condition' => (bool)$sIconHtml,
  2084. 'content' => ['icon' => $sIconHtml],
  2085. ],
  2086. 'bx_if:image_inline' => [
  2087. 'condition' => false,
  2088. 'content' => ['image' => ''],
  2089. ]
  2090. ]);
  2091. }
  2092. function genInfoIcon($sInfo)
  2093. {
  2094. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small mt-1">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  2095. }
  2096. function genErrorIcon( $sError = '' )
  2097. {
  2098. if ($this->bEnableErrorIcon) {
  2099. $sStyle = '';
  2100. if (!$sError)
  2101. $sStyle = 'style="display:none;"';
  2102. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  2103. }
  2104. }
  2105. function getOpenSection($aAttrs = [], $sTitle = '', $aWrapperAttrs = [])
  2106. {
  2107. $sClose = '';
  2108. if($this->_isSectionOpened)
  2109. $sClose = $this->{$this->_sSectionClose}();
  2110. $sWrapperAttrs = bx_convert_array2attrs($aWrapperAttrs, "bx-form-section-wrapper my-4");
  2111. if(!$aAttrs || !is_array($aAttrs))
  2112. $aAttrs = [];
  2113. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-section-" . ($sTitle ? "header" : "divider"));
  2114. $this->_isSectionOpened = true;
  2115. return $sClose . "<!-- form header content begins -->\n <div $sWrapperAttrs> <div $sAttrs> $sTitle <div class=\"bx-form-section-content pt-4 pb-6" . ($sTitle ? ' px-6' : '') . "\">\n";
  2116. }
  2117. function getCloseSection()
  2118. {
  2119. if(!$this->_isSectionOpened)
  2120. return '';
  2121. $this->_isSectionOpened = false;
  2122. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  2123. }
  2124. function getOpenSectionViewMode($aAttrs = array(), $sTitle = '', $aWrapperAttrs = [])
  2125. {
  2126. if (!$this->_isSectionOpened) {
  2127. if (!$aAttrs || !is_array($aAttrs))
  2128. $aAttrs = array();
  2129. if ($sTitle)
  2130. $sClassesAdd = "bx-form-section-header";
  2131. else
  2132. $sClassesAdd = "bx-form-section-divider";
  2133. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  2134. $sWrapperAttrs = bx_convert_array2attrs($aWrapperAttrs, "bx-form-section-wrapper bx-def-margin-top");
  2135. $this->_isSectionOpened = true;
  2136. return "<!-- form header content begins -->\n <div $sWrapperAttrs> <div $sAttrs> $sTitle <div class=\"bx-form-section-content bx-def-padding-top\">\n";
  2137. } else {
  2138. return '';
  2139. }
  2140. }
  2141. function getCloseSectionViewMode()
  2142. {
  2143. if ($this->_isSectionOpened) {
  2144. $this->_isSectionOpened = false;
  2145. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  2146. } else {
  2147. return '';
  2148. }
  2149. }
  2150. public static function getJsCalendarLangs ()
  2151. {
  2152. return array ('ar-dz' => 1, 'ar' => 1, 'at' => 1, 'az' => 1, 'be' => 1, 'bg' => 1, 'bn' => 1, 'bs' => 1, 'cat' => 1, 'ckb' => 1, 'cs' => 1, 'cy' => 1, 'da' => 1, 'de' => 1, 'default' => 1, 'eo' => 1, 'es' => 1, 'et' => 1, 'fa' => 1, 'fi' => 1, 'fo' => 1, 'fr' => 1, 'ga' => 1, 'gr' => 1, 'he' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'index' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'ka' => 1, 'km' => 1, 'ko' => 1, 'kz' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'mn' => 1, 'ms' => 1, 'my' => 1, 'nl' => 1, 'nn' => 1, 'no' => 1, 'pa' => 1, 'pl' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'si' => 1, 'sk' => 1, 'sl' => 1, 'sq' => 1, 'sr-cyr' => 1, 'sr' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'uz' => 1, 'uz_latn' => 1, 'vn' => 1, 'zh-tw' => 1, 'zh' => 1);
  2153. }
  2154. public static function getCssJsCalendar()
  2155. {
  2156. $aLangs = self::getJsCalendarLangs();
  2157. $sLang = BxDolLanguages::getInstance()->detectLanguageFromArray($aLangs);
  2158. return [[
  2159. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flatpickr/dist/|flatpickr.min.css'
  2160. ], [
  2161. 'flatpickr/dist/flatpickr.min.js',
  2162. 'flatpickr/dist/l10n/' . $sLang . '.js',
  2163. ]
  2164. ];
  2165. }
  2166. function addCssJsUi ()
  2167. {
  2168. if (self::$_isCssJsUiAdded)
  2169. return;
  2170. $this->_addJs(array(
  2171. 'jquery-ui/jquery-ui.min.js',
  2172. ), "'undefined' === typeof(jQuery.ui.position)");
  2173. $this->_addCss('jquery-ui/jquery-ui.min.css');
  2174. self::$_isCssJsUiAdded = true;
  2175. }
  2176. function addCssJsUiSortable ()
  2177. {
  2178. if (self::$_isCssJsUiSortableAdded)
  2179. return;
  2180. $this->_addJs(array(
  2181. 'jquery-ui/jquery-ui.min.js'
  2182. ), "'undefined' === typeof(jQuery.ui.position)");
  2183. self::$_isCssJsUiSortableAdded = true;
  2184. }
  2185. function addCssJsTimepicker ()
  2186. {
  2187. if (self::$_isCssJsTimepickerAdded)
  2188. return;
  2189. list($aCss, $aJs) = self::getCssJsCalendar();
  2190. $this->_addCss($aCss);
  2191. $this->_addJs($aJs, "'undefined' === typeof(flatpickr)");
  2192. self::$_isCssJsTimepickerAdded = true;
  2193. }
  2194. function addCssJsMinicolors ()
  2195. {
  2196. if (self::$_isCssJsMinicolorsAdded)
  2197. return;
  2198. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  2199. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  2200. self::$_isCssJsMinicolorsAdded = true;
  2201. }
  2202. function addCssJsCodeMirror ()
  2203. {
  2204. if (self::$_isCssJsCodeMirrorAdded)
  2205. return;
  2206. $this->_addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css']);
  2207. $this->_addJs(['codemirror/codemirror.min.js'], "'undefined' === typeof(CodeMirror)");
  2208. self::$_isCssJsCodeMirrorAdded = true;
  2209. }
  2210. function addCssJsViewMode ()
  2211. {
  2212. if (self::$_isCssJsAddedViewMode)
  2213. return;
  2214. $this->_addCss('forms.css');
  2215. self::$_isCssJsAddedViewMode = true;
  2216. }
  2217. function addCssJsCore ()
  2218. {
  2219. if (self::$_isCssJsAdded)
  2220. return;
  2221. $this->_addCss('forms.css');
  2222. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  2223. $this->_addJs(array('BxDolForm.js', 'BxDolNestedForm.js'), "true");
  2224. self::$_isCssJsAdded = true;
  2225. }
  2226. function addCssJs ()
  2227. {
  2228. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  2229. $this->addCssJsViewMode ();
  2230. } else {
  2231. $this->addCssJsCore();
  2232. }
  2233. }
  2234. protected function _processCssJs()
  2235. {
  2236. $sRet = '';
  2237. if ($this->_bDynamicMode) {
  2238. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  2239. $sRet .= "\n<script>\n";
  2240. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  2241. foreach ($this->_aJs as $sCondition => $aJs) {
  2242. $sJs = $this->oTemplate->addJs($aJs, true);
  2243. if (!$sJs)
  2244. continue;
  2245. $sRet .= "if ($sCondition)\n";
  2246. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  2247. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  2248. }
  2249. $sRet .= "\n</script>\n";
  2250. }
  2251. else {
  2252. $this->oTemplate->addCss($this->_aCss);
  2253. $aJs = array();
  2254. foreach ($this->_aJs as $sCondition => $a)
  2255. $aJs = array_merge($aJs, $a);
  2256. $this->oTemplate->addJs($aJs);
  2257. }
  2258. return $sRet;
  2259. }
  2260. protected function _addJs($mixed, $sJsCondition)
  2261. {
  2262. if (!is_array($mixed))
  2263. $mixed = array($mixed);
  2264. $this->_aJs[$sJsCondition] = $mixed;
  2265. }
  2266. protected function _addCss($mixed)
  2267. {
  2268. if (!is_array($mixed))
  2269. $mixed = array($mixed);
  2270. foreach ($mixed as $s)
  2271. $this->_aCss[$s] = $s;
  2272. }
  2273. }
  2274. /** @} */