BxBaseFormView.php 55 KB

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