BxBaseFormView.php 59 KB

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