BxBaseFormView.php 57 KB

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