BxBaseFormView.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolForm');
  9. class BxBaseFormView extends BxDolForm {
  10. protected static $_isToggleJsAdded = false;
  11. protected static $_isCssJsAdded = false;
  12. protected static $_isCssJsAddedViewMode = false;
  13. var $bEnableErrorIcon = true;
  14. /**
  15. * HTML Code of this form
  16. *
  17. * @var string
  18. */
  19. var $sCode;
  20. /**
  21. * Code which will be added to the beginning of the form.
  22. * For example, hidden inputs.
  23. * For internal use only
  24. *
  25. * @var string
  26. */
  27. var $_sCodeAdd = '';
  28. /**
  29. * for internal use only
  30. *
  31. * @var boolean
  32. */
  33. var $_isSectionOpened = false;
  34. /**
  35. *
  36. *
  37. * @var boolean
  38. */
  39. var $_bHtmlEditorAdded = false;
  40. /**
  41. * Default divider for several inputs
  42. * @var string
  43. */
  44. var $_sDivider = '<span class="bx-def-margin-left"></span>';
  45. /**
  46. * Alternative divider for several inputs
  47. * @var string
  48. */
  49. var $_sDividerAlt = '<br />';
  50. /**
  51. * Form is added dynamically.
  52. * @var boolean
  53. */
  54. var $_bDynamicMode = false;
  55. /**
  56. * Function name for generation close form section HTML.
  57. * @var string
  58. */
  59. var $_sSectionClose = 'getCloseSection';
  60. /**
  61. * Function name for generation open form section HTML.
  62. * @var string
  63. */
  64. var $_sSectionOpen = 'getOpenSection';
  65. /**
  66. * Constructor
  67. *
  68. * @param array $aInfo Form contents
  69. *
  70. * $aInfo['params'] = array(
  71. * 'remove_form' => true|false,
  72. * );
  73. *
  74. * @return BxBaseFormView
  75. */
  76. function BxBaseFormView($aInfo, $oTemplate) {
  77. parent::BxDolForm($aInfo, $oTemplate);
  78. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  79. $this->_sSectionClose = 'getCloseSectionViewMode';
  80. $this->_sSectionOpen = 'getOpenSectionViewMode';
  81. }
  82. }
  83. /**
  84. * Return Form code
  85. * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup.
  86. * @return string
  87. */
  88. function getCode($bDynamicMode = false) {
  89. $this->_bDynamicMode = $bDynamicMode;
  90. $this->addCssJs ();
  91. return ($this->sCode = $this->genForm());
  92. }
  93. /**
  94. * Generate the whole form
  95. *
  96. * @return string
  97. */
  98. function genForm() {
  99. $this->_sCodeAdd = '';
  100. $this->_bHtmlEditorAdded = false;
  101. $sTable = $this->genRows();
  102. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  103. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  104. if (!empty($this->aParams['remove_form']) || (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])) {
  105. $sForm = <<<BLAH
  106. $sHtmlBefore
  107. {$this->_sCodeAdd}
  108. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  109. $sTable
  110. </div>
  111. $sHtmlAfter
  112. BLAH;
  113. } else {
  114. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  115. $sForm = <<<BLAH
  116. $sHtmlBefore
  117. <form $sFormAttrs>
  118. {$this->_sCodeAdd}
  119. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  120. $sTable
  121. </div>
  122. </form>
  123. <script>
  124. $(document).ready(function() {
  125. $(this).addWebForms();
  126. });
  127. </script>
  128. $sHtmlAfter
  129. BLAH;
  130. }
  131. return $sForm;
  132. }
  133. /**
  134. * Generate Table HTML code
  135. *
  136. * @return string
  137. */
  138. function genRows() {
  139. // add CSRF token if it's needed.
  140. if (!(isset($this->aParams['view_mode']) && $this->aParams['view_mode']) && BxDolDb::getInstance()->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) {
  141. $this->aInputs['csrf_token'] = array(
  142. 'type' => 'hidden',
  143. 'name' => 'csrf_token',
  144. 'value' => $mixedCsrfToken,
  145. 'db' => array ('pass' => 'Xss'),
  146. 'visible_for_levels' => PHP_INT_MAX,
  147. );
  148. }
  149. // check if we need to generate open section clause
  150. $sOpenSection = '';
  151. foreach ($this->aInputs as $aInput) {
  152. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  153. continue;
  154. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  155. $sOpenSection = $this->{$this->_sSectionOpen}();
  156. break;
  157. }
  158. // generate rows contents
  159. $sCont = '';
  160. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  161. foreach ($this->aInputs as $aInput)
  162. if (!isset($aInput['visible_for_levels']) || $this->_isVisible($aInput))
  163. $sCont .= $this->$sFuncGenRow($aInput);
  164. $sCloseSection = $this->{$this->_sSectionClose}();
  165. return $sOpenSection . $sCont . $sCloseSection;
  166. }
  167. /**
  168. * Generate single Table Row
  169. *
  170. * @param array $aInput
  171. * @return string
  172. */
  173. function genRow(&$aInput) {
  174. if (!isset($aInput['type']))
  175. $aInput['type'] = false;
  176. if (!empty($aInput['name'])) {
  177. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  178. if (method_exists($this, $sCustomMethod))
  179. return $this->$sCustomMethod($aInput);
  180. }
  181. switch ($aInput['type']) {
  182. case 'block_header':
  183. $sRow = $this->genRowBlockHeader($aInput);
  184. break;
  185. case 'block_end':
  186. $sRow = $this->genBlockEnd($aInput);
  187. break;
  188. case 'hidden':
  189. // do not generate row for hidden inputs
  190. $sRow = '';
  191. $this->_sCodeAdd .= $this->genInput($aInput);
  192. break;
  193. case 'select_box':
  194. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  195. break;
  196. case 'files':
  197. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  198. break;
  199. case 'switcher':
  200. case 'checkbox':
  201. $sRow = $this->genRowStandard($aInput, true);
  202. break;
  203. default:
  204. $sRow = $this->genRowStandard($aInput);
  205. }
  206. return $sRow;
  207. }
  208. /**
  209. * Generate single Table Row for view mode
  210. *
  211. * @param array $aInput
  212. * @return string
  213. */
  214. function genViewRow(&$aInput) {
  215. if (!isset($aInput['type']))
  216. $aInput['type'] = false;
  217. if (!empty($aInput['name'])) {
  218. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  219. if (method_exists($this, $sCustomMethod))
  220. return $this->$sCustomMethod($aInput);
  221. }
  222. switch ($aInput['type']) {
  223. case 'block_header':
  224. $sRow = $this->genRowBlockHeader($aInput);
  225. break;
  226. case 'block_end':
  227. $sRow = $this->genBlockEnd($aInput);
  228. break;
  229. default:
  230. $sRow = $this->genViewRowWrapped($aInput);
  231. }
  232. return $sRow;
  233. }
  234. /**
  235. * Generate complete wrapped row for view mode form
  236. *
  237. * @param array $aInput
  238. * @return string
  239. */
  240. function genViewRowWrapped(&$aInput) {
  241. $sValue = $this->genViewRowValue($aInput);
  242. if (null === $sValue)
  243. return '';
  244. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  245. return <<<EOS
  246. <div class="bx-form-row-view-wrapper bx-form-row-view-wrapper-{$aInput['type']} bx-def-padding-sec-top">
  247. <div class="bx-form-row-view-caption bx-def-margin-sec-right">{$sCaption}:</div> <!-- TODO: temporary ':' - replace with lang key -->
  248. <div class="bx-form-row-view-value">{$sValue}</div>
  249. </div>
  250. EOS;
  251. }
  252. /**
  253. * Generate value for view mode row
  254. *
  255. * @param array $aInput
  256. * @return string
  257. */
  258. function genViewRowValue(&$aInput) {
  259. switch ($aInput['type']) {
  260. case 'hidden':
  261. $sValue = null;
  262. break;
  263. case 'select':
  264. case 'radio_set':
  265. $sValue = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  266. break;
  267. case 'datepicker':
  268. $iTime = bx_process_input ($aInput['value'], BX_DATA_DATE_TS, false, false);
  269. $sValue = getLocaleDate($iTime);
  270. break;
  271. case 'datetime':
  272. $iTime = bx_process_input ($aInput['value'], BX_DATA_DATETIME_TS, false, false);
  273. $sValue = defineTimeInterval ($iTime);
  274. break;
  275. case 'checkbox_set':
  276. case 'select_multiple':
  277. $sValue = null;
  278. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  279. $sValue = '';
  280. foreach ($aInput['value'] as $sVal)
  281. $sValue .= $aInput['values'][$sVal] . ', ';
  282. $sValue = trim ($sValue, ', ');
  283. }
  284. break;
  285. case 'checkbox':
  286. case 'switcher':
  287. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  288. break;
  289. case 'textarea':
  290. $sValue = isset($aInput['value']) ? $aInput['value'] : null;
  291. break;
  292. default:
  293. $sValue = isset($aInput['value']) ? bx_process_output($aInput['value']) : null;
  294. }
  295. return $sValue;
  296. }
  297. /**
  298. * Generate standard row
  299. *
  300. * @param array $aInput
  301. * @return string
  302. */
  303. function genRowStandard(&$aInput, $isOneLine = false) {
  304. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  305. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  306. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  307. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  308. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  309. if (isset($aInput['name']))
  310. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  311. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), "bx-form-element-wrapper bx-def-margin-top");
  312. $sClassOneLineCaption = '';
  313. $sClassOneLineValue = '';
  314. if ($isOneLine) {
  315. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  316. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  317. $aInput['attrs']['id'] = $this->getInputId($aInput);
  318. if ($sCaption)
  319. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  320. }
  321. $sInput = $this->genInput($aInput);
  322. $sCaptionCode = '';
  323. if ($sCaption)
  324. $sCaptionCode = '<div class="bx-form-caption' . $sClassOneLineCaption . '">' . $sCaption . $sRequired . '</div>';
  325. else
  326. $sInput .= $sRequired;
  327. $sInputCode = $this->genWrapperInput($aInput, $sInput);
  328. if (empty($sInputCodeExtra)) $sInputCodeExtra = '';
  329. if (empty($sInfoIcon)) $sInfoIcon = '';
  330. if (empty($sInputCode)) $sInputCode = '';
  331. if (empty($sErrorIcon)) $sErrorIcon = '';
  332. $sValueCode = '
  333. <div class="bx-form-value' . $sClassAdd . $sClassOneLineValue . '">
  334. <div class="bx-clear"></div>
  335. ' . $sInputCode . '
  336. ' . ($isOneLine ? '' : $sInfoIcon . $sErrorIcon) . '
  337. ' . $sInputCodeExtra . '
  338. <div class="bx-clear"></div>
  339. </div>';
  340. if ($isOneLine)
  341. $sCode = $sValueCode . $sCaptionCode . '<div class="bx-clear"></div>' . $sInfoIcon . $sErrorIcon;
  342. else
  343. $sCode = $sCaptionCode . $sValueCode;
  344. return "<div $sTrAttrs>" . $sCode . "</div>";
  345. }
  346. function genWrapperInput($aInput, $sContent) {
  347. $sAttr = isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? bx_convert_array2attrs($aInput['attrs_wrapper']) : '';
  348. $sCode = <<<BLAH
  349. <div class="bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}" $sAttr>
  350. $sContent
  351. </div>
  352. BLAH;
  353. return $sCode;
  354. }
  355. /**
  356. * Generate custom row
  357. *
  358. * @param array $aInput
  359. * @param string $sCustomMethod custom method to generate code for input
  360. * @return string
  361. */
  362. function genRowCustom(&$aInput, $sCustomMethod) {
  363. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  364. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  365. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  366. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  367. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  368. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  369. if (isset($aInput['name']))
  370. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  371. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  372. $sCaptionCode = '';
  373. if ($sCaption)
  374. $sCaptionCode = '<div class="bx-form-caption">' . $sCaption . $sRequired . '</div>';
  375. $sCode = <<<BLAH
  376. <div $sTrAttrs>
  377. $sCaptionCode
  378. <div class="bx-form-value$sClassAdd">
  379. <div class="bx-clear"></div>
  380. $sInput
  381. <div class="bx-clear"></div>
  382. </div>
  383. </div>
  384. BLAH;
  385. return $sCode;
  386. }
  387. /**
  388. * Generate Block Headers row
  389. *
  390. * @param array $aInput
  391. * @return string
  392. */
  393. function genRowBlockHeader(&$aInput) {
  394. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  395. // if there is no caption - show divider only
  396. if (empty($aInput['caption'])) {
  397. $sCode = $this->{$this->_sSectionClose}();
  398. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  399. return $sCode;
  400. }
  401. // if section is collapsed by default, add necessary code
  402. $sClassAddCollapsable = 'bx-form-collapsable';
  403. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  404. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  405. // display section with caption
  406. $sCode = $this->{$this->_sSectionClose}();
  407. if (empty($aAttrs))
  408. $aAttrs['class'] = 'bx-form-collapsable ' . $sClassAddCollapsable;
  409. else
  410. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  411. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  412. $sLegend = '<legend class="bx-def-padding-sec-right bx-def-font-grayed bx-def-font-h3">' . bx_process_output($aInput['caption']) . '</legend>';
  413. else
  414. $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></legend>';
  415. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  416. return $sCode;
  417. }
  418. function genBlockEnd() {
  419. $aNextTbodyAdd = false; // need to have some default
  420. $sCode = '';
  421. $sCode .= $this->{$this->_sSectionClose}();
  422. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  423. return $sCode;
  424. }
  425. /**
  426. * Generate HTML Input Element
  427. *
  428. * @param array $aInput
  429. * @return string Output HTML Code
  430. */
  431. function genInput(&$aInput) {
  432. if (!empty($aInput['name'])) {
  433. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  434. if (method_exists($this, $sCustomMethod))
  435. return $this->$sCustomMethod($aInput);
  436. }
  437. switch ($aInput['type']) {
  438. // standard inputs (and non-standard, interpreted as standard)
  439. case 'text':
  440. case 'datepicker':
  441. case 'datetime':
  442. case 'number':
  443. case 'checkbox':
  444. case 'radio':
  445. case 'file':
  446. case 'image':
  447. case 'password':
  448. case 'slider':
  449. case 'doublerange':
  450. case 'hidden':
  451. $sInput = $this->genInputStandard($aInput);
  452. break;
  453. case 'switcher':
  454. $sInput = $this->genInputSwitcher($aInput);
  455. break;
  456. case 'button':
  457. case 'reset':
  458. case 'submit':
  459. $sInput = $this->genInputButton($aInput);
  460. break;
  461. case 'textarea':
  462. $sInput = $this->genInputTextarea($aInput);
  463. break;
  464. case 'select':
  465. $sInput = $this->genInputSelect($aInput);
  466. break;
  467. case 'select_multiple':
  468. $sInput = $this->genInputSelectMultiple($aInput);
  469. break;
  470. case 'checkbox_set':
  471. $sInput = $this->genInputCheckboxSet($aInput);
  472. break;
  473. case 'radio_set':
  474. $sInput = $this->genInputRadioSet($aInput);
  475. break;
  476. case 'input_set': // numeric array of inputs
  477. $sInput = '';
  478. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  479. foreach ($aInput as $iKey => $aSubInput) {
  480. if (!is_int($iKey) or !$aSubInput)
  481. continue; // parse only integer keys and existing values
  482. $sInput .= $this->genInput($aSubInput); // recursive call
  483. $sInput .= $sDivider;
  484. }
  485. $sInput .= '<div class="bx-clear"></div>';
  486. break;
  487. case 'custom':
  488. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  489. break;
  490. case 'captcha':
  491. $sInput = $this->genInputCaptcha($aInput);
  492. break;
  493. case 'value':
  494. $sInput = $aInput['value'];
  495. break;
  496. default:
  497. //unknown control type
  498. $sInput = 'Unknown control type';
  499. }
  500. // create input label
  501. $sInput .= $this->genLabel($aInput);
  502. return $sInput;
  503. }
  504. /**
  505. * Generate new Input Element id
  506. *
  507. * @param array $aInput
  508. * @return string
  509. */
  510. function getInputId(&$aInput) {
  511. if (isset($aInput['id']))
  512. return $aInput['id'];
  513. $sPattern = 'a-z0-9';
  514. $sName = preg_replace("/[^$sPattern]/i", '_', $aInput['name']);
  515. $sID = $this->id . '_input_' . $sName;
  516. if ( // multiple elements cause identical id's
  517. (
  518. (
  519. $aInput['type'] == 'checkbox' and
  520. substr($aInput['name'], -2) == '[]' // it is multiple element
  521. ) or
  522. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  523. ) and
  524. isset($aInput['value']) // if we can make difference
  525. ) {
  526. $sValue = preg_replace("/[^$sPattern]/i", '_', $aInput['value']);
  527. // add value
  528. $sID .= '_' . $sValue;
  529. }
  530. $sID = trim($sID, '_');
  531. $aInput['id'] = $sID; // just for repeated calls
  532. return $sID;
  533. }
  534. /**
  535. * Generate standard Input Element
  536. *
  537. * @param array $aInput
  538. * @return string
  539. */
  540. function genInputStandard(&$aInput) {
  541. // clone attributes for system use ;)
  542. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  543. // add default className to attributes
  544. $aAttrs['type'] = $aInput['type'];
  545. if ('datetime' == $aAttrs['type'])
  546. $aAttrs['type'] = 'text';
  547. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  548. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  549. // for inputs with labels generate id
  550. if (isset($aInput['label']))
  551. $aAttrs['id'] = $this->getInputId($aInput);
  552. // for checkboxes
  553. if (isset($aInput['checked']) and $aInput['checked'])
  554. $aAttrs['checked'] = 'checked';
  555. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  556. return "<input $sAttrs />\n";
  557. }
  558. /**
  559. * Generate Switcher Input Element (based on checkbox)
  560. *
  561. * @param array $aInput
  562. * @return string
  563. */
  564. function genInputSwitcher(&$aInput) {
  565. $aInput['type'] = 'checkbox';
  566. $sCheckbox = $this->genInputStandard(&$aInput);
  567. $aInput['type'] = 'switcher';
  568. $sClass = 'off';
  569. if (isset($aInput['checked']) and $aInput['checked'])
  570. $sClass = 'on';
  571. return '
  572. <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
  573. <div class="bx-switcher-canvas">
  574. <div class="bx-switcher-on">&nbsp;</div>
  575. <div class="bx-switcher-off"><div></div></div>
  576. <div class="bx-switcher-handler">&nbsp;</div>
  577. </div>
  578. </div>';
  579. }
  580. /**
  581. * Generate Button Input Element
  582. *
  583. * @param array $aInput
  584. * @return string
  585. */
  586. function genInputButton(&$aInput) {
  587. // clone attributes for system use ;)
  588. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  589. // add default className to attributes
  590. $aAttrs['type'] = $aInput['type'];
  591. if (isset($aInput['value']))
  592. $aAttrs['value'] = $aInput['value'];
  593. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  594. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
  595. return "<button $sAttrs>" . $aInput['value'] . "</button>\n";
  596. }
  597. /**
  598. * Generate Textarea Element
  599. *
  600. * @param array $aInput
  601. * @return string
  602. */
  603. function genInputTextarea(&$aInput) {
  604. // clone attributes for system use ;)
  605. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  606. $aAttrs['name'] = $aInput['name'];
  607. // for inputs with labels generate id
  608. if (isset($aInput['label']))
  609. $aAttrs['id'] = $this->getInputId($aInput);
  610. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}" . ((isset($aInput['html']) and $aInput['html'] and $this->addHtmlEditor($aInput['html'], $aInput)) ? ' bx-form-input-html' : ''));
  611. $sValue = isset($aInput['value']) ? bx_process_output($aInput['value']) : '';
  612. return "<textarea $sAttrs>$sValue</textarea>";
  613. }
  614. function addHtmlEditor($iViewMode, &$aInput) {
  615. bx_import('BxDolEditor');
  616. $oEditor = BxDolEditor::getObjectInstance();
  617. if (!$oEditor)
  618. return false;
  619. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  620. return true;
  621. }
  622. /**
  623. * Generate Select Box Element
  624. *
  625. * @param array $aInput
  626. * @return string
  627. */
  628. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '') {
  629. $aNewInput = $aInput;
  630. $aNewInput['type'] = 'select';
  631. $aNewInput['name'] .= '[]';
  632. $sInput = $this->genInput($aNewInput);
  633. return <<<BLAH
  634. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  635. $sInput
  636. </div>
  637. $sInfo
  638. $sError
  639. BLAH;
  640. }
  641. /**
  642. * Generate Select Box Element
  643. *
  644. * @param array $aInput
  645. * @return string
  646. */
  647. function genInputFiles(&$aInput, $sInfo = '', $sError = '') {
  648. $sUniqId = genRndPwd (8, false);
  649. $sUploaders = '';
  650. $oUploader = null;
  651. foreach ($aInput['uploaders'] as $sUploaderObject) {
  652. bx_import('BxDolUploader');
  653. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId);
  654. if (!$oUploader)
  655. continue;
  656. $sGhostTemplate = false;
  657. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  658. $oFormNested = $aInput['ghost_template'];
  659. if ($oFormNested instanceof BxDolFormNested)
  660. $sGhostTemplate = $oFormNested->getCode();
  661. } 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
  662. bx_import('BxDolFormNested');
  663. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  664. $sGhostTemplate = $oFormNested->getCode();
  665. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  666. bx_import('BxDolFormNested');
  667. $sGhostTemplate = array ();
  668. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  669. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNested)
  670. $sGhostTemplate[$iFileId] = $oFormNested->getCode();
  671. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  672. $sGhostTemplate = $aInput['ghost_template'];
  673. }
  674. $aParams = array(
  675. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  676. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  677. );
  678. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  679. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  680. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams);
  681. }
  682. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  683. 'uploaders_buttons' => $sUploaders,
  684. 'info' => $sInfo,
  685. 'error' => $sError,
  686. 'id_container_errors' => $oUploader->getIdContainerErrors(),
  687. 'id_container_result' => $oUploader->getIdContainerResult(),
  688. 'uploader_instance_name' => $oUploader->getNameJsInstanceUploader(),
  689. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  690. ));
  691. }
  692. /**
  693. * Generate Select Element
  694. *
  695. * @param array $aInput
  696. * @return string
  697. */
  698. function genInputSelect(&$aInput) {
  699. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  700. return $this->_genInputSelect(&$aInput, false, $sCurValue, '_isSelected');
  701. }
  702. /**
  703. * Generate Multiple Select Element
  704. *
  705. * @param array $aInput
  706. * @return string
  707. */
  708. function genInputSelectMultiple(&$aInput) {
  709. $aCurValues = array();
  710. if (isset($aInput['value']) && $aInput['value'])
  711. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  712. return $this->_genInputSelect(&$aInput, true, $aCurValues, '_isSelectedMultiple');
  713. }
  714. /**
  715. * Generate Checkbox Set Element
  716. *
  717. * @param array $aInput
  718. * @return string
  719. */
  720. function genInputCheckboxSet(&$aInput) {
  721. $aCurValues = array();
  722. if (isset($aInput['value']) && $aInput['value'])
  723. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  724. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  725. }
  726. /**
  727. * Generate Radiobuttons Set Element
  728. *
  729. * @param array $aInput
  730. * @return string
  731. */
  732. function genInputRadioSet(&$aInput) {
  733. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  734. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  735. }
  736. function _isSelected ($sValue, $sCurValue) {
  737. return ((string)$sValue === (string)$sCurValue);
  738. }
  739. function _isSelectedMultiple ($sValue, $aCurValues) {
  740. return in_array($sValue, $aCurValues);
  741. }
  742. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc) {
  743. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  744. $aAttrs['name'] = $aInput['name'];
  745. if ($isMultiple) {
  746. $aAttrs['name'] .= '[]';
  747. $aAttrs['multiple'] = 'multiple';
  748. }
  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-form-input-{$aInput['type']}");
  753. // generate options
  754. $sOptions = '';
  755. if (isset($aInput['values']) and is_array($aInput['values'])) {
  756. foreach ($aInput['values'] as $sValue => $sTitle) {
  757. $sClassC = $sStyleC = "";
  758. if(is_array($sTitle)) {
  759. if(isset($sTitle['type'])) {
  760. switch($sTitle['type']) {
  761. case 'group_header':
  762. $sTitle = bx_process_output($sTitle['value']);
  763. $sOptions .= <<<BLAH
  764. <optgroup label="$sTitle">
  765. BLAH;
  766. break;
  767. case 'group_end':
  768. $sOptions .= <<<BLAH
  769. </optgroup>
  770. BLAH;
  771. break;
  772. }
  773. continue;
  774. }
  775. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  776. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  777. $sValue = $sTitle['key'];
  778. $sTitle = $sTitle['value'];
  779. }
  780. $sValueC = bx_html_attribute($sValue);
  781. $sTitleC = bx_process_output($sTitle);
  782. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  783. $sOptions .= <<<BLAH
  784. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  785. BLAH;
  786. }
  787. }
  788. // generate element
  789. $sCode = <<<BLAH
  790. <select $sAttrs>
  791. $sOptions
  792. </select>
  793. BLAH;
  794. return $sCode;
  795. }
  796. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '') {
  797. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  798. $aAttrs['name'] = $aInput['name'];
  799. // for inputs with labels generate id
  800. if (isset($aInput['label']))
  801. $aAttrs['id'] = $this->getInputId($aInput);
  802. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  803. // generate options
  804. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  805. $sOptions = '';
  806. if (isset($aInput['values']) and is_array($aInput['values'])) {
  807. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  808. $sDivider = $this->_sDividerAlt;
  809. // generate complex input using simple standard inputs
  810. foreach ($aInput['values'] as $sValue => $sLabel) {
  811. // create new simple input
  812. $aNewInput = array(
  813. 'type' => $sType,
  814. 'name' => $aInput['name'] . $sNameAppend,
  815. 'value' => $sValue,
  816. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  817. 'label' => $sLabel,
  818. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  819. );
  820. $sNewInput = $this->genInput($aNewInput);
  821. // attach new input to complex
  822. $sOptions .= ($sNewInput . $sDivider);
  823. }
  824. }
  825. // generate element
  826. $sCode = <<<BLAH
  827. <div $sAttrs>
  828. $sOptions
  829. </div>
  830. BLAH;
  831. return $sCode;
  832. }
  833. function genInputCaptcha(&$aInput) {
  834. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  835. // for inputs with labels generate id
  836. if (isset($aInput['label']))
  837. $aAttrs['id'] = $this->getInputId($aInput);
  838. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  839. bx_import('BxDolCaptcha');
  840. $oCaptcha = BxDolCaptcha::getObjectInstance();
  841. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  842. }
  843. /**
  844. * Generate Label Element
  845. *
  846. * @param string $sLabel Text of the Label
  847. * @param string $sInputID Dependant Input Element ID
  848. * @return string HTML code
  849. */
  850. function genLabel(&$aInput) {
  851. if (!isset($aInput['label']) or empty($aInput['label']))
  852. return '';
  853. $sLabel = $aInput['label'];
  854. $sInputID = $this->getInputId($aInput);
  855. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  856. return $sRet;
  857. }
  858. function genInfoIcon($sInfo) {
  859. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo) . '</div>';
  860. }
  861. function genErrorIcon( $sError = '' ) {
  862. if ($this->bEnableErrorIcon) {
  863. $sStyle = '';
  864. if (!$sError)
  865. $sStyle = 'style="display:none;"';
  866. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  867. }
  868. }
  869. function getOpenSection($aAttrs = array(), $sLegend = '') {
  870. if (!$this->_isSectionOpened) {
  871. if (!$aAttrs || !is_array($aAttrs))
  872. $aAttrs = array();
  873. if ($sLegend)
  874. $sClassesAdd = "bx-form-section-header";
  875. else
  876. $sClassesAdd = "bx-form-section-divider";
  877. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  878. $this->_isSectionOpened = true;
  879. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <fieldset $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";
  880. } else {
  881. return '';
  882. }
  883. }
  884. function getCloseSection() {
  885. if ($this->_isSectionOpened) {
  886. $this->_isSectionOpened = false;
  887. return "</div> </fieldset> </div> \n<!-- form header content ends -->\n";
  888. } else {
  889. return '';
  890. }
  891. }
  892. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '') {
  893. if (!$this->_isSectionOpened) {
  894. if (!$aAttrs || !is_array($aAttrs))
  895. $aAttrs = array();
  896. if ($sLegend)
  897. $sClassesAdd = "bx-form-section-header";
  898. else
  899. $sClassesAdd = "bx-form-section-divider";
  900. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  901. $this->_isSectionOpened = true;
  902. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <fieldset $sAttrs> $sLegend <div class=\"bx-form-section-content bx-def-padding-top\">\n";
  903. } else {
  904. return '';
  905. }
  906. }
  907. function getCloseSectionViewMode() {
  908. if ($this->_isSectionOpened) {
  909. $this->_isSectionOpened = false;
  910. return "</div> </fieldset> </div> \n<!-- form header content ends -->\n";
  911. } else {
  912. return '';
  913. }
  914. }
  915. function addCssJs () {
  916. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  917. if (self::$_isCssJsAddedViewMode)
  918. return;
  919. $this->oTemplate->addCss('forms.css');
  920. self::$_isCssJsAddedViewMode = true;
  921. } else {
  922. if (self::$_isCssJsAdded)
  923. return;
  924. $aCss = array(
  925. 'forms.css',
  926. // TODO: move css to the templates folder
  927. 'plugins/jquery/themes/|calendar-blue.css',
  928. 'plugins/jquery/themes/|jquery-ui.css',
  929. );
  930. $aCalendarLangs = array ('en' => 1, 'af' => 1, 'al' => 1, 'bg' => 1, 'br' => 1, 'ca' => 1, 'cs' => 1, 'cy-GB' => 1, 'da' => 1, 'de' => 1, 'du' => 1, 'el' => 1, 'es' => 1, 'fi' => 1, 'fr' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'it' => 1, 'ja' => 1, 'jp' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'si' => 1, 'sk' => 1, 'sp' => 1, 'sv' => 1, 'tr' => 1, 'zh' => 1);
  931. $aUiLangs = array ('af' => 1, 'ar-DZ' => 1, 'ar' => 1, 'az' => 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-CH' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'kk' => 1, 'ko' => 1, 'lb' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'ml' => 1, 'ms' => 1, 'nl-BE' => 1, 'nl' => 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);
  932. bx_import('BxDolLanguages');
  933. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  934. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  935. $aJs = array(
  936. 'jquery.webForms.js',
  937. 'jquery.ui.core.min.js',
  938. 'jquery.ui.widget.min.js',
  939. 'jquery.ui.mouse.min.js',
  940. 'jquery.ui.position.min.js',
  941. 'jquery.ui.slider.min.js',
  942. 'jquery.ui.datepicker.min.js',
  943. 'plugins/jquery/i18n/|jquery.ui.datepicker-' . $sUiLang . '.js',
  944. 'jquery.dynDateTime.js',
  945. 'plugins/jquery/langs/|calendar-' . $sCalendarLang . '.js',
  946. );
  947. $this->oTemplate->addJs($aJs);
  948. $this->oTemplate->addCss($aCss);
  949. self::$_isCssJsAdded = true;
  950. }
  951. }
  952. }