BxBaseFormView.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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 ('doublerange' == $aAttrs['type'] && (!isset($aInput['value']) || !$aInput['value']) && !empty($aAttrs['min']) && !empty($aAttrs['max'])) {
  704. $aInput['value'] = $aAttrs['min'] . '-' . $aAttrs['max'];
  705. }
  706. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  707. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  708. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  709. // for inputs with labels generate id
  710. if (isset($aInput['label']))
  711. $aAttrs['id'] = $this->getInputId($aInput);
  712. // for checkboxes
  713. if (isset($aInput['checked']) and $aInput['checked'])
  714. $aAttrs['checked'] = 'checked';
  715. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  716. return "<input $sAttrs />\n";
  717. }
  718. /**
  719. * Generate Switcher Input Element (based on checkbox)
  720. *
  721. * @param array $aInput
  722. * @return string
  723. */
  724. function genInputSwitcher(&$aInput)
  725. {
  726. $aInput['type'] = 'checkbox';
  727. $sCheckbox = $this->genInputStandard($aInput);
  728. $aInput['type'] = 'switcher';
  729. $sClass = 'off';
  730. if (isset($aInput['checked']) and $aInput['checked'])
  731. $sClass = 'on';
  732. return '
  733. <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
  734. <div class="bx-switcher-canvas">
  735. <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
  736. <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
  737. <div class="bx-switcher-handler">&nbsp;</div>
  738. </div>
  739. </div>';
  740. }
  741. /**
  742. * Generate Button Input Element
  743. *
  744. * @param array $aInput
  745. * @return string
  746. */
  747. function genInputButton(&$aInput)
  748. {
  749. // clone attributes for system use ;)
  750. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  751. // add default className to attributes
  752. $aAttrs['type'] = $aInput['type'];
  753. if (isset($aInput['value']))
  754. $aAttrs['value'] = $aInput['value'];
  755. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  756. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
  757. return "<button $sAttrs>" . $aInput['value'] . "</button>\n";
  758. }
  759. /**
  760. * Generate Textarea Element
  761. *
  762. * @param array $aInput
  763. * @return string
  764. */
  765. function genInputTextarea(&$aInput)
  766. {
  767. // clone attributes for system use ;)
  768. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  769. $aAttrs['name'] = $aInput['name'];
  770. // for inputs with labels generate id
  771. if (isset($aInput['label']))
  772. $aAttrs['id'] = $this->getInputId($aInput);
  773. $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' : ''));
  774. $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value'])) : '';
  775. return "<textarea $sAttrs>$sValue</textarea>";
  776. }
  777. function isHtmlEditor($iViewMode, &$aInput)
  778. {
  779. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  780. }
  781. function addHtmlEditor($iViewMode, &$aInput)
  782. {
  783. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  784. if (!$oEditor)
  785. return false;
  786. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  787. return true;
  788. }
  789. /**
  790. * Generate Select Box Element
  791. *
  792. * @param array $aInput
  793. * @return string
  794. */
  795. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  796. {
  797. $aNewInput = $aInput;
  798. $aNewInput['type'] = 'select';
  799. $aNewInput['name'] .= '[]';
  800. $sInput = $this->genInput($aNewInput);
  801. return <<<BLAH
  802. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  803. $sInput
  804. </div>
  805. $sInfo
  806. $sError
  807. BLAH;
  808. }
  809. /**
  810. * Generate Browse File Element
  811. *
  812. * @param array $aInput
  813. * @return string
  814. */
  815. function genInputFile(&$aInput)
  816. {
  817. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  818. if(!empty($aInput['attrs']['onchange']))
  819. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  820. else
  821. $aInput['attrs']['onchange'] = $sOnChange;
  822. $sInput = $this->genInputStandard($aInput);
  823. 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>';
  824. }
  825. /**
  826. * Generate Select Box Element
  827. *
  828. * @param array $aInput
  829. * @return string
  830. */
  831. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  832. {
  833. $sUniqId = genRndPwd (8, false);
  834. $sUploaders = '';
  835. $oUploader = null;
  836. foreach ($aInput['uploaders'] as $sUploaderObject) {
  837. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  838. if (!$oUploader)
  839. continue;
  840. $sGhostTemplate = $this->genGhostTemplate($aInput);
  841. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  842. if (empty($aInput['attrs']['disabled']))
  843. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  844. $aParams = array(
  845. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  846. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  847. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  848. 'attrs' => bx_convert_array2attrs($aAttrs),
  849. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  850. );
  851. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  852. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  853. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams, $this->_bDynamicMode);
  854. }
  855. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  856. 'uploaders_buttons' => $sUploaders,
  857. 'info' => $sInfo,
  858. 'error' => $sError,
  859. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  860. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  861. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  862. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  863. ));
  864. }
  865. protected function genGhostTemplate(&$aInput)
  866. {
  867. $sGhostTemplate = false;
  868. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  869. $oFormNested = $aInput['ghost_template'];
  870. if ($oFormNested instanceof BxDolFormNested)
  871. $sGhostTemplate = $oFormNested->genForm();
  872. } 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
  873. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  874. $sGhostTemplate = $oFormNested->getCode();
  875. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  876. $sGhostTemplate = array ();
  877. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  878. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNested)
  879. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  880. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  881. $sGhostTemplate = $aInput['ghost_template'];
  882. }
  883. return $sGhostTemplate;
  884. }
  885. protected function genCustomInputUsernamesSuggestions ($aInput)
  886. {
  887. $this->addCssJsUi();
  888. $sVals = '';
  889. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  890. foreach ($aInput['value'] as $sVal) {
  891. if (!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  892. continue;
  893. $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>';
  894. }
  895. $sVals = trim($sVals, ',');
  896. }
  897. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  898. 'id' => $aInput['name'] . time() . mt_rand(0, 100),
  899. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  900. 'name' => $aInput['name'],
  901. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  902. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  903. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  904. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  905. 'vals' => $sVals,
  906. ));
  907. }
  908. protected function genCustomViewRowValueLabels ($aInput)
  909. {
  910. if (empty($aInput['value']) || !is_array($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  911. return null;
  912. $s = '';
  913. foreach ($aInput['value'] as $sLabel)
  914. $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>';
  915. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  916. 'values' => $s,
  917. ));
  918. }
  919. protected function genCustomInputLabels ($aInput)
  920. {
  921. // generate currently selected labels
  922. $sValue = '';
  923. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  924. sort($aInput['value'], SORT_LOCALE_STRING);
  925. foreach ($aInput['value'] as $sVal) {
  926. $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>';
  927. }
  928. $sValue = trim($sValue, ',');
  929. }
  930. // generate "following" labels
  931. $aFollowingLabels = bx_srv('bx_channels', 'get_following_channels_names', array(bx_get_logged_profile_id()));
  932. if (!is_array($aFollowingLabels))
  933. $aFollowingLabels = array();
  934. $aFollowingLabels = array_intersect($aFollowingLabels, $aInput['values']);
  935. $aFollowingLabels = array_diff($aFollowingLabels, !empty($aInput['value']) && is_array($aInput['value']) ? $aInput['value'] : array());
  936. $aValues = array_diff($aInput['values'],
  937. !empty($aInput['value']) && is_array($aInput['value']) ? $aInput['value'] : array(),
  938. !empty($aFollowingLabels) && is_array($aFollowingLabels) ? $aFollowingLabels : array());
  939. $sValues = '';
  940. if (!empty($aFollowingLabels)) {
  941. sort($aFollowingLabels, SORT_LOCALE_STRING);
  942. foreach ($aFollowingLabels as $sVal) {
  943. $sValues .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . trim($sVal) . '</b>';
  944. }
  945. if (!empty($aValues))
  946. $sValues .= '<a href="#" class="bx-form-input-autotoken-labels-show-more">' . _t('_sys_show_more') . '</a><i class="bx-form-input-autotoken-labels-more">';
  947. }
  948. // generate all possible labels
  949. if (!empty($aValues)) {
  950. sort($aValues, SORT_LOCALE_STRING);
  951. foreach ($aValues as $sVal) {
  952. $sValues .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . trim($sVal) . '</b>';
  953. }
  954. $sValues = trim($sValues, ',');
  955. }
  956. if (!empty($aFollowingLabels) && !empty($aValues))
  957. $sValues .= '</i>';
  958. return $this->oTemplate->parseHtmlByName('form_field_labels.html', array(
  959. 'id' => $aInput['name'] . time() . mt_rand(0, 100),
  960. 'name' => $aInput['name'],
  961. 'value' => $sValue,
  962. 'values' => $sValues,
  963. ));
  964. }
  965. /**
  966. * Generate Select Element
  967. *
  968. * @param array $aInput
  969. * @return string
  970. */
  971. function genInputSelect(&$aInput)
  972. {
  973. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  974. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  975. }
  976. /**
  977. * Generate Multiple Select Element
  978. *
  979. * @param array $aInput
  980. * @return string
  981. */
  982. function genInputSelectMultiple(&$aInput)
  983. {
  984. $aCurValues = array();
  985. if (isset($aInput['value']) && $aInput['value'])
  986. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  987. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  988. }
  989. /**
  990. * Generate Checkbox Set Element
  991. *
  992. * @param array $aInput
  993. * @return string
  994. */
  995. function genInputCheckboxSet(&$aInput)
  996. {
  997. $aCurValues = array();
  998. if (isset($aInput['value']) && $aInput['value'])
  999. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1000. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  1001. }
  1002. /**
  1003. * Generate Radiobuttons Set Element
  1004. *
  1005. * @param array $aInput
  1006. * @return string
  1007. */
  1008. function genInputRadioSet(&$aInput)
  1009. {
  1010. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1011. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  1012. }
  1013. function _isSelected ($sValue, $sCurValue)
  1014. {
  1015. return ((string)$sValue === (string)$sCurValue);
  1016. }
  1017. function _isSelectedMultiple ($sValue, $aCurValues)
  1018. {
  1019. return in_array($sValue, $aCurValues);
  1020. }
  1021. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  1022. {
  1023. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1024. $aAttrs['name'] = $aInput['name'];
  1025. if ($isMultiple) {
  1026. $aAttrs['name'] .= '[]';
  1027. $aAttrs['multiple'] = 'multiple';
  1028. }
  1029. // for inputs with labels generate id
  1030. if (isset($aInput['label']))
  1031. $aAttrs['id'] = $this->getInputId($aInput);
  1032. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  1033. // generate options
  1034. $sOptions = '';
  1035. if (isset($aInput['values']) and is_array($aInput['values'])) {
  1036. foreach ($aInput['values'] as $sValue => $sTitle) {
  1037. $sClassC = $sStyleC = "";
  1038. if(is_array($sTitle)) {
  1039. if(isset($sTitle['type'])) {
  1040. switch($sTitle['type']) {
  1041. case 'group_header':
  1042. $sTitle = bx_process_output($sTitle['value']);
  1043. $sOptions .= <<<BLAH
  1044. <optgroup label="$sTitle">
  1045. BLAH;
  1046. break;
  1047. case 'group_end':
  1048. $sOptions .= <<<BLAH
  1049. </optgroup>
  1050. BLAH;
  1051. break;
  1052. }
  1053. continue;
  1054. }
  1055. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  1056. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  1057. $sValue = $sTitle['key'];
  1058. $sTitle = $sTitle['value'];
  1059. }
  1060. $sValueC = bx_html_attribute($sValue);
  1061. $sTitleC = bx_process_output($sTitle);
  1062. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  1063. $sOptions .= <<<BLAH
  1064. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  1065. BLAH;
  1066. }
  1067. }
  1068. // generate element
  1069. $sCode = <<<BLAH
  1070. <select $sAttrs>
  1071. $sOptions
  1072. </select>
  1073. BLAH;
  1074. return $sCode;
  1075. }
  1076. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  1077. {
  1078. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  1079. $aAttrs['name'] = $aInput['name'];
  1080. // for inputs with labels generate id
  1081. if (isset($aInput['label']))
  1082. $aAttrs['id'] = $this->getInputId($aInput);
  1083. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1084. // generate options
  1085. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1086. $sOptions = '';
  1087. if (isset($aInput['values']) and is_array($aInput['values'])) {
  1088. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  1089. $sDivider = $this->_sDividerAlt;
  1090. // generate complex input using simple standard inputs
  1091. foreach ($aInput['values'] as $sValue => $sLabel) {
  1092. // create new simple input
  1093. $aNewInput = array(
  1094. 'type' => $sType,
  1095. 'name' => $aInput['name'] . $sNameAppend,
  1096. 'value' => $sValue,
  1097. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1098. 'label' => $sLabel,
  1099. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1100. );
  1101. $sNewInput = $this->genInput($aNewInput);
  1102. // attach new input to complex
  1103. $sOptions .= ($sNewInput . $sDivider);
  1104. }
  1105. }
  1106. // generate element
  1107. $sCode = <<<BLAH
  1108. <div $sAttrs>
  1109. $sOptions
  1110. </div>
  1111. BLAH;
  1112. return $sCode;
  1113. }
  1114. function genInputLocation(&$aInput)
  1115. {
  1116. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  1117. $sIdStatus = $this->getInputId($aInput) . '_status';
  1118. $sIdInput = $this->getInputId($aInput) . '_location';
  1119. $sProto = (0 === strncmp('https', BX_DOL_URL_ROOT, 5)) ? 'https' : 'http';
  1120. $aVars = array (
  1121. 'key' => trim(getParam('sys_maps_api_key')),
  1122. 'lang' => bx_lang_name(),
  1123. 'name' => $aInput['name'],
  1124. 'id_status' => $sIdStatus,
  1125. 'id_input' => $sIdInput,
  1126. 'manual_input' => $isManualInput,
  1127. 'bx_if:manual_input' => array(
  1128. 'condition' => $isManualInput,
  1129. 'content' => array(),
  1130. ),
  1131. 'bx_if:auto_input' => array(
  1132. 'condition' => !$isManualInput,
  1133. 'content' => array(
  1134. 'id_status' => $sIdStatus,
  1135. 'location_string' => _t('_sys_location_undefined'),
  1136. ),
  1137. ),
  1138. );
  1139. $aLocationIndexes = array ('lat', 'lng', 'country', 'state', 'city', 'zip', 'street', 'street_number');
  1140. foreach ($aLocationIndexes as $sKey)
  1141. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey);
  1142. if ($aVars['country']) {
  1143. $aCountries = BxDolFormQuery::getDataItems('Country');
  1144. $s = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  1145. $aVars['bx_if:auto_input']['content']['location_string'] = $s;
  1146. }
  1147. if ($isManualInput) {
  1148. $aInput['type'] = 'text';
  1149. $aInput['attrs']['id'] = $sIdInput;
  1150. $aVars['input'] = $this->genInputStandard($aInput);
  1151. }
  1152. else {
  1153. if ($this->getLocationVal($aInput, 'lat') && $this->getLocationVal($aInput, 'lng'))
  1154. $aInput['checked'] = true;
  1155. else
  1156. $aInput['checked'] = $this->getCleanValue($aInput['name'] . '_lat') && $this->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  1157. $aVars['input'] = $this->genInputSwitcher($aInput);
  1158. }
  1159. return $this->oTemplate->parseHtmlByName('form_field_location.html', $aVars);
  1160. }
  1161. public function setLocationVal ($aInput, $sIndex, $sVal)
  1162. {
  1163. $s = $aInput['name'] . '_' . $sIndex;
  1164. $this->_aSpecificValues[$s] = $sVal;
  1165. }
  1166. protected function getLocationVal ($aInput, $sIndex)
  1167. {
  1168. $s = $aInput['name'] . '_' . $sIndex;
  1169. if (isset($this->_aSpecificValues[$s]))
  1170. return $this->_aSpecificValues[$s];
  1171. return $this->getCleanValue($s);
  1172. }
  1173. function genInputCaptcha(&$aInput)
  1174. {
  1175. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1176. // for inputs with labels generate id
  1177. if (isset($aInput['label']))
  1178. $aAttrs['id'] = $this->getInputId($aInput);
  1179. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1180. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1181. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1182. }
  1183. /**
  1184. * Generate Label Element
  1185. *
  1186. * @param string $sLabel Text of the Label
  1187. * @param string $sInputID Dependant Input Element ID
  1188. * @return string HTML code
  1189. */
  1190. function genLabel(&$aInput)
  1191. {
  1192. if (!isset($aInput['label']) or empty($aInput['label']))
  1193. return '';
  1194. $sLabel = $aInput['label'];
  1195. $sInputID = $this->getInputId($aInput);
  1196. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  1197. return $sRet;
  1198. }
  1199. function genInfoIcon($sInfo)
  1200. {
  1201. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1202. }
  1203. function genErrorIcon( $sError = '' )
  1204. {
  1205. if ($this->bEnableErrorIcon) {
  1206. $sStyle = '';
  1207. if (!$sError)
  1208. $sStyle = 'style="display:none;"';
  1209. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1210. }
  1211. }
  1212. function getOpenSection($aAttrs = array(), $sLegend = '')
  1213. {
  1214. if (!$this->_isSectionOpened) {
  1215. if (!$aAttrs || !is_array($aAttrs))
  1216. $aAttrs = array();
  1217. if ($sLegend)
  1218. $sClassesAdd = "bx-form-section-header";
  1219. else
  1220. $sClassesAdd = "bx-form-section-divider";
  1221. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1222. $this->_isSectionOpened = true;
  1223. 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";
  1224. } else {
  1225. return '';
  1226. }
  1227. }
  1228. function getCloseSection()
  1229. {
  1230. if ($this->_isSectionOpened) {
  1231. $this->_isSectionOpened = false;
  1232. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1233. } else {
  1234. return '';
  1235. }
  1236. }
  1237. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1238. {
  1239. if (!$this->_isSectionOpened) {
  1240. if (!$aAttrs || !is_array($aAttrs))
  1241. $aAttrs = array();
  1242. if ($sLegend)
  1243. $sClassesAdd = "bx-form-section-header";
  1244. else
  1245. $sClassesAdd = "bx-form-section-divider";
  1246. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1247. $this->_isSectionOpened = true;
  1248. 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";
  1249. } else {
  1250. return '';
  1251. }
  1252. }
  1253. function getCloseSectionViewMode()
  1254. {
  1255. if ($this->_isSectionOpened) {
  1256. $this->_isSectionOpened = false;
  1257. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1258. } else {
  1259. return '';
  1260. }
  1261. }
  1262. static function getJsUiLangs ()
  1263. {
  1264. 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);
  1265. }
  1266. function addCssJsUi ()
  1267. {
  1268. if (self::$_isCssJsUiAdded)
  1269. return;
  1270. $aUiLangs = $this->getJsUiLangs ();
  1271. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1272. $this->_addJs(array(
  1273. 'jquery-ui/jquery-ui.custom.min.js',
  1274. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1275. ), "'undefined' === typeof($.datepicker)");
  1276. $this->_addCss('jquery-ui/jquery-ui.css');
  1277. self::$_isCssJsUiAdded = true;
  1278. }
  1279. function addCssJsTimepicker ()
  1280. {
  1281. if (self::$_isCssJsTimepickerAdded)
  1282. return;
  1283. $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);
  1284. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1285. $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
  1286. $this->_addJs(array(
  1287. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1288. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1289. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1290. 'jquery.ui.touch-punch.min.js',
  1291. ), "'undefined' === typeof($.fn.datetimepicker)");
  1292. self::$_isCssJsTimepickerAdded = true;
  1293. }
  1294. function addCssJsMinicolors ()
  1295. {
  1296. if (self::$_isCssJsMinicolorsAdded)
  1297. return;
  1298. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  1299. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  1300. self::$_isCssJsMinicolorsAdded = true;
  1301. }
  1302. function addCssJsViewMode ()
  1303. {
  1304. if (self::$_isCssJsAddedViewMode)
  1305. return;
  1306. $this->_addCss('forms.css');
  1307. self::$_isCssJsAddedViewMode = true;
  1308. }
  1309. function addCssJsCore ()
  1310. {
  1311. if (self::$_isCssJsAdded)
  1312. return;
  1313. $this->_addCss('forms.css');
  1314. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  1315. self::$_isCssJsAdded = true;
  1316. }
  1317. function addCssJs ()
  1318. {
  1319. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1320. $this->addCssJsViewMode ();
  1321. } else {
  1322. $this->addCssJsCore();
  1323. }
  1324. }
  1325. protected function _processCssJs()
  1326. {
  1327. $sRet = '';
  1328. if ($this->_bDynamicMode) {
  1329. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1330. $sRet .= "\n<script>\n";
  1331. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  1332. foreach ($this->_aJs as $sCondition => $aJs) {
  1333. $sJs = $this->oTemplate->addJs($aJs, true);
  1334. if (!$sJs)
  1335. continue;
  1336. $sRet .= "if ($sCondition)\n";
  1337. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1338. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  1339. }
  1340. $sRet .= "\n</script>\n";
  1341. }
  1342. else {
  1343. $this->oTemplate->addCss($this->_aCss);
  1344. $aJs = array();
  1345. foreach ($this->_aJs as $sCondition => $a)
  1346. $aJs = array_merge($aJs, $a);
  1347. $this->oTemplate->addJs($aJs);
  1348. }
  1349. return $sRet;
  1350. }
  1351. protected function _addJs($mixed, $sJsCondition)
  1352. {
  1353. if (!is_array($mixed))
  1354. $mixed = array($mixed);
  1355. $this->_aJs[$sJsCondition] = $mixed;
  1356. }
  1357. protected function _addCss($mixed)
  1358. {
  1359. if (!is_array($mixed))
  1360. $mixed = array($mixed);
  1361. foreach ($mixed as $s)
  1362. $this->_aCss[$s] = $s;
  1363. }
  1364. }
  1365. /** @} */