1
0

BxBaseFormView.php 53 KB

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