BxBaseFormView.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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 $_isCssJsMinicolorsAdded = false;
  15. protected static $_isCssJsDatepickerAdded = false;
  16. protected static $_isCssJsTimepickerAdded = false;
  17. protected static $_isCssJsAddedViewMode = false;
  18. /**
  19. * Enable or disable error message displaying
  20. */
  21. protected $bEnableErrorIcon = true;
  22. /**
  23. * HTML Code of this form
  24. */
  25. protected $sCode;
  26. /**
  27. * Code which will be added to the beginning of the form.
  28. * For example, hidden inputs.
  29. * For internal use only
  30. */
  31. protected $_sCodeAdd = '';
  32. /**
  33. * for internal use only
  34. */
  35. protected $_isSectionOpened = false;
  36. /**
  37. * Default divider for several inputs
  38. */
  39. protected $_sDivider = '<span class="bx-def-margin-left"></span>';
  40. /**
  41. * Alternative divider for several inputs
  42. */
  43. protected $_sDividerAlt = '<br />';
  44. /**
  45. * Form is added dynamically.
  46. */
  47. protected $_bDynamicMode = false;
  48. /**
  49. * Form is submitted dynamically (using Ajax Submit).
  50. */
  51. protected $_bAjaxMode = false;
  52. /**
  53. * Form is displayed in view mode.
  54. */
  55. protected $_bViewMode = false;
  56. /**
  57. * Function name for generation close form section HTML.
  58. */
  59. protected $_sSectionClose = 'getCloseSection';
  60. /**
  61. * Function name for generation open form section HTML.
  62. */
  63. protected $_sSectionOpen = 'getOpenSection';
  64. /**
  65. * JS files list for form
  66. */
  67. protected $_aJs = array();
  68. /**
  69. * CSS files list for form
  70. */
  71. protected $_aCss = array();
  72. /**
  73. * Constructor
  74. *
  75. * @param array $aInfo Form contents
  76. *
  77. * $aInfo['params'] = array(
  78. * 'remove_form' => true|false,
  79. * );
  80. *
  81. * @return BxBaseFormView
  82. */
  83. function __construct($aInfo, $oTemplate)
  84. {
  85. parent::__construct($aInfo, $oTemplate);
  86. if(isset($this->aParams['ajax_mode']))
  87. $this->setAjaxMode($this->aParams['ajax_mode']);
  88. $mixedAjaxMode = bx_get('ajax_mode');
  89. if($mixedAjaxMode !== false)
  90. $this->setAjaxMode($mixedAjaxMode);
  91. $this->_bViewMode = isset($this->aParams['view_mode']) && $this->aParams['view_mode'];
  92. if ($this->_bViewMode) {
  93. $this->_sSectionClose = 'getCloseSectionViewMode';
  94. $this->_sSectionOpen = 'getOpenSectionViewMode';
  95. }
  96. }
  97. function setAjaxMode($bAjaxMode)
  98. {
  99. $this->_bAjaxMode = (bool)$bAjaxMode;
  100. }
  101. function isAjaxMode()
  102. {
  103. return $this->_bAjaxMode;
  104. }
  105. /**
  106. * Return Form code
  107. * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup.
  108. * @return string
  109. */
  110. function getCode($bDynamicMode = false)
  111. {
  112. if (!$bDynamicMode && bx_is_dynamic_request())
  113. $bDynamicMode = true;
  114. $this->_bDynamicMode = $bDynamicMode;
  115. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  116. $this->sCode = $this->genForm();
  117. $this->addCssJs ();
  118. $sDynamicCssJs = $this->_processCssJs();
  119. return $sDynamicCssJs . $this->sCode;
  120. }
  121. /**
  122. * Generate the whole form
  123. *
  124. * @return string
  125. */
  126. function genForm()
  127. {
  128. $this->_sCodeAdd = '';
  129. $sTable = $this->genRows();
  130. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  131. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  132. if (!empty($this->aParams['remove_form']) || (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])) {
  133. $sForm = <<<BLAH
  134. $sHtmlBefore
  135. {$this->_sCodeAdd}
  136. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  137. $sTable
  138. </div>
  139. $sHtmlAfter
  140. BLAH;
  141. } else {
  142. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  143. $sAjaxFormJs = '';
  144. if($this->_bAjaxMode)
  145. $sAjaxFormJs = <<<BLAH
  146. $("form#{$this->id}").ajaxForm({
  147. dataType: "json",
  148. beforeSubmit: function (formData, jqForm, options) {
  149. bx_loading($("form#{$this->id}"), true);
  150. },
  151. success: function (oData) {
  152. bx_loading($("form#{$this->id}"), false);
  153. processJsonData(oData);
  154. }
  155. });
  156. BLAH;
  157. $sForm = <<<BLAH
  158. $sHtmlBefore
  159. <form $sFormAttrs>
  160. {$this->_sCodeAdd}
  161. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  162. $sTable
  163. </div>
  164. </form>
  165. <script>
  166. $(document).ready(function() {
  167. $(this).addWebForms();
  168. });
  169. $sAjaxFormJs
  170. </script>
  171. $sHtmlAfter
  172. BLAH;
  173. }
  174. return $sForm;
  175. }
  176. /**
  177. * Generate Table HTML code
  178. *
  179. * @return string
  180. */
  181. function genRows()
  182. {
  183. // add CSRF token if it's needed.
  184. 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) {
  185. $this->aInputs['csrf_token'] = array(
  186. 'type' => 'hidden',
  187. 'name' => 'csrf_token',
  188. 'value' => $mixedCsrfToken,
  189. 'db' => array ('pass' => 'Xss'),
  190. 'visible_for_levels' => PHP_INT_MAX,
  191. );
  192. }
  193. // add 'Ajax Mode' flag
  194. if($this->_bAjaxMode)
  195. $this->aInputs['ajax_mode'] = array(
  196. 'type' => 'hidden',
  197. 'name' => 'ajax_mode',
  198. 'value' => 1,
  199. 'db' => array ('pass' => 'Int'),
  200. 'visible_for_levels' => PHP_INT_MAX,
  201. );
  202. // check if we need to generate open section clause
  203. $sOpenSection = '';
  204. foreach ($this->aInputs as $aInput) {
  205. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  206. continue;
  207. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  208. $sOpenSection = $this->{$this->_sSectionOpen}();
  209. break;
  210. }
  211. // generate rows contents
  212. $sCont = '';
  213. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  214. foreach ($this->aInputs as $aInput)
  215. if (!isset($aInput['visible_for_levels']) || self::isVisible($aInput))
  216. $sCont .= $this->$sFuncGenRow($aInput);
  217. $sCloseSection = $this->{$this->_sSectionClose}();
  218. return $sOpenSection . $sCont . $sCloseSection;
  219. }
  220. /**
  221. * Generate single Table Row
  222. *
  223. * @param array $aInput
  224. * @return string
  225. */
  226. function genRow(&$aInput)
  227. {
  228. if (!isset($aInput['type']))
  229. $aInput['type'] = false;
  230. if (!empty($aInput['name'])) {
  231. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  232. if (method_exists($this, $sCustomMethod))
  233. return $this->$sCustomMethod($aInput);
  234. }
  235. switch ($aInput['type']) {
  236. case 'block_header':
  237. $sRow = $this->genRowBlockHeader($aInput);
  238. break;
  239. case 'block_end':
  240. $sRow = $this->genBlockEnd($aInput);
  241. break;
  242. case 'hidden':
  243. // do not generate row for hidden inputs
  244. $sRow = '';
  245. $this->_sCodeAdd .= $this->genInput($aInput);
  246. break;
  247. case 'select_box':
  248. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  249. break;
  250. case 'files':
  251. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  252. break;
  253. case 'switcher':
  254. case 'checkbox':
  255. $sRow = $this->genRowStandard($aInput, true);
  256. break;
  257. default:
  258. $sRow = $this->genRowStandard($aInput);
  259. }
  260. return $sRow;
  261. }
  262. /**
  263. * Generate single Table Row for view mode
  264. *
  265. * @param array $aInput
  266. * @return string
  267. */
  268. function genViewRow(&$aInput)
  269. {
  270. if (!isset($aInput['type']))
  271. $aInput['type'] = false;
  272. if (!empty($aInput['name'])) {
  273. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  274. if (method_exists($this, $sCustomMethod))
  275. return $this->$sCustomMethod($aInput);
  276. }
  277. switch ($aInput['type']) {
  278. case 'block_header':
  279. $sRow = $this->genRowBlockHeader($aInput);
  280. break;
  281. case 'block_end':
  282. $sRow = $this->genBlockEnd($aInput);
  283. break;
  284. default:
  285. $sRow = $this->genViewRowWrapped($aInput);
  286. }
  287. return $sRow;
  288. }
  289. /**
  290. * Generate complete wrapped row for view mode form
  291. *
  292. * @param array $aInput
  293. * @return string
  294. */
  295. function genViewRowWrapped(&$aInput)
  296. {
  297. $sValue = $this->genViewRowValue($aInput);
  298. if (null === $sValue)
  299. return '';
  300. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  301. return <<<EOS
  302. <div class="bx-form-row-view-wrapper bx-form-row-view-wrapper-{$aInput['type']} bx-def-padding-sec-top">
  303. <div class="bx-form-row-view-caption">{$sCaption}:</div>
  304. <div class="bx-form-row-view-value">{$sValue}</div>
  305. </div>
  306. EOS;
  307. }
  308. /**
  309. * Generate value for view mode row
  310. *
  311. * @param array $aInput
  312. * @return string
  313. */
  314. function genViewRowValue(&$aInput)
  315. {
  316. if (!empty($aInput['name'])) {
  317. $sCustomMethod = 'genCustomViewRowValue' . $this->_genMethodName($aInput['name']);
  318. if (method_exists($this, $sCustomMethod))
  319. return $this->$sCustomMethod($aInput);
  320. }
  321. switch ($aInput['type']) {
  322. case 'hidden':
  323. $sValue = null;
  324. break;
  325. case 'select':
  326. case 'radio_set':
  327. $sValue = $this->genViewRowValueForSelect($aInput);
  328. break;
  329. case 'datepicker':
  330. $sValue = null;
  331. if (empty($aInput['value']) || !$aInput['value'] || '0000-00-00' == $aInput['value'])
  332. break;
  333. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  334. break;
  335. case 'date_time':
  336. case 'datetime':
  337. $sValue = null;
  338. if(empty($aInput['value']) || !$aInput['value'] || '0000-00-00 00:00:00' == $aInput['value'] || '0000-00-00 00:00' == $aInput['value'])
  339. break;
  340. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  341. break;
  342. case 'checkbox_set':
  343. case 'select_multiple':
  344. $sValue = null;
  345. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  346. $sValue = '';
  347. foreach ($aInput['value'] as $sVal)
  348. $sValue .= $aInput['values'][$sVal] . ', ';
  349. $sValue = trim ($sValue, ', ');
  350. }
  351. break;
  352. case 'checkbox':
  353. case 'switcher':
  354. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  355. break;
  356. case 'textarea':
  357. if (isset($aInput['value']) && '' !== $aInput['value'])
  358. $sValue = (isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : bx_process_output($aInput['value'], BX_DATA_TEXT_MULTILINE);
  359. else
  360. $sValue = null;
  361. break;
  362. case 'text':
  363. if(isset($aInput['value']) && '' !== $aInput['value'])
  364. $sValue = bx_linkify(bx_process_output($aInput['value']));
  365. else
  366. $sValue = null;
  367. break;
  368. default:
  369. $sValue = isset($aInput['value']) && '' !== $aInput['value'] ? bx_process_output($aInput['value']) : null;
  370. }
  371. return $sValue;
  372. }
  373. function genViewRowValueForSelect(&$aInput)
  374. {
  375. if (!isset($aInput['value']) || !$aInput['value'])
  376. return null;
  377. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  378. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])))
  379. return $oCategory->getCategoryLink($s, $aInput['value']);
  380. return $s;
  381. }
  382. protected function genCustomRowBirthday(&$aInput)
  383. {
  384. if(!$this->_bViewMode)
  385. return $this->genRowStandard($aInput);
  386. $aInput['caption_src'] = '_sys_form_input_age';
  387. $aInput['caption'] = _t($aInput['caption_src']);
  388. return $this->genViewRowWrapped($aInput);
  389. }
  390. protected function genCustomViewRowValueBirthday(&$aInput)
  391. {
  392. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00')))
  393. return null;
  394. $sValue = $aInput['value'];
  395. $iPosSpace = strpos($sValue, ' ');
  396. if($iPosSpace !== false)
  397. $sValue = trim(substr($sValue, 0, $iPosSpace));
  398. $aDate = explode('-', $sValue);
  399. $iCdYear = (int)date('Y');
  400. $iCdMonth = (int)date('n');
  401. $iCdDay = (int)date('j');
  402. $iResult = $iCdYear - (int)$aDate[0];
  403. if($iCdMonth < (int)$aDate[1] || ($iCdMonth == (int)$aDate[1] && $iCdDay < (int)$aDate[2]))
  404. $iResult -= 1;
  405. return $iResult;
  406. }
  407. /**
  408. * Generate standard row
  409. *
  410. * @param array $aInput
  411. * @return string
  412. */
  413. function genRowStandard(&$aInput, $isOneLine = false)
  414. {
  415. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  416. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  417. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  418. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  419. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  420. $sClassWrapper = 'bx-form-element-wrapper';
  421. if($isOneLine)
  422. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  423. $sClassWrapper .= ' bx-def-margin-top';
  424. if (isset($aInput['name']))
  425. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  426. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  427. $sClassOneLineCaption = '';
  428. $sClassOneLineValue = '';
  429. if ($isOneLine) {
  430. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  431. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  432. $aInput['attrs']['id'] = $this->getInputId($aInput);
  433. if ($sCaption)
  434. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  435. }
  436. $sInput = $this->genInput($aInput);
  437. $sCaptionCode = '';
  438. if ($sCaption)
  439. $sCaptionCode = '<div class="bx-form-caption' . $sClassOneLineCaption . '">' . $sCaption . $sRequired . '</div>';
  440. else
  441. $sInput .= $sRequired;
  442. $sInputCode = $this->genWrapperInput($aInput, $sInput);
  443. if (empty($sInfoIcon)) $sInfoIcon = '';
  444. if (empty($sInputCode)) $sInputCode = '';
  445. if (empty($sErrorIcon)) $sErrorIcon = '';
  446. $sValueCode = '<div class="bx-form-value bx-clearfix' . $sClassAdd . $sClassOneLineValue . '">' . $sInputCode . '</div>';
  447. if ($isOneLine)
  448. $sCode = $sValueCode . $sCaptionCode;
  449. else
  450. $sCode = $sCaptionCode . $sValueCode;
  451. return '<div ' . $sTrAttrs . '><div class="bx-form-element">' . $sCode . '</div>' . $sInfoIcon . $sErrorIcon . '</div>';
  452. }
  453. function genWrapperInput($aInput, $sContent)
  454. {
  455. $sAttr = isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? bx_convert_array2attrs($aInput['attrs_wrapper']) : '';
  456. return "<div class=\"bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}" . ((isset($aInput['html']) && $aInput['html'] && $this->isHtmlEditor($aInput['html'], $aInput)) ? ' bx-form-input-wrapper-html' : '') . "\" $sAttr>$sContent</div>";
  457. }
  458. /**
  459. * Generate custom row
  460. *
  461. * @param array $aInput
  462. * @param string $sCustomMethod custom method to generate code for input
  463. * @return string
  464. */
  465. function genRowCustom(&$aInput, $sCustomMethod)
  466. {
  467. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  468. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  469. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  470. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  471. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  472. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  473. if (isset($aInput['name']))
  474. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  475. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  476. $sCaptionCode = '';
  477. if ($sCaption)
  478. $sCaptionCode = '<div class="bx-form-caption">' . $sCaption . $sRequired . '</div>';
  479. $sCode = <<<BLAH
  480. <div $sTrAttrs>
  481. $sCaptionCode
  482. <div class="bx-form-value$sClassAdd">
  483. <div class="bx-clear"></div>
  484. $sInput
  485. <div class="bx-clear"></div>
  486. </div>
  487. </div>
  488. BLAH;
  489. return $sCode;
  490. }
  491. /**
  492. * Generate Block Headers row
  493. *
  494. * @param array $aInput
  495. * @return string
  496. */
  497. function genRowBlockHeader(&$aInput)
  498. {
  499. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  500. // if there is no caption - show divider only
  501. if (empty($aInput['caption'])) {
  502. $sCode = $this->{$this->_sSectionClose}();
  503. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  504. return $sCode;
  505. }
  506. // if section is collapsed by default, add necessary code
  507. $sClassAddCollapsable = 'bx-form-collapsable';
  508. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  509. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  510. // display section with caption
  511. $sCode = $this->{$this->_sSectionClose}();
  512. if (empty($aAttrs))
  513. $aAttrs = array('class' => 'bx-form-collapsable ' . $sClassAddCollapsable);
  514. else
  515. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  516. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  517. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-h3">' . bx_process_output($aInput['caption']) . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  518. else
  519. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-h3"><a href="javascript:void(0);">' . bx_process_output($aInput['caption']) . '</a>' . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  520. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  521. return $sCode;
  522. }
  523. function genBlockEnd()
  524. {
  525. $aNextTbodyAdd = false; // need to have some default
  526. $sCode = '';
  527. $sCode .= $this->{$this->_sSectionClose}();
  528. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  529. return $sCode;
  530. }
  531. /**
  532. * Generate HTML Input Element
  533. *
  534. * @param array $aInput
  535. * @return string Output HTML Code
  536. */
  537. function genInput(&$aInput)
  538. {
  539. if (!empty($aInput['name'])) {
  540. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  541. if (method_exists($this, $sCustomMethod))
  542. return $this->$sCustomMethod($aInput);
  543. }
  544. switch ($aInput['type']) {
  545. // standard inputs (and non-standard, interpreted as standard)
  546. case 'text':
  547. case 'datepicker':
  548. case 'date_time':
  549. case 'datetime':
  550. case 'number':
  551. case 'checkbox':
  552. case 'radio':
  553. case 'image':
  554. case 'password':
  555. case 'slider':
  556. case 'doublerange':
  557. case 'hidden':
  558. $sInput = $this->genInputStandard($aInput);
  559. break;
  560. case 'file':
  561. $sInput = $this->genInputFile($aInput);
  562. break;
  563. case 'rgb':
  564. case 'rgba':
  565. $sClass = 'bx-form-input-' . $aInput['type'];
  566. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  567. $aInput['type'] = 'text';
  568. $sInput = $this->genInputStandard($aInput);
  569. break;
  570. case 'switcher':
  571. $sInput = $this->genInputSwitcher($aInput);
  572. break;
  573. case 'button':
  574. case 'reset':
  575. case 'submit':
  576. $sInput = $this->genInputButton($aInput);
  577. break;
  578. case 'textarea':
  579. $sInput = $this->genInputTextarea($aInput);
  580. break;
  581. case 'select':
  582. $sInput = $this->genInputSelect($aInput);
  583. break;
  584. case 'select_multiple':
  585. $sInput = $this->genInputSelectMultiple($aInput);
  586. break;
  587. case 'checkbox_set':
  588. $sInput = $this->genInputCheckboxSet($aInput);
  589. break;
  590. case 'radio_set':
  591. $sInput = $this->genInputRadioSet($aInput);
  592. break;
  593. case 'input_set': // numeric array of inputs
  594. $sInput = '';
  595. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  596. foreach ($aInput as $iKey => $aSubInput) {
  597. if (!is_int($iKey) or !$aSubInput)
  598. continue; // parse only integer keys and existing values
  599. $sInput .= $this->genInput($aSubInput); // recursive call
  600. $sInput .= $sDivider;
  601. }
  602. $sInput .= '<div class="bx-clear"></div>';
  603. break;
  604. case 'custom':
  605. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  606. break;
  607. case 'captcha':
  608. $sInput = $this->genInputCaptcha($aInput);
  609. break;
  610. case 'location':
  611. $sInput = $this->genInputLocation($aInput);
  612. break;
  613. case 'value':
  614. $sInput = isset($aInput['value']) ? $aInput['value'] : '';
  615. break;
  616. default:
  617. //unknown control type
  618. $sInput = 'Unknown control type';
  619. }
  620. // create input label
  621. $sInput .= $this->genLabel($aInput);
  622. return $sInput;
  623. }
  624. /**
  625. * Generate new Input Element id
  626. *
  627. * @param array $aInput
  628. * @return string
  629. */
  630. function getInputId(&$aInput)
  631. {
  632. if (isset($aInput['id']))
  633. return $aInput['id'];
  634. $sName = md5($aInput['name']);
  635. $sID = $this->id . '_input_' . $sName;
  636. if ( // multiple elements cause identical id's
  637. (
  638. (
  639. $aInput['type'] == 'checkbox' and
  640. substr($aInput['name'], -2) == '[]' // it is multiple element
  641. ) or
  642. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  643. ) and
  644. isset($aInput['value']) // if we can make difference
  645. ) {
  646. $sValue = md5($aInput['value']);
  647. // add value
  648. $sID .= '_' . $sValue;
  649. }
  650. $sID = trim($sID, '_');
  651. $aInput['id'] = $sID; // just for repeated calls
  652. return $sID;
  653. }
  654. /**
  655. * Generate standard Input Element
  656. *
  657. * @param array $aInput
  658. * @return string
  659. */
  660. function genInputStandard(&$aInput)
  661. {
  662. // clone attributes for system use ;)
  663. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  664. // add default className to attributes
  665. $aAttrs['type'] = $aInput['type'];
  666. if ('datetime' == $aAttrs['type'])
  667. $aAttrs['type'] = 'date_time';
  668. switch($aAttrs['type']) {
  669. case 'date_time':
  670. $this->addCssJsUi ();
  671. $this->addCssJsDatepicker ();
  672. $this->addCssJsTimepicker ();
  673. break;
  674. case 'slider':
  675. case 'doublerange':
  676. case 'datepicker':
  677. $this->addCssJsUi ();
  678. $this->addCssJsDatepicker ();
  679. break;
  680. case 'text':
  681. if (isset($aAttrs['class']) && false !== strpos($aAttrs['class'], 'bx-form-input-rgb'))
  682. $this->addCssJsMinicolors ();
  683. break;
  684. }
  685. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  686. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  687. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  688. // for inputs with labels generate id
  689. if (isset($aInput['label']))
  690. $aAttrs['id'] = $this->getInputId($aInput);
  691. // for checkboxes
  692. if (isset($aInput['checked']) and $aInput['checked'])
  693. $aAttrs['checked'] = 'checked';
  694. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  695. return "<input $sAttrs />\n";
  696. }
  697. /**
  698. * Generate Switcher Input Element (based on checkbox)
  699. *
  700. * @param array $aInput
  701. * @return string
  702. */
  703. function genInputSwitcher(&$aInput)
  704. {
  705. $aInput['type'] = 'checkbox';
  706. $sCheckbox = $this->genInputStandard($aInput);
  707. $aInput['type'] = 'switcher';
  708. $sClass = 'off';
  709. if (isset($aInput['checked']) and $aInput['checked'])
  710. $sClass = 'on';
  711. return '
  712. <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
  713. <div class="bx-switcher-canvas">
  714. <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
  715. <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
  716. <div class="bx-switcher-handler">&nbsp;</div>
  717. </div>
  718. </div>';
  719. }
  720. /**
  721. * Generate Button Input Element
  722. *
  723. * @param array $aInput
  724. * @return string
  725. */
  726. function genInputButton(&$aInput)
  727. {
  728. // clone attributes for system use ;)
  729. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  730. // add default className to attributes
  731. $aAttrs['type'] = $aInput['type'];
  732. if (isset($aInput['value']))
  733. $aAttrs['value'] = $aInput['value'];
  734. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  735. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
  736. return "<button $sAttrs>" . $aInput['value'] . "</button>\n";
  737. }
  738. /**
  739. * Generate Textarea Element
  740. *
  741. * @param array $aInput
  742. * @return string
  743. */
  744. function genInputTextarea(&$aInput)
  745. {
  746. // clone attributes for system use ;)
  747. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  748. $aAttrs['name'] = $aInput['name'];
  749. // for inputs with labels generate id
  750. if (isset($aInput['label']))
  751. $aAttrs['id'] = $this->getInputId($aInput);
  752. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}" . ((isset($aInput['html']) and $aInput['html'] and $this->addHtmlEditor($aInput['html'], $aInput)) ? ' bx-form-input-html' : ''));
  753. $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value'])) : '';
  754. return "<textarea $sAttrs>$sValue</textarea>";
  755. }
  756. function isHtmlEditor($iViewMode, &$aInput)
  757. {
  758. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  759. }
  760. function addHtmlEditor($iViewMode, &$aInput)
  761. {
  762. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  763. if (!$oEditor)
  764. return false;
  765. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  766. return true;
  767. }
  768. /**
  769. * Generate Select Box Element
  770. *
  771. * @param array $aInput
  772. * @return string
  773. */
  774. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  775. {
  776. $aNewInput = $aInput;
  777. $aNewInput['type'] = 'select';
  778. $aNewInput['name'] .= '[]';
  779. $sInput = $this->genInput($aNewInput);
  780. return <<<BLAH
  781. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  782. $sInput
  783. </div>
  784. $sInfo
  785. $sError
  786. BLAH;
  787. }
  788. /**
  789. * Generate Browse File Element
  790. *
  791. * @param array $aInput
  792. * @return string
  793. */
  794. function genInputFile(&$aInput)
  795. {
  796. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  797. if(!empty($aInput['attrs']['onchange']))
  798. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  799. else
  800. $aInput['attrs']['onchange'] = $sOnChange;
  801. $sInput = $this->genInputStandard($aInput);
  802. 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>';
  803. }
  804. /**
  805. * Generate Select Box Element
  806. *
  807. * @param array $aInput
  808. * @return string
  809. */
  810. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  811. {
  812. $sUniqId = genRndPwd (8, false);
  813. $sUploaders = '';
  814. $oUploader = null;
  815. foreach ($aInput['uploaders'] as $sUploaderObject) {
  816. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  817. if (!$oUploader)
  818. continue;
  819. $sGhostTemplate = $this->genGhostTemplate($aInput);
  820. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  821. if (empty($aInput['attrs']['disabled']))
  822. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  823. $aParams = array(
  824. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  825. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  826. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  827. 'attrs' => bx_convert_array2attrs($aAttrs),
  828. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  829. );
  830. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  831. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  832. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams);
  833. }
  834. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  835. 'uploaders_buttons' => $sUploaders,
  836. 'info' => $sInfo,
  837. 'error' => $sError,
  838. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  839. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  840. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  841. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  842. ));
  843. }
  844. protected function genGhostTemplate(&$aInput)
  845. {
  846. $sGhostTemplate = false;
  847. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  848. $oFormNested = $aInput['ghost_template'];
  849. if ($oFormNested instanceof BxDolFormNested)
  850. $sGhostTemplate = $oFormNested->genForm();
  851. } 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
  852. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  853. $sGhostTemplate = $oFormNested->getCode();
  854. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  855. $sGhostTemplate = array ();
  856. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  857. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNested)
  858. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  859. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  860. $sGhostTemplate = $aInput['ghost_template'];
  861. }
  862. return $sGhostTemplate;
  863. }
  864. protected function genCustomInputUsernamesSuggestions ($aInput)
  865. {
  866. $this->addCssJsUi();
  867. $sVals = '';
  868. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  869. foreach ($aInput['value'] as $sVal) {
  870. if (!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  871. continue;
  872. $sVals .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . $oProfile->getUnit(0, array('template' => 'unit_wo_info')) . $oProfile->getDisplayName() . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . $sVal . '" /></b>';
  873. }
  874. $sVals = trim($sVals, ',');
  875. }
  876. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  877. 'id' => $aInput['name'] . time() . mt_rand(0, 100),
  878. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  879. 'name' => $aInput['name'],
  880. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  881. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  882. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  883. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  884. 'vals' => $sVals,
  885. ));
  886. }
  887. protected function genCustomViewRowValueLabels ($aInput)
  888. {
  889. if (!$aInput['value'] || !is_array($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  890. return '';
  891. $s = '';
  892. foreach ($aInput['value'] as $sLabel)
  893. $s .= '<a href="' . $oMetatags->keywordsGetHashTagUrl($sLabel, $aInput['content_id']) . '"><b class="val bx-def-color-bg-hl bx-def-round-corners">' . trim($sLabel) . '</b></a>';
  894. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  895. 'values' => $s,
  896. ));
  897. }
  898. protected function genCustomInputLabels ($aInput)
  899. {
  900. $sValue = '';
  901. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  902. foreach ($aInput['value'] as $sVal) {
  903. $sValue .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . trim($sVal) . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . trim($sVal) . '" /></b>';
  904. }
  905. $sValue = trim($sValue, ',');
  906. }
  907. $sValues = '';
  908. if (!empty($aInput['values']) && is_array($aInput['values'])) {
  909. $a = array_diff($aInput['values'], !empty($aInput['value']) && is_array($aInput['value']) ? $aInput['value'] : array());
  910. foreach ($a as $sVal) {
  911. $sValues .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . trim($sVal) . '</b>';
  912. }
  913. $sValues = trim($sValues, ',');
  914. }
  915. return $this->oTemplate->parseHtmlByName('form_field_labels.html', array(
  916. 'id' => $aInput['name'] . time() . mt_rand(0, 100),
  917. 'name' => $aInput['name'],
  918. 'value' => $sValue,
  919. 'values' => $sValues,
  920. ));
  921. }
  922. /**
  923. * Generate Select Element
  924. *
  925. * @param array $aInput
  926. * @return string
  927. */
  928. function genInputSelect(&$aInput)
  929. {
  930. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  931. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  932. }
  933. /**
  934. * Generate Multiple Select Element
  935. *
  936. * @param array $aInput
  937. * @return string
  938. */
  939. function genInputSelectMultiple(&$aInput)
  940. {
  941. $aCurValues = array();
  942. if (isset($aInput['value']) && $aInput['value'])
  943. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  944. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  945. }
  946. /**
  947. * Generate Checkbox Set Element
  948. *
  949. * @param array $aInput
  950. * @return string
  951. */
  952. function genInputCheckboxSet(&$aInput)
  953. {
  954. $aCurValues = array();
  955. if (isset($aInput['value']) && $aInput['value'])
  956. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  957. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  958. }
  959. /**
  960. * Generate Radiobuttons Set Element
  961. *
  962. * @param array $aInput
  963. * @return string
  964. */
  965. function genInputRadioSet(&$aInput)
  966. {
  967. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  968. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  969. }
  970. function _isSelected ($sValue, $sCurValue)
  971. {
  972. return ((string)$sValue === (string)$sCurValue);
  973. }
  974. function _isSelectedMultiple ($sValue, $aCurValues)
  975. {
  976. return in_array($sValue, $aCurValues);
  977. }
  978. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  979. {
  980. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  981. $aAttrs['name'] = $aInput['name'];
  982. if ($isMultiple) {
  983. $aAttrs['name'] .= '[]';
  984. $aAttrs['multiple'] = 'multiple';
  985. }
  986. // for inputs with labels generate id
  987. if (isset($aInput['label']))
  988. $aAttrs['id'] = $this->getInputId($aInput);
  989. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  990. // generate options
  991. $sOptions = '';
  992. if (isset($aInput['values']) and is_array($aInput['values'])) {
  993. foreach ($aInput['values'] as $sValue => $sTitle) {
  994. $sClassC = $sStyleC = "";
  995. if(is_array($sTitle)) {
  996. if(isset($sTitle['type'])) {
  997. switch($sTitle['type']) {
  998. case 'group_header':
  999. $sTitle = bx_process_output($sTitle['value']);
  1000. $sOptions .= <<<BLAH
  1001. <optgroup label="$sTitle">
  1002. BLAH;
  1003. break;
  1004. case 'group_end':
  1005. $sOptions .= <<<BLAH
  1006. </optgroup>
  1007. BLAH;
  1008. break;
  1009. }
  1010. continue;
  1011. }
  1012. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  1013. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  1014. $sValue = $sTitle['key'];
  1015. $sTitle = $sTitle['value'];
  1016. }
  1017. $sValueC = bx_html_attribute($sValue);
  1018. $sTitleC = bx_process_output($sTitle);
  1019. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  1020. $sOptions .= <<<BLAH
  1021. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  1022. BLAH;
  1023. }
  1024. }
  1025. // generate element
  1026. $sCode = <<<BLAH
  1027. <select $sAttrs>
  1028. $sOptions
  1029. </select>
  1030. BLAH;
  1031. return $sCode;
  1032. }
  1033. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  1034. {
  1035. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  1036. $aAttrs['name'] = $aInput['name'];
  1037. // for inputs with labels generate id
  1038. if (isset($aInput['label']))
  1039. $aAttrs['id'] = $this->getInputId($aInput);
  1040. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1041. // generate options
  1042. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1043. $sOptions = '';
  1044. if (isset($aInput['values']) and is_array($aInput['values'])) {
  1045. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  1046. $sDivider = $this->_sDividerAlt;
  1047. // generate complex input using simple standard inputs
  1048. foreach ($aInput['values'] as $sValue => $sLabel) {
  1049. // create new simple input
  1050. $aNewInput = array(
  1051. 'type' => $sType,
  1052. 'name' => $aInput['name'] . $sNameAppend,
  1053. 'value' => $sValue,
  1054. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1055. 'label' => $sLabel,
  1056. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1057. );
  1058. $sNewInput = $this->genInput($aNewInput);
  1059. // attach new input to complex
  1060. $sOptions .= ($sNewInput . $sDivider);
  1061. }
  1062. }
  1063. // generate element
  1064. $sCode = <<<BLAH
  1065. <div $sAttrs>
  1066. $sOptions
  1067. </div>
  1068. BLAH;
  1069. return $sCode;
  1070. }
  1071. function genInputLocation(&$aInput)
  1072. {
  1073. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  1074. $sIdStatus = $this->getInputId($aInput) . '_status';
  1075. $sIdInput = $this->getInputId($aInput) . '_location';
  1076. $sProto = (0 === strncmp('https', BX_DOL_URL_ROOT, 5)) ? 'https' : 'http';
  1077. $aVars = array (
  1078. 'key' => trim(getParam('sys_maps_api_key')),
  1079. 'lang' => bx_lang_name(),
  1080. 'name' => $aInput['name'],
  1081. 'id_status' => $sIdStatus,
  1082. 'id_input' => $sIdInput,
  1083. 'manual_input' => $isManualInput,
  1084. 'bx_if:manual_input' => array(
  1085. 'condition' => $isManualInput,
  1086. 'content' => array(),
  1087. ),
  1088. 'bx_if:auto_input' => array(
  1089. 'condition' => !$isManualInput,
  1090. 'content' => array(
  1091. 'id_status' => $sIdStatus,
  1092. 'location_string' => _t('_sys_location_undefined'),
  1093. ),
  1094. ),
  1095. );
  1096. $aLocationIndexes = array ('lat', 'lng', 'country', 'state', 'city', 'zip', 'street', 'street_number');
  1097. foreach ($aLocationIndexes as $sKey)
  1098. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey);
  1099. if ($aVars['country']) {
  1100. $aCountries = BxDolFormQuery::getDataItems('Country');
  1101. $s = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  1102. $aVars['bx_if:auto_input']['content']['location_string'] = $s;
  1103. }
  1104. if ($isManualInput) {
  1105. $aInput['type'] = 'text';
  1106. $aInput['attrs']['id'] = $sIdInput;
  1107. $aVars['input'] = $this->genInputStandard($aInput);
  1108. }
  1109. else {
  1110. if ($this->getLocationVal($aInput, 'lat') && $this->getLocationVal($aInput, 'lng'))
  1111. $aInput['checked'] = true;
  1112. else
  1113. $aInput['checked'] = $this->getCleanValue($aInput['name'] . '_lat') && $this->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  1114. $aVars['input'] = $this->genInputSwitcher($aInput);
  1115. }
  1116. return $this->oTemplate->parseHtmlByName('form_field_location.html', $aVars);
  1117. }
  1118. public function setLocationVal ($aInput, $sIndex, $sVal)
  1119. {
  1120. $s = $aInput['name'] . '_' . $sIndex;
  1121. $this->_aSpecificValues[$s] = $sVal;
  1122. }
  1123. protected function getLocationVal ($aInput, $sIndex)
  1124. {
  1125. $s = $aInput['name'] . '_' . $sIndex;
  1126. if (isset($this->_aSpecificValues[$s]))
  1127. return $this->_aSpecificValues[$s];
  1128. return $this->getCleanValue($s);
  1129. }
  1130. function genInputCaptcha(&$aInput)
  1131. {
  1132. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1133. // for inputs with labels generate id
  1134. if (isset($aInput['label']))
  1135. $aAttrs['id'] = $this->getInputId($aInput);
  1136. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1137. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1138. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1139. }
  1140. /**
  1141. * Generate Label Element
  1142. *
  1143. * @param string $sLabel Text of the Label
  1144. * @param string $sInputID Dependant Input Element ID
  1145. * @return string HTML code
  1146. */
  1147. function genLabel(&$aInput)
  1148. {
  1149. if (!isset($aInput['label']) or empty($aInput['label']))
  1150. return '';
  1151. $sLabel = $aInput['label'];
  1152. $sInputID = $this->getInputId($aInput);
  1153. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  1154. return $sRet;
  1155. }
  1156. function genInfoIcon($sInfo)
  1157. {
  1158. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1159. }
  1160. function genErrorIcon( $sError = '' )
  1161. {
  1162. if ($this->bEnableErrorIcon) {
  1163. $sStyle = '';
  1164. if (!$sError)
  1165. $sStyle = 'style="display:none;"';
  1166. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1167. }
  1168. }
  1169. function getOpenSection($aAttrs = array(), $sLegend = '')
  1170. {
  1171. if (!$this->_isSectionOpened) {
  1172. if (!$aAttrs || !is_array($aAttrs))
  1173. $aAttrs = array();
  1174. if ($sLegend)
  1175. $sClassesAdd = "bx-form-section-header";
  1176. else
  1177. $sClassesAdd = "bx-form-section-divider";
  1178. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1179. $this->_isSectionOpened = true;
  1180. 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";
  1181. } else {
  1182. return '';
  1183. }
  1184. }
  1185. function getCloseSection()
  1186. {
  1187. if ($this->_isSectionOpened) {
  1188. $this->_isSectionOpened = false;
  1189. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1190. } else {
  1191. return '';
  1192. }
  1193. }
  1194. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1195. {
  1196. if (!$this->_isSectionOpened) {
  1197. if (!$aAttrs || !is_array($aAttrs))
  1198. $aAttrs = array();
  1199. if ($sLegend)
  1200. $sClassesAdd = "bx-form-section-header";
  1201. else
  1202. $sClassesAdd = "bx-form-section-divider";
  1203. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1204. $this->_isSectionOpened = true;
  1205. 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";
  1206. } else {
  1207. return '';
  1208. }
  1209. }
  1210. function getCloseSectionViewMode()
  1211. {
  1212. if ($this->_isSectionOpened) {
  1213. $this->_isSectionOpened = false;
  1214. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1215. } else {
  1216. return '';
  1217. }
  1218. }
  1219. function addCssJsUi ()
  1220. {
  1221. if (self::$_isCssJsUiAdded)
  1222. return;
  1223. $this->_addCss('jquery-ui/jquery-ui.css');
  1224. $this->_addJs(array(
  1225. 'jquery-ui/jquery-ui.custom.min.js'
  1226. ), "'undefined' === typeof($.ui)");
  1227. self::$_isCssJsUiAdded = true;
  1228. }
  1229. function addCssJsTimepicker ()
  1230. {
  1231. if (self::$_isCssJsTimepickerAdded)
  1232. return;
  1233. $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);
  1234. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1235. $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
  1236. $this->_addJs(array(
  1237. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1238. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1239. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1240. 'jquery.ui.touch-punch.min.js',
  1241. ), "'undefined' === typeof($.fn.datetimepicker)");
  1242. self::$_isCssJsTimepickerAdded = true;
  1243. }
  1244. function addCssJsDatepicker ()
  1245. {
  1246. if (self::$_isCssJsDatepickerAdded)
  1247. return;
  1248. $aUiLangs = 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);
  1249. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1250. $this->_addJs(array(
  1251. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1252. ), "'undefined' === typeof($.datepicker)");
  1253. self::$_isCssJsDatepickerAdded = true;
  1254. }
  1255. function addCssJsMinicolors ()
  1256. {
  1257. if (self::$_isCssJsMinicolorsAdded)
  1258. return;
  1259. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  1260. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  1261. self::$_isCssJsMinicolorsAdded = true;
  1262. }
  1263. function addCssJsViewMode ()
  1264. {
  1265. if (self::$_isCssJsAddedViewMode)
  1266. return;
  1267. $this->_addCss('forms.css');
  1268. self::$_isCssJsAddedViewMode = true;
  1269. }
  1270. function addCssJsCore ()
  1271. {
  1272. if (self::$_isCssJsAdded)
  1273. return;
  1274. $this->_addCss('forms.css');
  1275. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  1276. self::$_isCssJsAdded = true;
  1277. }
  1278. function addCssJs ()
  1279. {
  1280. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1281. $this->addCssJsViewMode ();
  1282. } else {
  1283. $this->addCssJsCore();
  1284. }
  1285. }
  1286. protected function _processCssJs()
  1287. {
  1288. $sRet = '';
  1289. if ($this->_bDynamicMode) {
  1290. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1291. $sRet .= "\n<script>\n";
  1292. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  1293. foreach ($this->_aJs as $sCondition => $aJs) {
  1294. $sJs = $this->oTemplate->addJs($aJs, true);
  1295. if (!$sJs)
  1296. continue;
  1297. $sRet .= "if ($sCondition)\n";
  1298. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1299. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  1300. }
  1301. $sRet .= "\n</script>\n";
  1302. }
  1303. else {
  1304. $this->oTemplate->addCss($this->_aCss);
  1305. $aJs = array();
  1306. foreach ($this->_aJs as $sCondition => $a)
  1307. $aJs = array_merge($aJs, $a);
  1308. $this->oTemplate->addJs($aJs);
  1309. }
  1310. return $sRet;
  1311. }
  1312. protected function _addJs($mixed, $sJsCondition)
  1313. {
  1314. if (!is_array($mixed))
  1315. $mixed = array($mixed);
  1316. $this->_aJs[$sJsCondition] = $mixed;
  1317. }
  1318. protected function _addCss($mixed)
  1319. {
  1320. if (!is_array($mixed))
  1321. $mixed = array($mixed);
  1322. foreach ($mixed as $s)
  1323. $this->_aCss[$s] = $s;
  1324. }
  1325. }
  1326. /** @} */