BxBaseFormView.php 107 KB

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