BxBaseFormView.php 58 KB

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