BxBaseFormView.php 40 KB

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