BxBaseFormView.php 101 KB

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