BxBaseFormView.php 99 KB

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