BxBaseFormView.php 98 KB

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