BxBaseFormView.php 106 KB

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