BxBaseFormView.php 77 KB

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