BxBaseFormView.php 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  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. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  699. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  700. $sClassOneLineCaption = '';
  701. $sClassOneLineValue = '';
  702. if ($isOneLine) {
  703. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  704. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  705. $aInput['attrs']['id'] = $this->getInputId($aInput);
  706. if ($sCaption)
  707. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  708. }
  709. $sInput = $this->genInput($aInput);
  710. if(isset($aInput['error_updated']) && $aInput['error_updated'] === true)
  711. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  712. if(empty($sErrorIcon))
  713. $sErrorIcon = '';
  714. $aTmplVarsRow = array(
  715. 'bx_if:show_caption' => array(
  716. 'condition' => !empty($sCaption),
  717. 'content' => array(
  718. 'class_caption' => $sClassOneLineCaption,
  719. 'caption' => $sCaption,
  720. 'required' => $sRequired,
  721. 'help' => $sHelp,
  722. )
  723. ),
  724. 'class_value' => $sClassAdd . $sClassOneLineValue,
  725. 'value' => $this->genWrapperInput($aInput, $sInput),
  726. 'info' => $sInfoIcon,
  727. 'error' => $sErrorIcon,
  728. );
  729. return $this->oTemplate->parseHtmlByName('form_row_standard.html', array(
  730. 'tr_attrs' => $sTrAttrs,
  731. 'bx_if:show_one_line' => array(
  732. 'condition' => $isOneLine,
  733. 'content' => $aTmplVarsRow
  734. ),
  735. 'bx_if:show_lined' => array(
  736. 'condition' => !$isOneLine,
  737. 'content' => $aTmplVarsRow
  738. ),
  739. 'info' => $sInfoIcon,
  740. 'error' => $sErrorIcon
  741. ));
  742. }
  743. function genWrapperInput($aInput, $sContent)
  744. {
  745. $aAttrs = $this->_genWrapperInputAttrs($aInput);
  746. $sClass = "bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}";
  747. if(isset($aInput['html']) && $aInput['html'] && $this->isHtmlEditor($aInput['html'], $aInput))
  748. $sClass .= ' bx-form-input-wrapper-html';
  749. return $this->oTemplate->parseHtmlByName('form_input_wrapper.html', array(
  750. 'attrs' => bx_convert_array2attrs($aAttrs, $sClass),
  751. 'content' => $sContent
  752. ));
  753. }
  754. protected function _genWrapperInputAttrs(&$aInput)
  755. {
  756. return isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? $aInput['attrs_wrapper'] : [];
  757. }
  758. /**
  759. * Generate custom row
  760. *
  761. * @param array $aInput
  762. * @param string $sCustomMethod custom method to generate code for input
  763. * @return string
  764. */
  765. function genRowCustom(&$aInput, $sCustomMethod)
  766. {
  767. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  768. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  769. $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> ' : '';
  770. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  771. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  772. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  773. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  774. if (isset($aInput['name']))
  775. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  776. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  777. return $this->oTemplate->parseHtmlByName('form_row_custom.html', array(
  778. 'tr_attrs' => $sTrAttrs,
  779. 'bx_if:show_caption' => array(
  780. 'condition' => !empty($sCaption),
  781. 'content' => array(
  782. 'caption' => $sCaption,
  783. 'required' => $sRequired,
  784. 'help' => $sHelp
  785. )
  786. ),
  787. 'class_value' => $sClassAdd,
  788. 'value' => $sInput,
  789. ));
  790. }
  791. /**
  792. * Generate Block Headers row
  793. *
  794. * @param array $aInput
  795. * @return string
  796. */
  797. function genRowBlockHeader(&$aInput)
  798. {
  799. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  800. // if there is no caption - show divider only
  801. if (empty($aInput['caption'])) {
  802. $sCode = $this->{$this->_sSectionClose}();
  803. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  804. return $sCode;
  805. }
  806. // if section is collapsed by default, add necessary code
  807. $sClassAddCollapsable = 'bx-form-collapsable';
  808. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  809. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  810. // display section with caption
  811. $sCode = $this->{$this->_sSectionClose}();
  812. if (empty($aAttrs))
  813. $aAttrs = array('class' => 'bx-form-collapsable ' . $sClassAddCollapsable);
  814. else
  815. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  816. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  817. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-large">' . bx_process_output($aInput['caption']) . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  818. else
  819. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-large"><a href="javascript:void(0);">' . bx_process_output($aInput['caption']) . '</a>' . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  820. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  821. return $sCode;
  822. }
  823. function genBlockEnd()
  824. {
  825. $aNextTbodyAdd = false; // need to have some default
  826. $sCode = '';
  827. $sCode .= $this->{$this->_sSectionClose}();
  828. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  829. return $sCode;
  830. }
  831. /**
  832. * Generate HTML Input Element
  833. *
  834. * @param array $aInput
  835. * @return string Output HTML Code
  836. */
  837. function genInput(&$aInput)
  838. {
  839. if (!empty($aInput['name'])) {
  840. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  841. if (method_exists($this, $sCustomMethod))
  842. return $this->$sCustomMethod($aInput);
  843. }
  844. switch ($aInput['type']) {
  845. case 'datepicker':
  846. if (!isset($aInput['attrs']))
  847. $aInput['attrs'] = array();
  848. $aInput['attrs']['autosuggestion'] = 'off';
  849. $sInput = $this->genInputStandard($aInput);
  850. break;
  851. // standard inputs (and non-standard, interpreted as standard)
  852. case 'text':
  853. case 'datepicker':
  854. case 'dateselect':
  855. case 'date_time':
  856. case 'datetime':
  857. case 'number':
  858. case 'time':
  859. case 'checkbox':
  860. case 'radio':
  861. case 'image':
  862. case 'slider':
  863. case 'doublerange':
  864. case 'hidden':
  865. $sInput = $this->genInputStandard($aInput);
  866. break;
  867. case 'password':
  868. $sInput = $this->genInputPassword($aInput);
  869. break;
  870. case 'price':
  871. $sInput = $this->genInputPrice($aInput);
  872. break;
  873. case 'file':
  874. $sInput = $this->genInputFile($aInput);
  875. break;
  876. case 'rgb':
  877. case 'rgba':
  878. $sClass = 'bx-form-input-' . $aInput['type'];
  879. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  880. $aInput['type'] = 'text';
  881. $sInput = $this->genInputStandard($aInput);
  882. break;
  883. case 'switcher':
  884. $sInput = $this->genInputSwitcher($aInput);
  885. break;
  886. case 'button':
  887. case 'reset':
  888. case 'submit':
  889. $sInput = $this->genInputButton($aInput);
  890. break;
  891. case 'textarea':
  892. $sInput = $this->genInputTextarea($aInput);
  893. break;
  894. case 'select':
  895. $sInput = $this->genInputSelect($aInput);
  896. break;
  897. case 'select_multiple':
  898. $sInput = $this->genInputSelectMultiple($aInput);
  899. break;
  900. case 'checkbox_set':
  901. $sInput = $this->genInputCheckboxSet($aInput);
  902. break;
  903. case 'radio_set':
  904. $sInput = $this->genInputRadioSet($aInput);
  905. break;
  906. case 'input_set': // numeric array of inputs
  907. $sInput = '';
  908. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  909. foreach ($aInput as $iKey => $aSubInput) {
  910. if (!is_int($iKey) or !$aSubInput)
  911. continue; // parse only integer keys and existing values
  912. $sInput .= $this->genInput($aSubInput); // recursive call
  913. $sInput .= $sDivider;
  914. }
  915. $sInput .= '<div class="bx-clear"></div>';
  916. break;
  917. case 'custom':
  918. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  919. break;
  920. case 'captcha':
  921. $sInput = $this->genInputCaptcha($aInput);
  922. break;
  923. case 'location':
  924. $sInput = $this->genInputLocation($aInput);
  925. break;
  926. case 'nested_form':
  927. $sInput = $this->genInputNestedForm($aInput);
  928. break;
  929. case 'value':
  930. $sInput = '';
  931. if(!isset($aInput['value']))
  932. break;
  933. $sInput = $aInput['value'];
  934. if(get_mb_substr($sInput, 0, 1) == '_')
  935. $sInput = _t($sInput);
  936. break;
  937. default:
  938. //unknown control type
  939. $sInput = 'Unknown control type';
  940. }
  941. // create input label
  942. $sInput .= $this->genLabel($aInput);
  943. // create input privacy group chooser
  944. $sInput .= $this->genPrivacyGroupChooser($aInput);
  945. return $sInput;
  946. }
  947. /**
  948. * Generate new Input Element id
  949. *
  950. * @param array $aInput
  951. * @return string
  952. */
  953. function getInputId(&$aInput)
  954. {
  955. if (isset($aInput['id']))
  956. return $aInput['id'];
  957. $sName = md5($aInput['name']);
  958. $sID = $this->id . '_input_' . $sName;
  959. if ( // multiple elements cause identical id's
  960. (
  961. (
  962. $aInput['type'] == 'checkbox' and
  963. substr($aInput['name'], -2) == '[]' // it is multiple element
  964. ) or
  965. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  966. ) and
  967. isset($aInput['value']) // if we can make difference
  968. ) {
  969. $sValue = md5($aInput['value']);
  970. // add value
  971. $sID .= '_' . $sValue;
  972. }
  973. $sID = trim($sID, '_');
  974. $aInput['id'] = $sID; // just for repeated calls
  975. return $sID;
  976. }
  977. /**
  978. * Generate standard Input Element
  979. *
  980. * @param array $aInput
  981. * @return string
  982. */
  983. function genInputStandard(&$aInput)
  984. {
  985. $aAttrs = $this->_genInputStandardAttrs($aInput);
  986. return $this->oTemplate->parseHtmlByName('form_field_standard.html', [
  987. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}")
  988. ]);
  989. }
  990. protected function _genInputStandardAttrs(&$aInput)
  991. {
  992. // clone attributes for system use ;)
  993. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  994. // add default className to attributes
  995. $aAttrs['type'] = $aInput['type'];
  996. if ('datetime' == $aAttrs['type'])
  997. $aAttrs['type'] = 'date_time';
  998. switch($aAttrs['type']) {
  999. case 'date_time':
  1000. $this->addCssJsUi ();
  1001. $this->addCssJsTimepicker ();
  1002. break;
  1003. case 'slider':
  1004. case 'doublerange':
  1005. case 'datepicker':
  1006. $this->addCssJsUi ();
  1007. break;
  1008. case 'dateselect':
  1009. $this->_addJs(array(
  1010. 'combodate/combodate.js',
  1011. ), "'undefined' === typeof($.combodate)");
  1012. break;
  1013. case 'text':
  1014. if (isset($aAttrs['class']) && false !== strpos($aAttrs['class'], 'bx-form-input-rgb'))
  1015. $this->addCssJsMinicolors ();
  1016. break;
  1017. }
  1018. if ('doublerange' == $aAttrs['type'] && (!isset($aInput['value']) || !$aInput['value']) && !empty($aAttrs['min']) && !empty($aAttrs['max'])) {
  1019. $aInput['value'] = $aAttrs['min'] . '-' . $aAttrs['max'];
  1020. }
  1021. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  1022. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  1023. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  1024. // for inputs with labels generate id
  1025. if (isset($aInput['label']))
  1026. $aAttrs['id'] = $this->getInputId($aInput);
  1027. // for checkboxes
  1028. if (isset($aInput['checked']) and $aInput['checked'])
  1029. $aAttrs['checked'] = 'checked';
  1030. return $aAttrs;
  1031. }
  1032. /**
  1033. * Generate Switcher Input Element (based on checkbox)
  1034. *
  1035. * @param array $aInput
  1036. * @return string
  1037. */
  1038. function genInputSwitcher(&$aInput)
  1039. {
  1040. $aInput['type'] = 'checkbox';
  1041. $sCheckbox = $this->genInputStandard($aInput);
  1042. $aInput['type'] = 'switcher';
  1043. $sClass = 'off';
  1044. if(isset($aInput['checked']) && $aInput['checked'])
  1045. $sClass = 'on';
  1046. return $this->oTemplate->parseHtmlByName('form_field_switcher.html', [
  1047. 'class' => $sClass,
  1048. 'checkbox' => $sCheckbox
  1049. ]);
  1050. }
  1051. /**
  1052. * Generate Button Input Element
  1053. *
  1054. * @param array $aInput
  1055. * @return string
  1056. */
  1057. function genInputButton(&$aInput)
  1058. {
  1059. $aAttrs = $this->_genInputButtonAttrs($aInput);
  1060. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn";
  1061. if($aInput['type'] == 'submit')
  1062. $sClassAdd .= ' bx-btn-primary';
  1063. return $this->oTemplate->parseHtmlByName('form_field_button.html', [
  1064. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1065. 'value' => $aInput['value']
  1066. ]);
  1067. }
  1068. protected function _genInputButtonAttrs(&$aInput)
  1069. {
  1070. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1071. // add default className to attributes
  1072. $aAttrs['type'] = $aInput['type'];
  1073. if(isset($aInput['value']))
  1074. $aAttrs['value'] = $aInput['value'];
  1075. if(isset($aInput['name']))
  1076. $aAttrs['name'] = $aInput['name'];
  1077. return $aAttrs;
  1078. }
  1079. /**
  1080. * Generate Textarea Element
  1081. *
  1082. * @param array $aInput
  1083. * @return string
  1084. */
  1085. function genInputTextarea(&$aInput)
  1086. {
  1087. $aAttrs = $this->_genInputTextareaAttrs($aInput);
  1088. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']}";
  1089. if(isset($aInput['html']) && $aInput['html'] && $this->addHtmlEditor($aInput['html'], $aInput))
  1090. $sClassAdd .= ' bx-form-input-html';
  1091. $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')) : '';
  1092. return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
  1093. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1094. 'value' => $sValue
  1095. ]);
  1096. }
  1097. protected function _genInputTextareaAttrs(&$aInput)
  1098. {
  1099. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1100. $aAttrs['name'] = $aInput['name'];
  1101. // for inputs with labels generate id
  1102. if (isset($aInput['label']))
  1103. $aAttrs['id'] = $this->getInputId($aInput);
  1104. return $aAttrs;
  1105. }
  1106. function isHtmlEditor($iViewMode, &$aInput)
  1107. {
  1108. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  1109. }
  1110. function addHtmlEditor($iViewMode, &$aInput)
  1111. {
  1112. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1113. if (!$oEditor)
  1114. return false;
  1115. $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)]);
  1116. return true;
  1117. }
  1118. function getHtmlEditorQueryParams($aInput)
  1119. {
  1120. return ['i' => $aInput['name'], 'f' => $this->aFormAttrs['id']];
  1121. }
  1122. /**
  1123. * Generate Select Box Element
  1124. *
  1125. * @param array $aInput
  1126. * @return string
  1127. */
  1128. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  1129. {
  1130. $aNewInput = $aInput;
  1131. $aNewInput['type'] = 'select';
  1132. $aNewInput['name'] .= '[]';
  1133. $sInput = $this->genInput($aNewInput);
  1134. return <<<BLAH
  1135. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  1136. $sInput
  1137. </div>
  1138. $sInfo
  1139. $sError
  1140. BLAH;
  1141. }
  1142. /**
  1143. * Generate Browse File Element
  1144. *
  1145. * @param array $aInput
  1146. * @return string
  1147. */
  1148. function genInputFile(&$aInput)
  1149. {
  1150. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  1151. if(!empty($aInput['attrs']['onchange']))
  1152. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  1153. else
  1154. $aInput['attrs']['onchange'] = $sOnChange;
  1155. $sInput = $this->genInputStandard($aInput);
  1156. 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>';
  1157. }
  1158. /**
  1159. * Generate Select Box Element
  1160. *
  1161. * @param array $aInput
  1162. * @return string
  1163. */
  1164. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  1165. {
  1166. if(empty($aInput['uploaders']) || !is_array($aInput['uploaders']))
  1167. return '';
  1168. $sUniqId = !empty($aInput['uploaders_id']) ? $aInput['uploaders_id'] : genRndPwd (8, false);
  1169. $bInitGhosts = isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1;
  1170. $bInitReordering = empty($aInput['init_reordering']) ? 0 : 1;
  1171. $oUploader = null;
  1172. $sUploadersButtons = $sUploadersJs = '';
  1173. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1174. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  1175. if(!$oUploader)
  1176. continue;
  1177. //--- Get Button code.
  1178. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1179. if(empty($aInput['attrs']['disabled']))
  1180. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  1181. $aParamsButtons = [
  1182. 'attrs' => bx_convert_array2attrs($aAttrs),
  1183. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  1184. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  1185. ];
  1186. $sUploadersButtons .= $oUploader->getUploaderButton($aParamsButtons);
  1187. //--- Get JS code.
  1188. $sGhostTemplate = $this->genGhostTemplate($aInput);
  1189. $aParamsJs = [
  1190. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1191. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1192. 'is_init_ghosts' => $bInitGhosts,
  1193. 'is_init_reordering' => $bInitReordering
  1194. ];
  1195. if(isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  1196. $aParamsJs['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  1197. $sUploadersJs .= $oUploader->getUploaderJs($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParamsJs, $this->_bDynamicMode);
  1198. }
  1199. if(!$oUploader)
  1200. return '';
  1201. if($bInitReordering) {
  1202. $this->addCssJsUi();
  1203. $this->addCssJsUiSortable();
  1204. }
  1205. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  1206. 'uploaders_buttons' => $sUploadersButtons,
  1207. 'info' => $sInfo,
  1208. 'error' => $sError,
  1209. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  1210. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  1211. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  1212. 'uploaders_js' => $sUploadersJs,
  1213. ));
  1214. }
  1215. protected function genGhostTemplate(&$aInput)
  1216. {
  1217. $sGhostTemplate = false;
  1218. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  1219. $oFormNested = $aInput['ghost_template'];
  1220. if ($oFormNested instanceof BxDolFormNestedGhost)
  1221. $sGhostTemplate = $oFormNested->genForm();
  1222. } 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
  1223. $oFormNested = new BxDolFormNestedGhost($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  1224. $sGhostTemplate = $oFormNested->getCode();
  1225. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  1226. $sGhostTemplate = array ();
  1227. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  1228. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNestedGhost)
  1229. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  1230. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  1231. $sGhostTemplate = $aInput['ghost_template'];
  1232. }
  1233. return $sGhostTemplate;
  1234. }
  1235. protected function genCustomInputUsernamesSuggestions ($aInput)
  1236. {
  1237. $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
  1238. $aAttrs = $this->_genCustomInputUsernamesSuggestionsAttrs($aInput, $bDisabled);
  1239. $aTmplVarsVals = [];
  1240. if(!empty($aInput['value'])) {
  1241. if(is_array($aInput['value']) || (is_numeric($aInput['value']))) {
  1242. if (!is_array($aInput['value']))
  1243. $aInput['value'] = [$aInput['value']];
  1244. foreach($aInput['value'] as $sVal) {
  1245. if(!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  1246. continue;
  1247. $aTmplVarsVals[] = [
  1248. 'item_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  1249. 'item_name' => $oProfile->getDisplayName(),
  1250. 'name' => $aInput['name'] . (isset($aInput['custom']['only_once']) && $aInput['custom']['only_once'] == 1 ? '' : '[]'),
  1251. 'value' => $sVal
  1252. ];
  1253. }
  1254. }
  1255. else if(is_string($aInput['value']))
  1256. $aTmplVarsVals = $aInput['value'];
  1257. }
  1258. $aTmplVarsInputText = [];
  1259. if(!$bDisabled) {
  1260. $aInputText = $aInput;
  1261. $aInputText['type'] = 'text';
  1262. $aInputText['value'] = '';
  1263. $aInputText['attrs'] = $this->_genCustomInputUsernamesSuggestionsTextAttrs($aInputText, $bDisabled);
  1264. unset($aInputText['name']);
  1265. $aTmplVarsInputText['input'] = $this->genInputStandard($aInputText);
  1266. }
  1267. $this->addCssJsUi();
  1268. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  1269. 'name' => $aInput['name'],
  1270. 'attrs' => bx_convert_array2attrs($aAttrs),
  1271. 'bx_repeat:vals' => $aTmplVarsVals,
  1272. 'bx_if:input' => array(
  1273. 'condition' => !$bDisabled,
  1274. 'content' => $aTmplVarsInputText
  1275. ),
  1276. 'bx_if:init' => array(
  1277. 'condition' => !$bDisabled,
  1278. 'content' => array(
  1279. 'id' => $aAttrs['id'],
  1280. 'name' => $aInput['name'],
  1281. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  1282. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  1283. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  1284. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  1285. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  1286. )
  1287. )
  1288. ));
  1289. }
  1290. protected function _genCustomInputUsernamesSuggestionsAttrs (&$aInput, $bDisabled = false)
  1291. {
  1292. $aAttrs = ['id' => $aInput['name'] . time() . mt_rand(0, 100)];
  1293. $aAttrs['class'] = 'bx-form-input-autotoken bx-form-input-text bx-def-font-inputs';
  1294. if($bDisabled)
  1295. $aAttrs['class'] .= ' bx-form-input-disabled';
  1296. if(!empty($aInput['attrs']['class']))
  1297. $aAttrs['class'] .= ' ' . $aInput['attrs']['class'];
  1298. return $aAttrs;
  1299. }
  1300. protected function _genCustomInputUsernamesSuggestionsTextAttrs (&$aInput, $bDisabled = false)
  1301. {
  1302. $aAttrs = ['value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off'];
  1303. if(!empty($aInput['attrs']) && is_array($aInput['attrs'])) {
  1304. if(isset($aInput['attrs']['class']))
  1305. unset($aInput['attrs']['class']);
  1306. $aAttrs = array_merge($aAttrs, $aInput['attrs']);
  1307. }
  1308. return $aAttrs;
  1309. }
  1310. protected function genCustomViewRowValueLabels ($aInput)
  1311. {
  1312. if (empty($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  1313. return null;
  1314. if (!empty($aInput['value']) && !is_array($aInput['value']))
  1315. $aInput['value'] = unserialize($aInput['value']);
  1316. $s = '';
  1317. foreach ($aInput['value'] as $sLabel)
  1318. $s .= '<a href="' . $oMetatags->keywordsGetHashTagUrl($sLabel, $aInput['content_id']) . '"><b class="bx-def-label val">' . trim($sLabel) . '</b></a>';
  1319. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  1320. 'values' => $s,
  1321. ));
  1322. }
  1323. protected function genCustomInputLabels ($aInput)
  1324. {
  1325. $oLabel = BxDolLabel::getInstance();
  1326. $sName = !empty($aInput['name']) ? $aInput['name'] : 'labels';
  1327. $sValue = '';
  1328. if(!empty($aInput['value'])) {
  1329. if(!is_array($aInput['value']))
  1330. $aInput['value'] = unserialize($aInput['value']);
  1331. if(is_array($aInput['value']))
  1332. foreach($aInput['value'] as $sLabel)
  1333. $sValue .= $oLabel->getLabel($sName, $sLabel);
  1334. }
  1335. $sKeyPlaceholder = $aInput['caption_src'] . '_placeholder';
  1336. if(strcmp($sKeyPlaceholder, _t($sKeyPlaceholder)) != 0)
  1337. $sValue .= $oLabel->getLabelPlaceholder($sKeyPlaceholder);
  1338. $aInputLabels = array(
  1339. 'type' => 'custom',
  1340. 'name' => $sName,
  1341. 'caption' => '',
  1342. 'value' => $sValue,
  1343. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . bx_append_url_params('label.php', array(
  1344. 'action' => 'labels_list',
  1345. )),
  1346. 'attrs' => array(
  1347. 'class' => 'bx-form-input-labels',
  1348. 'disabled' => 'disabled'
  1349. )
  1350. );
  1351. return $this->oTemplate->parseHtmlByName('label_select_field.html', array(
  1352. 'js_object' => $oLabel->getJsObjectName(),
  1353. 'js_code' => $oLabel->getJsCodeForm(),
  1354. 'html_id' => $oLabel->getFormFieldId($aInput),
  1355. 'name' => $sName,
  1356. 'input_labels' => $this->genCustomInputUsernamesSuggestions($aInputLabels)
  1357. ));
  1358. }
  1359. /**
  1360. * Generate Select Element
  1361. *
  1362. * @param array $aInput
  1363. * @return string
  1364. */
  1365. function genInputSelect(&$aInput)
  1366. {
  1367. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1368. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1369. }
  1370. /**
  1371. * Generate Multiple Select Element
  1372. *
  1373. * @param array $aInput
  1374. * @return string
  1375. */
  1376. function genInputSelectMultiple(&$aInput)
  1377. {
  1378. $aCurValues = array();
  1379. if (isset($aInput['value']) && $aInput['value'])
  1380. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1381. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  1382. }
  1383. /**
  1384. * Generate Checkbox Set Element
  1385. *
  1386. * @param array $aInput
  1387. * @return string
  1388. */
  1389. function genInputCheckboxSet(&$aInput)
  1390. {
  1391. $aCurValues = array();
  1392. if (isset($aInput['value']) && $aInput['value'])
  1393. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1394. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  1395. }
  1396. /**
  1397. * Generate Radiobuttons Set Element
  1398. *
  1399. * @param array $aInput
  1400. * @return string
  1401. */
  1402. function genInputRadioSet(&$aInput)
  1403. {
  1404. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1405. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  1406. }
  1407. function _isSelected ($sValue, $sCurValue)
  1408. {
  1409. return ((string)$sValue === (string)$sCurValue);
  1410. }
  1411. function _isSelectedMultiple ($sValue, $aCurValues)
  1412. {
  1413. return in_array($sValue, $aCurValues);
  1414. }
  1415. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  1416. {
  1417. $aOptions = [];
  1418. if(isset($aInput['values']) && is_array($aInput['values'])) {
  1419. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1420. $aOption = array(
  1421. 'bx_if:show_option_group_begin' => [
  1422. 'condition' => false,
  1423. 'content' => []
  1424. ],
  1425. 'bx_if:show_option_group_end' => [
  1426. 'condition' => false,
  1427. 'content' => []
  1428. ],
  1429. 'bx_if:show_option' => [
  1430. 'condition' => false,
  1431. 'content' => []
  1432. ]
  1433. );
  1434. $sOptAttrs = "";
  1435. if(is_array($sOptTitle)) {
  1436. if(isset($sOptTitle['type'])) {
  1437. switch($sOptTitle['type']) {
  1438. case 'group_header':
  1439. $aOption = array_merge($aOption, [
  1440. 'bx_if:show_option_group_begin' => [
  1441. 'condition' => true,
  1442. 'content' => [
  1443. 'opt_label' => bx_process_output($sOptTitle['value'])
  1444. ]
  1445. ]
  1446. ]);
  1447. $aOptions[] = $aOption;
  1448. break;
  1449. case 'group_end':
  1450. $aOption = array_merge($aOption, [
  1451. 'bx_if:show_option_group_end' => [
  1452. 'condition' => true,
  1453. 'content' => []
  1454. ]
  1455. ]);
  1456. $aOptions[] = $aOption;
  1457. break;
  1458. }
  1459. continue;
  1460. }
  1461. $aOptAttrs = $this->_genInputSelectOptionAttrs($sOptTitle);
  1462. if(!empty($aOptAttrs))
  1463. $sOptAttrs = bx_convert_array2attrs($aOptAttrs);
  1464. $sOptValue = $sOptTitle['key'];
  1465. $sOptTitle = $sOptTitle['value'];
  1466. }
  1467. $aOption = array_merge($aOption, [
  1468. 'bx_if:show_option' => [
  1469. 'condition' => true,
  1470. 'content' => [
  1471. 'opt_title' => bx_process_output($sOptTitle),
  1472. 'opt_value' => bx_html_attribute($sOptValue),
  1473. 'opt_attrs' => $sOptAttrs,
  1474. 'bx_if:show_selected' => [
  1475. 'condition' => $this->$sIsSelectedFunc($sOptValue, $mixedCurrentVal),
  1476. 'content' => []
  1477. ]
  1478. ]
  1479. ]
  1480. ]);
  1481. $aOptions[] = $aOption;
  1482. }
  1483. }
  1484. $aAttrs = $this->_genInputSelectAttrs($aInput, $isMultiple);
  1485. return $this->_parseInputSelect('form_field_select.html', [
  1486. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
  1487. 'bx_repeat:options' => $aOptions,
  1488. 'content' => !empty($aInput['content']) ? $aInput['content'] : ''
  1489. ]);
  1490. }
  1491. protected function _genInputSelectAttrs(&$aInput, $isMultiple)
  1492. {
  1493. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1494. $aAttrs['name'] = $aInput['name'];
  1495. if ($isMultiple) {
  1496. $aAttrs['name'] .= '[]';
  1497. $aAttrs['multiple'] = 'multiple';
  1498. }
  1499. // for inputs with labels generate id
  1500. if (isset($aInput['label']))
  1501. $aAttrs['id'] = $this->getInputId($aInput);
  1502. return $aAttrs;
  1503. }
  1504. protected function _genInputSelectOptionAttrs(&$aOption)
  1505. {
  1506. $aAttrs = [];
  1507. if(!empty($aOption['attrs']) && is_array($aOption['attrs']))
  1508. $aAttrs = $aOption['attrs'];
  1509. if(isset($aOption['class']))
  1510. $aAttrs['class'] = !empty($aAttrs['class']) ? $aAttrs['class'] . ' ' . $aOption['class'] : $aOption['class'];
  1511. if(isset($aOption['style']))
  1512. $aAttrs['style'] = !empty($aAttrs['style']) ? $aAttrs['style'] . ' ' . $aOption['style'] : $aOption['style'];
  1513. return $aAttrs;
  1514. }
  1515. protected function _parseInputSelect($sTmplName, $aTmplVars)
  1516. {
  1517. return $this->oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  1518. }
  1519. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  1520. {
  1521. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  1522. $aAttrs['name'] = $aInput['name'];
  1523. // for inputs with labels generate id
  1524. if (isset($aInput['label']))
  1525. $aAttrs['id'] = $this->getInputId($aInput);
  1526. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1527. // generate options
  1528. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1529. $iDividerThreshold = !empty($aInput['dv_thd']) ? (int)$aInput['dv_thd'] : 3;
  1530. $sOptions = '';
  1531. if (isset($aInput['values']) && is_array($aInput['values'])) {
  1532. if (count($aInput['values']) > $iDividerThreshold && $sDivider == $this->_sDivider)
  1533. $sDivider = $this->_sDividerAlt;
  1534. // generate complex input using simple standard inputs
  1535. foreach ($aInput['values'] as $sValue => $sLabel) {
  1536. if(is_array($sLabel)) {
  1537. $sValue = $sLabel['key'];
  1538. $sLabel = $sLabel['value'];
  1539. }
  1540. // create new simple input
  1541. $aNewInput = array(
  1542. 'type' => $sType,
  1543. 'name' => $aInput['name'] . $sNameAppend,
  1544. 'value' => $sValue,
  1545. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1546. 'label' => $sLabel,
  1547. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1548. );
  1549. if (isset($aInput['label_as_html']))
  1550. $aNewInput['label_as_html'] = $aInput['label_as_html'];
  1551. $sNewInput = $this->genInput($aNewInput);
  1552. // attach new input to complex
  1553. $sOptions .= ($sNewInput . $sDivider);
  1554. }
  1555. }
  1556. // generate element
  1557. $sCode = <<<BLAH
  1558. <div $sAttrs>
  1559. $sOptions
  1560. </div>
  1561. BLAH;
  1562. return $sCode;
  1563. }
  1564. function genInputLocation(&$aInput)
  1565. {
  1566. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1567. if (!$o)
  1568. return _t('_sys_txt_error_occured');
  1569. return $o->genInputLocation($aInput, $this);
  1570. }
  1571. public function setLocationVals ($aInput, $aVals)
  1572. {
  1573. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1574. if (!$o)
  1575. return false;
  1576. return $o->setLocationVals ($aInput, $aVals, $this);
  1577. }
  1578. public function setLocationVal ($aInput, $sIndex, $sVal)
  1579. {
  1580. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1581. if (!$o)
  1582. return false;
  1583. return $o->setLocationVal ($aInput, $sIndex, $sVal, $this);
  1584. }
  1585. protected function getLocationVal ($aInput, $sIndex)
  1586. {
  1587. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1588. if (!$o)
  1589. return false;
  1590. return $o->getLocationVal ($aInput, $sIndex, $this);
  1591. }
  1592. function genInputPassword(&$aInput)
  1593. {
  1594. return $this->oTemplate->parseHtmlByName('form_field_password.html', array('input' => $this->genInputStandard($aInput)));
  1595. }
  1596. function genInputPrice(&$aInput)
  1597. {
  1598. /*
  1599. if(!isset($aInput['attrs_wrapper']['class'])) {
  1600. if(!isset($aInput['attrs_wrapper']))
  1601. $aInput['attrs_wrapper'] = [];
  1602. $aInput['attrs_wrapper']['class'] = '';
  1603. }
  1604. $aInput['attrs_wrapper']['class'] .= 'bx-form-input-wrapper-text';
  1605. if(!isset($aInput['attrs']['class'])) {
  1606. if(!isset($aInput['attrs']))
  1607. $aInput['attrs'] = [];
  1608. $aInput['attrs']['class'] = '';
  1609. }
  1610. $aInput['attrs']['class'] .= 'bx-form-input-text';
  1611. */
  1612. $sCurrency = '';
  1613. if(!isset($aInput['value_currency'])) {
  1614. $sCurrency = BxDolPayments::getInstance()->getOption('default_currency_code');
  1615. if(empty($sCurrency))
  1616. $sCurrency = getParam('currency_code');
  1617. }
  1618. else
  1619. $sCurrency = $aInput['value_currency'];
  1620. return $this->oTemplate->parseHtmlByName('form_field_price.html', array(
  1621. 'bx_if:show_currency' => [
  1622. 'condition' => !empty($sCurrency),
  1623. 'content' => [
  1624. 'currency' => $sCurrency,
  1625. ]
  1626. ],
  1627. 'input' => $this->genInputStandard($aInput)
  1628. ));
  1629. }
  1630. function genInputNestedForm(&$aInput)
  1631. {
  1632. return '';
  1633. }
  1634. function genNestedForm(&$aInput)
  1635. {
  1636. return '';
  1637. }
  1638. function genInputCaptcha(&$aInput)
  1639. {
  1640. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1641. // for inputs with labels generate id
  1642. if (isset($aInput['label']))
  1643. $aAttrs['id'] = $this->getInputId($aInput);
  1644. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1645. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1646. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1647. }
  1648. /**
  1649. * Generate Label Element
  1650. *
  1651. * @param string $aInput input array
  1652. * @return string HTML code
  1653. */
  1654. function genLabel(&$aInput)
  1655. {
  1656. if (!isset($aInput['label']) || empty($aInput['label']))
  1657. return '';
  1658. $sInputID = $this->getInputId($aInput);
  1659. $sLabel = bx_process_output($aInput['label']);
  1660. if (isset($aInput['label_as_html']) && $aInput['label_as_html'] == true)
  1661. $sLabel = $aInput['label'];
  1662. return '<label for="' . $sInputID . '">' . $sLabel . '</label>';
  1663. }
  1664. function genPrivacyGroupChooser(&$aInput, $sPrivacyObject = '')
  1665. {
  1666. if(!isset($aInput['privacy']) || empty($aInput['privacy']))
  1667. return '';
  1668. $iAuthorId = bx_get_logged_profile_id();
  1669. $mixedResult = checkActionModule($iAuthorId, 'set form fields privacy', 'system');
  1670. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1671. return '';
  1672. if(empty($sPrivacyObject))
  1673. $sPrivacyObject = $this->_sPrivacyObjectView;
  1674. $iInputId = (int)$aInput['id'];
  1675. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  1676. if(!isset($aInput['attrs_wrapper']['class']))
  1677. $aInput['attrs_wrapper']['class'] = '';
  1678. $aInput['attrs_wrapper']['class'] .= ' bx-form-input-wrapper-pgc';
  1679. return $this->oTemplate->parseHtmlByName('form_field_privacy.html', array(
  1680. 'js_object' => $this->getJsObjectName(),
  1681. 'html_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  1682. 'id' => $iInputId,
  1683. 'privacy_object' => $sPrivacyObject,
  1684. 'icon' => $this->_getPrivacyIcon($mixedPrivacyGroup)
  1685. ));
  1686. }
  1687. function genInfoIcon($sInfo)
  1688. {
  1689. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1690. }
  1691. function genErrorIcon( $sError = '' )
  1692. {
  1693. if ($this->bEnableErrorIcon) {
  1694. $sStyle = '';
  1695. if (!$sError)
  1696. $sStyle = 'style="display:none;"';
  1697. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1698. }
  1699. }
  1700. function getOpenSection($aAttrs = array(), $sLegend = '')
  1701. {
  1702. if (!$this->_isSectionOpened) {
  1703. if (!$aAttrs || !is_array($aAttrs))
  1704. $aAttrs = array();
  1705. if ($sLegend)
  1706. $sClassesAdd = "bx-form-section-header";
  1707. else
  1708. $sClassesAdd = "bx-form-section-divider";
  1709. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1710. $this->_isSectionOpened = true;
  1711. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <div $sAttrs> $sLegend <div class=\"bx-form-section-content bx-def-padding-top bx-def-padding-bottom" . ($sLegend ? ' bx-def-padding-left bx-def-padding-right' : '') . "\">\n";
  1712. } else {
  1713. return '';
  1714. }
  1715. }
  1716. function getCloseSection()
  1717. {
  1718. if ($this->_isSectionOpened) {
  1719. $this->_isSectionOpened = false;
  1720. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1721. } else {
  1722. return '';
  1723. }
  1724. }
  1725. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1726. {
  1727. if (!$this->_isSectionOpened) {
  1728. if (!$aAttrs || !is_array($aAttrs))
  1729. $aAttrs = array();
  1730. if ($sLegend)
  1731. $sClassesAdd = "bx-form-section-header";
  1732. else
  1733. $sClassesAdd = "bx-form-section-divider";
  1734. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1735. $this->_isSectionOpened = true;
  1736. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <div $sAttrs> $sLegend <div class=\"bx-form-section-content bx-def-padding-top\">\n";
  1737. } else {
  1738. return '';
  1739. }
  1740. }
  1741. function getCloseSectionViewMode()
  1742. {
  1743. if ($this->_isSectionOpened) {
  1744. $this->_isSectionOpened = false;
  1745. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1746. } else {
  1747. return '';
  1748. }
  1749. }
  1750. static function getJsUiLangs ()
  1751. {
  1752. return array ('af' => 1, 'ar-DZ' => 1, 'ar' => 1, 'az' => 1, 'be' => 1, 'bg' => 1, 'bs' => 1, 'ca' => 1, 'cs' => 1, 'cy-GB' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'en-AU' => 1, 'en-GB' => 1, 'en-NZ' => 1, 'en' => 1, 'eo' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fo' => 1, 'fr-CA' => 1, 'fr-CH' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'ka' => 1, 'kk' => 1, 'km' => 1, 'ko' => 1, 'ky' => 1, 'lb' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'ml' => 1, 'ms' => 1, 'nb' => 1, 'nl-BE' => 1, 'nl' => 1, 'nn' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'rm' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sq' => 1, 'sr-SR' => 1, 'sr' => 1, 'sv' => 1, 'ta' => 1, 'th' => 1, 'tj' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-HK' => 1, 'zh-TW' => 1);
  1753. }
  1754. function addCssJsUi ()
  1755. {
  1756. if (self::$_isCssJsUiAdded)
  1757. return;
  1758. $aUiLangs = $this->getJsUiLangs ();
  1759. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1760. $this->_addJs(array(
  1761. 'jquery-ui/jquery-ui.custom.min.js',
  1762. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1763. ), "'undefined' === typeof($.datepicker)");
  1764. $this->_addCss('jquery-ui/jquery-ui.css');
  1765. self::$_isCssJsUiAdded = true;
  1766. }
  1767. function addCssJsUiSortable ()
  1768. {
  1769. if (self::$_isCssJsUiSortableAdded)
  1770. return;
  1771. $this->_addJs(array(
  1772. 'jquery-ui/jquery.ui.sortable.min.js'
  1773. ), "'undefined' === typeof($.sortable)");
  1774. self::$_isCssJsUiSortableAdded = true;
  1775. }
  1776. function addCssJsTimepicker ()
  1777. {
  1778. if (self::$_isCssJsTimepickerAdded)
  1779. return;
  1780. $aCalendarLangs = array ('af' => 1, 'am' => 1, 'bg' => 1, 'ca' => 1, 'cs' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1, 'it' => 1, 'ja' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sr-RS' => 1, 'sr-YU' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-TW' => 1);
  1781. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1782. $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
  1783. $this->_addJs(array(
  1784. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1785. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1786. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1787. 'jquery.ui.touch-punch.min.js',
  1788. ), "'undefined' === typeof($.fn.datetimepicker)");
  1789. self::$_isCssJsTimepickerAdded = true;
  1790. }
  1791. function addCssJsMinicolors ()
  1792. {
  1793. if (self::$_isCssJsMinicolorsAdded)
  1794. return;
  1795. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  1796. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  1797. self::$_isCssJsMinicolorsAdded = true;
  1798. }
  1799. function addCssJsViewMode ()
  1800. {
  1801. if (self::$_isCssJsAddedViewMode)
  1802. return;
  1803. $this->_addCss('forms.css');
  1804. self::$_isCssJsAddedViewMode = true;
  1805. }
  1806. function addCssJsCore ()
  1807. {
  1808. if (self::$_isCssJsAdded)
  1809. return;
  1810. $this->_addCss('forms.css');
  1811. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  1812. $this->_addJs(array('BxDolForm.js', 'BxDolNestedForm.js'), "true");
  1813. self::$_isCssJsAdded = true;
  1814. }
  1815. function addCssJs ()
  1816. {
  1817. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1818. $this->addCssJsViewMode ();
  1819. } else {
  1820. $this->addCssJsCore();
  1821. }
  1822. }
  1823. protected function _processCssJs()
  1824. {
  1825. $sRet = '';
  1826. if ($this->_bDynamicMode) {
  1827. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1828. $sRet .= "\n<script>\n";
  1829. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  1830. foreach ($this->_aJs as $sCondition => $aJs) {
  1831. $sJs = $this->oTemplate->addJs($aJs, true);
  1832. if (!$sJs)
  1833. continue;
  1834. $sRet .= "if ($sCondition)\n";
  1835. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1836. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  1837. }
  1838. $sRet .= "\n</script>\n";
  1839. }
  1840. else {
  1841. $this->oTemplate->addCss($this->_aCss);
  1842. $aJs = array();
  1843. foreach ($this->_aJs as $sCondition => $a)
  1844. $aJs = array_merge($aJs, $a);
  1845. $this->oTemplate->addJs($aJs);
  1846. }
  1847. return $sRet;
  1848. }
  1849. protected function _addJs($mixed, $sJsCondition)
  1850. {
  1851. if (!is_array($mixed))
  1852. $mixed = array($mixed);
  1853. $this->_aJs[$sJsCondition] = $mixed;
  1854. }
  1855. protected function _addCss($mixed)
  1856. {
  1857. if (!is_array($mixed))
  1858. $mixed = array($mixed);
  1859. foreach ($mixed as $s)
  1860. $this->_aCss[$s] = $s;
  1861. }
  1862. }
  1863. /** @} */