BxBaseFormView.php 58 KB

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