BxBaseFormView.php 94 KB

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