BxBaseFormView.php 41 KB

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