1
0

BxBaseFormView.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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 $_isCssJsAddedViewMode = false;
  14. /**
  15. * Enable or disable error message displaying
  16. */
  17. protected $bEnableErrorIcon = true;
  18. /**
  19. * HTML Code of this form
  20. */
  21. protected $sCode;
  22. /**
  23. * Code which will be added to the beginning of the form.
  24. * For example, hidden inputs.
  25. * For internal use only
  26. */
  27. protected $_sCodeAdd = '';
  28. /**
  29. * for internal use only
  30. */
  31. protected $_isSectionOpened = false;
  32. /**
  33. * Default divider for several inputs
  34. */
  35. protected $_sDivider = '<span class="bx-def-margin-left"></span>';
  36. /**
  37. * Alternative divider for several inputs
  38. */
  39. protected $_sDividerAlt = '<br />';
  40. /**
  41. * Form is added dynamically.
  42. */
  43. protected $_bDynamicMode = false;
  44. /**
  45. * Form is submitted dynamically (using Ajax Submit).
  46. */
  47. protected $_bAjaxMode = false;
  48. /**
  49. * Form is displayed in view mode.
  50. */
  51. protected $_bViewMode = false;
  52. /**
  53. * Function name for generation close form section HTML.
  54. */
  55. protected $_sSectionClose = 'getCloseSection';
  56. /**
  57. * Function name for generation open form section HTML.
  58. */
  59. protected $_sSectionOpen = 'getOpenSection';
  60. /**
  61. * JS files list for form
  62. */
  63. protected $_aJs = array();
  64. /**
  65. * CSS files list for form
  66. */
  67. protected $_aCss = array();
  68. /**
  69. * Constructor
  70. *
  71. * @param array $aInfo Form contents
  72. *
  73. * $aInfo['params'] = array(
  74. * 'remove_form' => true|false,
  75. * );
  76. *
  77. * @return BxBaseFormView
  78. */
  79. function __construct($aInfo, $oTemplate)
  80. {
  81. parent::__construct($aInfo, $oTemplate);
  82. if(isset($this->aParams['ajax_mode']))
  83. $this->setAjaxMode($this->aParams['ajax_mode']);
  84. $mixedAjaxMode = bx_get('ajax_mode');
  85. if($mixedAjaxMode !== false)
  86. $this->setAjaxMode($mixedAjaxMode);
  87. $this->_bViewMode = isset($this->aParams['view_mode']) && $this->aParams['view_mode'];
  88. if ($this->_bViewMode) {
  89. $this->_sSectionClose = 'getCloseSectionViewMode';
  90. $this->_sSectionOpen = 'getOpenSectionViewMode';
  91. }
  92. }
  93. function setAjaxMode($bAjaxMode)
  94. {
  95. $this->_bAjaxMode = (bool)$bAjaxMode;
  96. }
  97. function isAjaxMode()
  98. {
  99. return $this->_bAjaxMode;
  100. }
  101. /**
  102. * Return Form code
  103. * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup.
  104. * @return string
  105. */
  106. function getCode($bDynamicMode = false)
  107. {
  108. if (!$bDynamicMode && bx_is_dynamic_request())
  109. $bDynamicMode = true;
  110. $this->_bDynamicMode = $bDynamicMode;
  111. $this->addCssJs ();
  112. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  113. $sDynamicCssJs = $this->_processCssJs();
  114. return $sDynamicCssJs . ($this->sCode = $this->genForm());
  115. }
  116. /**
  117. * Generate the whole form
  118. *
  119. * @return string
  120. */
  121. function genForm()
  122. {
  123. $this->_sCodeAdd = '';
  124. $sTable = $this->genRows();
  125. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  126. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  127. if (!empty($this->aParams['remove_form']) || (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])) {
  128. $sForm = <<<BLAH
  129. $sHtmlBefore
  130. {$this->_sCodeAdd}
  131. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  132. $sTable
  133. </div>
  134. $sHtmlAfter
  135. BLAH;
  136. } else {
  137. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  138. $sAjaxFormJs = '';
  139. if($this->_bAjaxMode)
  140. $sAjaxFormJs = <<<BLAH
  141. $("form#{$this->id}").ajaxForm({
  142. dataType: "json",
  143. beforeSubmit: function (formData, jqForm, options) {
  144. bx_loading($("form#{$this->id}"), true);
  145. },
  146. success: function (oData) {
  147. bx_loading($("form#{$this->id}"), false);
  148. processJsonData(oData);
  149. }
  150. });
  151. BLAH;
  152. $sForm = <<<BLAH
  153. $sHtmlBefore
  154. <form $sFormAttrs>
  155. {$this->_sCodeAdd}
  156. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  157. $sTable
  158. </div>
  159. </form>
  160. <script>
  161. $(document).ready(function() {
  162. $(this).addWebForms();
  163. });
  164. $sAjaxFormJs
  165. </script>
  166. $sHtmlAfter
  167. BLAH;
  168. }
  169. return $sForm;
  170. }
  171. /**
  172. * Generate Table HTML code
  173. *
  174. * @return string
  175. */
  176. function genRows()
  177. {
  178. // add CSRF token if it's needed.
  179. 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) {
  180. $this->aInputs['csrf_token'] = array(
  181. 'type' => 'hidden',
  182. 'name' => 'csrf_token',
  183. 'value' => $mixedCsrfToken,
  184. 'db' => array ('pass' => 'Xss'),
  185. 'visible_for_levels' => PHP_INT_MAX,
  186. );
  187. }
  188. // add 'Ajax Mode' flag
  189. if($this->_bAjaxMode)
  190. $this->aInputs['ajax_mode'] = array(
  191. 'type' => 'hidden',
  192. 'name' => 'ajax_mode',
  193. 'value' => 1,
  194. 'db' => array ('pass' => 'Int'),
  195. 'visible_for_levels' => PHP_INT_MAX,
  196. );
  197. // check if we need to generate open section clause
  198. $sOpenSection = '';
  199. foreach ($this->aInputs as $aInput) {
  200. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  201. continue;
  202. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  203. $sOpenSection = $this->{$this->_sSectionOpen}();
  204. break;
  205. }
  206. // generate rows contents
  207. $sCont = '';
  208. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  209. foreach ($this->aInputs as $aInput)
  210. if (!isset($aInput['visible_for_levels']) || self::isVisible($aInput))
  211. $sCont .= $this->$sFuncGenRow($aInput);
  212. $sCloseSection = $this->{$this->_sSectionClose}();
  213. return $sOpenSection . $sCont . $sCloseSection;
  214. }
  215. /**
  216. * Generate single Table Row
  217. *
  218. * @param array $aInput
  219. * @return string
  220. */
  221. function genRow(&$aInput)
  222. {
  223. if (!isset($aInput['type']))
  224. $aInput['type'] = false;
  225. if (!empty($aInput['name'])) {
  226. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  227. if (method_exists($this, $sCustomMethod))
  228. return $this->$sCustomMethod($aInput);
  229. }
  230. switch ($aInput['type']) {
  231. case 'block_header':
  232. $sRow = $this->genRowBlockHeader($aInput);
  233. break;
  234. case 'block_end':
  235. $sRow = $this->genBlockEnd($aInput);
  236. break;
  237. case 'hidden':
  238. // do not generate row for hidden inputs
  239. $sRow = '';
  240. $this->_sCodeAdd .= $this->genInput($aInput);
  241. break;
  242. case 'select_box':
  243. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  244. break;
  245. case 'files':
  246. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  247. break;
  248. case 'switcher':
  249. case 'checkbox':
  250. $sRow = $this->genRowStandard($aInput, true);
  251. break;
  252. default:
  253. $sRow = $this->genRowStandard($aInput);
  254. }
  255. return $sRow;
  256. }
  257. /**
  258. * Generate single Table Row for view mode
  259. *
  260. * @param array $aInput
  261. * @return string
  262. */
  263. function genViewRow(&$aInput)
  264. {
  265. if (!isset($aInput['type']))
  266. $aInput['type'] = false;
  267. if (!empty($aInput['name'])) {
  268. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  269. if (method_exists($this, $sCustomMethod))
  270. return $this->$sCustomMethod($aInput);
  271. }
  272. switch ($aInput['type']) {
  273. case 'block_header':
  274. $sRow = $this->genRowBlockHeader($aInput);
  275. break;
  276. case 'block_end':
  277. $sRow = $this->genBlockEnd($aInput);
  278. break;
  279. default:
  280. $sRow = $this->genViewRowWrapped($aInput);
  281. }
  282. return $sRow;
  283. }
  284. /**
  285. * Generate complete wrapped row for view mode form
  286. *
  287. * @param array $aInput
  288. * @return string
  289. */
  290. function genViewRowWrapped(&$aInput)
  291. {
  292. $sValue = $this->genViewRowValue($aInput);
  293. if (null === $sValue)
  294. return '';
  295. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  296. return <<<EOS
  297. <div class="bx-form-row-view-wrapper bx-form-row-view-wrapper-{$aInput['type']} bx-def-padding-sec-top">
  298. <div class="bx-form-row-view-caption">{$sCaption}:</div>
  299. <div class="bx-form-row-view-value">{$sValue}</div>
  300. </div>
  301. EOS;
  302. }
  303. /**
  304. * Generate value for view mode row
  305. *
  306. * @param array $aInput
  307. * @return string
  308. */
  309. function genViewRowValue(&$aInput)
  310. {
  311. if (!empty($aInput['name'])) {
  312. $sCustomMethod = 'genCustomViewRowValue' . $this->_genMethodName($aInput['name']);
  313. if (method_exists($this, $sCustomMethod))
  314. return $this->$sCustomMethod($aInput);
  315. }
  316. switch ($aInput['type']) {
  317. case 'hidden':
  318. $sValue = null;
  319. break;
  320. case 'select':
  321. case 'radio_set':
  322. $sValue = $this->genViewRowValueForSelect($aInput);
  323. break;
  324. case 'datepicker':
  325. $sValue = null;
  326. if (empty($aInput['value']) || !$aInput['value'] || '0000-00-00' == $aInput['value'])
  327. break;
  328. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  329. break;
  330. case 'date_time':
  331. case 'datetime':
  332. $sValue = null;
  333. if(empty($aInput['value']) || !$aInput['value'] || '0000-00-00 00:00:00' == $aInput['value'] || '0000-00-00 00:00' == $aInput['value'])
  334. break;
  335. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  336. break;
  337. case 'checkbox_set':
  338. case 'select_multiple':
  339. $sValue = null;
  340. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  341. $sValue = '';
  342. foreach ($aInput['value'] as $sVal)
  343. $sValue .= $aInput['values'][$sVal] . ', ';
  344. $sValue = trim ($sValue, ', ');
  345. }
  346. break;
  347. case 'checkbox':
  348. case 'switcher':
  349. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  350. break;
  351. case 'textarea':
  352. if (isset($aInput['value']) && '' !== $aInput['value'])
  353. $sValue = (isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : bx_process_output($aInput['value'], BX_DATA_TEXT_MULTILINE);
  354. else
  355. $sValue = null;
  356. break;
  357. case 'text':
  358. if(isset($aInput['value']) && '' !== $aInput['value'])
  359. $sValue = bx_linkify(bx_process_output($aInput['value']));
  360. else
  361. $sValue = null;
  362. break;
  363. default:
  364. $sValue = isset($aInput['value']) && '' !== $aInput['value'] ? bx_process_output($aInput['value']) : null;
  365. }
  366. return $sValue;
  367. }
  368. function genViewRowValueForSelect(&$aInput)
  369. {
  370. if (!isset($aInput['value']) || !$aInput['value'])
  371. return null;
  372. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  373. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])))
  374. return $oCategory->getCategoryLink($s, $aInput['value']);
  375. return $s;
  376. }
  377. protected function genCustomRowBirthday(&$aInput)
  378. {
  379. if(!$this->_bViewMode)
  380. return $this->genRowStandard($aInput);
  381. $aInput['caption_src'] = '_sys_form_input_age';
  382. $aInput['caption'] = _t($aInput['caption_src']);
  383. return $this->genViewRowWrapped($aInput);
  384. }
  385. protected function genCustomViewRowValueBirthday(&$aInput)
  386. {
  387. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00')))
  388. return null;
  389. $sValue = $aInput['value'];
  390. $iPosSpace = strpos($sValue, ' ');
  391. if($iPosSpace !== false)
  392. $sValue = trim(substr($sValue, 0, $iPosSpace));
  393. $aDate = explode('-', $sValue);
  394. $iCdYear = (int)date('Y');
  395. $iCdMonth = (int)date('n');
  396. $iCdDay = (int)date('j');
  397. $iResult = $iCdYear - (int)$aDate[0];
  398. if($iCdMonth < (int)$aDate[1] || ($iCdMonth == (int)$aDate[1] && $iCdDay < (int)$aDate[2]))
  399. $iResult -= 1;
  400. return $iResult;
  401. }
  402. /**
  403. * Generate standard row
  404. *
  405. * @param array $aInput
  406. * @return string
  407. */
  408. function genRowStandard(&$aInput, $isOneLine = false)
  409. {
  410. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  411. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  412. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  413. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  414. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  415. $sClassWrapper = 'bx-form-element-wrapper';
  416. if($isOneLine)
  417. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  418. $sClassWrapper .= ' bx-def-margin-top';
  419. if (isset($aInput['name']))
  420. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  421. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  422. $sClassOneLineCaption = '';
  423. $sClassOneLineValue = '';
  424. if ($isOneLine) {
  425. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  426. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  427. $aInput['attrs']['id'] = $this->getInputId($aInput);
  428. if ($sCaption)
  429. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  430. }
  431. $sInput = $this->genInput($aInput);
  432. $sCaptionCode = '';
  433. if ($sCaption)
  434. $sCaptionCode = '<div class="bx-form-caption' . $sClassOneLineCaption . '">' . $sCaption . $sRequired . '</div>';
  435. else
  436. $sInput .= $sRequired;
  437. $sInputCode = $this->genWrapperInput($aInput, $sInput);
  438. if (empty($sInputCodeExtra)) $sInputCodeExtra = '';
  439. if (empty($sInfoIcon)) $sInfoIcon = '';
  440. if (empty($sInputCode)) $sInputCode = '';
  441. if (empty($sErrorIcon)) $sErrorIcon = '';
  442. $sValueCode = '
  443. <div class="bx-form-value bx-clearfix' . $sClassAdd . $sClassOneLineValue . '">
  444. ' . $sInputCode . '
  445. ' . ($isOneLine ? '' : $sInfoIcon . $sErrorIcon) . '
  446. ' . $sInputCodeExtra . '
  447. </div>';
  448. if ($isOneLine)
  449. $sCode = $sValueCode . $sCaptionCode . '<div class="bx-clear"></div>' . $sInfoIcon . $sErrorIcon;
  450. else
  451. $sCode = $sCaptionCode . $sValueCode;
  452. return "<div $sTrAttrs>" . $sCode . "</div>";
  453. }
  454. function genWrapperInput($aInput, $sContent)
  455. {
  456. $sAttr = isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? bx_convert_array2attrs($aInput['attrs_wrapper']) : '';
  457. 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>";
  458. }
  459. /**
  460. * Generate custom row
  461. *
  462. * @param array $aInput
  463. * @param string $sCustomMethod custom method to generate code for input
  464. * @return string
  465. */
  466. function genRowCustom(&$aInput, $sCustomMethod)
  467. {
  468. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  469. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span> ' : '';
  470. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  471. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  472. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  473. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  474. if (isset($aInput['name']))
  475. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  476. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  477. $sCaptionCode = '';
  478. if ($sCaption)
  479. $sCaptionCode = '<div class="bx-form-caption">' . $sCaption . $sRequired . '</div>';
  480. $sCode = <<<BLAH
  481. <div $sTrAttrs>
  482. $sCaptionCode
  483. <div class="bx-form-value$sClassAdd">
  484. <div class="bx-clear"></div>
  485. $sInput
  486. <div class="bx-clear"></div>
  487. </div>
  488. </div>
  489. BLAH;
  490. return $sCode;
  491. }
  492. /**
  493. * Generate Block Headers row
  494. *
  495. * @param array $aInput
  496. * @return string
  497. */
  498. function genRowBlockHeader(&$aInput)
  499. {
  500. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  501. // if there is no caption - show divider only
  502. if (empty($aInput['caption'])) {
  503. $sCode = $this->{$this->_sSectionClose}();
  504. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  505. return $sCode;
  506. }
  507. // if section is collapsed by default, add necessary code
  508. $sClassAddCollapsable = 'bx-form-collapsable';
  509. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  510. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  511. // display section with caption
  512. $sCode = $this->{$this->_sSectionClose}();
  513. if (empty($aAttrs))
  514. $aAttrs = array('class' => 'bx-form-collapsable ' . $sClassAddCollapsable);
  515. else
  516. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  517. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  518. $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>';
  519. else
  520. $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>';
  521. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  522. return $sCode;
  523. }
  524. function genBlockEnd()
  525. {
  526. $aNextTbodyAdd = false; // need to have some default
  527. $sCode = '';
  528. $sCode .= $this->{$this->_sSectionClose}();
  529. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  530. return $sCode;
  531. }
  532. /**
  533. * Generate HTML Input Element
  534. *
  535. * @param array $aInput
  536. * @return string Output HTML Code
  537. */
  538. function genInput(&$aInput)
  539. {
  540. if (!empty($aInput['name'])) {
  541. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  542. if (method_exists($this, $sCustomMethod))
  543. return $this->$sCustomMethod($aInput);
  544. }
  545. switch ($aInput['type']) {
  546. // standard inputs (and non-standard, interpreted as standard)
  547. case 'text':
  548. case 'datepicker':
  549. case 'date_time':
  550. case 'datetime':
  551. case 'number':
  552. case 'checkbox':
  553. case 'radio':
  554. case 'image':
  555. case 'password':
  556. case 'slider':
  557. case 'doublerange':
  558. case 'hidden':
  559. $sInput = $this->genInputStandard($aInput);
  560. break;
  561. case 'file':
  562. $sInput = $this->genInputFile($aInput);
  563. break;
  564. case 'rgb':
  565. case 'rgba':
  566. $sClass = 'bx-form-input-' . $aInput['type'];
  567. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  568. $aInput['type'] = 'text';
  569. $sInput = $this->genInputStandard($aInput);
  570. break;
  571. case 'switcher':
  572. $sInput = $this->genInputSwitcher($aInput);
  573. break;
  574. case 'button':
  575. case 'reset':
  576. case 'submit':
  577. $sInput = $this->genInputButton($aInput);
  578. break;
  579. case 'textarea':
  580. $sInput = $this->genInputTextarea($aInput);
  581. break;
  582. case 'select':
  583. $sInput = $this->genInputSelect($aInput);
  584. break;
  585. case 'select_multiple':
  586. $sInput = $this->genInputSelectMultiple($aInput);
  587. break;
  588. case 'checkbox_set':
  589. $sInput = $this->genInputCheckboxSet($aInput);
  590. break;
  591. case 'radio_set':
  592. $sInput = $this->genInputRadioSet($aInput);
  593. break;
  594. case 'input_set': // numeric array of inputs
  595. $sInput = '';
  596. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  597. foreach ($aInput as $iKey => $aSubInput) {
  598. if (!is_int($iKey) or !$aSubInput)
  599. continue; // parse only integer keys and existing values
  600. $sInput .= $this->genInput($aSubInput); // recursive call
  601. $sInput .= $sDivider;
  602. }
  603. $sInput .= '<div class="bx-clear"></div>';
  604. break;
  605. case 'custom':
  606. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  607. break;
  608. case 'captcha':
  609. $sInput = $this->genInputCaptcha($aInput);
  610. break;
  611. case 'location':
  612. $sInput = $this->genInputLocation($aInput);
  613. break;
  614. case 'value':
  615. $sInput = isset($aInput['value']) ? $aInput['value'] : '';
  616. break;
  617. default:
  618. //unknown control type
  619. $sInput = 'Unknown control type';
  620. }
  621. // create input label
  622. $sInput .= $this->genLabel($aInput);
  623. return $sInput;
  624. }
  625. /**
  626. * Generate new Input Element id
  627. *
  628. * @param array $aInput
  629. * @return string
  630. */
  631. function getInputId(&$aInput)
  632. {
  633. if (isset($aInput['id']))
  634. return $aInput['id'];
  635. $sName = md5($aInput['name']);
  636. $sID = $this->id . '_input_' . $sName;
  637. if ( // multiple elements cause identical id's
  638. (
  639. (
  640. $aInput['type'] == 'checkbox' and
  641. substr($aInput['name'], -2) == '[]' // it is multiple element
  642. ) or
  643. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  644. ) and
  645. isset($aInput['value']) // if we can make difference
  646. ) {
  647. $sValue = md5($aInput['value']);
  648. // add value
  649. $sID .= '_' . $sValue;
  650. }
  651. $sID = trim($sID, '_');
  652. $aInput['id'] = $sID; // just for repeated calls
  653. return $sID;
  654. }
  655. /**
  656. * Generate standard Input Element
  657. *
  658. * @param array $aInput
  659. * @return string
  660. */
  661. function genInputStandard(&$aInput)
  662. {
  663. // clone attributes for system use ;)
  664. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  665. // add default className to attributes
  666. $aAttrs['type'] = $aInput['type'];
  667. if ('datetime' == $aAttrs['type'])
  668. $aAttrs['type'] = 'date_time';
  669. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  670. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  671. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  672. // for inputs with labels generate id
  673. if (isset($aInput['label']))
  674. $aAttrs['id'] = $this->getInputId($aInput);
  675. // for checkboxes
  676. if (isset($aInput['checked']) and $aInput['checked'])
  677. $aAttrs['checked'] = 'checked';
  678. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  679. return "<input $sAttrs />\n";
  680. }
  681. /**
  682. * Generate Switcher Input Element (based on checkbox)
  683. *
  684. * @param array $aInput
  685. * @return string
  686. */
  687. function genInputSwitcher(&$aInput)
  688. {
  689. $aInput['type'] = 'checkbox';
  690. $sCheckbox = $this->genInputStandard($aInput);
  691. $aInput['type'] = 'switcher';
  692. $sClass = 'off';
  693. if (isset($aInput['checked']) and $aInput['checked'])
  694. $sClass = 'on';
  695. return '
  696. <div class="bx-switcher-cont ' . $sClass . '">' . $sCheckbox . '
  697. <div class="bx-switcher-canvas">
  698. <div class="bx-switcher-on"><i class="sys-icon check"></i></div>
  699. <div class="bx-switcher-off"><i class="sys-icon times"></i></div>
  700. <div class="bx-switcher-handler">&nbsp;</div>
  701. </div>
  702. </div>';
  703. }
  704. /**
  705. * Generate Button Input Element
  706. *
  707. * @param array $aInput
  708. * @return string
  709. */
  710. function genInputButton(&$aInput)
  711. {
  712. // clone attributes for system use ;)
  713. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  714. // add default className to attributes
  715. $aAttrs['type'] = $aInput['type'];
  716. if (isset($aInput['value']))
  717. $aAttrs['value'] = $aInput['value'];
  718. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  719. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn" . ('submit' == $aInput['type'] ? ' bx-btn-primary' : ''));
  720. return "<button $sAttrs>" . $aInput['value'] . "</button>\n";
  721. }
  722. /**
  723. * Generate Textarea Element
  724. *
  725. * @param array $aInput
  726. * @return string
  727. */
  728. function genInputTextarea(&$aInput)
  729. {
  730. // clone attributes for system use ;)
  731. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  732. $aAttrs['name'] = $aInput['name'];
  733. // for inputs with labels generate id
  734. if (isset($aInput['label']))
  735. $aAttrs['id'] = $this->getInputId($aInput);
  736. $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' : ''));
  737. $sValue = isset($aInput['value']) ? bx_process_output((isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : strip_tags($aInput['value'])) : '';
  738. return "<textarea $sAttrs>$sValue</textarea>";
  739. }
  740. function isHtmlEditor($iViewMode, &$aInput)
  741. {
  742. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  743. }
  744. function addHtmlEditor($iViewMode, &$aInput)
  745. {
  746. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  747. if (!$oEditor)
  748. return false;
  749. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  750. return true;
  751. }
  752. /**
  753. * Generate Select Box Element
  754. *
  755. * @param array $aInput
  756. * @return string
  757. */
  758. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  759. {
  760. $aNewInput = $aInput;
  761. $aNewInput['type'] = 'select';
  762. $aNewInput['name'] .= '[]';
  763. $sInput = $this->genInput($aNewInput);
  764. return <<<BLAH
  765. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  766. $sInput
  767. </div>
  768. $sInfo
  769. $sError
  770. BLAH;
  771. }
  772. /**
  773. * Generate Browse File Element
  774. *
  775. * @param array $aInput
  776. * @return string
  777. */
  778. function genInputFile(&$aInput)
  779. {
  780. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  781. if(!empty($aInput['attrs']['onchange']))
  782. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  783. else
  784. $aInput['attrs']['onchange'] = $sOnChange;
  785. $sInput = $this->genInputStandard($aInput);
  786. 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>';
  787. }
  788. /**
  789. * Generate Select Box Element
  790. *
  791. * @param array $aInput
  792. * @return string
  793. */
  794. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  795. {
  796. $sUniqId = genRndPwd (8, false);
  797. $sUploaders = '';
  798. $oUploader = null;
  799. foreach ($aInput['uploaders'] as $sUploaderObject) {
  800. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  801. if (!$oUploader)
  802. continue;
  803. $sGhostTemplate = $this->genGhostTemplate($aInput);
  804. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  805. if (empty($aInput['attrs']['disabled']))
  806. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  807. $aParams = array(
  808. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  809. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  810. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  811. 'attrs' => bx_convert_array2attrs($aAttrs),
  812. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  813. );
  814. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  815. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  816. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams);
  817. }
  818. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  819. 'uploaders_buttons' => $sUploaders,
  820. 'info' => $sInfo,
  821. 'error' => $sError,
  822. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  823. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  824. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  825. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  826. ));
  827. }
  828. protected function genGhostTemplate(&$aInput)
  829. {
  830. $sGhostTemplate = false;
  831. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  832. $oFormNested = $aInput['ghost_template'];
  833. if ($oFormNested instanceof BxDolFormNested)
  834. $sGhostTemplate = $oFormNested->genForm();
  835. } 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
  836. $oFormNested = new BxDolFormNested($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  837. $sGhostTemplate = $oFormNested->getCode();
  838. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  839. $sGhostTemplate = array ();
  840. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  841. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNested)
  842. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  843. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  844. $sGhostTemplate = $aInput['ghost_template'];
  845. }
  846. return $sGhostTemplate;
  847. }
  848. protected function genCustomInputUsernamesSuggestions ($aInput)
  849. {
  850. $this->_addJs(array(
  851. 'jquery.form.min.js',
  852. ));
  853. $sVals = '';
  854. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  855. foreach ($aInput['value'] as $sVal) {
  856. if (!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  857. continue;
  858. $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>';
  859. }
  860. $sVals = trim($sVals, ',');
  861. }
  862. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  863. 'id' => $aInput['name'] . time() . mt_rand(0, 100),
  864. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  865. 'name' => $aInput['name'],
  866. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  867. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  868. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  869. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  870. 'vals' => $sVals,
  871. ));
  872. }
  873. /**
  874. * Generate Select Element
  875. *
  876. * @param array $aInput
  877. * @return string
  878. */
  879. function genInputSelect(&$aInput)
  880. {
  881. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  882. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  883. }
  884. /**
  885. * Generate Multiple Select Element
  886. *
  887. * @param array $aInput
  888. * @return string
  889. */
  890. function genInputSelectMultiple(&$aInput)
  891. {
  892. $aCurValues = array();
  893. if (isset($aInput['value']) && $aInput['value'])
  894. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  895. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  896. }
  897. /**
  898. * Generate Checkbox Set Element
  899. *
  900. * @param array $aInput
  901. * @return string
  902. */
  903. function genInputCheckboxSet(&$aInput)
  904. {
  905. $aCurValues = array();
  906. if (isset($aInput['value']) && $aInput['value'])
  907. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  908. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  909. }
  910. /**
  911. * Generate Radiobuttons Set Element
  912. *
  913. * @param array $aInput
  914. * @return string
  915. */
  916. function genInputRadioSet(&$aInput)
  917. {
  918. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  919. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  920. }
  921. function _isSelected ($sValue, $sCurValue)
  922. {
  923. return ((string)$sValue === (string)$sCurValue);
  924. }
  925. function _isSelectedMultiple ($sValue, $aCurValues)
  926. {
  927. return in_array($sValue, $aCurValues);
  928. }
  929. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  930. {
  931. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  932. $aAttrs['name'] = $aInput['name'];
  933. if ($isMultiple) {
  934. $aAttrs['name'] .= '[]';
  935. $aAttrs['multiple'] = 'multiple';
  936. }
  937. // for inputs with labels generate id
  938. if (isset($aInput['label']))
  939. $aAttrs['id'] = $this->getInputId($aInput);
  940. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}");
  941. // generate options
  942. $sOptions = '';
  943. if (isset($aInput['values']) and is_array($aInput['values'])) {
  944. foreach ($aInput['values'] as $sValue => $sTitle) {
  945. $sClassC = $sStyleC = "";
  946. if(is_array($sTitle)) {
  947. if(isset($sTitle['type'])) {
  948. switch($sTitle['type']) {
  949. case 'group_header':
  950. $sTitle = bx_process_output($sTitle['value']);
  951. $sOptions .= <<<BLAH
  952. <optgroup label="$sTitle">
  953. BLAH;
  954. break;
  955. case 'group_end':
  956. $sOptions .= <<<BLAH
  957. </optgroup>
  958. BLAH;
  959. break;
  960. }
  961. continue;
  962. }
  963. $sClassC = isset($sTitle['class']) ? " class=\"" . $sTitle['class'] . "\"" : "";
  964. $sStyleC = isset($sTitle['style']) ? " style=\"" . $sTitle['style'] . "\"" : "";
  965. $sValue = $sTitle['key'];
  966. $sTitle = $sTitle['value'];
  967. }
  968. $sValueC = bx_html_attribute($sValue);
  969. $sTitleC = bx_process_output($sTitle);
  970. $sSelected = $this->$sIsSelectedFunc($sValue, $mixedCurrentVal) ? 'selected="selected"' : '';
  971. $sOptions .= <<<BLAH
  972. <option value="$sValueC"$sClassC$sStyleC $sSelected>$sTitleC</option>
  973. BLAH;
  974. }
  975. }
  976. // generate element
  977. $sCode = <<<BLAH
  978. <select $sAttrs>
  979. $sOptions
  980. </select>
  981. BLAH;
  982. return $sCode;
  983. }
  984. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  985. {
  986. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  987. $aAttrs['name'] = $aInput['name'];
  988. // for inputs with labels generate id
  989. if (isset($aInput['label']))
  990. $aAttrs['id'] = $this->getInputId($aInput);
  991. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  992. // generate options
  993. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  994. $sOptions = '';
  995. if (isset($aInput['values']) and is_array($aInput['values'])) {
  996. if (count($aInput['values']) > 3 && $sDivider == $this->_sDivider)
  997. $sDivider = $this->_sDividerAlt;
  998. // generate complex input using simple standard inputs
  999. foreach ($aInput['values'] as $sValue => $sLabel) {
  1000. // create new simple input
  1001. $aNewInput = array(
  1002. 'type' => $sType,
  1003. 'name' => $aInput['name'] . $sNameAppend,
  1004. 'value' => $sValue,
  1005. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1006. 'label' => $sLabel,
  1007. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1008. );
  1009. $sNewInput = $this->genInput($aNewInput);
  1010. // attach new input to complex
  1011. $sOptions .= ($sNewInput . $sDivider);
  1012. }
  1013. }
  1014. // generate element
  1015. $sCode = <<<BLAH
  1016. <div $sAttrs>
  1017. $sOptions
  1018. </div>
  1019. BLAH;
  1020. return $sCode;
  1021. }
  1022. function genInputLocation(&$aInput)
  1023. {
  1024. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  1025. $sIdStatus = $this->getInputId($aInput) . '_status';
  1026. $sIdInput = $this->getInputId($aInput) . '_location';
  1027. $sProto = (0 === strncmp('https', BX_DOL_URL_ROOT, 5)) ? 'https' : 'http';
  1028. $aVars = array (
  1029. 'key' => trim(getParam('sys_maps_api_key')),
  1030. 'lang' => bx_lang_name(),
  1031. 'name' => $aInput['name'],
  1032. 'id_status' => $sIdStatus,
  1033. 'id_input' => $sIdInput,
  1034. 'manual_input' => $isManualInput,
  1035. 'bx_if:manual_input' => array(
  1036. 'condition' => $isManualInput,
  1037. 'content' => array(),
  1038. ),
  1039. 'bx_if:auto_input' => array(
  1040. 'condition' => !$isManualInput,
  1041. 'content' => array(
  1042. 'id_status' => $sIdStatus,
  1043. 'location_string' => _t('_sys_location_undefined'),
  1044. ),
  1045. ),
  1046. );
  1047. $aLocationIndexes = array ('lat', 'lng', 'country', 'state', 'city', 'zip', 'street', 'street_number');
  1048. foreach ($aLocationIndexes as $sKey)
  1049. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey);
  1050. if ($aVars['country']) {
  1051. $aCountries = BxDolFormQuery::getDataItems('Country');
  1052. $s = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  1053. $aVars['bx_if:auto_input']['content']['location_string'] = $s;
  1054. }
  1055. if ($isManualInput) {
  1056. $aInput['type'] = 'text';
  1057. $aInput['attrs']['id'] = $sIdInput;
  1058. $aVars['input'] = $this->genInputStandard($aInput);
  1059. }
  1060. else {
  1061. if ($this->getLocationVal($aInput, 'lat') && $this->getLocationVal($aInput, 'lng'))
  1062. $aInput['checked'] = true;
  1063. else
  1064. $aInput['checked'] = $this->getCleanValue($aInput['name'] . '_lat') && $this->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  1065. $aVars['input'] = $this->genInputSwitcher($aInput);
  1066. }
  1067. return $this->oTemplate->parseHtmlByName('form_field_location.html', $aVars);
  1068. }
  1069. public function setLocationVal ($aInput, $sIndex, $sVal)
  1070. {
  1071. $s = $aInput['name'] . '_' . $sIndex;
  1072. $this->_aSpecificValues[$s] = $sVal;
  1073. }
  1074. protected function getLocationVal ($aInput, $sIndex)
  1075. {
  1076. $s = $aInput['name'] . '_' . $sIndex;
  1077. if (isset($this->_aSpecificValues[$s]))
  1078. return $this->_aSpecificValues[$s];
  1079. return $this->getCleanValue($s);
  1080. }
  1081. function genInputCaptcha(&$aInput)
  1082. {
  1083. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1084. // for inputs with labels generate id
  1085. if (isset($aInput['label']))
  1086. $aAttrs['id'] = $this->getInputId($aInput);
  1087. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1088. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1089. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1090. }
  1091. /**
  1092. * Generate Label Element
  1093. *
  1094. * @param string $sLabel Text of the Label
  1095. * @param string $sInputID Dependant Input Element ID
  1096. * @return string HTML code
  1097. */
  1098. function genLabel(&$aInput)
  1099. {
  1100. if (!isset($aInput['label']) or empty($aInput['label']))
  1101. return '';
  1102. $sLabel = $aInput['label'];
  1103. $sInputID = $this->getInputId($aInput);
  1104. $sRet = '<label for="' . $sInputID . '">' . bx_process_output($sLabel) . '</label>';
  1105. return $sRet;
  1106. }
  1107. function genInfoIcon($sInfo)
  1108. {
  1109. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1110. }
  1111. function genErrorIcon( $sError = '' )
  1112. {
  1113. if ($this->bEnableErrorIcon) {
  1114. $sStyle = '';
  1115. if (!$sError)
  1116. $sStyle = 'style="display:none;"';
  1117. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1118. }
  1119. }
  1120. function getOpenSection($aAttrs = array(), $sLegend = '')
  1121. {
  1122. if (!$this->_isSectionOpened) {
  1123. if (!$aAttrs || !is_array($aAttrs))
  1124. $aAttrs = array();
  1125. if ($sLegend)
  1126. $sClassesAdd = "bx-form-section-header";
  1127. else
  1128. $sClassesAdd = "bx-form-section-divider";
  1129. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1130. $this->_isSectionOpened = true;
  1131. 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";
  1132. } else {
  1133. return '';
  1134. }
  1135. }
  1136. function getCloseSection()
  1137. {
  1138. if ($this->_isSectionOpened) {
  1139. $this->_isSectionOpened = false;
  1140. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1141. } else {
  1142. return '';
  1143. }
  1144. }
  1145. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1146. {
  1147. if (!$this->_isSectionOpened) {
  1148. if (!$aAttrs || !is_array($aAttrs))
  1149. $aAttrs = array();
  1150. if ($sLegend)
  1151. $sClassesAdd = "bx-form-section-header";
  1152. else
  1153. $sClassesAdd = "bx-form-section-divider";
  1154. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1155. $this->_isSectionOpened = true;
  1156. 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";
  1157. } else {
  1158. return '';
  1159. }
  1160. }
  1161. function getCloseSectionViewMode()
  1162. {
  1163. if ($this->_isSectionOpened) {
  1164. $this->_isSectionOpened = false;
  1165. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1166. } else {
  1167. return '';
  1168. }
  1169. }
  1170. function addCssJs ()
  1171. {
  1172. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1173. if (self::$_isCssJsAddedViewMode)
  1174. return;
  1175. $this->_addCss('forms.css');
  1176. self::$_isCssJsAddedViewMode = true;
  1177. } else {
  1178. if (self::$_isCssJsAdded)
  1179. return;
  1180. $aCss = array(
  1181. 'forms.css',
  1182. 'jquery-ui/jquery-ui.css',
  1183. 'timepicker-addon/jquery-ui-timepicker-addon.css',
  1184. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css',
  1185. );
  1186. $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);
  1187. $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);
  1188. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1189. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1190. $aJs = array(
  1191. 'jquery-ui/jquery-ui.custom.min.js',
  1192. 'jquery.webForms.js',
  1193. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1194. 'jquery-minicolors/jquery.minicolors.min.js',
  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. );
  1199. foreach ($this->aInputs as $aInput) {
  1200. if (!isset($aInput['type']) || 'files' != $aInput['type'] || !isset($aInput['uploaders']))
  1201. continue;
  1202. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1203. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], '', $this->oTemplate);
  1204. if ($oUploader)
  1205. $oUploader->addCssJs();
  1206. }
  1207. }
  1208. $this->_addJs($aJs);
  1209. $this->_addCss($aCss);
  1210. self::$_isCssJsAdded = true;
  1211. }
  1212. }
  1213. protected function _processCssJs()
  1214. {
  1215. $sRet = '';
  1216. if ($this->_bDynamicMode) {
  1217. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1218. $sJs = $this->oTemplate->addJs($this->_aJs, true);
  1219. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1220. $sRet .= "<script>
  1221. bx_get_scripts(" . json_encode($aMatches[1]) . ");
  1222. </script>";
  1223. }
  1224. else {
  1225. $this->oTemplate->addCss($this->_aCss);
  1226. $this->oTemplate->addJs($this->_aJs);
  1227. }
  1228. return $sRet;
  1229. }
  1230. protected function _addJs($mixed)
  1231. {
  1232. if (!is_array($mixed))
  1233. $mixed = array($mixed);
  1234. foreach ($mixed as $s)
  1235. $this->_aJs[$s] = $s;
  1236. }
  1237. protected function _addCss($mixed)
  1238. {
  1239. if (!is_array($mixed))
  1240. $mixed = array($mixed);
  1241. foreach ($mixed as $s)
  1242. $this->_aCss[$s] = $s;
  1243. }
  1244. }
  1245. /** @} */