BxBaseFormView.php 106 KB

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