BxBaseFormView.php 40 KB

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