BxBaseFormView.php 95 KB

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