BxBaseFormView.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. /**
  885. * Generate Select Element
  886. *
  887. * @param array $aInput
  888. * @return string
  889. */
  890. function genInputSelect(&$aInput)
  891. {
  892. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  893. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  894. }
  895. /**
  896. * Generate Multiple Select Element
  897. *
  898. * @param array $aInput
  899. * @return string
  900. */
  901. function genInputSelectMultiple(&$aInput)
  902. {
  903. $aCurValues = array();
  904. if (isset($aInput['value']) && $aInput['value'])
  905. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  906. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  907. }
  908. /**
  909. * Generate Checkbox Set Element
  910. *
  911. * @param array $aInput
  912. * @return string
  913. */
  914. function genInputCheckboxSet(&$aInput)
  915. {
  916. $aCurValues = array();
  917. if (isset($aInput['value']) && $aInput['value'])
  918. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  919. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  920. }
  921. /**
  922. * Generate Radiobuttons Set Element
  923. *
  924. * @param array $aInput
  925. * @return string
  926. */
  927. function genInputRadioSet(&$aInput)
  928. {
  929. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  930. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  931. }
  932. function _isSelected ($sValue, $sCurValue)
  933. {
  934. return ((string)$sValue === (string)$sCurValue);
  935. }
  936. function _isSelectedMultiple ($sValue, $aCurValues)
  937. {
  938. return in_array($sValue, $aCurValues);
  939. }
  940. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  941. {
  942. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  943. $aAttrs['name'] = $aInput['name'];
  944. if ($isMultiple) {
  945. $aAttrs['name'] .= '[]';
  946. $aAttrs['multiple'] = 'multiple';
  947. }
  948. // for inputs with labels generate id
  949. if (isset($aInput['label']))
  950. $aAttrs['id'] = $this->getInputId($aInput);
  951. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  952. // generate options
  953. $sOptions = '';
  954. if (isset($aInput['values']) and is_array($aInput['values'])) {
  955. foreach ($aInput['values'] as $sValue => $sTitle) {
  956. $sClassC = $sStyleC = "";
  957. if(is_array($sTitle)) {
  958. if(isset($sTitle['type'])) {
  959. switch($sTitle['type']) {
  960. case 'group_header':
  961. $sTitle = bx_process_output($sTitle['value']);
  962. $sOptions .= <<<BLAH
  963. <optgroup label="$sTitle">
  964. BLAH;
  965. break;
  966. case 'group_end':
  967. $sOptions .= <<<BLAH
  968. </optgroup>
  969. BLAH;
  970. break;
  971. }
  972. continue;
  973. }
  974. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  975. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  976. $sValue = $sTitle['key'];
  977. $sTitle = $sTitle['value'];
  978. }
  979. $sValueC = bx_html_attribute($sValue);
  980. $sTitleC = bx_process_output($sTitle);
  981. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  982. $sOptions .= <<<BLAH
  983. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  984. BLAH;
  985. }
  986. }
  987. // generate element
  988. $sCode = <<<BLAH
  989. <select $sAttrs>
  990. $sOptions
  991. </select>
  992. BLAH;
  993. return $sCode;
  994. }
  995. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  996. {
  997. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  998. $aAttrs['name'] = $aInput['name'];
  999. // for inputs with labels generate id
  1000. if (isset($aInput['label']))
  1001. $aAttrs['id'] = $this->getInputId($aInput);
  1002. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1003. // generate options
  1004. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1005. $sOptions = '';
  1006. if (isset($aInput['values']) and is_array($aInput['values'])) {
  1007. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  1008. $sDivider = $this->_sDividerAlt;
  1009. // generate complex input using simple standard inputs
  1010. foreach ($aInput['values'] as $sValue => $sLabel) {
  1011. // create new simple input
  1012. $aNewInput = array(
  1013. 'type' => $sType,
  1014. 'name' => $aInput['name'] . $sNameAppend,
  1015. 'value' => $sValue,
  1016. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1017. 'label' => $sLabel,
  1018. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1019. );
  1020. $sNewInput = $this->genInput($aNewInput);
  1021. // attach new input to complex
  1022. $sOptions .= ($sNewInput . $sDivider);
  1023. }
  1024. }
  1025. // generate element
  1026. $sCode = <<<BLAH
  1027. <div $sAttrs>
  1028. $sOptions
  1029. </div>
  1030. BLAH;
  1031. return $sCode;
  1032. }
  1033. function genInputLocation(&$aInput)
  1034. {
  1035. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  1036. $sIdStatus = $this->getInputId($aInput) . '_status';
  1037. $sIdInput = $this->getInputId($aInput) . '_location';
  1038. $sProto = (0 === strncmp('https', BX_DOL_URL_ROOT, 5)) ? 'https' : 'http';
  1039. $aVars = array (
  1040. 'key' => trim(getParam('sys_maps_api_key')),
  1041. 'lang' => bx_lang_name(),
  1042. 'name' => $aInput['name'],
  1043. 'id_status' => $sIdStatus,
  1044. 'id_input' => $sIdInput,
  1045. 'manual_input' => $isManualInput,
  1046. 'bx_if:manual_input' => array(
  1047. 'condition' => $isManualInput,
  1048. 'content' => array(),
  1049. ),
  1050. 'bx_if:auto_input' => array(
  1051. 'condition' => !$isManualInput,
  1052. 'content' => array(
  1053. 'id_status' => $sIdStatus,
  1054. 'location_string' => _t('_sys_location_undefined'),
  1055. ),
  1056. ),
  1057. );
  1058. $aLocationIndexes = array ('lat', 'lng', 'country', 'state', 'city', 'zip', 'street', 'street_number');
  1059. foreach ($aLocationIndexes as $sKey)
  1060. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey);
  1061. if ($aVars['country']) {
  1062. $aCountries = BxDolFormQuery::getDataItems('Country');
  1063. $s = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  1064. $aVars['bx_if:auto_input']['content']['location_string'] = $s;
  1065. }
  1066. if ($isManualInput) {
  1067. $aInput['type'] = 'text';
  1068. $aInput['attrs']['id'] = $sIdInput;
  1069. $aVars['input'] = $this->genInputStandard($aInput);
  1070. }
  1071. else {
  1072. if ($this->getLocationVal($aInput, 'lat') && $this->getLocationVal($aInput, 'lng'))
  1073. $aInput['checked'] = true;
  1074. else
  1075. $aInput['checked'] = $this->getCleanValue($aInput['name'] . '_lat') && $this->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  1076. $aVars['input'] = $this->genInputSwitcher($aInput);
  1077. }
  1078. return $this->oTemplate->parseHtmlByName('form_field_location.html', $aVars);
  1079. }
  1080. public function setLocationVal ($aInput, $sIndex, $sVal)
  1081. {
  1082. $s = $aInput['name'] . '_' . $sIndex;
  1083. $this->_aSpecificValues[$s] = $sVal;
  1084. }
  1085. protected function getLocationVal ($aInput, $sIndex)
  1086. {
  1087. $s = $aInput['name'] . '_' . $sIndex;
  1088. if (isset($this->_aSpecificValues[$s]))
  1089. return $this->_aSpecificValues[$s];
  1090. return $this->getCleanValue($s);
  1091. }
  1092. function genInputCaptcha(&$aInput)
  1093. {
  1094. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1095. // for inputs with labels generate id
  1096. if (isset($aInput['label']))
  1097. $aAttrs['id'] = $this->getInputId($aInput);
  1098. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1099. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1100. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1101. }
  1102. /**
  1103. * Generate Label Element
  1104. *
  1105. * @param string $sLabel Text of the Label
  1106. * @param string $sInputID Dependant Input Element ID
  1107. * @return string HTML code
  1108. */
  1109. function genLabel(&$aInput)
  1110. {
  1111. if (!isset($aInput['label']) or empty($aInput['label']))
  1112. return '';
  1113. $sLabel = $aInput['label'];
  1114. $sInputID = $this->getInputId($aInput);
  1115. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  1116. return $sRet;
  1117. }
  1118. function genInfoIcon($sInfo)
  1119. {
  1120. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1121. }
  1122. function genErrorIcon( $sError = '' )
  1123. {
  1124. if ($this->bEnableErrorIcon) {
  1125. $sStyle = '';
  1126. if (!$sError)
  1127. $sStyle = 'style="display:none;"';
  1128. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1129. }
  1130. }
  1131. function getOpenSection($aAttrs = array(), $sLegend = '')
  1132. {
  1133. if (!$this->_isSectionOpened) {
  1134. if (!$aAttrs || !is_array($aAttrs))
  1135. $aAttrs = array();
  1136. if ($sLegend)
  1137. $sClassesAdd = "bx-form-section-header";
  1138. else
  1139. $sClassesAdd = "bx-form-section-divider";
  1140. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1141. $this->_isSectionOpened = true;
  1142. 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";
  1143. } else {
  1144. return '';
  1145. }
  1146. }
  1147. function getCloseSection()
  1148. {
  1149. if ($this->_isSectionOpened) {
  1150. $this->_isSectionOpened = false;
  1151. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1152. } else {
  1153. return '';
  1154. }
  1155. }
  1156. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1157. {
  1158. if (!$this->_isSectionOpened) {
  1159. if (!$aAttrs || !is_array($aAttrs))
  1160. $aAttrs = array();
  1161. if ($sLegend)
  1162. $sClassesAdd = "bx-form-section-header";
  1163. else
  1164. $sClassesAdd = "bx-form-section-divider";
  1165. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1166. $this->_isSectionOpened = true;
  1167. 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";
  1168. } else {
  1169. return '';
  1170. }
  1171. }
  1172. function getCloseSectionViewMode()
  1173. {
  1174. if ($this->_isSectionOpened) {
  1175. $this->_isSectionOpened = false;
  1176. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1177. } else {
  1178. return '';
  1179. }
  1180. }
  1181. function addCssJsTimepicker ()
  1182. {
  1183. if (self::$_isCssJsTimepickerAdded)
  1184. return;
  1185. $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);
  1186. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1187. $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
  1188. $this->_addJs(array(
  1189. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1190. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1191. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1192. 'jquery.ui.touch-punch.min.js',
  1193. ), "'undefined' === typeof($.fn.datetimepicker)");
  1194. self::$_isCssJsTimepickerAdded = true;
  1195. }
  1196. function addCssJsUi ()
  1197. {
  1198. if (self::$_isCssJsUiAdded)
  1199. return;
  1200. $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);
  1201. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1202. $this->_addCss('jquery-ui/jquery-ui.css');
  1203. $this->_addJs(array(
  1204. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1205. 'jquery-ui/jquery-ui.custom.min.js',
  1206. ), "'undefined' === typeof($.datepicker)");
  1207. self::$_isCssJsUiAdded = true;
  1208. }
  1209. function addCssJsMinicolors ()
  1210. {
  1211. if (self::$_isCssJsMinicolorsAdded)
  1212. return;
  1213. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  1214. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  1215. self::$_isCssJsMinicolorsAdded = true;
  1216. }
  1217. function addCssJsViewMode ()
  1218. {
  1219. if (self::$_isCssJsAddedViewMode)
  1220. return;
  1221. $this->_addCss('forms.css');
  1222. self::$_isCssJsAddedViewMode = true;
  1223. }
  1224. function addCssJsCore ()
  1225. {
  1226. if (self::$_isCssJsAdded)
  1227. return;
  1228. $this->_addCss('forms.css');
  1229. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  1230. self::$_isCssJsAdded = true;
  1231. }
  1232. function addCssJs ()
  1233. {
  1234. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1235. $this->addCssJsViewMode ();
  1236. } else {
  1237. $this->addCssJsCore();
  1238. }
  1239. }
  1240. protected function _processCssJs()
  1241. {
  1242. $sRet = '';
  1243. if ($this->_bDynamicMode) {
  1244. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1245. $sRet .= "\n<script>\n";
  1246. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  1247. foreach ($this->_aJs as $sCondition => $aJs) {
  1248. $sJs = $this->oTemplate->addJs($aJs, true);
  1249. if (!$sJs)
  1250. continue;
  1251. $sRet .= "if ($sCondition)\n";
  1252. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1253. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  1254. }
  1255. $sRet .= "\n</script>\n";
  1256. }
  1257. else {
  1258. $this->oTemplate->addCss($this->_aCss);
  1259. $aJs = array();
  1260. foreach ($this->_aJs as $sCondition => $a)
  1261. $aJs = array_merge($aJs, $a);
  1262. $this->oTemplate->addJs($aJs);
  1263. }
  1264. return $sRet;
  1265. }
  1266. protected function _addJs($mixed, $sJsCondition)
  1267. {
  1268. if (!is_array($mixed))
  1269. $mixed = array($mixed);
  1270. $this->_aJs[$sJsCondition] = $mixed;
  1271. }
  1272. protected function _addCss($mixed)
  1273. {
  1274. if (!is_array($mixed))
  1275. $mixed = array($mixed);
  1276. foreach ($mixed as $s)
  1277. $this->_aCss[$s] = $s;
  1278. }
  1279. }
  1280. /** @} */