BxBaseFormView.php 56 KB

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