BxBaseFormView.php 81 KB

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