1
0

BxBaseFormView.php 58 KB

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