BxBaseFormView.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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 bx-clearfix' . $sClassAdd . $sClassOneLineValue . '">
  336. ' . $sInputCode . '
  337. ' . ($isOneLine ? '' : $sInfoIcon . $sErrorIcon) . '
  338. ' . $sInputCodeExtra . '
  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 'date_time':
  442. case 'datetime':
  443. case 'number':
  444. case 'checkbox':
  445. case 'radio':
  446. case 'file':
  447. case 'image':
  448. case 'password':
  449. case 'slider':
  450. case 'doublerange':
  451. case 'hidden':
  452. $sInput = $this->genInputStandard($aInput);
  453. break;
  454. case 'switcher':
  455. $sInput = $this->genInputSwitcher($aInput);
  456. break;
  457. case 'button':
  458. case 'reset':
  459. case 'submit':
  460. $sInput = $this->genInputButton($aInput);
  461. break;
  462. case 'textarea':
  463. $sInput = $this->genInputTextarea($aInput);
  464. break;
  465. case 'select':
  466. $sInput = $this->genInputSelect($aInput);
  467. break;
  468. case 'select_multiple':
  469. $sInput = $this->genInputSelectMultiple($aInput);
  470. break;
  471. case 'checkbox_set':
  472. $sInput = $this->genInputCheckboxSet($aInput);
  473. break;
  474. case 'radio_set':
  475. $sInput = $this->genInputRadioSet($aInput);
  476. break;
  477. case 'input_set': // numeric array of inputs
  478. $sInput = '';
  479. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  480. foreach ($aInput as $iKey => $aSubInput) {
  481. if (!is_int($iKey) or !$aSubInput)
  482. continue; // parse only integer keys and existing values
  483. $sInput .= $this->genInput($aSubInput); // recursive call
  484. $sInput .= $sDivider;
  485. }
  486. $sInput .= '<div class="bx-clear"></div>';
  487. break;
  488. case 'custom':
  489. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  490. break;
  491. case 'captcha':
  492. $sInput = $this->genInputCaptcha($aInput);
  493. break;
  494. case 'value':
  495. $sInput = $aInput['value'];
  496. break;
  497. default:
  498. //unknown control type
  499. $sInput = 'Unknown control type';
  500. }
  501. // create input label
  502. $sInput .= $this->genLabel($aInput);
  503. return $sInput;
  504. }
  505. /**
  506. * Generate new Input Element id
  507. *
  508. * @param array $aInput
  509. * @return string
  510. */
  511. function getInputId(&$aInput) {
  512. if (isset($aInput['id']))
  513. return $aInput['id'];
  514. $sPattern = 'a-z0-9';
  515. $sName = preg_replace("/[^$sPattern]/i", '_', $aInput['name']);
  516. $sID = $this->id . '_input_' . $sName;
  517. if ( // multiple elements cause identical id's
  518. (
  519. (
  520. $aInput['type'] == 'checkbox' and
  521. substr($aInput['name'], -2) == '[]' // it is multiple element
  522. ) or
  523. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  524. ) and
  525. isset($aInput['value']) // if we can make difference
  526. ) {
  527. $sValue = preg_replace("/[^$sPattern]/i", '_', $aInput['value']);
  528. // add value
  529. $sID .= '_' . $sValue;
  530. }
  531. $sID = trim($sID, '_');
  532. $aInput['id'] = $sID; // just for repeated calls
  533. return $sID;
  534. }
  535. /**
  536. * Generate standard Input Element
  537. *
  538. * @param array $aInput
  539. * @return string
  540. */
  541. function genInputStandard(&$aInput) {
  542. // clone attributes for system use ;)
  543. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  544. // add default className to attributes
  545. $aAttrs['type'] = $aInput['type'];
  546. if ('datetime' == $aAttrs['type'])
  547. $aAttrs['type'] = 'date_time';
  548. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  549. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  550. // for inputs with labels generate id
  551. if (isset($aInput['label']))
  552. $aAttrs['id'] = $this->getInputId($aInput);
  553. // for checkboxes
  554. if (isset($aInput['checked']) and $aInput['checked'])
  555. $aAttrs['checked'] = 'checked';
  556. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  557. return "<input $sAttrs />\n";
  558. }
  559. /**
  560. * Generate Switcher Input Element (based on checkbox)
  561. *
  562. * @param array $aInput
  563. * @return string
  564. */
  565. function genInputSwitcher(&$aInput) {
  566. $aInput['type'] = 'checkbox';
  567. $sCheckbox = $this->genInputStandard($aInput);
  568. $aInput['type'] = 'switcher';
  569. $sClass = 'off';
  570. if (isset($aInput['checked']) and $aInput['checked'])
  571. $sClass = 'on';
  572. return '
  573. <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
  574. <div class="bx-switcher-canvas">
  575. <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
  576. <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
  577. <div class="bx-switcher-handler">&nbsp;</div>
  578. </div>
  579. </div>';
  580. }
  581. /**
  582. * Generate Button Input Element
  583. *
  584. * @param array $aInput
  585. * @return string
  586. */
  587. function genInputButton(&$aInput) {
  588. // clone attributes for system use ;)
  589. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  590. // add default className to attributes
  591. $aAttrs['type'] = $aInput['type'];
  592. if (isset($aInput['value']))
  593. $aAttrs['value'] = $aInput['value'];
  594. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  595. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
  596. return "<button $sAttrs>" . $aInput['value'] . "</button>\n";
  597. }
  598. /**
  599. * Generate Textarea Element
  600. *
  601. * @param array $aInput
  602. * @return string
  603. */
  604. function genInputTextarea(&$aInput) {
  605. // clone attributes for system use ;)
  606. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  607. $aAttrs['name'] = $aInput['name'];
  608. // for inputs with labels generate id
  609. if (isset($aInput['label']))
  610. $aAttrs['id'] = $this->getInputId($aInput);
  611. $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' : ''));
  612. $sValue = isset($aInput['value']) ? bx_process_output($aInput['value']) : '';
  613. return "<textarea $sAttrs>$sValue</textarea>";
  614. }
  615. function addHtmlEditor($iViewMode, &$aInput) {
  616. bx_import('BxDolEditor');
  617. $oEditor = BxDolEditor::getObjectInstance();
  618. if (!$oEditor)
  619. return false;
  620. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  621. return true;
  622. }
  623. /**
  624. * Generate Select Box Element
  625. *
  626. * @param array $aInput
  627. * @return string
  628. */
  629. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '') {
  630. $aNewInput = $aInput;
  631. $aNewInput['type'] = 'select';
  632. $aNewInput['name'] .= '[]';
  633. $sInput = $this->genInput($aNewInput);
  634. return <<<BLAH
  635. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  636. $sInput
  637. </div>
  638. $sInfo
  639. $sError
  640. BLAH;
  641. }
  642. /**
  643. * Generate Select Box Element
  644. *
  645. * @param array $aInput
  646. * @return string
  647. */
  648. function genInputFiles(&$aInput, $sInfo = '', $sError = '') {
  649. $sUniqId = genRndPwd (8, false);
  650. $sUploaders = '';
  651. $oUploader = null;
  652. foreach ($aInput['uploaders'] as $sUploaderObject) {
  653. bx_import('BxDolUploader');
  654. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId);
  655. if (!$oUploader)
  656. continue;
  657. $sGhostTemplate = false;
  658. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  659. $oFormNested = $aInput['ghost_template'];
  660. if ($oFormNested instanceof BxDolFormNested)
  661. $sGhostTemplate = $oFormNested->getCode();
  662. } 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
  663. bx_import('BxDolFormNested');
  664. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  665. $sGhostTemplate = $oFormNested->getCode();
  666. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  667. bx_import('BxDolFormNested');
  668. $sGhostTemplate = array ();
  669. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  670. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNested)
  671. $sGhostTemplate[$iFileId] = $oFormNested->getCode();
  672. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  673. $sGhostTemplate = $aInput['ghost_template'];
  674. }
  675. $aParams = array(
  676. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  677. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  678. );
  679. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  680. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  681. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams);
  682. }
  683. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  684. 'uploaders_buttons' => $sUploaders,
  685. 'info' => $sInfo,
  686. 'error' => $sError,
  687. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  688. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  689. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  690. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  691. ));
  692. }
  693. /**
  694. * Generate Select Element
  695. *
  696. * @param array $aInput
  697. * @return string
  698. */
  699. function genInputSelect(&$aInput) {
  700. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  701. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  702. }
  703. /**
  704. * Generate Multiple Select Element
  705. *
  706. * @param array $aInput
  707. * @return string
  708. */
  709. function genInputSelectMultiple(&$aInput) {
  710. $aCurValues = array();
  711. if (isset($aInput['value']) && $aInput['value'])
  712. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  713. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  714. }
  715. /**
  716. * Generate Checkbox Set Element
  717. *
  718. * @param array $aInput
  719. * @return string
  720. */
  721. function genInputCheckboxSet(&$aInput) {
  722. $aCurValues = array();
  723. if (isset($aInput['value']) && $aInput['value'])
  724. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  725. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  726. }
  727. /**
  728. * Generate Radiobuttons Set Element
  729. *
  730. * @param array $aInput
  731. * @return string
  732. */
  733. function genInputRadioSet(&$aInput) {
  734. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  735. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  736. }
  737. function _isSelected ($sValue, $sCurValue) {
  738. return ((string)$sValue === (string)$sCurValue);
  739. }
  740. function _isSelectedMultiple ($sValue, $aCurValues) {
  741. return in_array($sValue, $aCurValues);
  742. }
  743. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc) {
  744. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  745. $aAttrs['name'] = $aInput['name'];
  746. if ($isMultiple) {
  747. $aAttrs['name'] .= '[]';
  748. $aAttrs['multiple'] = 'multiple';
  749. }
  750. // for inputs with labels generate id
  751. if (isset($aInput['label']))
  752. $aAttrs['id'] = $this->getInputId($aInput);
  753. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  754. // generate options
  755. $sOptions = '';
  756. if (isset($aInput['values']) and is_array($aInput['values'])) {
  757. foreach ($aInput['values'] as $sValue => $sTitle) {
  758. $sClassC = $sStyleC = "";
  759. if(is_array($sTitle)) {
  760. if(isset($sTitle['type'])) {
  761. switch($sTitle['type']) {
  762. case 'group_header':
  763. $sTitle = bx_process_output($sTitle['value']);
  764. $sOptions .= <<<BLAH
  765. <optgroup label="$sTitle">
  766. BLAH;
  767. break;
  768. case 'group_end':
  769. $sOptions .= <<<BLAH
  770. </optgroup>
  771. BLAH;
  772. break;
  773. }
  774. continue;
  775. }
  776. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  777. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  778. $sValue = $sTitle['key'];
  779. $sTitle = $sTitle['value'];
  780. }
  781. $sValueC = bx_html_attribute($sValue);
  782. $sTitleC = bx_process_output($sTitle);
  783. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  784. $sOptions .= <<<BLAH
  785. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  786. BLAH;
  787. }
  788. }
  789. // generate element
  790. $sCode = <<<BLAH
  791. <select $sAttrs>
  792. $sOptions
  793. </select>
  794. BLAH;
  795. return $sCode;
  796. }
  797. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '') {
  798. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  799. $aAttrs['name'] = $aInput['name'];
  800. // for inputs with labels generate id
  801. if (isset($aInput['label']))
  802. $aAttrs['id'] = $this->getInputId($aInput);
  803. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  804. // generate options
  805. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  806. $sOptions = '';
  807. if (isset($aInput['values']) and is_array($aInput['values'])) {
  808. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  809. $sDivider = $this->_sDividerAlt;
  810. // generate complex input using simple standard inputs
  811. foreach ($aInput['values'] as $sValue => $sLabel) {
  812. // create new simple input
  813. $aNewInput = array(
  814. 'type' => $sType,
  815. 'name' => $aInput['name'] . $sNameAppend,
  816. 'value' => $sValue,
  817. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  818. 'label' => $sLabel,
  819. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  820. );
  821. $sNewInput = $this->genInput($aNewInput);
  822. // attach new input to complex
  823. $sOptions .= ($sNewInput . $sDivider);
  824. }
  825. }
  826. // generate element
  827. $sCode = <<<BLAH
  828. <div $sAttrs>
  829. $sOptions
  830. </div>
  831. BLAH;
  832. return $sCode;
  833. }
  834. function genInputCaptcha(&$aInput) {
  835. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  836. // for inputs with labels generate id
  837. if (isset($aInput['label']))
  838. $aAttrs['id'] = $this->getInputId($aInput);
  839. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  840. bx_import('BxDolCaptcha');
  841. $oCaptcha = BxDolCaptcha::getObjectInstance();
  842. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  843. }
  844. /**
  845. * Generate Label Element
  846. *
  847. * @param string $sLabel Text of the Label
  848. * @param string $sInputID Dependant Input Element ID
  849. * @return string HTML code
  850. */
  851. function genLabel(&$aInput) {
  852. if (!isset($aInput['label']) or empty($aInput['label']))
  853. return '';
  854. $sLabel = $aInput['label'];
  855. $sInputID = $this->getInputId($aInput);
  856. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  857. return $sRet;
  858. }
  859. function genInfoIcon($sInfo) {
  860. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo) . '</div>';
  861. }
  862. function genErrorIcon( $sError = '' ) {
  863. if ($this->bEnableErrorIcon) {
  864. $sStyle = '';
  865. if (!$sError)
  866. $sStyle = 'style="display:none;"';
  867. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  868. }
  869. }
  870. function getOpenSection($aAttrs = array(), $sLegend = '') {
  871. if (!$this->_isSectionOpened) {
  872. if (!$aAttrs || !is_array($aAttrs))
  873. $aAttrs = array();
  874. if ($sLegend)
  875. $sClassesAdd = "bx-form-section-header";
  876. else
  877. $sClassesAdd = "bx-form-section-divider";
  878. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  879. $this->_isSectionOpened = true;
  880. 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";
  881. } else {
  882. return '';
  883. }
  884. }
  885. function getCloseSection() {
  886. if ($this->_isSectionOpened) {
  887. $this->_isSectionOpened = false;
  888. return "</div> </fieldset> </div> \n<!-- form header content ends -->\n";
  889. } else {
  890. return '';
  891. }
  892. }
  893. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '') {
  894. if (!$this->_isSectionOpened) {
  895. if (!$aAttrs || !is_array($aAttrs))
  896. $aAttrs = array();
  897. if ($sLegend)
  898. $sClassesAdd = "bx-form-section-header";
  899. else
  900. $sClassesAdd = "bx-form-section-divider";
  901. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  902. $this->_isSectionOpened = true;
  903. 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";
  904. } else {
  905. return '';
  906. }
  907. }
  908. function getCloseSectionViewMode() {
  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 addCssJs () {
  917. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  918. if (self::$_isCssJsAddedViewMode)
  919. return;
  920. $this->oTemplate->addCss('forms.css');
  921. self::$_isCssJsAddedViewMode = true;
  922. } else {
  923. if (self::$_isCssJsAdded)
  924. return;
  925. $aCss = array(
  926. 'forms.css',
  927. // TODO: move css to the templates folder
  928. 'plugins/jquery/themes/|jquery-ui.css',
  929. 'plugins/jquery/themes/|jquery-ui-timepicker-addon.css',
  930. );
  931. $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);
  932. $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);
  933. bx_import('BxDolLanguages');
  934. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  935. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  936. $aJs = array(
  937. 'jquery.webForms.js',
  938. 'jquery.ui.core.min.js',
  939. 'jquery.ui.widget.min.js',
  940. 'jquery.ui.mouse.min.js',
  941. 'jquery.ui.position.min.js',
  942. 'jquery.ui.slider.min.js',
  943. 'jquery.ui.datepicker.min.js',
  944. 'plugins/jquery/i18n/|jquery.ui.datepicker-' . $sUiLang . '.js',
  945. 'jquery-ui-timepicker-addon.js',
  946. 'jquery-ui-sliderAccess.js',
  947. 'plugins/jquery/i18n/|jquery-ui-timepicker-' . $sCalendarLang . '.js',
  948. );
  949. $this->oTemplate->addJs($aJs);
  950. $this->oTemplate->addCss($aCss);
  951. self::$_isCssJsAdded = true;
  952. }
  953. }
  954. }