1
0

BxBaseFormView.php 41 KB

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