BxBaseFormView.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxBaseFormView extends BxDolForm
  10. {
  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. * Form is submitted dynamically (using Ajax Submit).
  46. */
  47. protected $_bAjaxMode = false;
  48. /**
  49. * Form is displayed in view mode.
  50. */
  51. protected $_bViewMode = false;
  52. /**
  53. * Function name for generation close form section HTML.
  54. */
  55. protected $_sSectionClose = 'getCloseSection';
  56. /**
  57. * Function name for generation open form section HTML.
  58. */
  59. protected $_sSectionOpen = 'getOpenSection';
  60. /**
  61. * JS files list for form
  62. */
  63. protected $_aJs = array();
  64. /**
  65. * CSS files list for form
  66. */
  67. protected $_aCss = array();
  68. /**
  69. * Constructor
  70. *
  71. * @param array $aInfo Form contents
  72. *
  73. * $aInfo['params'] = array(
  74. * 'remove_form' => true|false,
  75. * );
  76. *
  77. * @return BxBaseFormView
  78. */
  79. function __construct($aInfo, $oTemplate)
  80. {
  81. parent::__construct($aInfo, $oTemplate);
  82. if(isset($this->aParams['ajax_mode']))
  83. $this->setAjaxMode($this->aParams['ajax_mode']);
  84. $mixedAjaxMode = bx_get('ajax_mode');
  85. if($mixedAjaxMode !== false)
  86. $this->setAjaxMode($mixedAjaxMode);
  87. $this->_bViewMode = isset($this->aParams['view_mode']) && $this->aParams['view_mode'];
  88. if ($this->_bViewMode) {
  89. $this->_sSectionClose = 'getCloseSectionViewMode';
  90. $this->_sSectionOpen = 'getOpenSectionViewMode';
  91. }
  92. }
  93. function setAjaxMode($bAjaxMode)
  94. {
  95. $this->_bAjaxMode = (bool)$bAjaxMode;
  96. }
  97. function isAjaxMode()
  98. {
  99. return $this->_bAjaxMode;
  100. }
  101. /**
  102. * Return Form code
  103. * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup.
  104. * @return string
  105. */
  106. function getCode($bDynamicMode = false)
  107. {
  108. $this->_bDynamicMode = $bDynamicMode;
  109. $this->addCssJs ();
  110. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  111. $sDynamicCssJs = $this->_processCssJs();
  112. return $sDynamicCssJs . ($this->sCode = $this->genForm());
  113. }
  114. /**
  115. * Generate the whole form
  116. *
  117. * @return string
  118. */
  119. function genForm()
  120. {
  121. $this->_sCodeAdd = '';
  122. $sTable = $this->genRows();
  123. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  124. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  125. if (!empty($this->aParams['remove_form']) || (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])) {
  126. $sForm = <<<BLAH
  127. $sHtmlBefore
  128. {$this->_sCodeAdd}
  129. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  130. $sTable
  131. </div>
  132. $sHtmlAfter
  133. BLAH;
  134. } else {
  135. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  136. $sAjaxFormJs = '';
  137. if($this->_bAjaxMode)
  138. $sAjaxFormJs = <<<BLAH
  139. $("form#{$this->id}").ajaxForm({
  140. dataType: "json",
  141. beforeSubmit: function (formData, jqForm, options) {
  142. bx_loading($("form#{$this->id}"), true);
  143. },
  144. success: function (oData) {
  145. bx_loading($("form#{$this->id}"), false);
  146. processJsonData(oData);
  147. }
  148. });
  149. BLAH;
  150. $sForm = <<<BLAH
  151. $sHtmlBefore
  152. <form $sFormAttrs>
  153. {$this->_sCodeAdd}
  154. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  155. $sTable
  156. </div>
  157. </form>
  158. <script>
  159. $(document).ready(function() {
  160. $(this).addWebForms();
  161. });
  162. $sAjaxFormJs
  163. </script>
  164. $sHtmlAfter
  165. BLAH;
  166. }
  167. return $sForm;
  168. }
  169. /**
  170. * Generate Table HTML code
  171. *
  172. * @return string
  173. */
  174. function genRows()
  175. {
  176. // add CSRF token if it's needed.
  177. 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) {
  178. $this->aInputs['csrf_token'] = array(
  179. 'type' => 'hidden',
  180. 'name' => 'csrf_token',
  181. 'value' => $mixedCsrfToken,
  182. 'db' => array ('pass' => 'Xss'),
  183. 'visible_for_levels' => PHP_INT_MAX,
  184. );
  185. }
  186. // add 'Ajax Mode' flag
  187. if($this->_bAjaxMode)
  188. $this->aInputs['ajax_mode'] = array(
  189. 'type' => 'hidden',
  190. 'name' => 'ajax_mode',
  191. 'value' => 1,
  192. 'db' => array ('pass' => 'Int'),
  193. 'visible_for_levels' => PHP_INT_MAX,
  194. );
  195. // check if we need to generate open section clause
  196. $sOpenSection = '';
  197. foreach ($this->aInputs as $aInput) {
  198. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  199. continue;
  200. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  201. $sOpenSection = $this->{$this->_sSectionOpen}();
  202. break;
  203. }
  204. // generate rows contents
  205. $sCont = '';
  206. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  207. foreach ($this->aInputs as $aInput)
  208. if (!isset($aInput['visible_for_levels']) || self::isVisible($aInput))
  209. $sCont .= $this->$sFuncGenRow($aInput);
  210. $sCloseSection = $this->{$this->_sSectionClose}();
  211. return $sOpenSection . $sCont . $sCloseSection;
  212. }
  213. /**
  214. * Generate single Table Row
  215. *
  216. * @param array $aInput
  217. * @return string
  218. */
  219. function genRow(&$aInput)
  220. {
  221. if (!isset($aInput['type']))
  222. $aInput['type'] = false;
  223. if (!empty($aInput['name'])) {
  224. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  225. if (method_exists($this, $sCustomMethod))
  226. return $this->$sCustomMethod($aInput);
  227. }
  228. switch ($aInput['type']) {
  229. case 'block_header':
  230. $sRow = $this->genRowBlockHeader($aInput);
  231. break;
  232. case 'block_end':
  233. $sRow = $this->genBlockEnd($aInput);
  234. break;
  235. case 'hidden':
  236. // do not generate row for hidden inputs
  237. $sRow = '';
  238. $this->_sCodeAdd .= $this->genInput($aInput);
  239. break;
  240. case 'select_box':
  241. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  242. break;
  243. case 'files':
  244. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  245. break;
  246. case 'switcher':
  247. case 'checkbox':
  248. $sRow = $this->genRowStandard($aInput, true);
  249. break;
  250. default:
  251. $sRow = $this->genRowStandard($aInput);
  252. }
  253. return $sRow;
  254. }
  255. /**
  256. * Generate single Table Row for view mode
  257. *
  258. * @param array $aInput
  259. * @return string
  260. */
  261. function genViewRow(&$aInput)
  262. {
  263. if (!isset($aInput['type']))
  264. $aInput['type'] = false;
  265. if (!empty($aInput['name'])) {
  266. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  267. if (method_exists($this, $sCustomMethod))
  268. return $this->$sCustomMethod($aInput);
  269. }
  270. switch ($aInput['type']) {
  271. case 'block_header':
  272. $sRow = $this->genRowBlockHeader($aInput);
  273. break;
  274. case 'block_end':
  275. $sRow = $this->genBlockEnd($aInput);
  276. break;
  277. default:
  278. $sRow = $this->genViewRowWrapped($aInput);
  279. }
  280. return $sRow;
  281. }
  282. /**
  283. * Generate complete wrapped row for view mode form
  284. *
  285. * @param array $aInput
  286. * @return string
  287. */
  288. function genViewRowWrapped(&$aInput)
  289. {
  290. $sValue = $this->genViewRowValue($aInput);
  291. if (null === $sValue)
  292. return '';
  293. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  294. return <<<EOS
  295. <div class="bx-form-row-view-wrapper bx-form-row-view-wrapper-{$aInput['type']} bx-def-padding-sec-top">
  296. <div class="bx-form-row-view-caption">{$sCaption}:</div>
  297. <div class="bx-form-row-view-value">{$sValue}</div>
  298. </div>
  299. EOS;
  300. }
  301. /**
  302. * Generate value for view mode row
  303. *
  304. * @param array $aInput
  305. * @return string
  306. */
  307. function genViewRowValue(&$aInput)
  308. {
  309. if (!empty($aInput['name'])) {
  310. $sCustomMethod = 'genCustomViewRowValue' . $this->_genMethodName($aInput['name']);
  311. if (method_exists($this, $sCustomMethod))
  312. return $this->$sCustomMethod($aInput);
  313. }
  314. switch ($aInput['type']) {
  315. case 'hidden':
  316. $sValue = null;
  317. break;
  318. case 'select':
  319. case 'radio_set':
  320. $sValue = $this->genViewRowValueForSelect($aInput);
  321. break;
  322. case 'datepicker':
  323. $sValue = null;
  324. if (empty($aInput['value']) || !$aInput['value'] || '0000-00-00' == $aInput['value'])
  325. break;
  326. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  327. break;
  328. case 'date_time':
  329. case 'datetime':
  330. $sValue = null;
  331. if(empty($aInput['value']) || !$aInput['value'] || '0000-00-00 00:00:00' == $aInput['value'] || '0000-00-00 00:00' == $aInput['value'])
  332. break;
  333. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  334. break;
  335. case 'checkbox_set':
  336. case 'select_multiple':
  337. $sValue = null;
  338. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  339. $sValue = '';
  340. foreach ($aInput['value'] as $sVal)
  341. $sValue .= $aInput['values'][$sVal] . ', ';
  342. $sValue = trim ($sValue, ', ');
  343. }
  344. break;
  345. case 'checkbox':
  346. case 'switcher':
  347. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  348. break;
  349. case 'textarea':
  350. if (isset($aInput['value']) && '' !== $aInput['value'])
  351. $sValue = (isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : bx_process_output($aInput['value'], BX_DATA_TEXT_MULTILINE);
  352. else
  353. $sValue = null;
  354. break;
  355. case 'text':
  356. if(isset($aInput['value']) && '' !== $aInput['value'])
  357. $sValue = bx_linkify(bx_process_output($aInput['value']));
  358. else
  359. $sValue = null;
  360. break;
  361. default:
  362. $sValue = isset($aInput['value']) && '' !== $aInput['value'] ? bx_process_output($aInput['value']) : null;
  363. }
  364. return $sValue;
  365. }
  366. function genViewRowValueForSelect(&$aInput)
  367. {
  368. if (!isset($aInput['value']) || !$aInput['value'])
  369. return null;
  370. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  371. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])))
  372. return $oCategory->getCategoryLink($s, $aInput['value']);
  373. return $s;
  374. }
  375. protected function genCustomRowBirthday(&$aInput)
  376. {
  377. if(!$this->_bViewMode)
  378. return $this->genRowStandard($aInput);
  379. $aInput['caption_src'] = '_sys_form_input_age';
  380. $aInput['caption'] = _t($aInput['caption_src']);
  381. return $this->genViewRowWrapped($aInput);
  382. }
  383. protected function genCustomViewRowValueBirthday(&$aInput)
  384. {
  385. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00')))
  386. return null;
  387. $sValue = $aInput['value'];
  388. $iPosSpace = strpos($sValue, ' ');
  389. if($iPosSpace !== false)
  390. $sValue = trim(substr($sValue, 0, $iPosSpace));
  391. $aDate = explode('-', $sValue);
  392. $iCdYear = (int)date('Y');
  393. $iCdMonth = (int)date('n');
  394. $iCdDay = (int)date('j');
  395. $iResult = $iCdYear - (int)$aDate[0];
  396. if($iCdMonth < (int)$aDate[1] || ($iCdMonth == (int)$aDate[1] && $iCdDay < (int)$aDate[2]))
  397. $iResult -= 1;
  398. return $iResult;
  399. }
  400. /**
  401. * Generate standard row
  402. *
  403. * @param array $aInput
  404. * @return string
  405. */
  406. function genRowStandard(&$aInput, $isOneLine = false)
  407. {
  408. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  409. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  410. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  411. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  412. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  413. $sClassWrapper = 'bx-form-element-wrapper';
  414. if($isOneLine)
  415. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  416. $sClassWrapper .= ' bx-def-margin-top';
  417. if (isset($aInput['name']))
  418. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  419. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  420. $sClassOneLineCaption = '';
  421. $sClassOneLineValue = '';
  422. if ($isOneLine) {
  423. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  424. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  425. $aInput['attrs']['id'] = $this->getInputId($aInput);
  426. if ($sCaption)
  427. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  428. }
  429. $sInput = $this->genInput($aInput);
  430. $sCaptionCode = '';
  431. if ($sCaption)
  432. $sCaptionCode = '<div class="bx-form-caption' . $sClassOneLineCaption . '">' . $sCaption . $sRequired . '</div>';
  433. else
  434. $sInput .= $sRequired;
  435. $sInputCode = $this->genWrapperInput($aInput, $sInput);
  436. if (empty($sInputCodeExtra)) $sInputCodeExtra = '';
  437. if (empty($sInfoIcon)) $sInfoIcon = '';
  438. if (empty($sInputCode)) $sInputCode = '';
  439. if (empty($sErrorIcon)) $sErrorIcon = '';
  440. $sValueCode = '
  441. <div class="bx-form-value bx-clearfix' . $sClassAdd . $sClassOneLineValue . '">
  442. ' . $sInputCode . '
  443. ' . ($isOneLine ? '' : $sInfoIcon . $sErrorIcon) . '
  444. ' . $sInputCodeExtra . '
  445. </div>';
  446. if ($isOneLine)
  447. $sCode = $sValueCode . $sCaptionCode . '<div class="bx-clear"></div>' . $sInfoIcon . $sErrorIcon;
  448. else
  449. $sCode = $sCaptionCode . $sValueCode;
  450. return "<div $sTrAttrs>" . $sCode . "</div>";
  451. }
  452. function genWrapperInput($aInput, $sContent)
  453. {
  454. $sAttr = isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? bx_convert_array2attrs($aInput['attrs_wrapper']) : '';
  455. return "<div class=\"bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}\" $sAttr>$sContent</div>";
  456. }
  457. /**
  458. * Generate custom row
  459. *
  460. * @param array $aInput
  461. * @param string $sCustomMethod custom method to generate code for input
  462. * @return string
  463. */
  464. function genRowCustom(&$aInput, $sCustomMethod)
  465. {
  466. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  467. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  468. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  469. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  470. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  471. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  472. if (isset($aInput['name']))
  473. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  474. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  475. $sCaptionCode = '';
  476. if ($sCaption)
  477. $sCaptionCode = '<div class="bx-form-caption">' . $sCaption . $sRequired . '</div>';
  478. $sCode = <<<BLAH
  479. <div $sTrAttrs>
  480. $sCaptionCode
  481. <div class="bx-form-value$sClassAdd">
  482. <div class="bx-clear"></div>
  483. $sInput
  484. <div class="bx-clear"></div>
  485. </div>
  486. </div>
  487. BLAH;
  488. return $sCode;
  489. }
  490. /**
  491. * Generate Block Headers row
  492. *
  493. * @param array $aInput
  494. * @return string
  495. */
  496. function genRowBlockHeader(&$aInput)
  497. {
  498. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  499. // if there is no caption - show divider only
  500. if (empty($aInput['caption'])) {
  501. $sCode = $this->{$this->_sSectionClose}();
  502. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  503. return $sCode;
  504. }
  505. // if section is collapsed by default, add necessary code
  506. $sClassAddCollapsable = 'bx-form-collapsable';
  507. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  508. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  509. // display section with caption
  510. $sCode = $this->{$this->_sSectionClose}();
  511. if (empty($aAttrs))
  512. $aAttrs = array('class' => 'bx-form-collapsable ' . $sClassAddCollapsable);
  513. else
  514. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  515. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  516. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-h3">' . bx_process_output($aInput['caption']) . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  517. else
  518. $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>' . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  519. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  520. return $sCode;
  521. }
  522. function genBlockEnd()
  523. {
  524. $aNextTbodyAdd = false; // need to have some default
  525. $sCode = '';
  526. $sCode .= $this->{$this->_sSectionClose}();
  527. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  528. return $sCode;
  529. }
  530. /**
  531. * Generate HTML Input Element
  532. *
  533. * @param array $aInput
  534. * @return string Output HTML Code
  535. */
  536. function genInput(&$aInput)
  537. {
  538. if (!empty($aInput['name'])) {
  539. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  540. if (method_exists($this, $sCustomMethod))
  541. return $this->$sCustomMethod($aInput);
  542. }
  543. switch ($aInput['type']) {
  544. // standard inputs (and non-standard, interpreted as standard)
  545. case 'text':
  546. case 'datepicker':
  547. case 'date_time':
  548. case 'datetime':
  549. case 'number':
  550. case 'checkbox':
  551. case 'radio':
  552. case 'image':
  553. case 'password':
  554. case 'slider':
  555. case 'doublerange':
  556. case 'hidden':
  557. $sInput = $this->genInputStandard($aInput);
  558. break;
  559. case 'file':
  560. $sInput = $this->genInputFile($aInput);
  561. break;
  562. case 'rgb':
  563. case 'rgba':
  564. $sClass = 'bx-form-input-' . $aInput['type'];
  565. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  566. $aInput['type'] = 'text';
  567. $sInput = $this->genInputStandard($aInput);
  568. break;
  569. case 'switcher':
  570. $sInput = $this->genInputSwitcher($aInput);
  571. break;
  572. case 'button':
  573. case 'reset':
  574. case 'submit':
  575. $sInput = $this->genInputButton($aInput);
  576. break;
  577. case 'textarea':
  578. $sInput = $this->genInputTextarea($aInput);
  579. break;
  580. case 'select':
  581. $sInput = $this->genInputSelect($aInput);
  582. break;
  583. case 'select_multiple':
  584. $sInput = $this->genInputSelectMultiple($aInput);
  585. break;
  586. case 'checkbox_set':
  587. $sInput = $this->genInputCheckboxSet($aInput);
  588. break;
  589. case 'radio_set':
  590. $sInput = $this->genInputRadioSet($aInput);
  591. break;
  592. case 'input_set': // numeric array of inputs
  593. $sInput = '';
  594. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  595. foreach ($aInput as $iKey => $aSubInput) {
  596. if (!is_int($iKey) or !$aSubInput)
  597. continue; // parse only integer keys and existing values
  598. $sInput .= $this->genInput($aSubInput); // recursive call
  599. $sInput .= $sDivider;
  600. }
  601. $sInput .= '<div class="bx-clear"></div>';
  602. break;
  603. case 'custom':
  604. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  605. break;
  606. case 'captcha':
  607. $sInput = $this->genInputCaptcha($aInput);
  608. break;
  609. case 'location':
  610. $sInput = $this->genInputLocation($aInput);
  611. break;
  612. case 'value':
  613. $sInput = isset($aInput['value']) ? $aInput['value'] : '';
  614. break;
  615. default:
  616. //unknown control type
  617. $sInput = 'Unknown control type';
  618. }
  619. // create input label
  620. $sInput .= $this->genLabel($aInput);
  621. return $sInput;
  622. }
  623. /**
  624. * Generate new Input Element id
  625. *
  626. * @param array $aInput
  627. * @return string
  628. */
  629. function getInputId(&$aInput)
  630. {
  631. if (isset($aInput['id']))
  632. return $aInput['id'];
  633. $sName = md5($aInput['name']);
  634. $sID = $this->id . '_input_' . $sName;
  635. if ( // multiple elements cause identical id's
  636. (
  637. (
  638. $aInput['type'] == 'checkbox' and
  639. substr($aInput['name'], -2) == '[]' // it is multiple element
  640. ) or
  641. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  642. ) and
  643. isset($aInput['value']) // if we can make difference
  644. ) {
  645. $sValue = md5($aInput['value']);
  646. // add value
  647. $sID .= '_' . $sValue;
  648. }
  649. $sID = trim($sID, '_');
  650. $aInput['id'] = $sID; // just for repeated calls
  651. return $sID;
  652. }
  653. /**
  654. * Generate standard Input Element
  655. *
  656. * @param array $aInput
  657. * @return string
  658. */
  659. function genInputStandard(&$aInput)
  660. {
  661. // clone attributes for system use ;)
  662. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  663. // add default className to attributes
  664. $aAttrs['type'] = $aInput['type'];
  665. if ('datetime' == $aAttrs['type'])
  666. $aAttrs['type'] = 'date_time';
  667. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  668. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  669. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  670. // for inputs with labels generate id
  671. if (isset($aInput['label']))
  672. $aAttrs['id'] = $this->getInputId($aInput);
  673. // for checkboxes
  674. if (isset($aInput['checked']) and $aInput['checked'])
  675. $aAttrs['checked'] = 'checked';
  676. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  677. return "<input $sAttrs />\n";
  678. }
  679. /**
  680. * Generate Switcher Input Element (based on checkbox)
  681. *
  682. * @param array $aInput
  683. * @return string
  684. */
  685. function genInputSwitcher(&$aInput)
  686. {
  687. $aInput['type'] = 'checkbox';
  688. $sCheckbox = $this->genInputStandard($aInput);
  689. $aInput['type'] = 'switcher';
  690. $sClass = 'off';
  691. if (isset($aInput['checked']) and $aInput['checked'])
  692. $sClass = 'on';
  693. return '
  694. <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
  695. <div class="bx-switcher-canvas">
  696. <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
  697. <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
  698. <div class="bx-switcher-handler">&nbsp;</div>
  699. </div>
  700. </div>';
  701. }
  702. /**
  703. * Generate Button Input Element
  704. *
  705. * @param array $aInput
  706. * @return string
  707. */
  708. function genInputButton(&$aInput)
  709. {
  710. // clone attributes for system use ;)
  711. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  712. // add default className to attributes
  713. $aAttrs['type'] = $aInput['type'];
  714. if (isset($aInput['value']))
  715. $aAttrs['value'] = $aInput['value'];
  716. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  717. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
  718. return "<button $sAttrs>" . $aInput['value'] . "</button>\n";
  719. }
  720. /**
  721. * Generate Textarea Element
  722. *
  723. * @param array $aInput
  724. * @return string
  725. */
  726. function genInputTextarea(&$aInput)
  727. {
  728. // clone attributes for system use ;)
  729. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  730. $aAttrs['name'] = $aInput['name'];
  731. // for inputs with labels generate id
  732. if (isset($aInput['label']))
  733. $aAttrs['id'] = $this->getInputId($aInput);
  734. $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' : ''));
  735. $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value'])) : '';
  736. return "<textarea $sAttrs>$sValue</textarea>";
  737. }
  738. function addHtmlEditor($iViewMode, &$aInput)
  739. {
  740. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  741. if (!$oEditor)
  742. return false;
  743. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  744. return true;
  745. }
  746. /**
  747. * Generate Select Box Element
  748. *
  749. * @param array $aInput
  750. * @return string
  751. */
  752. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  753. {
  754. $aNewInput = $aInput;
  755. $aNewInput['type'] = 'select';
  756. $aNewInput['name'] .= '[]';
  757. $sInput = $this->genInput($aNewInput);
  758. return <<<BLAH
  759. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  760. $sInput
  761. </div>
  762. $sInfo
  763. $sError
  764. BLAH;
  765. }
  766. /**
  767. * Generate Browse File Element
  768. *
  769. * @param array $aInput
  770. * @return string
  771. */
  772. function genInputFile(&$aInput)
  773. {
  774. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  775. if(!empty($aInput['attrs']['onchange']))
  776. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  777. else
  778. $aInput['attrs']['onchange'] = $sOnChange;
  779. $sInput = $this->genInputStandard($aInput);
  780. return '<label class="bx-btn">' . $sInput . '<span class="bx-fif-label">' . _t('_sys_form_txt_select_file') . '</span></label><span class="bx-fif-value bx-def-margin-thd-left"></span>';
  781. }
  782. /**
  783. * Generate Select Box Element
  784. *
  785. * @param array $aInput
  786. * @return string
  787. */
  788. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  789. {
  790. $sUniqId = genRndPwd (8, false);
  791. $sUploaders = '';
  792. $oUploader = null;
  793. foreach ($aInput['uploaders'] as $sUploaderObject) {
  794. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  795. if (!$oUploader)
  796. continue;
  797. $sGhostTemplate = $this->genGhostTemplate($aInput);
  798. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  799. if (empty($aInput['attrs']['disabled']))
  800. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  801. $aParams = array(
  802. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  803. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  804. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  805. 'attrs' => bx_convert_array2attrs($aAttrs),
  806. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  807. );
  808. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  809. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  810. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams);
  811. }
  812. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  813. 'uploaders_buttons' => $sUploaders,
  814. 'info' => $sInfo,
  815. 'error' => $sError,
  816. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  817. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  818. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  819. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  820. ));
  821. }
  822. protected function genGhostTemplate(&$aInput)
  823. {
  824. $sGhostTemplate = false;
  825. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  826. $oFormNested = $aInput['ghost_template'];
  827. if ($oFormNested instanceof BxDolFormNested)
  828. $sGhostTemplate = $oFormNested->genForm();
  829. } 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
  830. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  831. $sGhostTemplate = $oFormNested->getCode();
  832. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  833. $sGhostTemplate = array ();
  834. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  835. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNested)
  836. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  837. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  838. $sGhostTemplate = $aInput['ghost_template'];
  839. }
  840. return $sGhostTemplate;
  841. }
  842. protected function genCustomInputUsernamesSuggestions ($aInput)
  843. {
  844. $this->_addJs(array(
  845. 'jquery.form.min.js',
  846. ));
  847. $sVals = '';
  848. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  849. foreach ($aInput['value'] as $sVal) {
  850. if (!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  851. continue;
  852. $sVals .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . $oProfile->getUnit(0, array('template' => 'unit_wo_info')) . $oProfile->getDisplayName() . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . $sVal . '" /></b>';
  853. }
  854. $sVals = trim($sVals, ',');
  855. }
  856. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  857. 'id' => $aInput['name'] . time() . mt_rand(0, 100),
  858. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  859. 'name' => $aInput['name'],
  860. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  861. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  862. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  863. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  864. 'vals' => $sVals,
  865. ));
  866. }
  867. /**
  868. * Generate Select Element
  869. *
  870. * @param array $aInput
  871. * @return string
  872. */
  873. function genInputSelect(&$aInput)
  874. {
  875. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  876. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  877. }
  878. /**
  879. * Generate Multiple Select Element
  880. *
  881. * @param array $aInput
  882. * @return string
  883. */
  884. function genInputSelectMultiple(&$aInput)
  885. {
  886. $aCurValues = array();
  887. if (isset($aInput['value']) && $aInput['value'])
  888. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  889. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  890. }
  891. /**
  892. * Generate Checkbox Set Element
  893. *
  894. * @param array $aInput
  895. * @return string
  896. */
  897. function genInputCheckboxSet(&$aInput)
  898. {
  899. $aCurValues = array();
  900. if (isset($aInput['value']) && $aInput['value'])
  901. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  902. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  903. }
  904. /**
  905. * Generate Radiobuttons Set Element
  906. *
  907. * @param array $aInput
  908. * @return string
  909. */
  910. function genInputRadioSet(&$aInput)
  911. {
  912. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  913. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  914. }
  915. function _isSelected ($sValue, $sCurValue)
  916. {
  917. return ((string)$sValue === (string)$sCurValue);
  918. }
  919. function _isSelectedMultiple ($sValue, $aCurValues)
  920. {
  921. return in_array($sValue, $aCurValues);
  922. }
  923. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  924. {
  925. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  926. $aAttrs['name'] = $aInput['name'];
  927. if ($isMultiple) {
  928. $aAttrs['name'] .= '[]';
  929. $aAttrs['multiple'] = 'multiple';
  930. }
  931. // for inputs with labels generate id
  932. if (isset($aInput['label']))
  933. $aAttrs['id'] = $this->getInputId($aInput);
  934. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  935. // generate options
  936. $sOptions = '';
  937. if (isset($aInput['values']) and is_array($aInput['values'])) {
  938. foreach ($aInput['values'] as $sValue => $sTitle) {
  939. $sClassC = $sStyleC = "";
  940. if(is_array($sTitle)) {
  941. if(isset($sTitle['type'])) {
  942. switch($sTitle['type']) {
  943. case 'group_header':
  944. $sTitle = bx_process_output($sTitle['value']);
  945. $sOptions .= <<<BLAH
  946. <optgroup label="$sTitle">
  947. BLAH;
  948. break;
  949. case 'group_end':
  950. $sOptions .= <<<BLAH
  951. </optgroup>
  952. BLAH;
  953. break;
  954. }
  955. continue;
  956. }
  957. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  958. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  959. $sValue = $sTitle['key'];
  960. $sTitle = $sTitle['value'];
  961. }
  962. $sValueC = bx_html_attribute($sValue);
  963. $sTitleC = bx_process_output($sTitle);
  964. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  965. $sOptions .= <<<BLAH
  966. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  967. BLAH;
  968. }
  969. }
  970. // generate element
  971. $sCode = <<<BLAH
  972. <select $sAttrs>
  973. $sOptions
  974. </select>
  975. BLAH;
  976. return $sCode;
  977. }
  978. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  979. {
  980. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  981. $aAttrs['name'] = $aInput['name'];
  982. // for inputs with labels generate id
  983. if (isset($aInput['label']))
  984. $aAttrs['id'] = $this->getInputId($aInput);
  985. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  986. // generate options
  987. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  988. $sOptions = '';
  989. if (isset($aInput['values']) and is_array($aInput['values'])) {
  990. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  991. $sDivider = $this->_sDividerAlt;
  992. // generate complex input using simple standard inputs
  993. foreach ($aInput['values'] as $sValue => $sLabel) {
  994. // create new simple input
  995. $aNewInput = array(
  996. 'type' => $sType,
  997. 'name' => $aInput['name'] . $sNameAppend,
  998. 'value' => $sValue,
  999. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1000. 'label' => $sLabel,
  1001. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1002. );
  1003. $sNewInput = $this->genInput($aNewInput);
  1004. // attach new input to complex
  1005. $sOptions .= ($sNewInput . $sDivider);
  1006. }
  1007. }
  1008. // generate element
  1009. $sCode = <<<BLAH
  1010. <div $sAttrs>
  1011. $sOptions
  1012. </div>
  1013. BLAH;
  1014. return $sCode;
  1015. }
  1016. function genInputLocation(&$aInput)
  1017. {
  1018. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  1019. $sIdStatus = $this->getInputId($aInput) . '_status';
  1020. $sIdInput = $this->getInputId($aInput) . '_location';
  1021. $sProto = (0 === strncmp('https', BX_DOL_URL_ROOT, 5)) ? 'https' : 'http';
  1022. $aVars = array (
  1023. 'key' => trim(getParam('sys_maps_api_key')),
  1024. 'lang' => bx_lang_name(),
  1025. 'name' => $aInput['name'],
  1026. 'id_status' => $sIdStatus,
  1027. 'id_input' => $sIdInput,
  1028. 'manual_input' => $isManualInput,
  1029. 'bx_if:manual_input' => array(
  1030. 'condition' => $isManualInput,
  1031. 'content' => array(),
  1032. ),
  1033. 'bx_if:auto_input' => array(
  1034. 'condition' => !$isManualInput,
  1035. 'content' => array(
  1036. 'id_status' => $sIdStatus,
  1037. 'location_string' => _t('_sys_location_undefined'),
  1038. ),
  1039. ),
  1040. );
  1041. $aLocationIndexes = array ('lat', 'lng', 'country', 'state', 'city', 'zip', 'street', 'street_number');
  1042. foreach ($aLocationIndexes as $sKey)
  1043. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey);
  1044. if ($aVars['country']) {
  1045. $aCountries = BxDolFormQuery::getDataItems('Country');
  1046. $s = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  1047. $aVars['bx_if:auto_input']['content']['location_string'] = $s;
  1048. }
  1049. if ($isManualInput) {
  1050. $aInput['type'] = 'text';
  1051. $aInput['attrs']['id'] = $sIdInput;
  1052. $aVars['input'] = $this->genInputStandard($aInput);
  1053. }
  1054. else {
  1055. if ($this->getLocationVal($aInput, 'lat') && $this->getLocationVal($aInput, 'lng'))
  1056. $aInput['checked'] = true;
  1057. else
  1058. $aInput['checked'] = $this->getCleanValue($aInput['name'] . '_lat') && $this->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  1059. $aVars['input'] = $this->genInputSwitcher($aInput);
  1060. }
  1061. return $this->oTemplate->parseHtmlByName('form_field_location.html', $aVars);
  1062. }
  1063. public function setLocationVal ($aInput, $sIndex, $sVal)
  1064. {
  1065. $s = $aInput['name'] . '_' . $sIndex;
  1066. $this->_aSpecificValues[$s] = $sVal;
  1067. }
  1068. protected function getLocationVal ($aInput, $sIndex)
  1069. {
  1070. $s = $aInput['name'] . '_' . $sIndex;
  1071. if (isset($this->_aSpecificValues[$s]))
  1072. return $this->_aSpecificValues[$s];
  1073. return $this->getCleanValue($s);
  1074. }
  1075. function genInputCaptcha(&$aInput)
  1076. {
  1077. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1078. // for inputs with labels generate id
  1079. if (isset($aInput['label']))
  1080. $aAttrs['id'] = $this->getInputId($aInput);
  1081. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1082. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1083. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1084. }
  1085. /**
  1086. * Generate Label Element
  1087. *
  1088. * @param string $sLabel Text of the Label
  1089. * @param string $sInputID Dependant Input Element ID
  1090. * @return string HTML code
  1091. */
  1092. function genLabel(&$aInput)
  1093. {
  1094. if (!isset($aInput['label']) or empty($aInput['label']))
  1095. return '';
  1096. $sLabel = $aInput['label'];
  1097. $sInputID = $this->getInputId($aInput);
  1098. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  1099. return $sRet;
  1100. }
  1101. function genInfoIcon($sInfo)
  1102. {
  1103. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1104. }
  1105. function genErrorIcon( $sError = '' )
  1106. {
  1107. if ($this->bEnableErrorIcon) {
  1108. $sStyle = '';
  1109. if (!$sError)
  1110. $sStyle = 'style="display:none;"';
  1111. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1112. }
  1113. }
  1114. function getOpenSection($aAttrs = array(), $sLegend = '')
  1115. {
  1116. if (!$this->_isSectionOpened) {
  1117. if (!$aAttrs || !is_array($aAttrs))
  1118. $aAttrs = array();
  1119. if ($sLegend)
  1120. $sClassesAdd = "bx-form-section-header";
  1121. else
  1122. $sClassesAdd = "bx-form-section-divider";
  1123. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1124. $this->_isSectionOpened = true;
  1125. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <div $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";
  1126. } else {
  1127. return '';
  1128. }
  1129. }
  1130. function getCloseSection()
  1131. {
  1132. if ($this->_isSectionOpened) {
  1133. $this->_isSectionOpened = false;
  1134. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1135. } else {
  1136. return '';
  1137. }
  1138. }
  1139. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1140. {
  1141. if (!$this->_isSectionOpened) {
  1142. if (!$aAttrs || !is_array($aAttrs))
  1143. $aAttrs = array();
  1144. if ($sLegend)
  1145. $sClassesAdd = "bx-form-section-header";
  1146. else
  1147. $sClassesAdd = "bx-form-section-divider";
  1148. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1149. $this->_isSectionOpened = true;
  1150. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <div $sAttrs> $sLegend <div class=\"bx-form-section-content bx-def-padding-top\">\n";
  1151. } else {
  1152. return '';
  1153. }
  1154. }
  1155. function getCloseSectionViewMode()
  1156. {
  1157. if ($this->_isSectionOpened) {
  1158. $this->_isSectionOpened = false;
  1159. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1160. } else {
  1161. return '';
  1162. }
  1163. }
  1164. function addCssJs ()
  1165. {
  1166. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1167. if (self::$_isCssJsAddedViewMode)
  1168. return;
  1169. $this->_addCss('forms.css');
  1170. self::$_isCssJsAddedViewMode = true;
  1171. } else {
  1172. if (self::$_isCssJsAdded)
  1173. return;
  1174. $aCss = array(
  1175. 'forms.css',
  1176. 'jquery-ui/jquery-ui.css',
  1177. 'timepicker-addon/jquery-ui-timepicker-addon.css',
  1178. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css',
  1179. );
  1180. $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);
  1181. $aCalendarLangs = array ('af' => 1, 'am' => 1, 'bg' => 1, 'ca' => 1, 'cs' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1, 'it' => 1, 'ja' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sr-RS' => 1, 'sr-YU' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-TW' => 1);
  1182. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1183. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1184. $aJs = array(
  1185. 'jquery-ui/jquery-ui.custom.min.js',
  1186. 'jquery.webForms.js',
  1187. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1188. 'jquery-minicolors/jquery.minicolors.min.js',
  1189. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1190. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1191. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1192. );
  1193. foreach ($this->aInputs as $aInput) {
  1194. if (!isset($aInput['type']) || 'files' != $aInput['type'] || !isset($aInput['uploaders']))
  1195. continue;
  1196. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1197. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], '', $this->oTemplate);
  1198. if ($oUploader)
  1199. $oUploader->addCssJs();
  1200. }
  1201. }
  1202. $this->_addJs($aJs);
  1203. $this->_addCss($aCss);
  1204. self::$_isCssJsAdded = true;
  1205. }
  1206. }
  1207. protected function _processCssJs()
  1208. {
  1209. $sRet = '';
  1210. if ($this->_bDynamicMode) {
  1211. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1212. $sJs = $this->oTemplate->addJs($this->_aJs, true);
  1213. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1214. $sRet .= "<script>
  1215. bx_get_scripts(" . json_encode($aMatches[1]) . ");
  1216. </script>";
  1217. }
  1218. else {
  1219. $this->oTemplate->addCss($this->_aCss);
  1220. $this->oTemplate->addJs($this->_aJs);
  1221. }
  1222. return $sRet;
  1223. }
  1224. protected function _addJs($mixed)
  1225. {
  1226. if (!is_array($mixed))
  1227. $mixed = array($mixed);
  1228. foreach ($mixed as $s)
  1229. $this->_aJs[$s] = $s;
  1230. }
  1231. protected function _addCss($mixed)
  1232. {
  1233. if (!is_array($mixed))
  1234. $mixed = array($mixed);
  1235. foreach ($mixed as $s)
  1236. $this->_aCss[$s] = $s;
  1237. }
  1238. }
  1239. /** @} */