BxBaseFormView.php 96 KB

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